Skip to content

Commit 95697dd

Browse files
authored
fix: update grpc resumable upload error categorization to be more tolerant (#2644)
When a client is shutting down, the Channel pool will deliver an error to our stream observer. This could happen before we are able to actually write a message. Update GapicUnbufferedFinalizeOnCloseResumableWritableByteChannel to not attempt to pass null to ImmutableList.of().
1 parent 1be23c9 commit 95697dd

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

‎google-cloud-storage/src/main/java/com/google/cloud/storage/GapicUnbufferedFinalizeOnCloseResumableWritableByteChannel.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.google.cloud.storage;
1818

1919
import static com.google.cloud.storage.GrpcUtils.contextWithBucketName;
20+
import static com.google.cloud.storage.Utils.nullSafeList;
2021

2122
import com.google.api.core.SettableApiFuture;
2223
import com.google.api.gax.grpc.GrpcCallContext;
@@ -26,7 +27,6 @@
2627
import com.google.cloud.storage.ChunkSegmenter.ChunkSegment;
2728
import com.google.cloud.storage.Crc32cValue.Crc32cLengthKnown;
2829
import com.google.cloud.storage.UnbufferedWritableByteChannelSession.UnbufferedWritableByteChannel;
29-
import com.google.common.collect.ImmutableList;
3030
import com.google.protobuf.ByteString;
3131
import com.google.storage.v2.ChecksummedData;
3232
import com.google.storage.v2.ObjectChecksums;
@@ -230,7 +230,7 @@ public void onError(Throwable t) {
230230
tmp.getCode(),
231231
tmp.getMessage(),
232232
tmp.getReason(),
233-
ImmutableList.of(lastWrittenRequest),
233+
nullSafeList(lastWrittenRequest),
234234
null,
235235
context,
236236
t);
@@ -251,7 +251,7 @@ public void onCompleted() {
251251
0,
252252
"onComplete without preceding onNext, unable to determine success.",
253253
"invalid",
254-
ImmutableList.of(lastWrittenRequest),
254+
nullSafeList(lastWrittenRequest),
255255
null,
256256
context,
257257
null));
@@ -263,26 +263,26 @@ public void onCompleted() {
263263
} else if (finalSize < totalSentBytes) {
264264
clientDetectedError(
265265
ResumableSessionFailureScenario.SCENARIO_4_1.toStorageException(
266-
ImmutableList.of(lastWrittenRequest), last, context, null));
266+
nullSafeList(lastWrittenRequest), last, context, null));
267267
} else {
268268
clientDetectedError(
269269
ResumableSessionFailureScenario.SCENARIO_4_2.toStorageException(
270-
ImmutableList.of(lastWrittenRequest), last, context, null));
270+
nullSafeList(lastWrittenRequest), last, context, null));
271271
}
272272
} else if (!finalizing || last.hasPersistedSize()) { // unexpected incremental response
273273
clientDetectedError(
274274
ResumableSessionFailureScenario.toStorageException(
275275
0,
276276
"Unexpected incremental response for finalizing request.",
277277
"invalid",
278-
ImmutableList.of(lastWrittenRequest),
278+
nullSafeList(lastWrittenRequest),
279279
last,
280280
context,
281281
null));
282282
} else {
283283
clientDetectedError(
284284
ResumableSessionFailureScenario.SCENARIO_0.toStorageException(
285-
ImmutableList.of(lastWrittenRequest), last, context, null));
285+
nullSafeList(lastWrittenRequest), last, context, null));
286286
}
287287
}
288288

‎google-cloud-storage/src/main/java/com/google/cloud/storage/ResumableSessionFailureScenario.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ static StorageException toStorageException(
169169
Map<String, List<String>> extraHeaders = context.getExtraHeaders();
170170
recordHeadersTo(extraHeaders, PREFIX_O, sb);
171171
int length = reqs.size();
172+
if (length == 0) {
173+
sb.append("\n").append(PREFIX_O).append("[]");
174+
}
172175
for (int i = 0; i < length; i++) {
173176
if (i == 0) {
174177
sb.append("\n").append(PREFIX_O).append("[");

‎google-cloud-storage/src/main/java/com/google/cloud/storage/Utils.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.google.cloud.storage.Conversions.Codec;
2626
import com.google.cloud.storage.UnifiedOpts.NamedField;
2727
import com.google.common.annotations.VisibleForTesting;
28+
import com.google.common.collect.ImmutableList;
2829
import com.google.common.collect.MapDifference;
2930
import com.google.common.collect.Maps;
3031
import com.google.common.io.BaseEncoding;
@@ -310,4 +311,12 @@ private static String crc32cEncode(int from) {
310311
static GrpcCallContext merge(@NonNull GrpcCallContext l, @NonNull GrpcCallContext r) {
311312
return (GrpcCallContext) l.merge(r);
312313
}
314+
315+
static <T> ImmutableList<T> nullSafeList(@Nullable T t) {
316+
if (t == null) {
317+
return ImmutableList.of();
318+
} else {
319+
return ImmutableList.of(t);
320+
}
321+
}
313322
}

0 commit comments

Comments
 (0)