renamed all time intervals arguments and members to include the time unit in their name

This commit is contained in:
javanna 2016-06-09 16:27:52 +02:00 committed by Luca Cavanna
parent 437c4f210b
commit 85606e8a4b
6 changed files with 60 additions and 61 deletions

View File

@ -52,9 +52,9 @@ public class HostsSniffer {
private final Scheme scheme; private final Scheme scheme;
private final JsonFactory jsonFactory = new JsonFactory(); 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.restClient = restClient;
this.sniffRequestParams = Collections.<String, String>singletonMap("timeout", sniffRequestTimeout + "ms"); this.sniffRequestParams = Collections.<String, String>singletonMap("timeout", sniffRequestTimeoutMillis + "ms");
this.scheme = scheme; this.scheme = scheme;
} }
@ -155,7 +155,7 @@ public class HostsSniffer {
public static final long DEFAULT_SNIFF_REQUEST_TIMEOUT = TimeUnit.SECONDS.toMillis(1); public static final long DEFAULT_SNIFF_REQUEST_TIMEOUT = TimeUnit.SECONDS.toMillis(1);
private final RestClient restClient; private final RestClient restClient;
private long sniffRequestTimeout = DEFAULT_SNIFF_REQUEST_TIMEOUT; private long sniffRequestTimeoutMillis = DEFAULT_SNIFF_REQUEST_TIMEOUT;
private Scheme scheme; private Scheme scheme;
private Builder(RestClient restClient) { 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. * 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 * Allows to halt the request without any failure, as only the nodes that have responded within this timeout will be returned.
* within this timeout will be returned.
*/ */
public Builder setSniffRequestTimeout(int sniffRequestTimeout) { public Builder setSniffRequestTimeoutMillis(int sniffRequestTimeoutMillis) {
if (sniffRequestTimeout <= 0) { if (sniffRequestTimeoutMillis <= 0) {
throw new IllegalArgumentException("sniffRequestTimeout must be greater than 0"); throw new IllegalArgumentException("sniffRequestTimeoutMillis must be greater than 0");
} }
this.sniffRequestTimeout = sniffRequestTimeout; this.sniffRequestTimeoutMillis = sniffRequestTimeoutMillis;
return this; return this;
} }
@ -189,7 +188,7 @@ public class HostsSniffer {
* Creates a new {@link HostsSniffer} instance given the provided configuration * Creates a new {@link HostsSniffer} instance given the provided configuration
*/ */
public HostsSniffer build() { public HostsSniffer build() {
return new HostsSniffer(restClient, sniffRequestTimeout, scheme); return new HostsSniffer(restClient, sniffRequestTimeoutMillis, scheme);
} }
} }
} }

View File

@ -72,17 +72,17 @@ public final class Sniffer extends RestClient.FailureListener implements Closeab
private final HostsSniffer hostsSniffer; private final HostsSniffer hostsSniffer;
private final RestClient restClient; private final RestClient restClient;
private final long sniffInterval; private final long sniffIntervalMillis;
private final long sniffAfterFailureDelay; private final long sniffAfterFailureDelayMillis;
private final ScheduledExecutorService scheduledExecutorService; private final ScheduledExecutorService scheduledExecutorService;
private final AtomicBoolean running = new AtomicBoolean(false); private final AtomicBoolean running = new AtomicBoolean(false);
private ScheduledFuture<?> scheduledFuture; 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.hostsSniffer = hostsSniffer;
this.restClient = restClient; this.restClient = restClient;
this.sniffInterval = sniffInterval; this.sniffIntervalMillis = sniffIntervalMillis;
this.sniffAfterFailureDelay = sniffAfterFailureDelay; this.sniffAfterFailureDelayMillis = sniffAfterFailureDelayMillis;
this.scheduledExecutorService = Executors.newScheduledThreadPool(1); this.scheduledExecutorService = Executors.newScheduledThreadPool(1);
scheduleNextRun(0); scheduleNextRun(0);
} }
@ -104,11 +104,11 @@ public final class Sniffer extends RestClient.FailureListener implements Closeab
@Override @Override
public void run() { public void run() {
sniff(null, sniffInterval); sniff(null, sniffIntervalMillis);
} }
void sniffOnFailure(HttpHost failedHost) { void sniffOnFailure(HttpHost failedHost) {
sniff(failedHost, sniffAfterFailureDelay); sniff(failedHost, sniffAfterFailureDelayMillis);
} }
void sniff(HttpHost excludeHost, long nextSniffDelayMillis) { 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 RestClient restClient;
private final HostsSniffer hostsSniffer; private final HostsSniffer hostsSniffer;
private long sniffInterval = DEFAULT_SNIFF_INTERVAL; private long sniffIntervalMillis = DEFAULT_SNIFF_INTERVAL;
private boolean sniffOnFailure = true; 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, * 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 * Sets the interval between consecutive ordinary sniff executions in milliseconds. Will be honoured when
* when there are no failures between consecutive sniff executions. * sniffOnFailure is disabled or when there are no failures between consecutive sniff executions.
* @throws IllegalArgumentException if sniffInterval is not greater than 0 * @throws IllegalArgumentException if sniffIntervalMillis is not greater than 0
*/ */
public Builder setSniffInterval(int sniffInterval) { public Builder setSniffIntervalMillis(int sniffIntervalMillis) {
if (sniffInterval <= 0) { if (sniffIntervalMillis <= 0) {
throw new IllegalArgumentException("sniffInterval must be greater than 0"); throw new IllegalArgumentException("sniffIntervalMillis must be greater than 0");
} }
this.sniffInterval = sniffInterval; this.sniffIntervalMillis = sniffIntervalMillis;
return this; return this;
} }
/** /**
* Enables/disables sniffing on failure. If enabled, at each failure nodes will be reloaded, and a new sniff execution will * 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) { public Builder setSniffOnFailure(boolean sniffOnFailure) {
this.sniffOnFailure = 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) { public Builder setSniffAfterFailureDelayMillis(int sniffAfterFailureDelayMillis) {
if (sniffAfterFailureDelay <= 0) { if (sniffAfterFailureDelayMillis <= 0) {
throw new IllegalArgumentException("sniffAfterFailureDelay must be greater than 0"); throw new IllegalArgumentException("sniffAfterFailureDelayMillis must be greater than 0");
} }
this.sniffAfterFailureDelay = sniffAfterFailureDelay; this.sniffAfterFailureDelayMillis = sniffAfterFailureDelayMillis;
return this; return this;
} }
@ -210,7 +210,7 @@ public final class Sniffer extends RestClient.FailureListener implements Closeab
* Creates the {@link Sniffer} based on the provided configuration. * Creates the {@link Sniffer} based on the provided configuration.
*/ */
public Sniffer build() { public Sniffer build() {
return new Sniffer(restClient, hostsSniffer, sniffInterval, sniffOnFailure, sniffAfterFailureDelay); return new Sniffer(restClient, hostsSniffer, sniffIntervalMillis, sniffOnFailure, sniffAfterFailureDelayMillis);
} }
} }
} }

View File

@ -50,7 +50,7 @@ public class HostsSnifferBuilderTests extends LuceneTestCase {
} }
try { 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"); fail("should have failed");
} catch(IllegalArgumentException e) { } catch(IllegalArgumentException e) {
assertEquals(e.getMessage(), "sniffRequestTimeout must be greater than 0"); 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())); builder.setScheme(RandomPicks.randomFrom(random(), HostsSniffer.Scheme.values()));
} }
if (random().nextBoolean()) { if (random().nextBoolean()) {
builder.setSniffRequestTimeout(RandomInts.randomIntBetween(random(), 1, Integer.MAX_VALUE)); builder.setSniffRequestTimeoutMillis(RandomInts.randomIntBetween(random(), 1, Integer.MAX_VALUE));
} }
assertNotNull(builder.build()); assertNotNull(builder.build());
} }

View File

@ -55,14 +55,14 @@ public class SnifferBuilderTests extends LuceneTestCase {
} }
try { 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"); fail("should have failed");
} catch(IllegalArgumentException e) { } catch(IllegalArgumentException e) {
assertEquals("sniffInterval must be greater than 0", e.getMessage()); assertEquals("sniffInterval must be greater than 0", e.getMessage());
} }
try { 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"); fail("should have failed");
} catch(IllegalArgumentException e) { } catch(IllegalArgumentException e) {
assertEquals("sniffAfterFailureDelay must be greater than 0", e.getMessage()); 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); Sniffer.Builder builder = Sniffer.builder(client, hostsSniffer);
if (random().nextBoolean()) { if (random().nextBoolean()) {
builder.setSniffInterval(RandomInts.randomIntBetween(random(), 1, Integer.MAX_VALUE)); builder.setSniffIntervalMillis(RandomInts.randomIntBetween(random(), 1, Integer.MAX_VALUE));
} }
if (random().nextBoolean()) { if (random().nextBoolean()) {
builder.setSniffAfterFailureDelay(RandomInts.randomIntBetween(random(), 1, Integer.MAX_VALUE)); builder.setSniffAfterFailureDelayMillis(RandomInts.randomIntBetween(random(), 1, Integer.MAX_VALUE));
} }
if (random().nextBoolean()) { if (random().nextBoolean()) {
builder.setSniffOnFailure(random().nextBoolean()); builder.setSniffOnFailure(random().nextBoolean());

View File

@ -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 //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. //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 Header[] defaultHeaders;
private final long maxRetryTimeout; private final long maxRetryTimeoutMillis;
private final AtomicInteger lastHostIndex = new AtomicInteger(0); private final AtomicInteger lastHostIndex = new AtomicInteger(0);
private volatile Set<HttpHost> hosts; private volatile Set<HttpHost> hosts;
private final ConcurrentMap<HttpHost, DeadHostState> blacklist = new ConcurrentHashMap<>(); private final ConcurrentMap<HttpHost, DeadHostState> blacklist = new ConcurrentHashMap<>();
private volatile FailureListener failureListener = new FailureListener(); 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.client = client;
this.maxRetryTimeout = maxRetryTimeout; this.maxRetryTimeoutMillis = maxRetryTimeoutMillis;
this.defaultHeaders = defaultHeaders; this.defaultHeaders = defaultHeaders;
setHosts(hosts); setHosts(hosts);
} }
@ -139,7 +139,7 @@ public final class RestClient implements Closeable {
HttpRequestBase request = createHttpRequest(method, uri, entity); HttpRequestBase request = createHttpRequest(method, uri, entity);
setHeaders(request, headers); 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 //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; IOException lastSeenException = null;
long startTime = System.nanoTime(); long startTime = System.nanoTime();
Iterator<HttpHost> hostIterator = nextHost(); Iterator<HttpHost> hostIterator = nextHost();
@ -358,16 +358,16 @@ public final class RestClient implements Closeable {
* Rest client builder. Helps creating a new {@link RestClient}. * Rest client builder. Helps creating a new {@link RestClient}.
*/ */
public static final class Builder { public static final class Builder {
public static final int DEFAULT_CONNECT_TIMEOUT = 1000; public static final int DEFAULT_CONNECT_TIMEOUT_MILLIS = 1000;
public static final int DEFAULT_SOCKET_TIMEOUT = 10000; public static final int DEFAULT_SOCKET_TIMEOUT_MILLIS = 10000;
public static final int DEFAULT_MAX_RETRY_TIMEOUT = DEFAULT_SOCKET_TIMEOUT; public static final int DEFAULT_MAX_RETRY_TIMEOUT_MILLIS = DEFAULT_SOCKET_TIMEOUT_MILLIS;
public static final int DEFAULT_CONNECTION_REQUEST_TIMEOUT = 500; public static final int DEFAULT_CONNECTION_REQUEST_TIMEOUT_MILLIS = 500;
private static final Header[] EMPTY_HEADERS = new Header[0]; private static final Header[] EMPTY_HEADERS = new Header[0];
private final HttpHost[] hosts; private final HttpHost[] hosts;
private CloseableHttpClient httpClient; private CloseableHttpClient httpClient;
private int maxRetryTimeout = DEFAULT_MAX_RETRY_TIMEOUT; private int maxRetryTimeout = DEFAULT_MAX_RETRY_TIMEOUT_MILLIS;
private Header[] defaultHeaders = EMPTY_HEADERS; 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. * Sets the maximum timeout (in milliseconds) to honour in case of multiple retries of the same request.
* {@link #DEFAULT_MAX_RETRY_TIMEOUT} if not specified. * {@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) { public Builder setMaxRetryTimeoutMillis(int maxRetryTimeoutMillis) {
if (maxRetryTimeout <= 0) { if (maxRetryTimeoutMillis <= 0) {
throw new IllegalArgumentException("maxRetryTimeout must be greater than 0"); throw new IllegalArgumentException("maxRetryTimeoutMillis must be greater than 0");
} }
this.maxRetryTimeout = maxRetryTimeout; this.maxRetryTimeout = maxRetryTimeoutMillis;
return this; return this;
} }
@ -446,9 +446,9 @@ public final class RestClient implements Closeable {
connectionManager.setMaxTotal(30); connectionManager.setMaxTotal(30);
//default timeouts are all infinite //default timeouts are all infinite
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(DEFAULT_CONNECT_TIMEOUT) RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(DEFAULT_CONNECT_TIMEOUT_MILLIS)
.setSocketTimeout(DEFAULT_SOCKET_TIMEOUT) .setSocketTimeout(DEFAULT_SOCKET_TIMEOUT_MILLIS)
.setConnectionRequestTimeout(DEFAULT_CONNECTION_REQUEST_TIMEOUT).build(); .setConnectionRequestTimeout(DEFAULT_CONNECTION_REQUEST_TIMEOUT_MILLIS).build();
return HttpClientBuilder.create().setConnectionManager(connectionManager).setDefaultRequestConfig(requestConfig).build(); return HttpClientBuilder.create().setConnectionManager(connectionManager).setDefaultRequestConfig(requestConfig).build();
} }
} }

View File

@ -53,7 +53,7 @@ public class RestClientBuilderTests extends LuceneTestCase {
} }
try { 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"); fail("should have failed");
} catch(IllegalArgumentException e) { } catch(IllegalArgumentException e) {
assertEquals("maxRetryTimeout must be greater than 0", e.getMessage()); assertEquals("maxRetryTimeout must be greater than 0", e.getMessage());
@ -91,7 +91,7 @@ public class RestClientBuilderTests extends LuceneTestCase {
builder.setDefaultHeaders(headers); builder.setDefaultHeaders(headers);
} }
if (random().nextBoolean()) { 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()) { try (RestClient restClient = builder.build()) {
assertNotNull(restClient); assertNotNull(restClient);