remove Scheme enum

This commit is contained in:
javanna 2016-05-06 10:30:44 +02:00 committed by Luca Cavanna
parent bd29dc1572
commit 9ffdea9515
4 changed files with 40 additions and 26 deletions

View File

@ -30,6 +30,7 @@ dependencies {
//compile "org.apache.httpcomponents:httpasyncclient:4.1.1"
compile "commons-codec:commons-codec:1.9"
compile "commons-logging:commons-logging:1.2"
//jackson is only needed in the sniff package
compile "com.fasterxml.jackson.core:jackson-core:2.7.3"
testCompile "com.carrotsearch.randomizedtesting:randomizedtesting-runner:${versions.randomizedrunner}"

View File

@ -30,7 +30,6 @@ import org.elasticsearch.client.Connection;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
@ -51,7 +50,7 @@ public class SniffingConnectionPool extends AbstractStaticConnectionPool {
//TODO do we still need the sniff request timeout? or should we just use a low connect timeout?
public SniffingConnectionPool(int sniffInterval, boolean sniffOnFailure, int sniffAfterFailureDelay,
CloseableHttpClient client, RequestConfig sniffRequestConfig, int sniffRequestTimeout, Scheme scheme,
CloseableHttpClient client, RequestConfig sniffRequestConfig, int sniffRequestTimeout, String scheme,
HttpHost... hosts) {
if (sniffInterval <= 0) {
throw new IllegalArgumentException("sniffInterval must be greater than 0");
@ -60,11 +59,14 @@ public class SniffingConnectionPool extends AbstractStaticConnectionPool {
throw new IllegalArgumentException("sniffAfterFailureDelay must be greater than 0");
}
Objects.requireNonNull(scheme, "scheme cannot be null");
if (scheme.equals("http") == false && scheme.equals("https") == false) {
throw new IllegalArgumentException("scheme must be either http or https");
}
if (hosts == null || hosts.length == 0) {
throw new IllegalArgumentException("no hosts provided");
}
this.sniffOnFailure = sniffOnFailure;
this.sniffer = new Sniffer(client, sniffRequestConfig, sniffRequestTimeout, scheme.toString());
this.sniffer = new Sniffer(client, sniffRequestConfig, sniffRequestTimeout, scheme);
this.connections = createConnections(hosts);
this.snifferTask = new SnifferTask(sniffInterval, sniffAfterFailureDelay);
}
@ -93,15 +95,6 @@ public class SniffingConnectionPool extends AbstractStaticConnectionPool {
snifferTask.shutdown();
}
public enum Scheme {
HTTP, HTTPS;
@Override
public String toString() {
return name().toLowerCase(Locale.ROOT);
}
}
private class SnifferTask implements Runnable {
private final int sniffInterval;
private final int sniffAfterFailureDelay;

View File

@ -34,8 +34,6 @@ import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.lucene.util.LuceneTestCase;
import org.elasticsearch.client.ElasticsearchResponseException;
import org.elasticsearch.client.sniff.Sniffer;
import org.elasticsearch.client.sniff.SniffingConnectionPool;
import org.junit.After;
import org.junit.Before;
@ -46,6 +44,7 @@ import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
@ -66,14 +65,14 @@ public class SnifferTests extends LuceneTestCase {
}
private int sniffRequestTimeout;
private SniffingConnectionPool.Scheme scheme;
private String scheme;
private SniffResponse sniffResponse;
private MockWebServer server;
@Before
public void startMockWebServer() throws IOException {
this.sniffRequestTimeout = RandomInts.randomIntBetween(random(), 1000, 10000);
this.scheme = RandomPicks.randomFrom(random(), SniffingConnectionPool.Scheme.values());
this.scheme = RandomPicks.randomFrom(random(), Arrays.asList("http", "https"));
if (rarely()) {
this.sniffResponse = SniffResponse.buildFailure();
} else {
@ -139,7 +138,7 @@ public class SnifferTests extends LuceneTestCase {
return server;
}
private static SniffResponse buildSniffResponse(SniffingConnectionPool.Scheme scheme) throws IOException {
private static SniffResponse buildSniffResponse(String scheme) throws IOException {
int numNodes = RandomInts.randomIntBetween(random(), 1, 5);
List<HttpHost> hosts = new ArrayList<>(numNodes);
JsonFactory jsonFactory = new JsonFactory();

View File

@ -37,6 +37,7 @@ public class SniffingConnectionPoolTests extends LuceneTestCase {
public void testConstructor() throws Exception {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
String[] schemes = new String[]{"http", "https"};
int numNodes = RandomInts.randomIntBetween(random(), 1, 5);
HttpHost[] hosts = new HttpHost[numNodes];
for (int i = 0; i < numNodes; i++) {
@ -47,7 +48,7 @@ public class SniffingConnectionPoolTests extends LuceneTestCase {
RandomInts.randomIntBetween(random(), Integer.MIN_VALUE, 0), random().nextBoolean(),
RandomInts.randomIntBetween(random(), 1, Integer.MAX_VALUE), httpClient, RequestConfig.DEFAULT,
RandomInts.randomIntBetween(random(), 1, Integer.MAX_VALUE),
RandomPicks.randomFrom(random(), SniffingConnectionPool.Scheme.values()), hosts)) {
RandomPicks.randomFrom(random(), schemes), hosts)) {
fail("pool creation should have failed " + connectionPool);
} catch(IllegalArgumentException e) {
@ -58,7 +59,7 @@ public class SniffingConnectionPoolTests extends LuceneTestCase {
RandomInts.randomIntBetween(random(), 1, Integer.MAX_VALUE), random().nextBoolean(),
RandomInts.randomIntBetween(random(), Integer.MIN_VALUE, 0), httpClient, RequestConfig.DEFAULT,
RandomInts.randomIntBetween(random(), 1, Integer.MAX_VALUE),
RandomPicks.randomFrom(random(), SniffingConnectionPool.Scheme.values()), hosts)) {
RandomPicks.randomFrom(random(), schemes), hosts)) {
fail("pool creation should have failed " + connectionPool);
} catch(IllegalArgumentException e) {
assertEquals(e.getMessage(), "sniffAfterFailureDelay must be greater than 0");
@ -68,7 +69,7 @@ public class SniffingConnectionPoolTests extends LuceneTestCase {
RandomInts.randomIntBetween(random(), 1, Integer.MAX_VALUE), random().nextBoolean(),
RandomInts.randomIntBetween(random(), 1, Integer.MAX_VALUE), null, RequestConfig.DEFAULT,
RandomInts.randomIntBetween(random(), 1, Integer.MAX_VALUE),
RandomPicks.randomFrom(random(), SniffingConnectionPool.Scheme.values()), hosts)) {
RandomPicks.randomFrom(random(), schemes), hosts)) {
fail("pool creation should have failed " + connectionPool);
} catch(NullPointerException e) {
assertEquals(e.getMessage(), "client cannot be null");
@ -78,7 +79,7 @@ public class SniffingConnectionPoolTests extends LuceneTestCase {
RandomInts.randomIntBetween(random(), 1, Integer.MAX_VALUE), random().nextBoolean(),
RandomInts.randomIntBetween(random(), 1, Integer.MAX_VALUE), httpClient, null,
RandomInts.randomIntBetween(random(), 1, Integer.MAX_VALUE),
RandomPicks.randomFrom(random(), SniffingConnectionPool.Scheme.values()), hosts)) {
RandomPicks.randomFrom(random(), schemes), hosts)) {
fail("pool creation should have failed " + connectionPool);
} catch(NullPointerException e) {
assertEquals(e.getMessage(), "sniffRequestConfig cannot be null");
@ -88,17 +89,37 @@ public class SniffingConnectionPoolTests extends LuceneTestCase {
RandomInts.randomIntBetween(random(), 1, Integer.MAX_VALUE), random().nextBoolean(),
RandomInts.randomIntBetween(random(), 1, Integer.MAX_VALUE), httpClient, RequestConfig.DEFAULT,
RandomInts.randomIntBetween(random(), Integer.MIN_VALUE, 0),
RandomPicks.randomFrom(random(), SniffingConnectionPool.Scheme.values()), hosts)) {
RandomPicks.randomFrom(random(), schemes), hosts)) {
fail("pool creation should have failed " + connectionPool);
} catch(IllegalArgumentException e) {
assertEquals(e.getMessage(), "sniffRequestTimeout must be greater than 0");
}
try (SniffingConnectionPool connectionPool = new SniffingConnectionPool(
RandomInts.randomIntBetween(random(), 1, Integer.MAX_VALUE), random().nextBoolean(),
RandomInts.randomIntBetween(random(), 1, Integer.MAX_VALUE), httpClient, RequestConfig.DEFAULT,
RandomInts.randomIntBetween(random(), Integer.MIN_VALUE, 0),
null, hosts)) {
fail("pool creation should have failed " + connectionPool);
} catch(NullPointerException e) {
assertEquals(e.getMessage(), "scheme cannot be null");
}
try (SniffingConnectionPool connectionPool = new SniffingConnectionPool(
RandomInts.randomIntBetween(random(), 1, Integer.MAX_VALUE), random().nextBoolean(),
RandomInts.randomIntBetween(random(), 1, Integer.MAX_VALUE), httpClient, RequestConfig.DEFAULT,
RandomInts.randomIntBetween(random(), Integer.MIN_VALUE, 0),
"whatever", hosts)) {
fail("pool creation should have failed " + connectionPool);
} catch(IllegalArgumentException e) {
assertEquals(e.getMessage(), "scheme must be either http or https");
}
try (SniffingConnectionPool connectionPool = new SniffingConnectionPool(
RandomInts.randomIntBetween(random(), 1, Integer.MAX_VALUE), random().nextBoolean(),
RandomInts.randomIntBetween(random(), 1, Integer.MAX_VALUE), httpClient, RequestConfig.DEFAULT,
RandomInts.randomIntBetween(random(), 1, Integer.MAX_VALUE),
RandomPicks.randomFrom(random(), SniffingConnectionPool.Scheme.values()), (HttpHost[])null)) {
RandomPicks.randomFrom(random(), schemes), (HttpHost[])null)) {
fail("pool creation should have failed " + connectionPool);
} catch(IllegalArgumentException e) {
assertEquals(e.getMessage(), "no hosts provided");
@ -108,7 +129,7 @@ public class SniffingConnectionPoolTests extends LuceneTestCase {
RandomInts.randomIntBetween(random(), 1, Integer.MAX_VALUE), random().nextBoolean(),
RandomInts.randomIntBetween(random(), 1, Integer.MAX_VALUE), httpClient, RequestConfig.DEFAULT,
RandomInts.randomIntBetween(random(), 1, Integer.MAX_VALUE),
RandomPicks.randomFrom(random(), SniffingConnectionPool.Scheme.values()), (HttpHost) null)) {
RandomPicks.randomFrom(random(), schemes), (HttpHost) null)) {
fail("pool creation should have failed " + connectionPool);
} catch(NullPointerException e) {
assertEquals(e.getMessage(), "host cannot be null");
@ -118,7 +139,7 @@ public class SniffingConnectionPoolTests extends LuceneTestCase {
RandomInts.randomIntBetween(random(), 1, Integer.MAX_VALUE), random().nextBoolean(),
RandomInts.randomIntBetween(random(), 1, Integer.MAX_VALUE), httpClient, RequestConfig.DEFAULT,
RandomInts.randomIntBetween(random(), 1, Integer.MAX_VALUE),
RandomPicks.randomFrom(random(), SniffingConnectionPool.Scheme.values()))) {
RandomPicks.randomFrom(random(), schemes))) {
fail("pool creation should have failed " + connectionPool);
} catch(IllegalArgumentException e) {
assertEquals(e.getMessage(), "no hosts provided");
@ -128,7 +149,7 @@ public class SniffingConnectionPoolTests extends LuceneTestCase {
RandomInts.randomIntBetween(random(), 1, Integer.MAX_VALUE), random().nextBoolean(),
RandomInts.randomIntBetween(random(), 1, Integer.MAX_VALUE), httpClient, RequestConfig.DEFAULT,
RandomInts.randomIntBetween(random(), 1, Integer.MAX_VALUE),
RandomPicks.randomFrom(random(), SniffingConnectionPool.Scheme.values()), hosts)) {
RandomPicks.randomFrom(random(), schemes), hosts)) {
assertNotNull(sniffingConnectionPool);
}
}