Skip to content

Commit 065d249

Browse files
committed
chore: update BidiReadObject*Error location logic to leverage new method ErrorDetails#getMessage(Class<T extends Message>)
1 parent 61824e6 commit 065d249

File tree

3 files changed

+25
-71
lines changed

3 files changed

+25
-71
lines changed

google-cloud-storage/src/main/java/com/google/cloud/storage/GrpcUtils.java

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.google.api.gax.rpc.BidiStreamingCallable;
2626
import com.google.api.gax.rpc.ClientStream;
2727
import com.google.api.gax.rpc.ClientStreamReadyObserver;
28+
import com.google.api.gax.rpc.ErrorDetails;
2829
import com.google.api.gax.rpc.ResponseObserver;
2930
import com.google.api.gax.rpc.ServerStream;
3031
import com.google.api.gax.rpc.ServerStreamingCallable;
@@ -33,15 +34,9 @@
3334
import com.google.api.gax.rpc.UnaryCallable;
3435
import com.google.common.collect.ImmutableList;
3536
import com.google.common.collect.ImmutableMap;
36-
import com.google.protobuf.Any;
37-
import com.google.protobuf.InvalidProtocolBufferException;
3837
import com.google.protobuf.Message;
39-
import com.google.rpc.Status;
4038
import com.google.storage.v2.BidiReadObjectError;
4139
import com.google.storage.v2.BidiReadObjectRedirectedError;
42-
import io.grpc.Metadata;
43-
import io.grpc.StatusRuntimeException;
44-
import io.grpc.protobuf.ProtoUtils;
4540
import java.io.Closeable;
4641
import java.io.IOException;
4742
import java.util.Collection;
@@ -53,10 +48,6 @@
5348

5449
final class GrpcUtils {
5550

56-
static final Metadata.Key<Status> GRPC_STATUS_DETAILS_KEY =
57-
Metadata.Key.of(
58-
"grpc-status-details-bin", ProtoUtils.metadataMarshaller(Status.getDefaultInstance()));
59-
6051
private GrpcUtils() {}
6152

6253
static GrpcCallContext contextWithBucketName(String bucketName, GrpcCallContext baseContext) {
@@ -104,53 +95,32 @@ static <C extends Closeable> void closeAll(Collection<C> closeables) throws IOEx
10495
}
10596

10697
/**
107-
* Returns the first occurrence of a {@link BidiReadObjectRedirectedError} if the throwable is or
108-
* is caused by a {@link StatusRuntimeException} that contains trailers, the trailers contain an
109-
* entry {@code grpc-status-details-bin}, which contains a valid {@link Status}, and the status
110-
* contains an entry in its details that is a {@link BidiReadObjectRedirectedError} (evaluated
111-
* from index 0 to length). {@code null} otherwise.
98+
* Returns the first occurrence of a {@link BidiReadObjectRedirectedError} if the throwable is an
99+
* {@link ApiException} with {@link ErrorDetails} that contain an entry that is a {@link
100+
* BidiReadObjectRedirectedError} (evaluated from index 0 to length). {@code null} otherwise.
112101
*/
113102
@Nullable
114103
static BidiReadObjectRedirectedError getBidiReadObjectRedirectedError(Throwable t) {
115104
return findFirstPackedAny(t, BidiReadObjectRedirectedError.class);
116105
}
117106

118107
/**
119-
* Returns the first occurrence of a {@link BidiReadObjectError} if the throwable is or is caused
120-
* by a {@link StatusRuntimeException} that contains trailers, the trailers contain an entry
121-
* {@code grpc-status-details-bin}, which contains a valid {@link Status}, and the status contains
122-
* an entry in its details that is a {@link BidiReadObjectError} (evaluated from index 0 to
123-
* length). {@code null} otherwise.
108+
* Returns the first occurrence of a {@link BidiReadObjectError} if the throwable is an {@link
109+
* ApiException} with {@link ErrorDetails} that contain an entry that is a {@link
110+
* BidiReadObjectError} (evaluated from index 0 to length). {@code null} otherwise.
124111
*/
112+
@Nullable
125113
static BidiReadObjectError getBidiReadObjectError(Throwable t) {
126114
return findFirstPackedAny(t, BidiReadObjectError.class);
127115
}
128116

129117
@Nullable
130118
private static <M extends Message> M findFirstPackedAny(Throwable t, Class<M> clazz) {
131119
if (t instanceof ApiException) {
132-
t = t.getCause();
133-
}
134-
if (!(t instanceof StatusRuntimeException)) {
135-
return null;
136-
}
137-
StatusRuntimeException sre = (StatusRuntimeException) t;
138-
Metadata trailers = sre.getTrailers();
139-
if (trailers == null) {
140-
return null;
141-
}
142-
Status status = trailers.get(GRPC_STATUS_DETAILS_KEY);
143-
if (status == null) {
144-
return null;
145-
}
146-
List<Any> detailsList = status.getDetailsList();
147-
for (Any any : detailsList) {
148-
if (any.is(clazz)) {
149-
try {
150-
return any.unpack(clazz);
151-
} catch (InvalidProtocolBufferException e) {
152-
// ignore it, falling back to regular retry behavior
153-
}
120+
ApiException apiException = (ApiException) t;
121+
ErrorDetails errorDetails = apiException.getErrorDetails();
122+
if (errorDetails != null) {
123+
return errorDetails.getMessage(clazz);
154124
}
155125
}
156126
return null;

google-cloud-storage/src/test/java/com/google/cloud/storage/ITBlobDescriptorFakeTest.java

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import io.grpc.Status;
4848
import io.grpc.Status.Code;
4949
import io.grpc.StatusRuntimeException;
50+
import io.grpc.protobuf.ProtoUtils;
5051
import io.grpc.stub.StreamObserver;
5152
import java.nio.ByteBuffer;
5253
import java.util.Arrays;
@@ -58,6 +59,11 @@
5859

5960
public final class ITBlobDescriptorFakeTest {
6061

62+
private static final Metadata.Key<com.google.rpc.Status> GRPC_STATUS_DETAILS_KEY =
63+
Metadata.Key.of(
64+
"grpc-status-details-bin",
65+
ProtoUtils.metadataMarshaller(com.google.rpc.Status.getDefaultInstance()));
66+
6167
/**
6268
*
6369
*
@@ -156,7 +162,7 @@ public void onNext(BidiReadObjectRequest value) {
156162
.build();
157163

158164
Metadata trailers = new Metadata();
159-
trailers.put(GrpcUtils.GRPC_STATUS_DETAILS_KEY, grpcStatusDetails);
165+
trailers.put(GRPC_STATUS_DETAILS_KEY, grpcStatusDetails);
160166
StatusRuntimeException statusRuntimeException =
161167
Status.UNAVAILABLE.withDescription("redirect").asRuntimeException(trailers);
162168
respond.onError(statusRuntimeException);
@@ -181,14 +187,7 @@ public void onCompleted() {
181187
};
182188

183189
try (FakeServer fakeServer = FakeServer.of(fake);
184-
Storage storage =
185-
fakeServer
186-
.getGrpcStorageOptions()
187-
.toBuilder()
188-
.setGrpcInterceptorProvider(
189-
GrpcPlainRequestLoggingInterceptor.getInterceptorProvider())
190-
.build()
191-
.getService()) {
190+
Storage storage = fakeServer.getGrpcStorageOptions().toBuilder().build().getService()) {
192191

193192
BlobId id = BlobId.of("b", "o");
194193
ApiFuture<BlobDescriptor> futureBlobDescriptor = storage.getBlobDescriptor(id);
@@ -270,7 +269,7 @@ public void onNext(BidiReadObjectRequest value) {
270269
.build();
271270

272271
Metadata trailers = new Metadata();
273-
trailers.put(GrpcUtils.GRPC_STATUS_DETAILS_KEY, grpcStatusDetails);
272+
trailers.put(GRPC_STATUS_DETAILS_KEY, grpcStatusDetails);
274273
StatusRuntimeException statusRuntimeException =
275274
Status.UNAVAILABLE.withDescription("redirect").asRuntimeException(trailers);
276275
respond.onError(statusRuntimeException);
@@ -344,7 +343,7 @@ public void onNext(BidiReadObjectRequest value) {
344343
.build();
345344

346345
Metadata trailers = new Metadata();
347-
trailers.put(GrpcUtils.GRPC_STATUS_DETAILS_KEY, grpcStatusDetails);
346+
trailers.put(GRPC_STATUS_DETAILS_KEY, grpcStatusDetails);
348347
StatusRuntimeException statusRuntimeException =
349348
Status.UNAVAILABLE
350349
.withDescription(String.format("redirect %03d", requestCount))
@@ -366,14 +365,7 @@ public void onCompleted() {
366365
};
367366

368367
try (FakeServer fakeServer = FakeServer.of(fake);
369-
Storage storage =
370-
fakeServer
371-
.getGrpcStorageOptions()
372-
.toBuilder()
373-
.setGrpcInterceptorProvider(
374-
GrpcPlainRequestLoggingInterceptor.getInterceptorProvider())
375-
.build()
376-
.getService()) {
368+
Storage storage = fakeServer.getGrpcStorageOptions().toBuilder().build().getService()) {
377369

378370
BlobId id = BlobId.of("b", "o");
379371
ApiFuture<BlobDescriptor> futureBlobDescriptor = storage.getBlobDescriptor(id);
@@ -499,7 +491,7 @@ public void onNext(BidiReadObjectRequest value) {
499491
.build();
500492

501493
Metadata trailers = new Metadata();
502-
trailers.put(GrpcUtils.GRPC_STATUS_DETAILS_KEY, grpcStatusDetails);
494+
trailers.put(GRPC_STATUS_DETAILS_KEY, grpcStatusDetails);
503495
StatusRuntimeException statusRuntimeException =
504496
Status.UNAVAILABLE.withDescription("redirect").asRuntimeException(trailers);
505497
respond.onNext(res2);
@@ -525,14 +517,7 @@ public void onCompleted() {
525517
};
526518

527519
try (FakeServer fakeServer = FakeServer.of(fake);
528-
Storage storage =
529-
fakeServer
530-
.getGrpcStorageOptions()
531-
.toBuilder()
532-
.setGrpcInterceptorProvider(
533-
GrpcPlainRequestLoggingInterceptor.getInterceptorProvider())
534-
.build()
535-
.getService()) {
520+
Storage storage = fakeServer.getGrpcStorageOptions().toBuilder().build().getService()) {
536521

537522
BlobId id = BlobId.of("b", "o");
538523
ApiFuture<BlobDescriptor> futureBlobDescriptor = storage.getBlobDescriptor(id);

google-cloud-storage/src/test/java/com/google/cloud/storage/it/runner/registry/BackendResources.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ static BackendResources of(
135135
.setGrpcInterceptorProvider(
136136
GrpcPlainRequestLoggingInterceptor.getInterceptorProvider())
137137
.setEnableGrpcClientMetrics(false)
138-
.setAttemptDirectPath(false)
139138
.build();
140139
return new StorageInstance(built, protectedBucketNames);
141140
});

0 commit comments

Comments
 (0)