renamed all time intervals arguments and members to include the time unit in their name
This commit is contained in:
parent
437c4f210b
commit
85606e8a4b
|
@ -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.<String, String>singletonMap("timeout", sniffRequestTimeout + "ms");
|
||||
this.sniffRequestParams = Collections.<String, String>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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
|
|
|
@ -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<HttpHost> hosts;
|
||||
private final ConcurrentMap<HttpHost, DeadHostState> 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<HttpHost> 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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue