Skip to content

Commit 63f3a61

Browse files
authored
Refactor Sniffer and make it testable (#29638)
This commit reworks the Sniffer component to simplify it and make it possible to test it. In particular, it no longer takes out the host that failed when sniffing on failure, but rather relies on whatever the cluster returns. This is the result of some valid comments from #27985. Taking out one single host is too naive, hard to test and debug. A new Scheduler abstraction is introduced to abstract the tasks scheduling away and make it possible to plug in any test implementation and take out timing aspects when testing. Concurrency aspects have also been improved, synchronized methods are no longer required. At the same time, we were able to take #27697 and #25701 into account and fix them, especially now that we can more easily add tests. Last but not least, unit tests are added for the Sniffer component, long overdue. Closes #27697 Closes #25701
1 parent 4777d8a commit 63f3a61

File tree

9 files changed

+913
-91
lines changed

9 files changed

+913
-91
lines changed

client/rest/src/main/java/org/elasticsearch/client/RestClient.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import java.util.HashMap;
6262
import java.util.HashSet;
6363
import java.util.Iterator;
64+
import java.util.LinkedHashSet;
6465
import java.util.List;
6566
import java.util.Locale;
6667
import java.util.Map;
@@ -132,7 +133,7 @@ public synchronized void setHosts(HttpHost... hosts) {
132133
if (hosts == null || hosts.length == 0) {
133134
throw new IllegalArgumentException("hosts must not be null nor empty");
134135
}
135-
Set<HttpHost> httpHosts = new HashSet<>();
136+
Set<HttpHost> httpHosts = new LinkedHashSet<>();
136137
AuthCache authCache = new BasicAuthCache();
137138
for (HttpHost host : hosts) {
138139
Objects.requireNonNull(host, "host cannot be null");
@@ -143,6 +144,13 @@ public synchronized void setHosts(HttpHost... hosts) {
143144
this.blacklist.clear();
144145
}
145146

147+
/**
148+
* Returns the configured hosts
149+
*/
150+
public List<HttpHost> getHosts() {
151+
return new ArrayList<>(hostTuple.hosts);
152+
}
153+
146154
/**
147155
* Sends a request to the Elasticsearch cluster that the client points to.
148156
* Blocks until the request is completed and returns its response or fails

client/rest/src/test/java/org/elasticsearch/client/RestClientTests.java

+32
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import java.io.IOException;
2727
import java.net.URI;
28+
import java.util.Arrays;
2829
import java.util.Collections;
2930
import java.util.concurrent.CountDownLatch;
3031
import java.util.concurrent.TimeUnit;
@@ -251,6 +252,37 @@ public void testSetHostsWrongArguments() throws IOException {
251252
}
252253
}
253254

255+
public void testSetHostsPreservesOrdering() throws Exception {
256+
try (RestClient restClient = createRestClient()) {
257+
HttpHost[] hosts = randomHosts();
258+
restClient.setHosts(hosts);
259+
assertEquals(Arrays.asList(hosts), restClient.getHosts());
260+
}
261+
}
262+
263+
private static HttpHost[] randomHosts() {
264+
int numHosts = randomIntBetween(1, 10);
265+
HttpHost[] hosts = new HttpHost[numHosts];
266+
for (int i = 0; i < hosts.length; i++) {
267+
hosts[i] = new HttpHost("host-" + i, 9200);
268+
}
269+
return hosts;
270+
}
271+
272+
public void testSetHostsDuplicatedHosts() throws Exception {
273+
try (RestClient restClient = createRestClient()) {
274+
int numHosts = randomIntBetween(1, 10);
275+
HttpHost[] hosts = new HttpHost[numHosts];
276+
HttpHost host = new HttpHost("host", 9200);
277+
for (int i = 0; i < hosts.length; i++) {
278+
hosts[i] = host;
279+
}
280+
restClient.setHosts(hosts);
281+
assertEquals(1, restClient.getHosts().size());
282+
assertEquals(host, restClient.getHosts().get(0));
283+
}
284+
}
285+
254286
/**
255287
* @deprecated will remove method in 7.0 but needs tests until then. Replaced by {@link RequestTests#testConstructor()}.
256288
*/

client/sniffer/src/main/java/org/elasticsearch/client/sniff/SniffOnFailureListener.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ public void onFailure(HttpHost host) {
5858
if (sniffer == null) {
5959
throw new IllegalStateException("sniffer was not set, unable to sniff on failure");
6060
}
61-
//re-sniff immediately but take out the node that failed
62-
sniffer.sniffOnFailure(host);
61+
sniffer.sniffOnFailure();
6362
}
6463
}

0 commit comments

Comments
 (0)