diff options
author | hakonhall <hakon@yahoo-inc.com> | 2016-10-14 11:26:02 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-10-14 11:26:02 +0200 |
commit | aec5a34fc520b066d99e3424d552031a914e26a5 (patch) | |
tree | 5b58361ee85a99f9495f8188bf69f67bc11804ba | |
parent | 9dbec23e389de09ccd67f6ac8d87710887dae3a6 (diff) |
Revert "Revert "Use HostName for message bus, but fix HostName to work with dev machines""
27 files changed, 172 insertions, 225 deletions
diff --git a/config-model/src/main/java/com/yahoo/config/model/provision/Hosts.java b/config-model/src/main/java/com/yahoo/config/model/provision/Hosts.java index 6a5abee7cb3..fbd5e712d3d 100644 --- a/config-model/src/main/java/com/yahoo/config/model/provision/Hosts.java +++ b/config-model/src/main/java/com/yahoo/config/model/provision/Hosts.java @@ -2,7 +2,6 @@ package com.yahoo.config.model.provision; import com.google.common.collect.ImmutableMap; -import com.yahoo.cloud.config.ConfigserverConfig; import com.yahoo.config.model.builder.xml.XmlHelper; import com.yahoo.net.HostName; import com.yahoo.text.XML; @@ -76,7 +75,7 @@ public class Hosts { throw new RuntimeException("Missing 'name' attribute for host."); } if ("localhost".equals(name)) { - name = HostName.getLocalhost(); + name = HostName.getHostName(); } final List<String> hostAliases = VespaDomBuilder.getHostAliases(hostE.getChildNodes()); if (hostAliases.isEmpty()) { diff --git a/config-model/src/main/java/com/yahoo/config/model/provision/SingleNodeProvisioner.java b/config-model/src/main/java/com/yahoo/config/model/provision/SingleNodeProvisioner.java index fe8b3935fcf..fb706e7201f 100644 --- a/config-model/src/main/java/com/yahoo/config/model/provision/SingleNodeProvisioner.java +++ b/config-model/src/main/java/com/yahoo/config/model/provision/SingleNodeProvisioner.java @@ -26,7 +26,7 @@ public class SingleNodeProvisioner implements HostProvisioner { public SingleNodeProvisioner() { try { - host = new Host(HostSystem.lookupCanonicalHostname(HostName.getLocalhost())); + host = new Host(HostSystem.lookupCanonicalHostname(HostName.getHostName())); this.hostSpec = new HostSpec(host.hostname(), host.aliases()); } catch (UnknownHostException e) { diff --git a/config-model/src/main/java/com/yahoo/vespa/model/container/configserver/ConfigserverCluster.java b/config-model/src/main/java/com/yahoo/vespa/model/container/configserver/ConfigserverCluster.java index ef0f4882f1c..96c80c57ab2 100644 --- a/config-model/src/main/java/com/yahoo/vespa/model/container/configserver/ConfigserverCluster.java +++ b/config-model/src/main/java/com/yahoo/vespa/model/container/configserver/ConfigserverCluster.java @@ -52,7 +52,7 @@ public class ConfigserverCluster extends AbstractConfigProducer @Override public void getConfig(ZookeeperServerConfig.Builder builder) { - String myhostname = HostName.getLocalhost(); + String myhostname = HostName.getHostName(); int myid = 0; int i = 0; for (ConfigServer server : getConfigServers()) { @@ -115,7 +115,7 @@ public class ConfigserverCluster extends AbstractConfigProducer builder.defaultContentFlavor(options.defaultContentFlavor().get()); } - builder.serverId(HostName.getLocalhost()); + builder.serverId(HostName.getHostName()); if (!containerCluster.getHttp().getHttpServer().getConnectorFactories().isEmpty()) { builder.httpport(containerCluster.getHttp().getHttpServer().getConnectorFactories().get(0).getListenPort()); } @@ -144,7 +144,7 @@ public class ConfigserverCluster extends AbstractConfigProducer if (options.allConfigServers().length > 0) { return options.allConfigServers(); } else { - return new ConfigServer[]{new ConfigServer(HostName.getLocalhost(), Optional.<Integer>empty()) }; + return new ConfigServer[]{new ConfigServer(HostName.getHostName(), Optional.<Integer>empty()) }; } } diff --git a/config-model/src/test/java/com/yahoo/config/model/deploy/SystemModelTestCase.java b/config-model/src/test/java/com/yahoo/config/model/deploy/SystemModelTestCase.java index 6be85fb19e4..dc0ace32c1b 100644 --- a/config-model/src/test/java/com/yahoo/config/model/deploy/SystemModelTestCase.java +++ b/config-model/src/test/java/com/yahoo/config/model/deploy/SystemModelTestCase.java @@ -78,7 +78,7 @@ public class SystemModelTestCase { // Verify configIds from vespaModel assertTrue(12 <= vespaModel.getConfigIds().size()); - String localhost = HostName.getLocalhost(); + String localhost = HostName.getHostName(); String localhostConfigId = "hosts/" + localhost; Set<String> configIds = vespaModel.getConfigIds(); assertTrue(configIds.contains("client")); diff --git a/config-model/src/test/java/com/yahoo/vespa/model/admin/AdminTestCase.java b/config-model/src/test/java/com/yahoo/vespa/model/admin/AdminTestCase.java index a8554c54867..f203fbce328 100644 --- a/config-model/src/test/java/com/yahoo/vespa/model/admin/AdminTestCase.java +++ b/config-model/src/test/java/com/yahoo/vespa/model/admin/AdminTestCase.java @@ -8,7 +8,6 @@ import com.yahoo.cloud.config.SentinelConfig; import com.yahoo.config.model.ApplicationConfigProducerRoot; import com.yahoo.config.model.deploy.DeployProperties; import com.yahoo.config.model.deploy.DeployState; -import com.yahoo.config.model.test.MockApplicationPackage; import com.yahoo.config.model.test.TestDriver; import com.yahoo.config.model.test.TestRoot; import com.yahoo.config.provision.ApplicationId; @@ -24,10 +23,7 @@ import com.yahoo.vespa.model.container.component.Component; import com.yahoo.vespa.model.container.component.StatisticsComponent; import com.yahoo.vespa.model.test.utils.VespaModelCreatorWithFilePkg; import org.junit.Test; -import org.xml.sax.SAXException; -import java.io.IOException; -import java.util.List; import java.util.Set; import static org.hamcrest.CoreMatchers.is; @@ -65,7 +61,7 @@ public class AdminTestCase { // Verify configIds Set<String> configIds = vespaModel.getConfigIds(); - String localhost = HostName.getLocalhost(); + String localhost = HostName.getHostName(); String localhostConfigId = "hosts/" + localhost; assertTrue(configIds.contains(localhostConfigId)); assertTrue(configIds.contains("admin/logserver")); @@ -119,7 +115,7 @@ public class AdminTestCase { // Verify configIds Set<String> configIds = vespaModel.getConfigIds(); - String localhost = HostName.getLocalhost(); + String localhost = HostName.getHostName(); String localhostConfigId = "hosts/" + localhost; assertTrue(configIds.contains(localhostConfigId)); assertTrue(configIds.contains("admin/logserver")); @@ -156,7 +152,7 @@ public class AdminTestCase { tenant("quux"). applicationName("foo").instanceName("bim").build()).build()).build(); TestRoot root = new TestDriver().buildModel(state); - String localhost = HostName.getLocalhost(); + String localhost = HostName.getHostName(); SentinelConfig config = root.getConfig(SentinelConfig.class, "hosts/" + localhost); assertThat(config.application().tenant(), is("quux")); assertThat(config.application().name(), is("foo")); @@ -179,7 +175,7 @@ public class AdminTestCase { // Verify configIds Set<String> configIds = vespaModel.getConfigIds(); - String localhost = HostName.getLocalhost(); + String localhost = HostName.getHostName(); String localhostConfigId = "hosts/" + localhost; assertTrue(configIds.contains(localhostConfigId)); assertTrue(configIds.contains("admin/logserver")); diff --git a/config-model/src/test/java/com/yahoo/vespa/model/clients/test/Gateway20TestCase.java b/config-model/src/test/java/com/yahoo/vespa/model/clients/test/Gateway20TestCase.java index 5987ab8410a..579abf6d2c6 100644 --- a/config-model/src/test/java/com/yahoo/vespa/model/clients/test/Gateway20TestCase.java +++ b/config-model/src/test/java/com/yahoo/vespa/model/clients/test/Gateway20TestCase.java @@ -21,7 +21,7 @@ import static org.junit.Assert.*; * @author <a href="mailto:gunnarga@yahoo-inc.com">Gunnar Gauslaa Bergem</a> */ public class Gateway20TestCase { - private static String hostname = HostName.getLocalhost(); // Using the same way of getting hostname as filedistribution model + private static String hostname = HostName.getHostName(); // Using the same way of getting hostname as filedistribution model @Test public void testSimpleDocprocV3() throws Exception { diff --git a/config-model/src/test/java/com/yahoo/vespa/model/container/configserver/ConfigserverClusterTest.java b/config-model/src/test/java/com/yahoo/vespa/model/container/configserver/ConfigserverClusterTest.java index 170586bef85..d9fc3cd9007 100644 --- a/config-model/src/test/java/com/yahoo/vespa/model/container/configserver/ConfigserverClusterTest.java +++ b/config-model/src/test/java/com/yahoo/vespa/model/container/configserver/ConfigserverClusterTest.java @@ -71,7 +71,7 @@ public class ConfigserverClusterTest { assertThat(config.configModelPluginDir().get(0), is(Defaults.getDefaults().vespaHome() + "lib/jars/config-models")); assertThat(config.rpcport(), is(12345)); assertThat(config.httpport(), is(1337)); - assertThat(config.serverId(), is(HostName.getLocalhost())); + assertThat(config.serverId(), is(HostName.getHostName())); assertTrue(config.useVespaVersionInRequest()); assertThat(config.numParallelTenantLoaders(), is(4)); assertFalse(config.multitenant()); diff --git a/config-model/src/test/java/com/yahoo/vespa/model/test/VespaModelTestCase.java b/config-model/src/test/java/com/yahoo/vespa/model/test/VespaModelTestCase.java index 87fb5e567a5..5bb01c87d0f 100644 --- a/config-model/src/test/java/com/yahoo/vespa/model/test/VespaModelTestCase.java +++ b/config-model/src/test/java/com/yahoo/vespa/model/test/VespaModelTestCase.java @@ -23,7 +23,6 @@ import com.yahoo.container.core.ContainerHttpConfig; import com.yahoo.document.config.DocumentmanagerConfig; import com.yahoo.messagebus.MessagebusConfig; import com.yahoo.net.HostName; -import com.yahoo.net.LinuxInetAddress; import com.yahoo.vespa.config.ConfigKey; import com.yahoo.vespa.config.ConfigPayload; import com.yahoo.vespa.config.ConfigPayloadBuilder; @@ -102,7 +101,7 @@ public class VespaModelTestCase { LogdConfig.Builder b = new LogdConfig.Builder(); b = (LogdConfig.Builder) model.getConfig(b, ""); LogdConfig c = new LogdConfig(b); - assertEquals(HostSystem.lookupCanonicalHostname(HostName.getLocalhost()), c.logserver().host()); + assertEquals(HostSystem.lookupCanonicalHostname(HostName.getHostName()), c.logserver().host()); SlobroksConfig.Builder sb = new SlobroksConfig.Builder(); sb = (com.yahoo.cloud.config.SlobroksConfig.Builder) model.getConfig(sb, ""); @@ -113,7 +112,7 @@ public class VespaModelTestCase { zb = (ZookeepersConfig.Builder) model.getConfig(zb, ""); ZookeepersConfig zc = new ZookeepersConfig(zb); assertEquals(zc.zookeeperserverlist().split(",").length, 2); - assertTrue(zc.zookeeperserverlist().startsWith(HostSystem.lookupCanonicalHostname(HostName.getLocalhost()))); + assertTrue(zc.zookeeperserverlist().startsWith(HostSystem.lookupCanonicalHostname(HostName.getHostName()))); ApplicationIdConfig.Builder appIdBuilder = new ApplicationIdConfig.Builder(); appIdBuilder = (ApplicationIdConfig.Builder) model.getConfig(appIdBuilder, ""); @@ -127,11 +126,11 @@ public class VespaModelTestCase { public void testHostsConfig() { VespaModel model = getVespaModel(TESTDIR + "app_qrserverandgw"); LogdConfig config = getLogdConfig(model, ""); - assertEquals(config.logserver().host(), HostName.getLocalhost()); + assertEquals(config.logserver().host(), HostName.getHostName()); assertNotNull(config); config = getLogdConfig(model, "hosts"); assertNotNull(config); - assertEquals(config.logserver().host(), HostName.getLocalhost()); + assertEquals(config.logserver().host(), HostName.getHostName()); } private static LogdConfig getLogdConfig(VespaModel model, String configId) { @@ -158,7 +157,7 @@ public class VespaModelTestCase { LogdConfig config = getLogdConfig(model, ""); assertNotNull(config); assertEquals(config.logserver().host(), "foo"); - config = getLogdConfig(model, "hosts/" + HostName.getLocalhost() + "/logd"); + config = getLogdConfig(model, "hosts/" + HostName.getHostName() + "/logd"); assertNotNull(config); assertEquals(config.logserver().host(), "foo"); } diff --git a/config/src/main/java/com/yahoo/vespa/config/util/ConfigUtils.java b/config/src/main/java/com/yahoo/vespa/config/util/ConfigUtils.java index 52df1cff117..b1eb05fa76b 100644 --- a/config/src/main/java/com/yahoo/vespa/config/util/ConfigUtils.java +++ b/config/src/main/java/com/yahoo/vespa/config/util/ConfigUtils.java @@ -424,7 +424,7 @@ public class ConfigUtils { } public static String getCanonicalHostName() { - return HostName.getLocalhost(); + return HostName.getHostName(); } /** diff --git a/configserver/src/main/java/com/yahoo/vespa/config/server/filedistribution/FileDBRegistry.java b/configserver/src/main/java/com/yahoo/vespa/config/server/filedistribution/FileDBRegistry.java index 37cea22e420..307a0163331 100644 --- a/configserver/src/main/java/com/yahoo/vespa/config/server/filedistribution/FileDBRegistry.java +++ b/configserver/src/main/java/com/yahoo/vespa/config/server/filedistribution/FileDBRegistry.java @@ -35,7 +35,7 @@ public class FileDBRegistry implements FileRegistry { @Override public String fileSourceHost() { - return HostName.getLocalhost(); + return HostName.getHostName(); } @Override diff --git a/configserver/src/main/java/com/yahoo/vespa/config/server/rpc/GetConfigProcessor.java b/configserver/src/main/java/com/yahoo/vespa/config/server/rpc/GetConfigProcessor.java index 310c7ceeef7..6d9e1a9e6c1 100644 --- a/configserver/src/main/java/com/yahoo/vespa/config/server/rpc/GetConfigProcessor.java +++ b/configserver/src/main/java/com/yahoo/vespa/config/server/rpc/GetConfigProcessor.java @@ -27,7 +27,7 @@ import java.util.logging.Logger; class GetConfigProcessor implements Runnable { private static final Logger log = Logger.getLogger(GetConfigProcessor.class.getName()); - private static final String localHostName = HostName.getLocalhost(); + private static final String localHostName = HostName.getHostName(); private final JRTServerConfigRequest request; /* True only when this request has expired its server timeout and we need to respond to the client */ diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/rpc/TestWithRpc.java b/configserver/src/test/java/com/yahoo/vespa/config/server/rpc/TestWithRpc.java index 8f1754357b2..4348b587596 100644 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/rpc/TestWithRpc.java +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/rpc/TestWithRpc.java @@ -43,7 +43,7 @@ import static org.junit.Assert.assertTrue; public class TestWithRpc { private final ManualClock clock = new ManualClock(Instant.ofEpochMilli(100)); - private final String myHostname = HostName.getLocalhost(); + private final String myHostname = HostName.getHostName(); private final HostLivenessTracker hostLivenessTracker = new ConfigRequestHostLivenessTracker(clock); protected RpcServer rpcServer; diff --git a/container-core/src/main/java/com/yahoo/container/osgi/ContainerRpcAdaptor.java b/container-core/src/main/java/com/yahoo/container/osgi/ContainerRpcAdaptor.java index 8b0ed41cd8c..3906e3a57ff 100644 --- a/container-core/src/main/java/com/yahoo/container/osgi/ContainerRpcAdaptor.java +++ b/container-core/src/main/java/com/yahoo/container/osgi/ContainerRpcAdaptor.java @@ -43,7 +43,7 @@ public class ContainerRpcAdaptor extends AbstractRpcAdaptor { public ContainerRpcAdaptor(Osgi osgi) { this.osgi = osgi; this.supervisor = new Supervisor(new Transport()); - this.hostname = HostName.getLocalhost(); + this.hostname = HostName.getHostName(); bindCommands(supervisor); } diff --git a/container-search/src/main/java/com/yahoo/prelude/cluster/ClusterSearcher.java b/container-search/src/main/java/com/yahoo/prelude/cluster/ClusterSearcher.java index 36ae057b418..c7829dfde7f 100644 --- a/container-search/src/main/java/com/yahoo/prelude/cluster/ClusterSearcher.java +++ b/container-search/src/main/java/com/yahoo/prelude/cluster/ClusterSearcher.java @@ -192,7 +192,7 @@ public class ClusterSearcher extends Searcher { if (InetAddress.getByName(host).isLoopbackAddress()) return false; else - return !host.equals(HostName.getLocalhost()); + return !host.equals(HostName.getHostName()); } private static ClusterParams makeClusterParams(int searchclusterIndex, diff --git a/container-search/src/main/java/com/yahoo/prelude/fastsearch/FastSearcher.java b/container-search/src/main/java/com/yahoo/prelude/fastsearch/FastSearcher.java index 504c5dd363a..ee059edb519 100644 --- a/container-search/src/main/java/com/yahoo/prelude/fastsearch/FastSearcher.java +++ b/container-search/src/main/java/com/yahoo/prelude/fastsearch/FastSearcher.java @@ -94,7 +94,7 @@ public class FastSearcher extends VespaBackEndSearcher { this.dispatchBackend = dispatchBackend; this.fs4ResourcePool = fs4ResourcePool; this.dispatcher = dispatcher; - this.selfHostname = HostName.getLocalhost(); + this.selfHostname = HostName.getHostName(); } private static SimpleDateFormat isoDateFormat; diff --git a/container-search/src/main/java/com/yahoo/search/dispatch/SearchCluster.java b/container-search/src/main/java/com/yahoo/search/dispatch/SearchCluster.java index b6ee70802d8..c1a07759718 100644 --- a/container-search/src/main/java/com/yahoo/search/dispatch/SearchCluster.java +++ b/container-search/src/main/java/com/yahoo/search/dispatch/SearchCluster.java @@ -91,7 +91,7 @@ public class SearchCluster implements NodeManager<SearchCluster.Node> { nodesByHostBuilder.put(node.hostname(), node); this.nodesByHost = nodesByHostBuilder.build(); - this.directDispatchTarget = findDirectDispatchTarget(HostName.getLocalhost(), size, containerClusterSize, + this.directDispatchTarget = findDirectDispatchTarget(HostName.getHostName(), size, containerClusterSize, nodesByHost, groups); // Set up monitoring of the fs4 interface of the nodes diff --git a/container-search/src/test/java/com/yahoo/fs4/mplex/BackendTestCase.java b/container-search/src/test/java/com/yahoo/fs4/mplex/BackendTestCase.java index 52a66fa6db9..f9c14a81d13 100644 --- a/container-search/src/test/java/com/yahoo/fs4/mplex/BackendTestCase.java +++ b/container-search/src/test/java/com/yahoo/fs4/mplex/BackendTestCase.java @@ -1,9 +1,18 @@ // Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.fs4.mplex; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import com.yahoo.container.search.Fs4Config; +import com.yahoo.fs4.BasicPacket; +import com.yahoo.fs4.ChannelTimeoutException; +import com.yahoo.fs4.PacketListener; +import com.yahoo.fs4.PingPacket; +import com.yahoo.fs4.QueryPacket; +import com.yahoo.fs4.mplex.Backend.BackendStatistics; +import com.yahoo.prelude.fastsearch.FS4ResourcePool; +import com.yahoo.search.Query; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; import java.io.IOException; import java.net.InetAddress; @@ -13,21 +22,9 @@ import java.net.Socket; import java.nio.ByteBuffer; import java.util.logging.Logger; -import com.yahoo.container.search.Fs4Config; -import com.yahoo.net.HostName; -import com.yahoo.net.LinuxInetAddress; -import com.yahoo.prelude.fastsearch.FS4ResourcePool; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import com.yahoo.fs4.BasicPacket; -import com.yahoo.fs4.ChannelTimeoutException; -import com.yahoo.fs4.PacketListener; -import com.yahoo.fs4.PingPacket; -import com.yahoo.fs4.QueryPacket; -import com.yahoo.fs4.mplex.Backend.BackendStatistics; -import com.yahoo.search.Query; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; /** * Test networking code for talking to dispatch. @@ -114,7 +111,6 @@ public class BackendTestCase { public MockServer() throws IOException { ServerSocket socket = new ServerSocket(0, 50, InetAddress.getLoopbackAddress()); - //ServerSocket socket = new ServerSocket(0, 50, LinuxInetAddress.getLocalHost()); host = (InetSocketAddress) socket.getLocalSocketAddress(); dispatch = new MockDispatch(socket); worker = new Thread(dispatch); diff --git a/container-search/src/test/java/com/yahoo/prelude/fastsearch/test/FastSearcherTester.java b/container-search/src/test/java/com/yahoo/prelude/fastsearch/test/FastSearcherTester.java index d2638be4bc7..93b18da215a 100644 --- a/container-search/src/test/java/com/yahoo/prelude/fastsearch/test/FastSearcherTester.java +++ b/container-search/src/test/java/com/yahoo/prelude/fastsearch/test/FastSearcherTester.java @@ -27,7 +27,7 @@ import static org.junit.Assert.assertEquals; */ class FastSearcherTester { - public static final String selfHostname = HostName.getLocalhost(); + public static final String selfHostname = HostName.getHostName(); private final MockFS4ResourcePool mockFS4ResourcePool; private final FastSearcher fastSearcher; diff --git a/docker-api/src/main/java/com/yahoo/vespa/hosted/dockerapi/DockerImpl.java b/docker-api/src/main/java/com/yahoo/vespa/hosted/dockerapi/DockerImpl.java index 5e5185bc6be..c0ffe367259 100644 --- a/docker-api/src/main/java/com/yahoo/vespa/hosted/dockerapi/DockerImpl.java +++ b/docker-api/src/main/java/com/yahoo/vespa/hosted/dockerapi/DockerImpl.java @@ -124,7 +124,7 @@ public class DockerImpl implements Docker { if (! dockerClient.listNetworksCmd().withNameFilter(DOCKER_CUSTOM_MACVLAN_NETWORK_NAME).exec().isEmpty()) return; // Use IPv6 address if there is a mix of IP4 and IPv6 by taking the longest address. - List<InetAddress> hostAddresses = Arrays.asList(InetAddress.getAllByName(com.yahoo.net.HostName.getLocalhost())); + List<InetAddress> hostAddresses = Arrays.asList(InetAddress.getAllByName(com.yahoo.net.HostName.getHostName())); InetAddress hostAddress = Collections.max(hostAddresses, (o1, o2) -> o1.getAddress().length - o2.getAddress().length); @@ -563,7 +563,7 @@ public class DockerImpl implements Docker { private void setMetrics(MetricReceiverWrapper metricReceiver) { Dimensions dimensions = new Dimensions.Builder() - .add("host", HostName.getLocalhost()) + .add("host", HostName.getHostName()) .add("role", "docker").build(); numberOfRunningContainersGauge = metricReceiver.declareGauge(dimensions, "containers.running"); diff --git a/messagebus/src/main/java/com/yahoo/messagebus/network/Identity.java b/messagebus/src/main/java/com/yahoo/messagebus/network/Identity.java index 45887b072ab..a10cd4b8a63 100644 --- a/messagebus/src/main/java/com/yahoo/messagebus/network/Identity.java +++ b/messagebus/src/main/java/com/yahoo/messagebus/network/Identity.java @@ -1,12 +1,7 @@ // Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.messagebus.network; -import com.yahoo.log.LogLevel; import com.yahoo.net.HostName; -import com.yahoo.net.LinuxInetAddress; - -import java.net.Inet6Address; -import java.net.InetAddress; /** * This class encapsulates the identity of the application that uses this instance of message bus. This identity @@ -28,11 +23,7 @@ public class Identity { * @param configId The config identifier for the application. */ public Identity(String configId) { - InetAddress addr = LinuxInetAddress.getLocalHost(); // try hard to get a resolvable address - if (addr instanceof Inet6Address) // - hostname = HostName.getLocalhost(); // ... but fallback to hostname if we get an IPv6 address - else - hostname = addr.getCanonicalHostName(); + hostname = HostName.getHostName(); // ... but fallback to hostname if we get an IPv6 address servicePrefix = configId; } diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/nodeadmin/NodeAdminImpl.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/nodeadmin/NodeAdminImpl.java index 15a33dddd3c..b86a3c11f70 100644 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/nodeadmin/NodeAdminImpl.java +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/nodeadmin/NodeAdminImpl.java @@ -75,7 +75,7 @@ public class NodeAdminImpl implements NodeAdmin { this.nodeAgentScanIntervalMillis = nodeAgentScanIntervalMillis; Dimensions dimensions = new Dimensions.Builder() - .add("host", HostName.getLocalhost()) + .add("host", HostName.getHostName()) .add("role", "docker").build(); this.numberOfContainersInActiveState = metricReceiver.declareGauge(dimensions, "nodes.state.active"); diff --git a/standalone-container/src/main/scala/com/yahoo/container/standalone/LocalFileDb.scala b/standalone-container/src/main/scala/com/yahoo/container/standalone/LocalFileDb.scala index 7af08a4685d..b1c7e9457ac 100644 --- a/standalone-container/src/main/scala/com/yahoo/container/standalone/LocalFileDb.scala +++ b/standalone-container/src/main/scala/com/yahoo/container/standalone/LocalFileDb.scala @@ -49,7 +49,7 @@ class LocalFileDb(appPath: Path) extends FileAcquirer with FileRegistry { } def fileSourceHost: String = - HostName.getLocalhost + HostName.getHostName def allRelativePaths: java.util.Set[String] = { new java.util.HashSet(fileReferenceToFile.values.map(_.getPath)) diff --git a/vespaclient-container-plugin/src/main/java/com/yahoo/vespa/http/server/FeedHandler.java b/vespaclient-container-plugin/src/main/java/com/yahoo/vespa/http/server/FeedHandler.java index c37cb51d7ca..78b33abedda 100644 --- a/vespaclient-container-plugin/src/main/java/com/yahoo/vespa/http/server/FeedHandler.java +++ b/vespaclient-container-plugin/src/main/java/com/yahoo/vespa/http/server/FeedHandler.java @@ -270,7 +270,7 @@ public class FeedHandler extends LoggingRequestHandler { } private static String resolveLocalHostname() { - String hostname = HostName.getLocalhost(); + String hostname = HostName.getHostName(); if (hostname.equals("localhost")) { return ""; } diff --git a/vespajlib/src/main/java/com/yahoo/net/HostName.java b/vespajlib/src/main/java/com/yahoo/net/HostName.java index 4e791ca117a..061b53216c0 100644 --- a/vespajlib/src/main/java/com/yahoo/net/HostName.java +++ b/vespajlib/src/main/java/com/yahoo/net/HostName.java @@ -2,40 +2,139 @@ package com.yahoo.net; import java.io.BufferedReader; +import java.io.IOException; import java.io.InputStreamReader; +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.net.SocketException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Objects; +import java.util.logging.Level; +import java.util.logging.Logger; +import java.util.stream.Collectors; /** - * Utilities for getting the hostname on a system running with the JVM. This is moved here from the old - * HostSystem#getHostName in config-model. + * Utilities for getting the hostname of the system running the JVM. * * @author lulf + * @author bratseth + * @author hakon */ public class HostName { - private static String myHost = null; + private static final Logger logger = Logger.getLogger(HostName.class.getName()); + + private static String cachedHostName = null; /** - * Static method that returns the name of localhost using shell command "hostname". - * If you need a guaranteed resolvable name see LinuxINetAddress. + * Return a fully qualified hostname that resolves to an IP address on a network interface. + * Normally this is the same as the 'hostname' command, but on dev machines on WiFi, + * that IP isn't configured so we prefer a WiFi network interface IP address which is both reachable and + * has a DNS entry. * - * @return the name of localhost. - * @throws RuntimeException if executing the command 'hostname' fails. + * @return the preferred name of localhost + * @throws RuntimeException if accessing the network or the 'hostname' command fails */ - public static synchronized String getLocalhost() { - if (myHost == null) { + public static synchronized String getHostName() { + if (cachedHostName == null) { try { - Process p = Runtime.getRuntime().exec("hostname"); - BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); - myHost = in.readLine(); - p.waitFor(); - if (p.exitValue() != 0) { - throw new RuntimeException("Command 'hostname' failed: exit("+p.exitValue()+")"); - } + cachedHostName = getPreferredAddress().canonicalHostName; } catch (Exception e) { - throw new RuntimeException("Failed when executing command 'hostname'", e); + throw new RuntimeException("Failed to find a preferred hostname", e); } } - return myHost; + return cachedHostName; + } + + private static Address getPreferredAddress() throws Exception { + List<Address> addresses = getReachableNetworkInterfaceAddresses(); + + // Prefer address matching the system hostname + String systemHostName = getSystemHostName(); + List<Address> systemAddresses = addresses.stream() + .filter(address -> Objects.equals(address.canonicalHostName, systemHostName)) + .collect(Collectors.toList()); + if (systemAddresses.size() >= 1) { + return systemAddresses.iterator().next(); + } + + // Otherwise, prefer non-local address. + List<Address> nonLocalAddresses = addresses.stream() + .filter(address -> !address.ipAddress.isAnyLocalAddress()) + .collect(Collectors.toList()); + if (nonLocalAddresses.size() >= 1) { + return nonLocalAddresses.iterator().next(); + } + + // Otherwise, pick a local address. + List<Address> localAddresses = addresses.stream() + .filter(address -> address.ipAddress.isAnyLocalAddress()) + .collect(Collectors.toList()); + if (localAddresses.size() >= 1) { + return localAddresses.iterator().next(); + } + + throw new RuntimeException("Failed to find any addresses on the network interfaces that resolves to a DNS name"); + } + + // public for testing purposes (all testing machines should have a hostname + public static String getSystemHostName() throws Exception { + Process process = Runtime.getRuntime().exec("hostname"); + BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream())); + String hostname = in.readLine(); + process.waitFor(); + if (process.exitValue() != 0) { + throw new RuntimeException("Command 'hostname' failed with exit code " + process.exitValue()); + } + + return hostname; + } + + private static class Address { + + public final InetAddress ipAddress; + public final String canonicalHostName; + + public Address(InetAddress ipAddress, String canonicalHostName) { + this.ipAddress = ipAddress; + this.canonicalHostName = canonicalHostName; + } + + } + + private static List<Address> getReachableNetworkInterfaceAddresses() throws SocketException { + List<Address> addresses = new ArrayList<>(); + + for (NetworkInterface networkInterface : Collections.list(NetworkInterface.getNetworkInterfaces())) { + for (InetAddress ipAddress : Collections.list(networkInterface.getInetAddresses())) { + String hostname = ipAddress.getCanonicalHostName(); + if (Objects.equals(hostname, ipAddress.getHostAddress())) { + // getCanonicalHostName() failed to get the fully qualified domain name + continue; + } + + try { + // ping says ~50ms on my Fedora Lenovo, but that seems a lot for pinging oneself - hakon + int timeoutMs = 100; + if ( ! ipAddress.isReachable(timeoutMs)) { + // The network interface may be down, ignore address + logger.log(Level.INFO, ipAddress.toString() + + " is unreachable w/" + timeoutMs + "ms timeout, ignoring address"); + continue; + } + } catch (IOException e) { + // Why would this be different from !isReachable ? + logger.log(Level.INFO, "Failed testing reachability of " + ipAddress + ", ignoring address", e); + continue; + } + + addresses.add(new Address(ipAddress, hostname)); + } + } + + return addresses; } } diff --git a/vespajlib/src/main/java/com/yahoo/net/LinuxInetAddress.java b/vespajlib/src/main/java/com/yahoo/net/LinuxInetAddress.java deleted file mode 100644 index 9d50c99d77c..00000000000 --- a/vespajlib/src/main/java/com/yahoo/net/LinuxInetAddress.java +++ /dev/null @@ -1,98 +0,0 @@ -// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.net; - -import java.net.*; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Enumeration; -import java.util.List; -import java.util.logging.Level; -import java.util.logging.Logger; -import java.util.stream.Collectors; - -/** - * Utilities for returning localhost addresses on Linux. - * See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4665037 on why this is necessary. - * - * @author bratseth - */ -public class LinuxInetAddress { - - /** - * Returns an InetAddress representing a resolvable localhost address. - * A non-loopback address is preferred if available. - * An address that resolves to a hostname is preferred among non-loopback addresses. - * IPv4 is preferred over IPv6 among resolving addresses. - * - * @return a localhost address - */ - // Note: Checking resolvability of ipV6 addresses takes a long time on some systems (over 5 seconds - // for some addresses on my mac). This method is written to minimize the number of resolution checks done - // and to defer ip6 checks until necessary. - public static InetAddress getLocalHost() { - InetAddress fallback = InetAddress.getLoopbackAddress(); - try { - fallback = InetAddress.getLocalHost(); - List<InetAddress> nonLoopback = - getAllLocalFromNetwork().stream().filter(a -> ! a.isLoopbackAddress()).collect(Collectors.toList()); - if (nonLoopback.isEmpty()) return fallback; - - // Invariant: We got all addresses without exception - - List<InetAddress> ipV4 = nonLoopback.stream().filter(a -> a instanceof Inet4Address).collect(Collectors.toList()); - for (InetAddress address : ipV4) - if (doesResolve(address)) - return address; - - // Invariant: There are no resolving ip4 addresses - - List<InetAddress> ipV6 = nonLoopback.stream().filter(a -> a instanceof Inet4Address).collect(Collectors.toList()); - for (InetAddress address : ipV6) - if (doesResolve(address)) - return address; - - // Invariant: There are no resolving ip6 addresses either - - if (! ipV4.isEmpty()) return ipV4.get(0); - return ipV6.get(0); - } catch (UnknownHostException e) { - return fallback; - } - } - - /** - * Returns all local addresses of this host. - * - * @return an array of the addresses of this - * @throws UnknownHostException if we cannot access the network - */ - public static InetAddress[] getAllLocal() throws UnknownHostException { - InetAddress[] localInetAddresses = InetAddress.getAllByName("127.0.0.1"); - if ( ! localInetAddresses[0].isLoopbackAddress()) return localInetAddresses; - return getAllLocalFromNetwork().toArray(new InetAddress[0]); - } - - /** - * Returns all local addresses of this host. - * - * @return a list of the addresses of this - * @throws UnknownHostException if we cannot access the network - */ - private static List<InetAddress> getAllLocalFromNetwork() throws UnknownHostException { - try { - List<InetAddress> addresses = new ArrayList<>(); - for (NetworkInterface networkInterface : Collections.list(NetworkInterface.getNetworkInterfaces())) - addresses.addAll(Collections.list(networkInterface.getInetAddresses())); - return addresses; - } - catch (SocketException ex) { - throw new UnknownHostException("127.0.0.1"); - } - } - - private static boolean doesResolve(InetAddress address) { - // The latter returns a name if resolvable to one and the host address otherwise - return ! address.getHostAddress().equals(address.getCanonicalHostName()); - } - -} diff --git a/vespajlib/src/test/java/com/yahoo/net/HostNameTestCase.java b/vespajlib/src/test/java/com/yahoo/net/HostNameTestCase.java index 98be9f0ef6f..0534b02db60 100644 --- a/vespajlib/src/test/java/com/yahoo/net/HostNameTestCase.java +++ b/vespajlib/src/test/java/com/yahoo/net/HostNameTestCase.java @@ -9,8 +9,15 @@ import static org.junit.Assert.assertFalse; * @author lulf */ public class HostNameTestCase { + @Test public void testHostnameIsFound() { - assertFalse(HostName.getLocalhost().isEmpty()); + assertFalse(HostName.getHostName().isEmpty()); } + + @Test + public void testSystemHostnameIsFound() throws Exception { + assertFalse(HostName.getSystemHostName().isEmpty()); + } + } diff --git a/vespajlib/src/test/java/com/yahoo/net/LinuxInetAddressTestCase.java b/vespajlib/src/test/java/com/yahoo/net/LinuxInetAddressTestCase.java deleted file mode 100755 index 27b85a0864c..00000000000 --- a/vespajlib/src/test/java/com/yahoo/net/LinuxInetAddressTestCase.java +++ /dev/null @@ -1,42 +0,0 @@ -// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-package com.yahoo.net;
-
-import java.net.UnknownHostException;
-import java.net.InetAddress;
-import java.net.Inet4Address;
-
-/**
- * @author Simon Thoresen
- */
-public class LinuxInetAddressTestCase extends junit.framework.TestCase {
-
- @SuppressWarnings("deprecation")
- public void testPreferIPv4() throws UnknownHostException {
- try {
- // This test only works if there is at least one inet address returned.
- InetAddress[] arr = LinuxInetAddress.getAllLocal();
- if (arr.length > 0) {
- // System.out.println("Got " + arr.length + " addresses.");
-
- // And it can only make sure it is preferred if there is at least one ip v4 address.
- boolean ipv4 = false;
- for (int i = 0; i < arr.length; ++i) {
- // System.out.println("Address " + i + " is an instance of " + arr[i].getClass() + ".");
- if (arr[i] instanceof Inet4Address) {
- ipv4 = true;
- }
- }
-
- // And the only thing we test is that an ip v4 address is preferred.
- if (ipv4) {
- InetAddress addr = LinuxInetAddress.getLocalHost();
- assertNotNull("IPv4 is prefered", addr instanceof Inet4Address);
- }
- }
- }
- catch (java.net.UnknownHostException e) {
- // We're on vpn or have no network
- }
- }
-
-}
|