Skip to content

Commit 9c0940d

Browse files
tjgqcopybara-github
authored andcommitted
Add profiler task for calling a credential helper.
This might help debug future performance issues related caused by slow credential helpers. Closes bazelbuild#16226. PiperOrigin-RevId: 472683449 Change-Id: Ib1f51a51763ccfa0e18be980fd4b4d77954a9814
1 parent 93725da commit 9c0940d

File tree

3 files changed

+52
-42
lines changed

3 files changed

+52
-42
lines changed

src/main/java/com/google/devtools/build/lib/authandtls/credentialhelper/BUILD

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ java_library(
1313
srcs = glob(["*.java"]),
1414
deps = [
1515
"//src/main/java/com/google/devtools/build/lib/events",
16+
"//src/main/java/com/google/devtools/build/lib/profiler",
1617
"//src/main/java/com/google/devtools/build/lib/shell",
1718
"//src/main/java/com/google/devtools/build/lib/vfs",
1819
"//third_party:auth",

src/main/java/com/google/devtools/build/lib/authandtls/credentialhelper/CredentialHelper.java

+50-42
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@
1414

1515
package com.google.devtools.build.lib.authandtls.credentialhelper;
1616

17+
import static com.google.devtools.build.lib.profiler.ProfilerTask.CREDENTIAL_HELPER;
1718
import static java.nio.charset.StandardCharsets.UTF_8;
1819

1920
import com.google.common.annotations.VisibleForTesting;
2021
import com.google.common.base.Preconditions;
2122
import com.google.common.collect.ImmutableList;
2223
import com.google.common.io.CharStreams;
24+
import com.google.devtools.build.lib.profiler.Profiler;
25+
import com.google.devtools.build.lib.profiler.SilentCloseable;
2326
import com.google.devtools.build.lib.shell.Subprocess;
2427
import com.google.devtools.build.lib.shell.SubprocessBuilder;
2528
import com.google.devtools.build.lib.vfs.Path;
@@ -68,57 +71,62 @@ public GetCredentialsResponse getCredentials(CredentialHelperEnvironment environ
6871
Preconditions.checkNotNull(environment);
6972
Preconditions.checkNotNull(uri);
7073

71-
Subprocess process = spawnSubprocess(environment, "get");
72-
try (Reader stdout = new InputStreamReader(process.getInputStream(), UTF_8);
73-
Reader stderr = new InputStreamReader(process.getErrorStream(), UTF_8)) {
74-
try (Writer stdin = new OutputStreamWriter(process.getOutputStream(), UTF_8)) {
75-
GSON.toJson(GetCredentialsRequest.newBuilder().setUri(uri).build(), stdin);
76-
}
74+
Profiler prof = Profiler.instance();
7775

78-
process.waitFor();
79-
if (process.timedout()) {
80-
throw new CredentialHelperException(
81-
String.format(
82-
Locale.US,
83-
"Failed to get credentials for '%s' from helper '%s': process timed out",
84-
uri,
85-
path));
86-
}
87-
if (process.exitValue() != 0) {
88-
throw new CredentialHelperException(
89-
String.format(
90-
Locale.US,
91-
"Failed to get credentials for '%s' from helper '%s': process exited with code %d."
92-
+ " stderr: %s",
93-
uri,
94-
path,
95-
process.exitValue(),
96-
CharStreams.toString(stderr)));
97-
}
76+
try (SilentCloseable c = prof.profile(CREDENTIAL_HELPER, "calling credential helper")) {
77+
Subprocess process = spawnSubprocess(environment, "get");
78+
try (Reader stdout = new InputStreamReader(process.getInputStream(), UTF_8);
79+
Reader stderr = new InputStreamReader(process.getErrorStream(), UTF_8)) {
80+
try (Writer stdin = new OutputStreamWriter(process.getOutputStream(), UTF_8)) {
81+
GSON.toJson(GetCredentialsRequest.newBuilder().setUri(uri).build(), stdin);
82+
}
83+
84+
process.waitFor();
9885

99-
try {
100-
GetCredentialsResponse response = GSON.fromJson(stdout, GetCredentialsResponse.class);
101-
if (response == null) {
86+
if (process.timedout()) {
10287
throw new CredentialHelperException(
10388
String.format(
10489
Locale.US,
105-
"Failed to get credentials for '%s' from helper '%s': process exited without"
106-
+ " output. stderr: %s",
90+
"Failed to get credentials for '%s' from helper '%s': process timed out",
91+
uri,
92+
path));
93+
}
94+
if (process.exitValue() != 0) {
95+
throw new CredentialHelperException(
96+
String.format(
97+
Locale.US,
98+
"Failed to get credentials for '%s' from helper '%s': process exited with code"
99+
+ " %d. stderr: %s",
107100
uri,
108101
path,
102+
process.exitValue(),
109103
CharStreams.toString(stderr)));
110104
}
111-
return response;
112-
} catch (JsonSyntaxException e) {
113-
throw new CredentialHelperException(
114-
String.format(
115-
Locale.US,
116-
"Failed to get credentials for '%s' from helper '%s': error parsing output. stderr:"
117-
+ " %s",
118-
uri,
119-
path,
120-
CharStreams.toString(stderr)),
121-
e);
105+
106+
try {
107+
GetCredentialsResponse response = GSON.fromJson(stdout, GetCredentialsResponse.class);
108+
if (response == null) {
109+
throw new CredentialHelperException(
110+
String.format(
111+
Locale.US,
112+
"Failed to get credentials for '%s' from helper '%s': process exited without"
113+
+ " output. stderr: %s",
114+
uri,
115+
path,
116+
CharStreams.toString(stderr)));
117+
}
118+
return response;
119+
} catch (JsonSyntaxException e) {
120+
throw new CredentialHelperException(
121+
String.format(
122+
Locale.US,
123+
"Failed to get credentials for '%s' from helper '%s': error parsing output."
124+
+ " stderr: %s",
125+
uri,
126+
path,
127+
CharStreams.toString(stderr)),
128+
e);
129+
}
122130
}
123131
}
124132
}

src/main/java/com/google/devtools/build/lib/profiler/ProfilerTask.java

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public enum ProfilerTask {
8686
WORKER_BORROW("borrowing a worker"),
8787
WORKER_WORKING("waiting for response from worker"),
8888
WORKER_COPYING_OUTPUTS("copying outputs from worker"),
89+
CREDENTIAL_HELPER("calling credential helper"),
8990
UNKNOWN("Unknown event");
9091

9192
private static class Threshold {

0 commit comments

Comments
 (0)