require RestClient when creating Sniffer.Builder

This commit is contained in:
javanna 2016-06-08 12:19:52 +02:00 committed by Luca Cavanna
parent 791db1fb48
commit a78fe1305d
2 changed files with 55 additions and 68 deletions

View File

@ -22,8 +22,6 @@ package org.elasticsearch.client.sniff;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpHost;
import org.apache.http.config.Registry;
import org.apache.http.impl.client.CloseableHttpClient;
import org.elasticsearch.client.RestClient;
import java.io.Closeable;
@ -145,8 +143,8 @@ public final class Sniffer extends RestClient.FailureListener implements Closeab
/**
* Returns a new {@link Builder} to help with {@link Sniffer} creation.
*/
public static Builder builder() {
return new Builder();
public static Builder builder(RestClient restClient) {
return new Builder(restClient);
}
/**
@ -157,15 +155,19 @@ public final class Sniffer extends RestClient.FailureListener implements Closeab
public static final long DEFAULT_SNIFF_AFTER_FAILURE_DELAY = TimeUnit.MINUTES.toMillis(1);
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 sniffInterval = DEFAULT_SNIFF_INTERVAL;
private boolean sniffOnFailure = true;
private long sniffAfterFailureDelay = DEFAULT_SNIFF_AFTER_FAILURE_DELAY;
private String scheme = "http";
private RestClient restClient;
private Builder() {
/**
* Creates a new builder instance and sets the {@link RestClient} that will be used to communicate with elasticsearch.
*/
private Builder(RestClient restClient) {
Objects.requireNonNull(restClient, "restClient cannot be null");
this.restClient = restClient;
}
/**
@ -201,24 +203,13 @@ public final class Sniffer extends RestClient.FailureListener implements Closeab
return this;
}
/**
* Sets the http client. Mandatory argument. Best practice is to use the same client used
* within {@link org.elasticsearch.client.RestClient} which can be created manually or
* through {@link RestClient.Builder#createDefaultHttpClient(Registry)}.
* @see CloseableHttpClient
*/
public Builder setRestClient(RestClient restClient) {
this.restClient = restClient;
return this;
}
/**
* 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.
*/
public Builder setSniffRequestTimeout(int sniffRequestTimeout) {
if (sniffRequestTimeout <=0) {
if (sniffRequestTimeout <= 0) {
throw new IllegalArgumentException("sniffRequestTimeout must be greater than 0");
}
this.sniffRequestTimeout = sniffRequestTimeout;
@ -242,7 +233,6 @@ public final class Sniffer extends RestClient.FailureListener implements Closeab
* Creates the {@link Sniffer} based on the provided configuration.
*/
public Sniffer build() {
Objects.requireNonNull(restClient, "restClient cannot be null");
return new Sniffer(restClient, sniffRequestTimeout, scheme, sniffInterval, sniffOnFailure, sniffAfterFailureDelay);
}
}

View File

@ -35,63 +35,60 @@ public class SnifferBuilderTests extends LuceneTestCase {
}
public void testBuild() throws Exception {
try {
Sniffer.builder().setScheme(null);
fail("should have failed");
} catch(NullPointerException e) {
assertEquals(e.getMessage(), "scheme cannot be null");
}
try {
Sniffer.builder().setScheme("whatever");
fail("should have failed");
} catch(IllegalArgumentException e) {
assertEquals(e.getMessage(), "scheme must be either http or https");
}
try {
Sniffer.builder().setSniffInterval(RandomInts.randomIntBetween(random(), Integer.MIN_VALUE, 0));
fail("should have failed");
} catch(IllegalArgumentException e) {
assertEquals(e.getMessage(), "sniffInterval must be greater than 0");
}
try {
Sniffer.builder().setSniffRequestTimeout(RandomInts.randomIntBetween(random(), Integer.MIN_VALUE, 0));
fail("should have failed");
} catch(IllegalArgumentException e) {
assertEquals(e.getMessage(), "sniffRequestTimeout must be greater than 0");
}
try {
Sniffer.builder().setSniffAfterFailureDelay(RandomInts.randomIntBetween(random(), Integer.MIN_VALUE, 0));
fail("should have failed");
} catch(IllegalArgumentException e) {
assertEquals(e.getMessage(), "sniffAfterFailureDelay must be greater than 0");
}
try {
Sniffer.builder().build();
fail("should have failed");
} catch(NullPointerException e) {
assertEquals(e.getMessage(), "restClient cannot be null");
}
int numNodes = RandomInts.randomIntBetween(random(), 1, 5);
HttpHost[] hosts = new HttpHost[numNodes];
for (int i = 0; i < numNodes; i++) {
hosts[i] = new HttpHost("localhost", 9200 + i);
}
try (RestClient client = RestClient.builder().setHosts(hosts).build()) {
try (Sniffer sniffer = Sniffer.builder().setRestClient(client).build()) {
try {
Sniffer.builder(client).setScheme(null);
fail("should have failed");
} catch(NullPointerException e) {
assertEquals(e.getMessage(), "scheme cannot be null");
}
try {
Sniffer.builder(client).setScheme("whatever");
fail("should have failed");
} catch(IllegalArgumentException e) {
assertEquals(e.getMessage(), "scheme must be either http or https");
}
try {
Sniffer.builder(client).setSniffInterval(RandomInts.randomIntBetween(random(), Integer.MIN_VALUE, 0));
fail("should have failed");
} catch(IllegalArgumentException e) {
assertEquals(e.getMessage(), "sniffInterval must be greater than 0");
}
try {
Sniffer.builder(client).setSniffRequestTimeout(RandomInts.randomIntBetween(random(), Integer.MIN_VALUE, 0));
fail("should have failed");
} catch(IllegalArgumentException e) {
assertEquals(e.getMessage(), "sniffRequestTimeout must be greater than 0");
}
try {
Sniffer.builder(client).setSniffAfterFailureDelay(RandomInts.randomIntBetween(random(), Integer.MIN_VALUE, 0));
fail("should have failed");
} catch(IllegalArgumentException e) {
assertEquals(e.getMessage(), "sniffAfterFailureDelay must be greater than 0");
}
try {
Sniffer.builder(null).build();
fail("should have failed");
} catch(NullPointerException e) {
assertEquals(e.getMessage(), "restClient cannot be null");
}
try (Sniffer sniffer = Sniffer.builder(client).build()) {
assertNotNull(sniffer);
}
}
try (RestClient client = RestClient.builder().setHosts(hosts).build()) {
Sniffer.Builder builder = Sniffer.builder().setRestClient(client);
Sniffer.Builder builder = Sniffer.builder(client);
if (random().nextBoolean()) {
builder.setScheme(RandomPicks.randomFrom(random(), Arrays.asList("http", "https")));
}