Browse Source

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.
Simon Willnauer 9 years ago
parent
commit
7452028e50

+ 5 - 2
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<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

+ 1 - 1
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<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);
     }
 
     /**

+ 11 - 13
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;
     }

+ 1 - 1
core/src/test/java/org/elasticsearch/tribe/TribeIT.java

@@ -207,7 +207,7 @@ public class TribeIT extends ESIntegTestCase {
             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()]));

+ 6 - 6
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);

+ 2 - 2
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));

+ 4 - 4
plugins/discovery-file/src/test/java/org/elasticsearch/discovery/file/FileBasedUnicastHostsProviderTests.java

@@ -81,13 +81,13 @@ public class FileBasedUnicastHostsProviderTests extends ESTestCase {
         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());
     }
@@ -117,7 +117,7 @@ public class FileBasedUnicastHostsProviderTests extends ESTestCase {
         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());
     }
 

+ 2 - 2
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 {