From 7452028e501a8e38d8277073e13fe45171c6a208 Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Fri, 7 Oct 2016 15:56:54 +0200 Subject: [PATCH] Simplify TransportAddress (#20798) since TransportAddress is now final we can simplify it's interface a bit and remove methods that are only used in tests or are plain delegates. --- .../bootstrap/BootstrapCheck.java | 7 ++++-- .../cluster/node/DiscoveryNode.java | 2 +- .../common/transport/TransportAddress.java | 24 +++++++++---------- .../java/org/elasticsearch/tribe/TribeIT.java | 2 +- .../ReindexFromRemoteWithAuthTests.java | 12 +++++----- .../index/reindex/RetryTests.java | 4 ++-- .../FileBasedUnicastHostsProviderTests.java | 8 +++---- .../AbstractSimpleTransportTestCase.java | 4 ++-- 8 files changed, 32 insertions(+), 31 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/bootstrap/BootstrapCheck.java b/core/src/main/java/org/elasticsearch/bootstrap/BootstrapCheck.java index de80b487c7e..2d085df43b2 100644 --- a/core/src/main/java/org/elasticsearch/bootstrap/BootstrapCheck.java +++ b/core/src/main/java/org/elasticsearch/bootstrap/BootstrapCheck.java @@ -43,6 +43,7 @@ import java.util.Arrays; 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 @@ -144,8 +145,10 @@ final class BootstrapCheck { */ // visible for testing static boolean enforceLimits(BoundTransportAddress boundTransportAddress) { - return !(Arrays.stream(boundTransportAddress.boundAddresses()).allMatch(TransportAddress::isLoopbackOrLinkLocalAddress) && - boundTransportAddress.publishAddress().isLoopbackOrLinkLocalAddress()); + Predicate 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 diff --git a/core/src/main/java/org/elasticsearch/cluster/node/DiscoveryNode.java b/core/src/main/java/org/elasticsearch/cluster/node/DiscoveryNode.java index c4197809ad0..e90ada51022 100644 --- a/core/src/main/java/org/elasticsearch/cluster/node/DiscoveryNode.java +++ b/core/src/main/java/org/elasticsearch/cluster/node/DiscoveryNode.java @@ -135,7 +135,7 @@ public class DiscoveryNode implements Writeable, ToXContent { */ public DiscoveryNode(String nodeName, String nodeId, TransportAddress address, Map attributes, Set 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); } /** diff --git a/core/src/main/java/org/elasticsearch/common/transport/TransportAddress.java b/core/src/main/java/org/elasticsearch/common/transport/TransportAddress.java index 3656fc251c4..9c55f2d2f18 100644 --- a/core/src/main/java/org/elasticsearch/common/transport/TransportAddress.java +++ b/core/src/main/java/org/elasticsearch/common/transport/TransportAddress.java @@ -78,7 +78,7 @@ public final class TransportAddress implements Writeable { @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!!!! @@ -87,26 +87,24 @@ public final class TransportAddress implements Writeable { 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; } diff --git a/core/src/test/java/org/elasticsearch/tribe/TribeIT.java b/core/src/test/java/org/elasticsearch/tribe/TribeIT.java index 3d8689d5ca9..89a32c11828 100644 --- a/core/src/test/java/org/elasticsearch/tribe/TribeIT.java +++ b/core/src/test/java/org/elasticsearch/tribe/TribeIT.java @@ -207,7 +207,7 @@ public class TribeIT extends ESIntegTestCase { Set 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()])); diff --git a/modules/reindex/src/test/java/org/elasticsearch/index/reindex/ReindexFromRemoteWithAuthTests.java b/modules/reindex/src/test/java/org/elasticsearch/index/reindex/ReindexFromRemoteWithAuthTests.java index 5c7a90157e7..e3401e4cf39 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/index/reindex/ReindexFromRemoteWithAuthTests.java +++ b/modules/reindex/src/test/java/org/elasticsearch/index/reindex/ReindexFromRemoteWithAuthTests.java @@ -89,7 +89,7 @@ public class ReindexFromRemoteWithAuthTests extends ESSingleNodeTestCase { } 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); @@ -97,8 +97,8 @@ public class ReindexFromRemoteWithAuthTests extends ESSingleNodeTestCase { } 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()); @@ -107,8 +107,8 @@ public class ReindexFromRemoteWithAuthTests extends ESSingleNodeTestCase { } 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()); @@ -118,7 +118,7 @@ public class ReindexFromRemoteWithAuthTests extends ESSingleNodeTestCase { } 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); diff --git a/modules/reindex/src/test/java/org/elasticsearch/index/reindex/RetryTests.java b/modules/reindex/src/test/java/org/elasticsearch/index/reindex/RetryTests.java index 92e2598a5aa..bd43a32945e 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/index/reindex/RetryTests.java +++ b/modules/reindex/src/test/java/org/elasticsearch/index/reindex/RetryTests.java @@ -125,8 +125,8 @@ public class RetryTests extends ESSingleNodeTestCase { 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)); diff --git a/plugins/discovery-file/src/test/java/org/elasticsearch/discovery/file/FileBasedUnicastHostsProviderTests.java b/plugins/discovery-file/src/test/java/org/elasticsearch/discovery/file/FileBasedUnicastHostsProviderTests.java index f38ae218ec0..fc7149ffafc 100644 --- a/plugins/discovery-file/src/test/java/org/elasticsearch/discovery/file/FileBasedUnicastHostsProviderTests.java +++ b/plugins/discovery-file/src/test/java/org/elasticsearch/discovery/file/FileBasedUnicastHostsProviderTests.java @@ -81,13 +81,13 @@ public class FileBasedUnicastHostsProviderTests extends ESTestCase { final List hostEntries = Arrays.asList("#comment, should be ignored", "192.168.0.1", "192.168.0.2:9305", "255.255.23.15"); final List 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()); } @@ -117,7 +117,7 @@ public class FileBasedUnicastHostsProviderTests extends ESTestCase { List hostEntries = Arrays.asList("192.168.0.1:9300:9300", "192.168.0.1:9301"); List 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()); } diff --git a/test/framework/src/main/java/org/elasticsearch/transport/AbstractSimpleTransportTestCase.java b/test/framework/src/main/java/org/elasticsearch/transport/AbstractSimpleTransportTestCase.java index c05afdc69e3..06c7b2c81fa 100644 --- a/test/framework/src/main/java/org/elasticsearch/transport/AbstractSimpleTransportTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/transport/AbstractSimpleTransportTestCase.java @@ -1412,8 +1412,8 @@ public abstract class AbstractSimpleTransportTestCase extends ESTestCase { 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 {