diff options
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/bazel/BazelWorkspaceStatusModule.java | 27 | ||||
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/util/NetUtil.java | 4 |
2 files changed, 25 insertions, 6 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 894a27c1d4..e4972e00b4 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 @@ -52,7 +52,6 @@ import com.google.devtools.build.lib.vfs.FileSystemUtils; import com.google.devtools.build.lib.vfs.Path; import com.google.devtools.build.lib.vfs.PathFragment; import com.google.devtools.common.options.OptionsBase; - import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.Map; @@ -80,7 +79,8 @@ public class BazelWorkspaceStatusModule extends BlazeModule { Map<String, String> clientEnv, Path workspace, Artifact stableStatus, - Artifact volatileStatus) { + Artifact volatileStatus, + String hostname) { super( ActionOwner.SYSTEM_ACTION_OWNER, Artifact.NO_ARTIFACTS, @@ -89,7 +89,7 @@ public class BazelWorkspaceStatusModule extends BlazeModule { this.stableStatus = stableStatus; this.volatileStatus = volatileStatus; this.username = USER_NAME.value(); - this.hostname = NetUtil.findShortHostName(); + this.hostname = hostname; this.getWorkspaceStatusCommand = options.workspaceStatusCommand.equals(PathFragment.EMPTY_FRAGMENT) ? null @@ -210,6 +210,9 @@ public class BazelWorkspaceStatusModule extends BlazeModule { } private class BazelStatusActionFactory implements WorkspaceStatusAction.Factory { + + private String hostname; + @Override public Map<String, String> createDummyWorkspaceStatus() { return ImmutableMap.of(); @@ -226,12 +229,28 @@ public class BazelWorkspaceStatusModule extends BlazeModule { new PathFragment("volatile-status.txt"), root, artifactOwner); return new BazelWorkspaceStatusAction(options, env.getClientEnv(), - env.getDirectories().getWorkspace(), stableArtifact, volatileArtifact); + env.getDirectories().getWorkspace(), stableArtifact, volatileArtifact, getHostname()); + } + + /** + * Returns cached short hostname. + * + * <p>Hostname lookup performs reverse DNS lookup which in bad cases can take seconds. To + * speedup builds we only lookup hostname once and cache the result. Therefore if hostname + * changes during bazel server lifetime, bazel will not see the change. + */ + private String getHostname() { + if (hostname == null) { + hostname = NetUtil.findShortHostName(); + } + + return hostname; } } @ExecutionStrategy(contextType = WorkspaceStatusAction.Context.class) private class BazelWorkspaceStatusActionContext implements WorkspaceStatusAction.Context { + @Override public ImmutableMap<String, Key> getStableKeys() { return ImmutableMap.of( 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 564ff6d1fa..f22146a71d 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 @@ -25,8 +25,8 @@ public final class NetUtil { } /** - * Returns the short hostname or <code>unknown</code> if the host name could - * not be determined. + * 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! */ public static String findShortHostName() { try { |