diff options
author | Jon Bratseth <jonbratseth@yahoo.com> | 2017-09-08 14:30:51 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-09-08 14:30:51 +0200 |
commit | 092f8895ca6e7ee8007086355c2d27047cbd8923 (patch) | |
tree | 7458b2a4c97a2037130526b429b659573d9eb4b0 | |
parent | 644873405274dbde3cb7152b8b546e1fe346d650 (diff) | |
parent | d6e45f8ce331e4d12f907ac132621a5cfd5d76ce (diff) |
Merge pull request #3373 from vespa-engine/arnej/allow-env-override
Arnej/allow env override
5 files changed, 13 insertions, 34 deletions
diff --git a/indexinglanguage/src/main/java/com/yahoo/vespa/indexinglanguage/expressions/HostNameExpression.java b/indexinglanguage/src/main/java/com/yahoo/vespa/indexinglanguage/expressions/HostNameExpression.java index 681dd22eca2..110cd1594de 100644 --- a/indexinglanguage/src/main/java/com/yahoo/vespa/indexinglanguage/expressions/HostNameExpression.java +++ b/indexinglanguage/src/main/java/com/yahoo/vespa/indexinglanguage/expressions/HostNameExpression.java @@ -4,8 +4,7 @@ package com.yahoo.vespa.indexinglanguage.expressions; import com.yahoo.document.DataType; import com.yahoo.document.DocumentType; import com.yahoo.document.datatypes.StringFieldValue; - -import java.net.InetAddress; +import com.yahoo.net.HostName; /** * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a> @@ -14,11 +13,7 @@ public class HostNameExpression extends Expression { @Override protected void doExecute(ExecutionContext ctx) { - try { - ctx.setValue(new StringFieldValue(normalizeHostName(InetAddress.getLocalHost().getHostName()))); - } catch (java.net.UnknownHostException e) { - throw new RuntimeException(e); - } + ctx.setValue(new StringFieldValue(normalizeHostName(HostName.getLocalhost()))); } @Override diff --git a/jdisc_core/src/main/java/com/yahoo/jdisc/core/ConsoleLogListener.java b/jdisc_core/src/main/java/com/yahoo/jdisc/core/ConsoleLogListener.java index 3b4dcbae082..e60c058f578 100644 --- a/jdisc_core/src/main/java/com/yahoo/jdisc/core/ConsoleLogListener.java +++ b/jdisc_core/src/main/java/com/yahoo/jdisc/core/ConsoleLogListener.java @@ -8,8 +8,7 @@ import java.io.File; import java.io.IOException; import java.io.PrintStream; import java.lang.management.ManagementFactory; -import java.net.InetAddress; -import java.net.UnknownHostException; +import com.yahoo.net.HostName; /** * @author <a href="mailto:vikasp@yahoo-inc.com">Vikas Panwar</a> @@ -100,10 +99,6 @@ class ConsoleLogListener implements LogListener { } static String getHostname() { - try { - return InetAddress.getLocalHost().getCanonicalHostName(); - } catch (UnknownHostException e) { - return null; - } + return HostName.getLocalhost(); } } diff --git a/vespaclient-container-plugin/src/main/java/com/yahoo/vespa/http/server/ClientFeederV3.java b/vespaclient-container-plugin/src/main/java/com/yahoo/vespa/http/server/ClientFeederV3.java index 13ee837e1d4..5f1c96d000f 100644 --- a/vespaclient-container-plugin/src/main/java/com/yahoo/vespa/http/server/ClientFeederV3.java +++ b/vespaclient-container-plugin/src/main/java/com/yahoo/vespa/http/server/ClientFeederV3.java @@ -14,6 +14,7 @@ import com.yahoo.messagebus.Message; import com.yahoo.messagebus.ReplyHandler; import com.yahoo.messagebus.Result; import com.yahoo.messagebus.shared.SharedSourceSession; +import com.yahoo.net.HostName; import com.yahoo.vespa.http.client.core.ErrorCode; import com.yahoo.vespa.http.client.core.Headers; import com.yahoo.vespa.http.client.core.OperationStatus; @@ -23,8 +24,6 @@ import com.yahoo.yolean.Exceptions; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.net.InetAddress; -import java.net.UnknownHostException; import java.time.Duration; import java.time.Instant; import java.util.Optional; @@ -75,12 +74,7 @@ class ClientFeederV3 { this.metric = metric; this.threadsAvailableForFeeding = threadsAvailableForFeeding; this.streamReaderV3 = new StreamReaderV3(feedReaderFactory, docTypeManager); - try { - this.hostName = InetAddress.getLocalHost().getHostName(); - } catch (UnknownHostException e) { - log.info("Could not find hostname, randomizing. " + e.getMessage()); - hostName = "UNKNOWNHOSTNAME" + System.nanoTime() % 10000; - } + this.hostName = HostName.getLocalhost(); } public boolean timedOut() { diff --git a/vespaclient-java/src/main/java/com/yahoo/vespavisit/VdsVisitTarget.java b/vespaclient-java/src/main/java/com/yahoo/vespavisit/VdsVisitTarget.java index ba40c1f1495..88a9a783f37 100644 --- a/vespaclient-java/src/main/java/com/yahoo/vespavisit/VdsVisitTarget.java +++ b/vespaclient-java/src/main/java/com/yahoo/vespavisit/VdsVisitTarget.java @@ -12,6 +12,7 @@ import com.yahoo.documentapi.messagebus.loadtypes.LoadTypeSet; import com.yahoo.log.LogLevel; import com.yahoo.log.LogSetup; import com.yahoo.messagebus.network.Identity; +import com.yahoo.net.HostName; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.DefaultParser; @@ -21,8 +22,6 @@ import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; import java.lang.reflect.Constructor; -import java.net.InetAddress; -import java.net.UnknownHostException; import java.util.logging.Logger; /** @@ -201,15 +200,8 @@ public class VdsVisitTarget { } private String getTcpAddress() { - try { - InetAddress addr = InetAddress.getLocalHost(); - String hostname = addr.getHostName(); - return "tcp/" + hostname + ":" + port + "/visit-destination"; - } catch (UnknownHostException e) { - System.err.println("Failed to detect hostname."); - System.exit(1); - } - return ""; + String hostname = HostName.getLocalhost(); + return "tcp/" + hostname + ":" + port + "/visit-destination"; } @SuppressWarnings("unchecked") diff --git a/vespajlib/src/main/java/com/yahoo/net/HostName.java b/vespajlib/src/main/java/com/yahoo/net/HostName.java index 4c51a5e1ba1..37f7fe80246 100644 --- a/vespajlib/src/main/java/com/yahoo/net/HostName.java +++ b/vespajlib/src/main/java/com/yahoo/net/HostName.java @@ -118,6 +118,10 @@ public class HostName { * DO NOT USE: Package-private for testing purposes (all testing machines should have a hostname) */ static String getSystemHostName() throws Exception { + String env = System.getenv("VESPA_HOSTNAME"); + if (env != null && ! env.trim().isEmpty()) { + return env.trim(); + } Process process = Runtime.getRuntime().exec("hostname"); BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream())); String hostname = in.readLine(); @@ -125,7 +129,6 @@ public class HostName { if (process.exitValue() != 0) { throw new RuntimeException("Command 'hostname' failed with exit code " + process.exitValue()); } - return hostname; } |