Remove usage or `InetAddress#getLocalHost`
this method is very confusing and if it's used it's likely the wrong thing with respect to the actual bound / published address. This change discourages it's use and removes all useage. It's replaced with the actual published address most of the time.
This commit is contained in:
parent
20851a4e4a
commit
1e511eda28
|
@ -138,7 +138,7 @@ public class DiscoveryNode implements Streamable, ToXContent {
|
|||
* @param version the version of the node.
|
||||
*/
|
||||
public DiscoveryNode(String nodeName, String nodeId, TransportAddress address, Map<String, String> attributes, Version version) {
|
||||
this(nodeName, nodeId, NetworkUtils.getLocalHost().getHostName(), NetworkUtils.getLocalHost().getHostAddress(), address, attributes, version);
|
||||
this(nodeName, nodeId, address.getHost(), address.getAddress(), address, attributes, version);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -40,6 +40,8 @@ import org.elasticsearch.common.logging.ESLogger;
|
|||
import org.elasticsearch.common.logging.Loggers;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.text.StringText;
|
||||
import org.elasticsearch.common.transport.BoundTransportAddress;
|
||||
import org.elasticsearch.common.transport.TransportAddress;
|
||||
import org.elasticsearch.common.unit.TimeValue;
|
||||
import org.elasticsearch.common.util.concurrent.*;
|
||||
import org.elasticsearch.discovery.Discovery;
|
||||
|
@ -159,7 +161,8 @@ public class InternalClusterService extends AbstractLifecycleComponent<ClusterSe
|
|||
Map<String, String> nodeAttributes = discoveryNodeService.buildAttributes();
|
||||
// note, we rely on the fact that its a new id each time we start, see FD and "kill -9" handling
|
||||
final String nodeId = DiscoveryService.generateNodeId(settings);
|
||||
DiscoveryNode localNode = new DiscoveryNode(settings.get("name"), nodeId, transportService.boundAddress().publishAddress(), nodeAttributes, version);
|
||||
final TransportAddress publishAddress = transportService.boundAddress().publishAddress();
|
||||
DiscoveryNode localNode = new DiscoveryNode(settings.get("name"), nodeId, publishAddress, nodeAttributes, version);
|
||||
DiscoveryNodes.Builder nodeBuilder = DiscoveryNodes.builder().put(localNode).localNodeId(localNode.id());
|
||||
this.clusterState = ClusterState.builder(clusterState).nodes(nodeBuilder).blocks(initialBlocks).build();
|
||||
this.transportService.setLocalNode(localNode);
|
||||
|
|
|
@ -127,17 +127,6 @@ public abstract class NetworkUtils {
|
|||
return Constants.WINDOWS ? false : true;
|
||||
}
|
||||
|
||||
/** Returns localhost, or if its misconfigured, falls back to loopback. Use with caution!!!! */
|
||||
// TODO: can we remove this?
|
||||
public static InetAddress getLocalHost() {
|
||||
try {
|
||||
return InetAddress.getLocalHost();
|
||||
} catch (UnknownHostException e) {
|
||||
logger.warn("failed to resolve local host, fallback to loopback", e);
|
||||
return InetAddress.getLoopbackAddress();
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns addresses for all loopback interfaces that are up. */
|
||||
public static InetAddress[] getLoopbackAddresses() throws SocketException {
|
||||
List<InetAddress> list = new ArrayList<>();
|
||||
|
|
|
@ -44,6 +44,21 @@ public class DummyTransportAddress implements TransportAddress {
|
|||
return other == INSTANCE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHost() {
|
||||
return "dummy";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAddress() {
|
||||
return "0.0.0.0"; // see https://en.wikipedia.org/wiki/0.0.0.0
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPort() {
|
||||
return 42;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DummyTransportAddress readFrom(StreamInput in) throws IOException {
|
||||
return INSTANCE;
|
||||
|
|
|
@ -30,7 +30,7 @@ import java.net.InetSocketAddress;
|
|||
/**
|
||||
* A transport address used for IP socket address (wraps {@link java.net.InetSocketAddress}).
|
||||
*/
|
||||
public class InetSocketTransportAddress implements TransportAddress {
|
||||
public final class InetSocketTransportAddress implements TransportAddress {
|
||||
|
||||
private static boolean resolveAddress = false;
|
||||
|
||||
|
@ -92,6 +92,21 @@ public class InetSocketTransportAddress implements TransportAddress {
|
|||
address.getAddress().equals(((InetSocketTransportAddress) other).address.getAddress());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHost() {
|
||||
return address.getHostName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAddress() {
|
||||
return address.getAddress().getHostAddress();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPort() {
|
||||
return address.getPort();
|
||||
}
|
||||
|
||||
public InetSocketAddress address() {
|
||||
return this.address;
|
||||
}
|
||||
|
|
|
@ -29,7 +29,7 @@ import java.io.IOException;
|
|||
/**
|
||||
*
|
||||
*/
|
||||
public class LocalTransportAddress implements TransportAddress {
|
||||
public final class LocalTransportAddress implements TransportAddress {
|
||||
|
||||
public static final LocalTransportAddress PROTO = new LocalTransportAddress("_na");
|
||||
|
||||
|
@ -57,6 +57,21 @@ public class LocalTransportAddress implements TransportAddress {
|
|||
return other instanceof LocalTransportAddress && id.equals(((LocalTransportAddress) other).id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHost() {
|
||||
return "local";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAddress() {
|
||||
return "0.0.0.0"; // see https://en.wikipedia.org/wiki/0.0.0.0
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPort() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LocalTransportAddress readFrom(StreamInput in) throws IOException {
|
||||
return new LocalTransportAddress(in);
|
||||
|
|
|
@ -28,7 +28,24 @@ import org.elasticsearch.common.io.stream.Writeable;
|
|||
*/
|
||||
public interface TransportAddress extends Writeable<TransportAddress> {
|
||||
|
||||
/**
|
||||
* Returns the host string for this transport address
|
||||
*/
|
||||
String getHost();
|
||||
|
||||
/**
|
||||
* Returns the address string for this transport address
|
||||
*/
|
||||
String getAddress();
|
||||
|
||||
/**
|
||||
* Returns the port of this transport address if applicable
|
||||
*/
|
||||
int getPort();
|
||||
|
||||
short uniqueAddressTypeId();
|
||||
|
||||
boolean sameHost(TransportAddress other);
|
||||
|
||||
public String toString();
|
||||
}
|
||||
|
|
|
@ -27,6 +27,7 @@ import org.elasticsearch.cluster.*;
|
|||
import org.elasticsearch.cluster.node.DiscoveryNode;
|
||||
import org.elasticsearch.cluster.routing.RoutingNode;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.transport.DummyTransportAddress;
|
||||
import org.elasticsearch.monitor.fs.FsInfo;
|
||||
import org.elasticsearch.test.ESIntegTestCase;
|
||||
import org.junit.Test;
|
||||
|
@ -167,7 +168,7 @@ public class MockDiskUsagesIT extends ESIntegTestCase {
|
|||
usage.getTotalBytes(), usage.getFreeBytes(), usage.getFreeBytes());
|
||||
paths[0] = path;
|
||||
FsInfo fsInfo = new FsInfo(System.currentTimeMillis(), paths);
|
||||
return new NodeStats(new DiscoveryNode(nodeName, null, Version.V_2_0_0_beta1),
|
||||
return new NodeStats(new DiscoveryNode(nodeName, DummyTransportAddress.INSTANCE, Version.CURRENT),
|
||||
System.currentTimeMillis(),
|
||||
null, null, null, null, null,
|
||||
fsInfo,
|
||||
|
|
|
@ -108,6 +108,7 @@ import org.junit.Assert;
|
|||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
|
@ -504,7 +505,6 @@ public final class InternalTestCluster extends TestCluster {
|
|||
public static String clusterName(String prefix, long clusterSeed) {
|
||||
StringBuilder builder = new StringBuilder(prefix);
|
||||
final int childVM = RandomizedTest.systemPropertyAsInt(SysGlobals.CHILDVM_SYSPROP_JVM_ID, 0);
|
||||
builder.append('-').append(NetworkUtils.getLocalHost().getHostName());
|
||||
builder.append("-CHILD_VM=[").append(childVM).append(']');
|
||||
builder.append("-CLUSTER_SEED=[").append(clusterSeed).append(']');
|
||||
// if multiple maven task run on a single host we better have an identifier that doesn't rely on input params
|
||||
|
|
|
@ -18,6 +18,9 @@
|
|||
java.net.URL#getPath()
|
||||
java.net.URL#getFile()
|
||||
|
||||
@defaultMessage Usage of getLocalHost is discouraged
|
||||
java.net.InetAddress#getLocalHost()
|
||||
|
||||
@defaultMessage Use java.nio.file instead of java.io.File API
|
||||
java.util.jar.JarFile
|
||||
java.util.zip.ZipFile
|
||||
|
|
Loading…
Reference in New Issue