Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LLClient: Support host selection #30523

Merged
merged 31 commits into from
Jun 11, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
99c8a33
LLClient: Support host selection
nik9000 May 10, 2018
81dd654
Fix test
nik9000 May 11, 2018
73d710f
Updates
nik9000 May 11, 2018
7eb276b
Move Compose
nik9000 May 11, 2018
9cda8c9
Merge branch 'master' into pr/30523
nik9000 May 14, 2018
f05a7ca
Merge branch 'master' into rest_node_selector
nik9000 May 31, 2018
d4c56a3
Merge branch 'master' into rest_node_selector
nik9000 Jun 1, 2018
0e6c479
Fix javadoc
nik9000 Jun 1, 2018
e13171d
Make NodeSelector work on mutable Iterable
nik9000 Jun 1, 2018
7339b26
Some cleanup
nik9000 Jun 1, 2018
2ae7c27
Backwards
nik9000 Jun 1, 2018
d551dcb
Drop equals from Node
nik9000 Jun 1, 2018
5e0b20b
Restort test
nik9000 Jun 4, 2018
1245ed3
Merge branch 'master' into rest_node_selector
nik9000 Jun 4, 2018
9504841
Sniff on yaml test start
nik9000 Jun 4, 2018
2bc05b3
Remove done todo
nik9000 Jun 4, 2018
8a781de
Merge branch 'master' into rest_node_selector
nik9000 Jun 4, 2018
0a56119
Merge branch 'master' into rest_node_selector
nik9000 Jun 5, 2018
159ad56
Cleanup from review
nik9000 Jun 5, 2018
baf572c
Fix rotation issue with NodeSelectors
nik9000 Jun 6, 2018
1f00b95
Wip
nik9000 Jun 6, 2018
4652073
Merge branch 'master' into rest_node_selector
nik9000 Jun 8, 2018
a6a5b46
Cleanup
nik9000 Jun 8, 2018
7d6ee4f
Reuse method
nik9000 Jun 8, 2018
25280ef
Docs!
nik9000 Jun 11, 2018
5eca290
Fix import order
nik9000 Jun 11, 2018
0ab1b30
Cleanups
nik9000 Jun 11, 2018
a9e682f
Revert "Drop equals from Node"
nik9000 Jun 11, 2018
e29723a
Remove method we don't need any more
nik9000 Jun 11, 2018
07004c2
Merge branch 'master' into rest_node_selector
nik9000 Jun 11, 2018
f030c9b
Words
nik9000 Jun 11, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ int getFailedAttempts() {

@Override
public int compareTo(DeadHostState other) {
if (timeSupplier != other.timeSupplier) {
throw new IllegalArgumentException("can't compare DeadHostStates with different clocks ["
+ timeSupplier + " != " + other.timeSupplier + "]");
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

++

return Long.compare(deadUntilNanos, other.deadUntilNanos);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ private void setHeaders(HttpRequest httpRequest, Collection<Header> requestHeade
*/
private NodeTuple<Iterator<Node>> nextNode(NodeSelector nodeSelector) throws IOException {
NodeTuple<List<Node>> nodeTuple = this.nodeTuple;
List<Node> hosts = selectHosts(nodeTuple, blacklist, lastNodeIndex, System.nanoTime(), nodeSelector);
List<Node> hosts = selectHosts(nodeTuple, blacklist, lastNodeIndex, nodeSelector);
return new NodeTuple<>(hosts.iterator(), nodeTuple.authCache);
}

Expand All @@ -633,7 +633,7 @@ private NodeTuple<Iterator<Node>> nextNode(NodeSelector nodeSelector) throws IOE
*/
static List<Node> selectHosts(NodeTuple<List<Node>> nodeTuple,
Map<HttpHost, DeadHostState> blacklist, AtomicInteger lastNodeIndex,
long now, NodeSelector nodeSelector) throws IOException {
NodeSelector nodeSelector) throws IOException {
/*
* Sort the nodes into living and dead lists.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void testDeadHostStateTimeouts() {
}
}

private static class ConfigurableTimeSupplier implements DeadHostState.TimeSupplier {
static class ConfigurableTimeSupplier implements DeadHostState.TimeSupplier {
long nanoTime;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.elasticsearch.client.DeadHostStateTests.ConfigurableTimeSupplier;
import org.elasticsearch.client.RestClient.NodeTuple;

import java.io.IOException;
Expand Down Expand Up @@ -379,8 +380,8 @@ public String toString() {
Map<HttpHost, DeadHostState> emptyBlacklist = Collections.emptyMap();

// Normal cases where the node selector doesn't reject all living nodes
assertSelectLivingHosts(Arrays.asList(n1, n2, n3), nodeTuple, emptyBlacklist, 0, NodeSelector.ANY);
assertSelectLivingHosts(Arrays.asList(n2, n3), nodeTuple, emptyBlacklist, 0, not1);
assertSelectLivingHosts(Arrays.asList(n1, n2, n3), nodeTuple, emptyBlacklist, NodeSelector.ANY);
assertSelectLivingHosts(Arrays.asList(n2, n3), nodeTuple, emptyBlacklist, not1);

/*
* Try a NodeSelector that excludes all nodes. This should
Expand All @@ -390,24 +391,24 @@ public String toString() {
String message = "NodeSelector [NONE] rejected all nodes, living ["
+ "[host=http://1, version=1], [host=http://2, version=2], "
+ "[host=http://3, version=3]] and dead []";
assertEquals(message, assertSelectAllRejected(nodeTuple, emptyBlacklist, 0, noNodes));
assertEquals(message, assertSelectAllRejected(nodeTuple, emptyBlacklist, noNodes));
}

// Mark all the nodes dead for a few test cases
{
long now = 0;
ConfigurableTimeSupplier timeSupplier = new ConfigurableTimeSupplier();
Map<HttpHost, DeadHostState> blacklist = new HashMap<>();
blacklist.put(n1.getHost(), new DeadHostState(now));
blacklist.put(n2.getHost(), new DeadHostState(new DeadHostState(now), now));
blacklist.put(n3.getHost(), new DeadHostState(new DeadHostState(new DeadHostState(now), now), now));
blacklist.put(n1.getHost(), new DeadHostState(timeSupplier));
blacklist.put(n2.getHost(), new DeadHostState(new DeadHostState(timeSupplier)));
blacklist.put(n3.getHost(), new DeadHostState(new DeadHostState(new DeadHostState(timeSupplier))));

/*
* selectHosts will revive a single host if regardless of
* blacklist time. It'll revive the node that is closest
* to being revived that the NodeSelector is ok with.
*/
assertEquals(singletonList(n1), RestClient.selectHosts(nodeTuple, blacklist, new AtomicInteger(), now, NodeSelector.ANY));
assertEquals(singletonList(n2), RestClient.selectHosts(nodeTuple, blacklist, new AtomicInteger(), now, not1));
assertEquals(singletonList(n1), RestClient.selectHosts(nodeTuple, blacklist, new AtomicInteger(), NodeSelector.ANY));
assertEquals(singletonList(n2), RestClient.selectHosts(nodeTuple, blacklist, new AtomicInteger(), not1));

/*
* Try a NodeSelector that excludes all nodes. This should
Expand All @@ -418,43 +419,42 @@ public String toString() {
String message = "NodeSelector [NONE] rejected all nodes, living [] and dead ["
+ "[host=http://1, version=1], [host=http://2, version=2], "
+ "[host=http://3, version=3]]";
assertEquals(message, assertSelectAllRejected(nodeTuple, blacklist, now, noNodes));
assertEquals(message, assertSelectAllRejected(nodeTuple, blacklist, noNodes));

/*
* Now lets wind the clock forward, past the timeout for one of
* the dead nodes. We should return it.
*/
now = new DeadHostState(now).getDeadUntilNanos();
assertSelectLivingHosts(Arrays.asList(n1), nodeTuple, blacklist, 0, NodeSelector.ANY);
timeSupplier.nanoTime = new DeadHostState(timeSupplier).getDeadUntilNanos();
assertSelectLivingHosts(Arrays.asList(n1), nodeTuple, blacklist, NodeSelector.ANY);

/*
* But if the NodeSelector rejects that node then we'll pick the
* first on that the NodeSelector doesn't reject.
*/
assertSelectLivingHosts(Arrays.asList(n2), nodeTuple, blacklist, 0, not1);
assertSelectLivingHosts(Arrays.asList(n2), nodeTuple, blacklist, not1);

/*
* If we wind the clock way into the future, past any of the
* blacklist timeouts then we function as though the nodes aren't
* in the blacklist at all.
*/
now += DeadHostState.MAX_CONNECTION_TIMEOUT_NANOS;
assertSelectLivingHosts(Arrays.asList(n1, n2, n3), nodeTuple, blacklist, now, NodeSelector.ANY);
assertSelectLivingHosts(Arrays.asList(n2, n3), nodeTuple, blacklist, now, not1);
timeSupplier.nanoTime += DeadHostState.MAX_CONNECTION_TIMEOUT_NANOS;
assertSelectLivingHosts(Arrays.asList(n1, n2, n3), nodeTuple, blacklist, NodeSelector.ANY);
assertSelectLivingHosts(Arrays.asList(n2, n3), nodeTuple, blacklist, not1);
}
}

private void assertSelectLivingHosts(List<Node> expectedNodes, NodeTuple<List<Node>> nodeTuple,
Map<HttpHost, DeadHostState> blacklist, long now, NodeSelector nodeSelector) throws IOException {
Map<HttpHost, DeadHostState> blacklist, NodeSelector nodeSelector) throws IOException {
int iterations = 1000;
AtomicInteger lastNodeIndex = new AtomicInteger(0);
assertEquals(expectedNodes, RestClient.selectHosts(nodeTuple, blacklist,
lastNodeIndex, now, nodeSelector));
assertEquals(expectedNodes, RestClient.selectHosts(nodeTuple, blacklist, lastNodeIndex, nodeSelector));
// Calling it again rotates the set of results
for (int i = 1; i < iterations; i++) {
Collections.rotate(expectedNodes, 1);
assertEquals("iteration " + i, expectedNodes, RestClient.selectHosts(nodeTuple, blacklist,
lastNodeIndex, now, nodeSelector));
assertEquals("iteration " + i, expectedNodes,
RestClient.selectHosts(nodeTuple, blacklist, lastNodeIndex, nodeSelector));
}
}

Expand All @@ -463,9 +463,9 @@ private void assertSelectLivingHosts(List<Node> expectedNodes, NodeTuple<List<No
* @return the message in the exception thrown by the failure
*/
private String assertSelectAllRejected( NodeTuple<List<Node>> nodeTuple,
Map<HttpHost, DeadHostState> blacklist, long now, NodeSelector nodeSelector) {
Map<HttpHost, DeadHostState> blacklist, NodeSelector nodeSelector) {
try {
RestClient.selectHosts(nodeTuple, blacklist, new AtomicInteger(0), now, nodeSelector);
RestClient.selectHosts(nodeTuple, blacklist, new AtomicInteger(0), nodeSelector);
throw new AssertionError("expected selectHosts to fail");
} catch (IOException e) {
return e.getMessage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.http.Consts;
import org.apache.http.HttpHost;
import org.apache.http.client.methods.HttpGet;
import org.elasticsearch.client.Node;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseException;
import org.elasticsearch.client.RestClient;
Expand Down