Skip to content

Commit 8bdd409

Browse files
hlopkophilwo
authored andcommitted
Only compute hostname once per server lifetime
NetUtil.getShortHostName can take seconds on mac and on windows (like, 20!), since it performs reverse dns lookup. We already cached hostname for the BazelWorkspaceStatusModule, let's cache it for entire bazel server. Also make sure that users of the method understand it's cached. Fixes #3586. RELNOTES: None. PiperOrigin-RevId: 168691615
1 parent fdc1e2f commit 8bdd409

File tree

4 files changed

+26
-14
lines changed

4 files changed

+26
-14
lines changed

src/main/java/com/google/devtools/build/lib/bazel/BazelWorkspaceStatusModule.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,6 @@ public Artifact getStableStatus() {
282282
}
283283

284284
private class BazelStatusActionFactory implements WorkspaceStatusAction.Factory {
285-
private String hostname;
286285

287286
@Override
288287
public Map<String, String> createDummyWorkspaceStatus() {
@@ -312,11 +311,7 @@ public WorkspaceStatusAction createWorkspaceStatusAction(
312311
* changes during bazel server lifetime, bazel will not see the change.
313312
*/
314313
private String getHostname() {
315-
if (hostname == null) {
316-
hostname = NetUtil.findShortHostName();
317-
}
318-
319-
return hostname;
314+
return NetUtil.getCachedShortHostName();
320315
}
321316
}
322317

src/main/java/com/google/devtools/build/lib/exec/local/LocalSpawnRunner.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public LocalSpawnRunner(
9090
this.execRoot = execRoot;
9191
this.processWrapper = getProcessWrapper(execRoot, localOs).getPathString();
9292
this.localExecutionOptions = Preconditions.checkNotNull(localExecutionOptions);
93-
this.hostName = NetUtil.findShortHostName();
93+
this.hostName = NetUtil.getCachedShortHostName();
9494
this.resourceManager = resourceManager;
9595
this.useProcessWrapper = useProcessWrapper;
9696
this.productName = productName;

src/main/java/com/google/devtools/build/lib/util/NetUtil.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,31 @@
2121
*/
2222
public final class NetUtil {
2323

24+
private static String hostname;
25+
2426
private NetUtil() {
2527
}
2628

29+
/**
30+
* Returns the *cached* short hostname (computed at most once per the lifetime of a server). Can
31+
* take seconds to complete when the cache is cold.
32+
*/
33+
public static String getCachedShortHostName() {
34+
if (hostname == null) {
35+
synchronized (NetUtil.class) {
36+
if (hostname == null) {
37+
hostname = computeShortHostName();
38+
}
39+
}
40+
}
41+
return computeShortHostName();
42+
}
43+
2744
/**
2845
* Returns the short hostname or <code>unknown</code> if the host name could not be determined.
29-
* Performs reverse DNS lookup and can take seconds to complete!
46+
* Performs reverse DNS lookup and can take seconds to complete.
3047
*/
31-
public static String findShortHostName() {
48+
private static String computeShortHostName() {
3249
try {
3350
return InetAddress.getLocalHost().getHostName();
3451
} catch (UnknownHostException e) {

src/test/java/com/google/devtools/build/lib/exec/local/LocalSpawnRunnerTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ public void vanillaZeroExit() throws Exception {
258258
assertThat(result.status()).isEqualTo(SpawnResult.Status.SUCCESS);
259259
assertThat(result.exitCode()).isEqualTo(0);
260260
assertThat(result.setupSuccess()).isTrue();
261-
assertThat(result.getExecutorHostName()).isEqualTo(NetUtil.findShortHostName());
261+
assertThat(result.getExecutorHostName()).isEqualTo(NetUtil.getCachedShortHostName());
262262

263263
assertThat(captor.getValue().getArgv())
264264
.containsExactlyElementsIn(
@@ -304,7 +304,7 @@ public void noProcessWrapper() throws Exception {
304304
assertThat(result.status()).isEqualTo(SpawnResult.Status.SUCCESS);
305305
assertThat(result.exitCode()).isEqualTo(0);
306306
assertThat(result.setupSuccess()).isTrue();
307-
assertThat(result.getExecutorHostName()).isEqualTo(NetUtil.findShortHostName());
307+
assertThat(result.getExecutorHostName()).isEqualTo(NetUtil.getCachedShortHostName());
308308

309309
assertThat(captor.getValue().getArgv())
310310
.containsExactlyElementsIn(ImmutableList.of("/bin/echo", "Hi!"));
@@ -339,7 +339,7 @@ public void nonZeroExit() throws Exception {
339339
assertThat(result.status()).isEqualTo(SpawnResult.Status.SUCCESS);
340340
assertThat(result.exitCode()).isEqualTo(3);
341341
assertThat(result.setupSuccess()).isTrue();
342-
assertThat(result.getExecutorHostName()).isEqualTo(NetUtil.findShortHostName());
342+
assertThat(result.getExecutorHostName()).isEqualTo(NetUtil.getCachedShortHostName());
343343

344344
assertThat(captor.getValue().getArgv())
345345
.containsExactlyElementsIn(
@@ -377,7 +377,7 @@ public void processStartupThrows() throws Exception {
377377
assertThat(result.exitCode()).isEqualTo(-1);
378378
assertThat(result.setupSuccess()).isFalse();
379379
assertThat(result.getWallTimeMillis()).isEqualTo(0);
380-
assertThat(result.getExecutorHostName()).isEqualTo(NetUtil.findShortHostName());
380+
assertThat(result.getExecutorHostName()).isEqualTo(NetUtil.getCachedShortHostName());
381381

382382
assertThat(FileSystemUtils.readContent(fs.getPath("/out/stderr"), StandardCharsets.UTF_8))
383383
.isEqualTo("Action failed to execute: java.io.IOException: I'm sorry, Dave\n");
@@ -399,7 +399,7 @@ public void disallowLocalExecution() throws Exception {
399399
assertThat(reply.exitCode()).isEqualTo(-1);
400400
assertThat(reply.setupSuccess()).isFalse();
401401
assertThat(reply.getWallTimeMillis()).isEqualTo(0);
402-
assertThat(reply.getExecutorHostName()).isEqualTo(NetUtil.findShortHostName());
402+
assertThat(reply.getExecutorHostName()).isEqualTo(NetUtil.getCachedShortHostName());
403403

404404
// TODO(ulfjack): Maybe we should only lock after checking?
405405
assertThat(policy.lockOutputFilesCalled).isTrue();

0 commit comments

Comments
 (0)