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

Simplify TransportAddress #20798

Merged
merged 2 commits into from Oct 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -43,6 +43,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.function.Predicate;

/**
* We enforce limits once any network host is configured. In this case we assume the node is running in production
Expand Down Expand Up @@ -144,8 +145,10 @@ static void log(final Logger logger, final String error) {
*/
// visible for testing
static boolean enforceLimits(BoundTransportAddress boundTransportAddress) {
return !(Arrays.stream(boundTransportAddress.boundAddresses()).allMatch(TransportAddress::isLoopbackOrLinkLocalAddress) &&
boundTransportAddress.publishAddress().isLoopbackOrLinkLocalAddress());
Predicate<TransportAddress> isLoopbackOrLinkLocalAddress = t -> t.address().getAddress().isLinkLocalAddress()
|| t.address().getAddress().isLoopbackAddress();
return !(Arrays.stream(boundTransportAddress.boundAddresses()).allMatch(isLoopbackOrLinkLocalAddress) &&
isLoopbackOrLinkLocalAddress.test(boundTransportAddress.publishAddress()));
}

// the list of checks to execute
Expand Down
Expand Up @@ -135,7 +135,7 @@ public DiscoveryNode(String id, TransportAddress address, Map<String, String> at
*/
public DiscoveryNode(String nodeName, String nodeId, TransportAddress address,
Map<String, String> attributes, Set<Role> roles, Version version) {
this(nodeName, nodeId, UUIDs.randomBase64UUID(), address.getHost(), address.getAddress(), address, attributes, roles, version);
this(nodeName, nodeId, UUIDs.randomBase64UUID(), address.getAddress(), address.getAddress(), address, attributes, roles, version);
}

/**
Expand Down
Expand Up @@ -78,7 +78,7 @@ public TransportAddress(StreamInput in) throws IOException {

@Override
public void writeTo(StreamOutput out) throws IOException {
byte[] bytes = address().getAddress().getAddress(); // 4 bytes (IPv4) or 16 bytes (IPv6)
byte[] bytes = address.getAddress().getAddress(); // 4 bytes (IPv4) or 16 bytes (IPv6)
out.writeByte((byte) bytes.length); // 1 byte
out.write(bytes, 0, bytes.length);
// don't serialize scope ids over the network!!!!
Expand All @@ -87,26 +87,24 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeInt(address.getPort());
}

public boolean sameHost(TransportAddress other) {
return address.getAddress().equals(other.address.getAddress());
}

public boolean isLoopbackOrLinkLocalAddress() {
return address.getAddress().isLinkLocalAddress() || address.getAddress().isLoopbackAddress();
}

public String getHost() {
return getAddress(); // just delegate no resolving
}

/**
* Returns a string representation of the enclosed {@link InetSocketAddress}
* @see NetworkAddress#format(InetAddress)
*/
public String getAddress() {
return NetworkAddress.format(address.getAddress());
}

/**
* Returns the addresses port
*/
public int getPort() {
return address.getPort();
}

/**
* Returns the enclosed {@link InetSocketAddress}
*/
public InetSocketAddress address() {
return this.address;
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/test/java/org/elasticsearch/tribe/TribeIT.java
Expand Up @@ -207,7 +207,7 @@ private Settings.Builder createTribeSettings(Predicate<InternalTestCluster> filt
Set<String> hosts = new HashSet<>();
for (Transport transport : c.getInstances(Transport.class)) {
TransportAddress address = transport.boundAddress().publishAddress();
hosts.add(address.getHost() + ":" + address.getPort());
hosts.add(address.getAddress() + ":" + address.getPort());
}
settings.putArray(tribeSetting + UnicastZenPing.DISCOVERY_ZEN_PING_UNICAST_HOSTS_SETTING.getKey(),
hosts.toArray(new String[hosts.size()]));
Expand Down
Expand Up @@ -89,16 +89,16 @@ public void fetchTransportAddress() {
}

public void testReindexFromRemoteWithAuthentication() throws Exception {
RemoteInfo remote = new RemoteInfo("http", address.getHost(), address.getPort(), new BytesArray("{\"match_all\":{}}"), "Aladdin",
RemoteInfo remote = new RemoteInfo("http", address.getAddress(), address.getPort(), new BytesArray("{\"match_all\":{}}"), "Aladdin",
"open sesame", emptyMap());
ReindexRequestBuilder request = ReindexAction.INSTANCE.newRequestBuilder(client()).source("source").destination("dest")
.setRemoteInfo(remote);
assertThat(request.get(), matcher().created(1));
}

public void testReindexSendsHeaders() throws Exception {
RemoteInfo remote = new RemoteInfo("http", address.getHost(), address.getPort(), new BytesArray("{\"match_all\":{}}"), null, null,
singletonMap(TestFilter.EXAMPLE_HEADER, "doesn't matter"));
RemoteInfo remote = new RemoteInfo("http", address.getAddress(), address.getPort(), new BytesArray("{\"match_all\":{}}"), null,
null, singletonMap(TestFilter.EXAMPLE_HEADER, "doesn't matter"));
ReindexRequestBuilder request = ReindexAction.INSTANCE.newRequestBuilder(client()).source("source").destination("dest")
.setRemoteInfo(remote);
ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, () -> request.get());
Expand All @@ -107,8 +107,8 @@ public void testReindexSendsHeaders() throws Exception {
}

public void testReindexWithoutAuthenticationWhenRequired() throws Exception {
RemoteInfo remote = new RemoteInfo("http", address.getHost(), address.getPort(), new BytesArray("{\"match_all\":{}}"), null, null,
emptyMap());
RemoteInfo remote = new RemoteInfo("http", address.getAddress(), address.getPort(), new BytesArray("{\"match_all\":{}}"), null,
null, emptyMap());
ReindexRequestBuilder request = ReindexAction.INSTANCE.newRequestBuilder(client()).source("source").destination("dest")
.setRemoteInfo(remote);
ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, () -> request.get());
Expand All @@ -118,7 +118,7 @@ public void testReindexWithoutAuthenticationWhenRequired() throws Exception {
}

public void testReindexWithBadAuthentication() throws Exception {
RemoteInfo remote = new RemoteInfo("http", address.getHost(), address.getPort(), new BytesArray("{\"match_all\":{}}"), "junk",
RemoteInfo remote = new RemoteInfo("http", address.getAddress(), address.getPort(), new BytesArray("{\"match_all\":{}}"), "junk",
"auth", emptyMap());
ReindexRequestBuilder request = ReindexAction.INSTANCE.newRequestBuilder(client()).source("source").destination("dest")
.setRemoteInfo(remote);
Expand Down
Expand Up @@ -125,8 +125,8 @@ public void testReindex() throws Exception {
public void testReindexFromRemote() throws Exception {
NodeInfo nodeInfo = client().admin().cluster().prepareNodesInfo().get().getNodes().get(0);
TransportAddress address = nodeInfo.getHttp().getAddress().publishAddress();
RemoteInfo remote = new RemoteInfo("http", address.getHost(), address.getPort(), new BytesArray("{\"match_all\":{}}"), null, null,
emptyMap());
RemoteInfo remote = new RemoteInfo("http", address.getAddress(), address.getPort(), new BytesArray("{\"match_all\":{}}"), null,
null, emptyMap());
ReindexRequestBuilder request = ReindexAction.INSTANCE.newRequestBuilder(client()).source("source").destination("dest")
.setRemoteInfo(remote);
testCase(ReindexAction.NAME, request, matcher().created(DOC_COUNT));
Expand Down
Expand Up @@ -81,13 +81,13 @@ public void testBuildDynamicNodes() throws Exception {
final List<String> hostEntries = Arrays.asList("#comment, should be ignored", "192.168.0.1", "192.168.0.2:9305", "255.255.23.15");
final List<DiscoveryNode> nodes = setupAndRunHostProvider(hostEntries);
assertEquals(hostEntries.size() - 1, nodes.size()); // minus 1 because we are ignoring the first line that's a comment
assertEquals("192.168.0.1", nodes.get(0).getAddress().getHost());
assertEquals("192.168.0.1", nodes.get(0).getAddress().getAddress());
assertEquals(9300, nodes.get(0).getAddress().getPort());
assertEquals(UNICAST_HOST_PREFIX + "1#", nodes.get(0).getId());
assertEquals("192.168.0.2", nodes.get(1).getAddress().getHost());
assertEquals("192.168.0.2", nodes.get(1).getAddress().getAddress());
assertEquals(9305, nodes.get(1).getAddress().getPort());
assertEquals(UNICAST_HOST_PREFIX + "2#", nodes.get(1).getId());
assertEquals("255.255.23.15", nodes.get(2).getAddress().getHost());
assertEquals("255.255.23.15", nodes.get(2).getAddress().getAddress());
assertEquals(9300, nodes.get(2).getAddress().getPort());
assertEquals(UNICAST_HOST_PREFIX + "3#", nodes.get(2).getId());
}
Expand Down Expand Up @@ -117,7 +117,7 @@ public void testSomeInvalidHostEntries() throws Exception {
List<String> hostEntries = Arrays.asList("192.168.0.1:9300:9300", "192.168.0.1:9301");
List<DiscoveryNode> nodes = setupAndRunHostProvider(hostEntries);
assertEquals(1, nodes.size()); // only one of the two is valid and will be used
assertEquals("192.168.0.1", nodes.get(0).getAddress().getHost());
assertEquals("192.168.0.1", nodes.get(0).getAddress().getAddress());
assertEquals(9301, nodes.get(0).getAddress().getPort());
}

Expand Down
Expand Up @@ -1412,8 +1412,8 @@ public String executor() {
fail("message round trip did not complete within a sensible time frame");
}

assertTrue(nodeA.getAddress().sameHost(addressA.get()));
assertTrue(nodeB.getAddress().sameHost(addressB.get()));
assertTrue(nodeA.getAddress().getAddress().equals(addressA.get().getAddress()));
assertTrue(nodeB.getAddress().getAddress().equals(addressB.get().getAddress()));
}

public void testBlockingIncomingRequests() throws Exception {
Expand Down