Merge pull request #13020 from s1monw/issues/13014

Remove support for address resolving in InetSocketTransportAddress
This commit is contained in:
Simon Willnauer 2015-08-20 23:10:50 +02:00
commit aea596241c
3 changed files with 47 additions and 78 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,37 +33,22 @@ 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
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(); 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) { inetAddress = Inet6Address.getByAddress(null, a);
inetAddress = Inet6Address.getByAddress(null, a);
} else {
inetAddress = InetAddress.getByAddress(a);
}
int port = in.readInt();
this.address = new InetSocketAddress(inetAddress, port);
} else { } else {
this.address = new InetSocketAddress(InetAddress.getByName(in.readString()), in.readInt()); inetAddress = InetAddress.getByAddress(a);
} }
int port = in.readInt();
this.address = new InetSocketAddress(inetAddress, port);
} }
private InetSocketTransportAddress() { private InetSocketTransportAddress() {
@ -76,6 +60,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 +82,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 +106,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) { byte[] bytes = address().getAddress().getAddress(); // 4 bytes (IPv4) or 16 bytes (IPv6)
out.writeByte((byte) 0); out.writeByte((byte) bytes.length); // 1 byte
byte[] bytes = address().getAddress().getAddress(); // 4 bytes (IPv4) or 16 bytes (IPv6) out.write(bytes, 0, bytes.length);
out.writeByte((byte) bytes.length); // 1 byte // don't serialize scope ids over the network!!!!
out.write(bytes, 0, bytes.length); // these only make sense with respect to the local machine, and will only formulate
// don't serialize scope ids over the network!!!! // the address incorrectly remotely.
// these only make sense with respect to the local machine, and will only formulate
// 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,40 +1046,34 @@ 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(); ClusterState masterClusterState = client().admin().cluster().prepareState().all().get().getState();
try { byte[] masterClusterStateBytes = ClusterState.Builder.toBytes(masterClusterState);
InetSocketTransportAddress.setResolveAddress(false); // remove local node reference
ClusterState masterClusterState = client().admin().cluster().prepareState().all().get().getState(); masterClusterState = ClusterState.Builder.fromBytes(masterClusterStateBytes, null);
byte[] masterClusterStateBytes = ClusterState.Builder.toBytes(masterClusterState); Map<String, Object> masterStateMap = convertToMap(masterClusterState);
int masterClusterStateSize = masterClusterState.toString().length();
String masterId = masterClusterState.nodes().masterNodeId();
for (Client client : cluster()) {
ClusterState localClusterState = client.admin().cluster().prepareState().all().setLocal(true).get().getState();
byte[] localClusterStateBytes = ClusterState.Builder.toBytes(localClusterState);
// remove local node reference // remove local node reference
masterClusterState = ClusterState.Builder.fromBytes(masterClusterStateBytes, null); localClusterState = ClusterState.Builder.fromBytes(localClusterStateBytes, null);
Map<String, Object> masterStateMap = convertToMap(masterClusterState); final Map<String, Object> localStateMap = convertToMap(localClusterState);
int masterClusterStateSize = masterClusterState.toString().length(); final int localClusterStateSize = localClusterState.toString().length();
String masterId = masterClusterState.nodes().masterNodeId(); // Check that the non-master node has the same version of the cluster state as the master and that this node didn't disconnect from the master
for (Client client : cluster()) { if (masterClusterState.version() == localClusterState.version() && localClusterState.nodes().nodes().containsKey(masterId)) {
ClusterState localClusterState = client.admin().cluster().prepareState().all().setLocal(true).get().getState(); try {
byte[] localClusterStateBytes = ClusterState.Builder.toBytes(localClusterState); assertEquals("clusterstate UUID does not match", masterClusterState.stateUUID(), localClusterState.stateUUID());
// remove local node reference // We cannot compare serialization bytes since serialization order of maps is not guaranteed
localClusterState = ClusterState.Builder.fromBytes(localClusterStateBytes, null); // but we can compare serialization sizes - they should be the same
final Map<String, Object> localStateMap = convertToMap(localClusterState); assertEquals("clusterstate size does not match", masterClusterStateSize, localClusterStateSize);
final int localClusterStateSize = localClusterState.toString().length(); // Compare JSON serialization
// Check that the non-master node has the same version of the cluster state as the master and that this node didn't disconnect from the master assertNull("clusterstate JSON serialization does not match", differenceBetweenMapsIgnoringArrayOrder(masterStateMap, localStateMap));
if (masterClusterState.version() == localClusterState.version() && localClusterState.nodes().nodes().containsKey(masterId)) { } catch (AssertionError error) {
try { logger.error("Cluster state from master:\n{}\nLocal cluster state:\n{}", masterClusterState.toString(), localClusterState.toString());
assertEquals("clusterstate UUID does not match", masterClusterState.stateUUID(), localClusterState.stateUUID()); throw error;
// We cannot compare serialization bytes since serialization order of maps is not guaranteed
// but we can compare serialization sizes - they should be the same
assertEquals("clusterstate size does not match", masterClusterStateSize, localClusterStateSize);
// Compare JSON serialization
assertNull("clusterstate JSON serialization does not match", differenceBetweenMapsIgnoringArrayOrder(masterStateMap, localStateMap));
} catch (AssertionError error) {
logger.error("Cluster state from master:\n{}\nLocal cluster state:\n{}", masterClusterState.toString(), localClusterState.toString());
throw error;
}
} }
} }
} finally {
InetSocketTransportAddress.setResolveAddress(getResolvedAddress);
} }
} }