Remove support for address resolving in InetSocketTransportAddress

this commit removes all support for reverse host name resolving from
InetSocketTransportAddress. This class now only returns IP addresses.

Closes #13014
This commit is contained in:
Simon Willnauer 2015-08-20 20:57:26 +02:00
parent e2ab62596f
commit 214d303e6a
3 changed files with 48 additions and 83 deletions

View File

@ -83,7 +83,6 @@ public class NetworkService extends AbstractComponent {
public NetworkService(Settings settings) { public NetworkService(Settings settings) {
super(settings); super(settings);
IfConfig.logIfNecessary(); IfConfig.logIfNecessary();
InetSocketTransportAddress.setResolveAddress(settings.getAsBoolean("network.address.serialization.resolve", false));
} }
/** /**

View File

@ -19,7 +19,6 @@
package org.elasticsearch.common.transport; package org.elasticsearch.common.transport;
import org.elasticsearch.common.SuppressForbidden;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.network.NetworkAddress; import org.elasticsearch.common.network.NetworkAddress;
@ -34,25 +33,13 @@ import java.net.InetSocketAddress;
*/ */
public final class InetSocketTransportAddress implements TransportAddress { public final class InetSocketTransportAddress implements TransportAddress {
// TODO: do we really need this option, why do resolving? - remove this as a follow-up public static final InetSocketTransportAddress PROTO = new InetSocketTransportAddress(new InetSocketAddress("127.0.0.1", 0));
private static boolean resolveAddress = false;
public static void setResolveAddress(boolean resolveAddress) {
InetSocketTransportAddress.resolveAddress = resolveAddress;
}
public static boolean getResolveAddress() {
return resolveAddress;
}
public static final InetSocketTransportAddress PROTO = new InetSocketTransportAddress();
private final InetSocketAddress address; private final InetSocketAddress address;
public InetSocketTransportAddress(StreamInput in) throws IOException { public InetSocketTransportAddress(StreamInput in) throws IOException {
if (in.readByte() == 0) { final int len = in.readByte();
int len = in.readByte(); final byte[] a = new byte[len]; // 4 bytes (IPv4) or 16 bytes (IPv6)
byte[] a = new byte[len]; // 4 bytes (IPv4) or 16 bytes (IPv6)
in.readFully(a); in.readFully(a);
InetAddress inetAddress; InetAddress inetAddress;
if (len == 16) { if (len == 16) {
@ -62,13 +49,6 @@ public final class InetSocketTransportAddress implements TransportAddress {
} }
int port = in.readInt(); int port = in.readInt();
this.address = new InetSocketAddress(inetAddress, port); this.address = new InetSocketAddress(inetAddress, port);
} else {
this.address = new InetSocketAddress(InetAddress.getByName(in.readString()), in.readInt());
}
}
private InetSocketTransportAddress() {
address = null;
} }
public InetSocketTransportAddress(InetAddress address, int port) { public InetSocketTransportAddress(InetAddress address, int port) {
@ -76,6 +56,12 @@ public final class InetSocketTransportAddress implements TransportAddress {
} }
public InetSocketTransportAddress(InetSocketAddress address) { public InetSocketTransportAddress(InetSocketAddress address) {
if (address == null) {
throw new IllegalArgumentException("InetSocketAddress must not be null");
}
if (address.getAddress() == null) {
throw new IllegalArgumentException("Address must be resolved but wasn't - InetSocketAddress#getAddress() returned null");
}
this.address = address; this.address = address;
} }
@ -92,7 +78,7 @@ public final class InetSocketTransportAddress implements TransportAddress {
@Override @Override
public String getHost() { public String getHost() {
return maybeLookupHostname(); return getAddress(); // just delegate no resolving
} }
@Override @Override
@ -116,29 +102,15 @@ public final class InetSocketTransportAddress implements TransportAddress {
@Override @Override
public void writeTo(StreamOutput out) throws IOException { public void writeTo(StreamOutput out) throws IOException {
if (!resolveAddress && address.getAddress() != null) {
out.writeByte((byte) 0);
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!!!!
// these only make sense with respect to the local machine, and will only formulate // these only make sense with respect to the local machine, and will only formulate
// the address incorrectly remotely. // the address incorrectly remotely.
} else {
out.writeByte((byte) 1);
out.writeString(maybeLookupHostname());
}
out.writeInt(address.getPort()); out.writeInt(address.getPort());
} }
@SuppressForbidden(reason = "if explicitly configured we do hostName reverse lookup") // TODO remove this?
private String maybeLookupHostname() {
if (resolveAddress) {
return address.getHostName();
} else {
return getAddress();
}
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {

View File

@ -1046,9 +1046,6 @@ public abstract class ESIntegTestCase extends ESTestCase {
*/ */
protected void ensureClusterStateConsistency() throws IOException { protected void ensureClusterStateConsistency() throws IOException {
if (cluster() != null) { if (cluster() != null) {
boolean getResolvedAddress = InetSocketTransportAddress.getResolveAddress();
try {
InetSocketTransportAddress.setResolveAddress(false);
ClusterState masterClusterState = client().admin().cluster().prepareState().all().get().getState(); ClusterState masterClusterState = client().admin().cluster().prepareState().all().get().getState();
byte[] masterClusterStateBytes = ClusterState.Builder.toBytes(masterClusterState); byte[] masterClusterStateBytes = ClusterState.Builder.toBytes(masterClusterState);
// remove local node reference // remove local node reference
@ -1078,9 +1075,6 @@ public abstract class ESIntegTestCase extends ESTestCase {
} }
} }
} }
} finally {
InetSocketTransportAddress.setResolveAddress(getResolvedAddress);
}
} }
} }