diff options
4 files changed, 26 insertions, 14 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/BazelWorkspaceStatusModule.java b/src/main/java/com/google/devtools/build/lib/bazel/BazelWorkspaceStatusModule.java index bb77abc020..16f882fd11 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/BazelWorkspaceStatusModule.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/BazelWorkspaceStatusModule.java @@ -282,7 +282,6 @@ public class BazelWorkspaceStatusModule extends BlazeModule { } private class BazelStatusActionFactory implements WorkspaceStatusAction.Factory { - private String hostname; @Override public Map<String, String> createDummyWorkspaceStatus() { @@ -312,11 +311,7 @@ public class BazelWorkspaceStatusModule extends BlazeModule { * changes during bazel server lifetime, bazel will not see the change. */ private String getHostname() { - if (hostname == null) { - hostname = NetUtil.findShortHostName(); - } - - return hostname; + return NetUtil.getCachedShortHostName(); } } diff --git a/src/main/java/com/google/devtools/build/lib/exec/local/LocalSpawnRunner.java b/src/main/java/com/google/devtools/build/lib/exec/local/LocalSpawnRunner.java index 57d757691d..9aed79b08d 100644 --- a/src/main/java/com/google/devtools/build/lib/exec/local/LocalSpawnRunner.java +++ b/src/main/java/com/google/devtools/build/lib/exec/local/LocalSpawnRunner.java @@ -90,7 +90,7 @@ public final class LocalSpawnRunner implements SpawnRunner { this.execRoot = execRoot; this.processWrapper = getProcessWrapper(execRoot, localOs).getPathString(); this.localExecutionOptions = Preconditions.checkNotNull(localExecutionOptions); - this.hostName = NetUtil.findShortHostName(); + this.hostName = NetUtil.getCachedShortHostName(); this.resourceManager = resourceManager; this.useProcessWrapper = useProcessWrapper; this.productName = productName; diff --git a/src/main/java/com/google/devtools/build/lib/util/NetUtil.java b/src/main/java/com/google/devtools/build/lib/util/NetUtil.java index f22146a71d..dc2ce8867e 100644 --- a/src/main/java/com/google/devtools/build/lib/util/NetUtil.java +++ b/src/main/java/com/google/devtools/build/lib/util/NetUtil.java @@ -21,14 +21,31 @@ import java.net.UnknownHostException; */ public final class NetUtil { + private static String hostname; + private NetUtil() { } /** + * Returns the *cached* short hostname (computed at most once per the lifetime of a server). Can + * take seconds to complete when the cache is cold. + */ + public static String getCachedShortHostName() { + if (hostname == null) { + synchronized (NetUtil.class) { + if (hostname == null) { + hostname = computeShortHostName(); + } + } + } + return computeShortHostName(); + } + + /** * Returns the short hostname or <code>unknown</code> if the host name could not be determined. - * Performs reverse DNS lookup and can take seconds to complete! + * Performs reverse DNS lookup and can take seconds to complete. */ - public static String findShortHostName() { + private static String computeShortHostName() { try { return InetAddress.getLocalHost().getHostName(); } catch (UnknownHostException e) { diff --git a/src/test/java/com/google/devtools/build/lib/exec/local/LocalSpawnRunnerTest.java b/src/test/java/com/google/devtools/build/lib/exec/local/LocalSpawnRunnerTest.java index fe84224cde..4baed38880 100644 --- a/src/test/java/com/google/devtools/build/lib/exec/local/LocalSpawnRunnerTest.java +++ b/src/test/java/com/google/devtools/build/lib/exec/local/LocalSpawnRunnerTest.java @@ -258,7 +258,7 @@ public class LocalSpawnRunnerTest { assertThat(result.status()).isEqualTo(SpawnResult.Status.SUCCESS); assertThat(result.exitCode()).isEqualTo(0); assertThat(result.setupSuccess()).isTrue(); - assertThat(result.getExecutorHostName()).isEqualTo(NetUtil.findShortHostName()); + assertThat(result.getExecutorHostName()).isEqualTo(NetUtil.getCachedShortHostName()); assertThat(captor.getValue().getArgv()) .containsExactlyElementsIn( @@ -304,7 +304,7 @@ public class LocalSpawnRunnerTest { assertThat(result.status()).isEqualTo(SpawnResult.Status.SUCCESS); assertThat(result.exitCode()).isEqualTo(0); assertThat(result.setupSuccess()).isTrue(); - assertThat(result.getExecutorHostName()).isEqualTo(NetUtil.findShortHostName()); + assertThat(result.getExecutorHostName()).isEqualTo(NetUtil.getCachedShortHostName()); assertThat(captor.getValue().getArgv()) .containsExactlyElementsIn(ImmutableList.of("/bin/echo", "Hi!")); @@ -339,7 +339,7 @@ public class LocalSpawnRunnerTest { assertThat(result.status()).isEqualTo(SpawnResult.Status.SUCCESS); assertThat(result.exitCode()).isEqualTo(3); assertThat(result.setupSuccess()).isTrue(); - assertThat(result.getExecutorHostName()).isEqualTo(NetUtil.findShortHostName()); + assertThat(result.getExecutorHostName()).isEqualTo(NetUtil.getCachedShortHostName()); assertThat(captor.getValue().getArgv()) .containsExactlyElementsIn( @@ -377,7 +377,7 @@ public class LocalSpawnRunnerTest { assertThat(result.exitCode()).isEqualTo(-1); assertThat(result.setupSuccess()).isFalse(); assertThat(result.getWallTimeMillis()).isEqualTo(0); - assertThat(result.getExecutorHostName()).isEqualTo(NetUtil.findShortHostName()); + assertThat(result.getExecutorHostName()).isEqualTo(NetUtil.getCachedShortHostName()); assertThat(FileSystemUtils.readContent(fs.getPath("/out/stderr"), StandardCharsets.UTF_8)) .isEqualTo("Action failed to execute: java.io.IOException: I'm sorry, Dave\n"); @@ -399,7 +399,7 @@ public class LocalSpawnRunnerTest { assertThat(reply.exitCode()).isEqualTo(-1); assertThat(reply.setupSuccess()).isFalse(); assertThat(reply.getWallTimeMillis()).isEqualTo(0); - assertThat(reply.getExecutorHostName()).isEqualTo(NetUtil.findShortHostName()); + assertThat(reply.getExecutorHostName()).isEqualTo(NetUtil.getCachedShortHostName()); // TODO(ulfjack): Maybe we should only lock after checking? assertThat(policy.lockOutputFilesCalled).isTrue(); |