Skip to content

Commit 0963f31

Browse files
authored
auth: Add support for Retryable interface
Retryable was added in google-auth-library 1.5.3 to make clear the situations that deserve a retry of the RPC. Bump to that version and swap away from the imprecise IOException heuristic. go/auth-correct-retry Fixes #6808
1 parent eeeeff0 commit 0963f31

File tree

6 files changed

+50
-15
lines changed

6 files changed

+50
-15
lines changed

auth/src/main/java/io/grpc/auth/GoogleAuthLibraryCallCredentials.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020

2121
import com.google.auth.Credentials;
2222
import com.google.auth.RequestMetadataCallback;
23+
import com.google.auth.Retryable;
2324
import com.google.common.annotations.VisibleForTesting;
2425
import com.google.common.io.BaseEncoding;
2526
import io.grpc.Metadata;
2627
import io.grpc.MethodDescriptor;
2728
import io.grpc.SecurityLevel;
2829
import io.grpc.Status;
2930
import io.grpc.StatusException;
30-
import java.io.IOException;
3131
import java.lang.reflect.InvocationTargetException;
3232
import java.lang.reflect.Method;
3333
import java.net.URI;
@@ -133,8 +133,8 @@ public void onSuccess(Map<String, List<String>> metadata) {
133133

134134
@Override
135135
public void onFailure(Throwable e) {
136-
if (e instanceof IOException) {
137-
// Since it's an I/O failure, let the call be retried with UNAVAILABLE.
136+
if (e instanceof Retryable && ((Retryable) e).isRetryable()) {
137+
// Let the call be retried with UNAVAILABLE.
138138
applier.fail(Status.UNAVAILABLE
139139
.withDescription("Credentials failed to obtain metadata")
140140
.withCause(e));

auth/src/test/java/io/grpc/auth/GoogleAuthLibraryCallCredentialsTest.java

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
import com.google.auth.Credentials;
3232
import com.google.auth.RequestMetadataCallback;
33+
import com.google.auth.Retryable;
3334
import com.google.auth.http.HttpTransportFactory;
3435
import com.google.auth.oauth2.AccessToken;
3536
import com.google.auth.oauth2.GoogleCredentials;
@@ -191,8 +192,9 @@ public void invalidBase64() throws Exception {
191192
}
192193

193194
@Test
194-
public void credentialsFailsWithIoException() throws Exception {
195-
Exception exception = new IOException("Broken");
195+
public void credentialsFailsWithRetryableRetryableException() throws Exception {
196+
boolean retryable = true;
197+
Exception exception = new RetryableException(retryable);
196198
when(credentials.getRequestMetadata(eq(expectedUri))).thenThrow(exception);
197199

198200
GoogleAuthLibraryCallCredentials callCredentials =
@@ -206,6 +208,23 @@ public void credentialsFailsWithIoException() throws Exception {
206208
assertEquals(exception, status.getCause());
207209
}
208210

211+
@Test
212+
public void credentialsFailsWithUnretryableRetryableException() throws Exception {
213+
boolean retryable = false;
214+
Exception exception = new RetryableException(retryable);
215+
when(credentials.getRequestMetadata(eq(expectedUri))).thenThrow(exception);
216+
217+
GoogleAuthLibraryCallCredentials callCredentials =
218+
new GoogleAuthLibraryCallCredentials(credentials);
219+
callCredentials.applyRequestMetadata(new RequestInfoImpl(), executor, applier);
220+
221+
verify(credentials).getRequestMetadata(eq(expectedUri));
222+
verify(applier).fail(statusCaptor.capture());
223+
Status status = statusCaptor.getValue();
224+
assertEquals(Status.Code.UNAUTHENTICATED, status.getCode());
225+
assertEquals(exception, status.getCause());
226+
}
227+
209228
@Test
210229
public void credentialsFailsWithRuntimeException() throws Exception {
211230
Exception exception = new RuntimeException("Broken");
@@ -453,4 +472,21 @@ public Attributes getTransportAttrs() {
453472
return Attributes.EMPTY;
454473
}
455474
}
475+
476+
private static class RetryableException extends IOException implements Retryable {
477+
private final boolean retryable;
478+
479+
public RetryableException(boolean retryable) {
480+
super("Broken");
481+
this.retryable = retryable;
482+
}
483+
484+
@Override public boolean isRetryable() {
485+
return retryable;
486+
}
487+
488+
@Override public int getRetryCount() {
489+
return 0;
490+
}
491+
}
456492
}

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ subprojects {
5757

5858
nettyVersion = '4.1.72.Final'
5959
guavaVersion = '31.0.1-android'
60-
googleauthVersion = '1.4.0'
60+
googleauthVersion = '1.5.3'
6161
protobufVersion = '3.19.2'
6262
protocVersion = protobufVersion
6363
opencensusVersion = '0.28.0'

gcp-observability/build.gradle

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,15 @@ dependencies {
2828
implementation project(':grpc-protobuf'),
2929
project(':grpc-stub'),
3030
project(':grpc-alts'),
31+
libraries.google_auth_credentials,
3132
libraries.google_auth_oauth2_http,
3233
libraries.autovalue_annotation,
3334
libraries.perfmark,
3435
('com.google.guava:guava:31.0.1-jre'),
3536
('com.google.errorprone:error_prone_annotations:2.11.0'),
36-
('com.google.auth:google-auth-library-credentials:1.4.0'),
3737
('org.checkerframework:checker-qual:3.20.0'),
38-
('com.google.auto.value:auto-value-annotations:1.9'),
39-
('com.google.http-client:google-http-client:1.41.0'),
40-
('com.google.http-client:google-http-client-gson:1.41.0'),
38+
('com.google.http-client:google-http-client:1.41.3'),
39+
('com.google.http-client:google-http-client-gson:1.41.3'),
4140
('com.google.api.grpc:proto-google-common-protos:2.7.1'),
4241
("com.google.cloud:google-cloud-logging:${cloudLoggingVersion}")
4342

interop-testing/src/main/java/io/grpc/testing/integration/AbstractInteropTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@
100100
import io.opencensus.trace.Span;
101101
import io.opencensus.trace.SpanContext;
102102
import io.opencensus.trace.Tracing;
103-
import io.opencensus.trace.unsafe.ContextUtils;
104103
import java.io.ByteArrayInputStream;
105104
import java.io.ByteArrayOutputStream;
106105
import java.io.IOException;
@@ -1547,6 +1546,7 @@ public void customMetadata() throws Exception {
15471546
Collections.singleton(streamingRequest), Collections.singleton(goldenStreamingResponse));
15481547
}
15491548

1549+
@SuppressWarnings("deprecation")
15501550
@Test(timeout = 10000)
15511551
public void censusContextsPropagated() {
15521552
Assume.assumeTrue("Skip the test because server is not in the same process.", server != null);
@@ -1561,7 +1561,7 @@ public void censusContextsPropagated() {
15611561
.emptyBuilder()
15621562
.putLocal(StatsTestUtils.EXTRA_TAG, TagValue.create("extra value"))
15631563
.build());
1564-
ctx = ContextUtils.withValue(ctx, clientParentSpan);
1564+
ctx = io.opencensus.trace.unsafe.ContextUtils.withValue(ctx, clientParentSpan);
15651565
Context origCtx = ctx.attach();
15661566
try {
15671567
blockingStub.unaryCall(SimpleRequest.getDefaultInstance());
@@ -1581,7 +1581,7 @@ public void censusContextsPropagated() {
15811581
}
15821582
assertTrue("tag not found", tagFound);
15831583

1584-
Span span = ContextUtils.getValue(serverCtx);
1584+
Span span = io.opencensus.trace.unsafe.ContextUtils.getValue(serverCtx);
15851585
assertNotNull(span);
15861586
SpanContext spanContext = span.getContext();
15871587
assertEquals(clientParentSpan.getContext().getTraceId(), spanContext.getTraceId());

repositories.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
1212
IO_GRPC_GRPC_JAVA_ARTIFACTS = [
1313
"com.google.android:annotations:4.1.1.4",
1414
"com.google.api.grpc:proto-google-common-protos:2.0.1",
15-
"com.google.auth:google-auth-library-credentials:0.22.0",
16-
"com.google.auth:google-auth-library-oauth2-http:0.22.0",
15+
"com.google.auth:google-auth-library-credentials:1.5.3",
16+
"com.google.auth:google-auth-library-oauth2-http:1.5.3",
1717
"com.google.code.findbugs:jsr305:3.0.2",
1818
"com.google.code.gson:gson:2.8.9",
1919
"com.google.auto.value:auto-value:1.7.4",

0 commit comments

Comments
 (0)