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.
This commit is contained in:
Simon Willnauer 2016-10-07 15:56:54 +02:00 committed by GitHub
parent eb62f90578
commit 7452028e50
8 changed files with 32 additions and 31 deletions

View File

@ -43,6 +43,7 @@ import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Locale; 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 * 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 // visible for testing
static boolean enforceLimits(BoundTransportAddress boundTransportAddress) { static boolean enforceLimits(BoundTransportAddress boundTransportAddress) {
return !(Arrays.stream(boundTransportAddress.boundAddresses()).allMatch(TransportAddress::isLoopbackOrLinkLocalAddress) && Predicate<TransportAddress> isLoopbackOrLinkLocalAddress = t -> t.address().getAddress().isLinkLocalAddress()
boundTransportAddress.publishAddress().isLoopbackOrLinkLocalAddress()); || t.address().getAddress().isLoopbackAddress();
return !(Arrays.stream(boundTransportAddress.boundAddresses()).allMatch(isLoopbackOrLinkLocalAddress) &&
isLoopbackOrLinkLocalAddress.test(boundTransportAddress.publishAddress()));
} }
// the list of checks to execute // the list of checks to execute

View File

@ -135,7 +135,7 @@ public class DiscoveryNode implements Writeable, ToXContent {
*/ */
public DiscoveryNode(String nodeName, String nodeId, TransportAddress address, public DiscoveryNode(String nodeName, String nodeId, TransportAddress address,
Map<String, String> attributes, Set<Role> roles, Version version) { 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);
} }
/** /**

View File

@ -78,7 +78,7 @@ public final class TransportAddress implements Writeable {
@Override @Override
public void writeTo(StreamOutput out) throws IOException { 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.writeByte((byte) bytes.length); // 1 byte
out.write(bytes, 0, bytes.length); out.write(bytes, 0, bytes.length);
// don't serialize scope ids over the network!!!! // don't serialize scope ids over the network!!!!
@ -87,26 +87,24 @@ public final class TransportAddress implements Writeable {
out.writeInt(address.getPort()); out.writeInt(address.getPort());
} }
public boolean sameHost(TransportAddress other) { /**
return address.getAddress().equals(other.address.getAddress()); * Returns a string representation of the enclosed {@link InetSocketAddress}
} * @see NetworkAddress#format(InetAddress)
*/
public boolean isLoopbackOrLinkLocalAddress() {
return address.getAddress().isLinkLocalAddress() || address.getAddress().isLoopbackAddress();
}
public String getHost() {
return getAddress(); // just delegate no resolving
}
public String getAddress() { public String getAddress() {
return NetworkAddress.format(address.getAddress()); return NetworkAddress.format(address.getAddress());
} }
/**
* Returns the addresses port
*/
public int getPort() { public int getPort() {
return address.getPort(); return address.getPort();
} }
/**
* Returns the enclosed {@link InetSocketAddress}
*/
public InetSocketAddress address() { public InetSocketAddress address() {
return this.address; return this.address;
} }

View File

@ -207,7 +207,7 @@ public class TribeIT extends ESIntegTestCase {
Set<String> hosts = new HashSet<>(); Set<String> hosts = new HashSet<>();
for (Transport transport : c.getInstances(Transport.class)) { for (Transport transport : c.getInstances(Transport.class)) {
TransportAddress address = transport.boundAddress().publishAddress(); 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(), settings.putArray(tribeSetting + UnicastZenPing.DISCOVERY_ZEN_PING_UNICAST_HOSTS_SETTING.getKey(),
hosts.toArray(new String[hosts.size()])); hosts.toArray(new String[hosts.size()]));

View File

@ -89,7 +89,7 @@ public class ReindexFromRemoteWithAuthTests extends ESSingleNodeTestCase {
} }
public void testReindexFromRemoteWithAuthentication() throws Exception { 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()); "open sesame", emptyMap());
ReindexRequestBuilder request = ReindexAction.INSTANCE.newRequestBuilder(client()).source("source").destination("dest") ReindexRequestBuilder request = ReindexAction.INSTANCE.newRequestBuilder(client()).source("source").destination("dest")
.setRemoteInfo(remote); .setRemoteInfo(remote);
@ -97,8 +97,8 @@ public class ReindexFromRemoteWithAuthTests extends ESSingleNodeTestCase {
} }
public void testReindexSendsHeaders() throws Exception { public void testReindexSendsHeaders() throws Exception {
RemoteInfo remote = new RemoteInfo("http", address.getHost(), address.getPort(), new BytesArray("{\"match_all\":{}}"), null, null, RemoteInfo remote = new RemoteInfo("http", address.getAddress(), address.getPort(), new BytesArray("{\"match_all\":{}}"), null,
singletonMap(TestFilter.EXAMPLE_HEADER, "doesn't matter")); null, singletonMap(TestFilter.EXAMPLE_HEADER, "doesn't matter"));
ReindexRequestBuilder request = ReindexAction.INSTANCE.newRequestBuilder(client()).source("source").destination("dest") ReindexRequestBuilder request = ReindexAction.INSTANCE.newRequestBuilder(client()).source("source").destination("dest")
.setRemoteInfo(remote); .setRemoteInfo(remote);
ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, () -> request.get()); ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, () -> request.get());
@ -107,8 +107,8 @@ public class ReindexFromRemoteWithAuthTests extends ESSingleNodeTestCase {
} }
public void testReindexWithoutAuthenticationWhenRequired() throws Exception { public void testReindexWithoutAuthenticationWhenRequired() throws Exception {
RemoteInfo remote = new RemoteInfo("http", address.getHost(), address.getPort(), new BytesArray("{\"match_all\":{}}"), null, null, RemoteInfo remote = new RemoteInfo("http", address.getAddress(), address.getPort(), new BytesArray("{\"match_all\":{}}"), null,
emptyMap()); null, emptyMap());
ReindexRequestBuilder request = ReindexAction.INSTANCE.newRequestBuilder(client()).source("source").destination("dest") ReindexRequestBuilder request = ReindexAction.INSTANCE.newRequestBuilder(client()).source("source").destination("dest")
.setRemoteInfo(remote); .setRemoteInfo(remote);
ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, () -> request.get()); ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, () -> request.get());
@ -118,7 +118,7 @@ public class ReindexFromRemoteWithAuthTests extends ESSingleNodeTestCase {
} }
public void testReindexWithBadAuthentication() 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()); "auth", emptyMap());
ReindexRequestBuilder request = ReindexAction.INSTANCE.newRequestBuilder(client()).source("source").destination("dest") ReindexRequestBuilder request = ReindexAction.INSTANCE.newRequestBuilder(client()).source("source").destination("dest")
.setRemoteInfo(remote); .setRemoteInfo(remote);

View File

@ -125,8 +125,8 @@ public class RetryTests extends ESSingleNodeTestCase {
public void testReindexFromRemote() throws Exception { public void testReindexFromRemote() throws Exception {
NodeInfo nodeInfo = client().admin().cluster().prepareNodesInfo().get().getNodes().get(0); NodeInfo nodeInfo = client().admin().cluster().prepareNodesInfo().get().getNodes().get(0);
TransportAddress address = nodeInfo.getHttp().getAddress().publishAddress(); TransportAddress address = nodeInfo.getHttp().getAddress().publishAddress();
RemoteInfo remote = new RemoteInfo("http", address.getHost(), address.getPort(), new BytesArray("{\"match_all\":{}}"), null, null, RemoteInfo remote = new RemoteInfo("http", address.getAddress(), address.getPort(), new BytesArray("{\"match_all\":{}}"), null,
emptyMap()); null, emptyMap());
ReindexRequestBuilder request = ReindexAction.INSTANCE.newRequestBuilder(client()).source("source").destination("dest") ReindexRequestBuilder request = ReindexAction.INSTANCE.newRequestBuilder(client()).source("source").destination("dest")
.setRemoteInfo(remote); .setRemoteInfo(remote);
testCase(ReindexAction.NAME, request, matcher().created(DOC_COUNT)); testCase(ReindexAction.NAME, request, matcher().created(DOC_COUNT));

View File

@ -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<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); 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(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(9300, nodes.get(0).getAddress().getPort());
assertEquals(UNICAST_HOST_PREFIX + "1#", nodes.get(0).getId()); 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(9305, nodes.get(1).getAddress().getPort());
assertEquals(UNICAST_HOST_PREFIX + "2#", nodes.get(1).getId()); 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(9300, nodes.get(2).getAddress().getPort());
assertEquals(UNICAST_HOST_PREFIX + "3#", nodes.get(2).getId()); 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<String> hostEntries = Arrays.asList("192.168.0.1:9300:9300", "192.168.0.1:9301");
List<DiscoveryNode> nodes = setupAndRunHostProvider(hostEntries); List<DiscoveryNode> nodes = setupAndRunHostProvider(hostEntries);
assertEquals(1, nodes.size()); // only one of the two is valid and will be used 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()); assertEquals(9301, nodes.get(0).getAddress().getPort());
} }

View File

@ -1412,8 +1412,8 @@ public abstract class AbstractSimpleTransportTestCase extends ESTestCase {
fail("message round trip did not complete within a sensible time frame"); fail("message round trip did not complete within a sensible time frame");
} }
assertTrue(nodeA.getAddress().sameHost(addressA.get())); assertTrue(nodeA.getAddress().getAddress().equals(addressA.get().getAddress()));
assertTrue(nodeB.getAddress().sameHost(addressB.get())); assertTrue(nodeB.getAddress().getAddress().equals(addressB.get().getAddress()));
} }
public void testBlockingIncomingRequests() throws Exception { public void testBlockingIncomingRequests() throws Exception {