Skip to content

Commit 79b5d85

Browse files
authored
fix: update gRPC Bidi resumable upload to have more robust error message generation (#2998)
onError can be called before a message is written to the stream, make sure we are accounting for the possibility that lastWrittenRequest can be null.
1 parent b096249 commit 79b5d85

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

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

Lines changed: 11 additions & 13 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;
@@ -32,7 +33,6 @@
3233
import com.google.cloud.storage.Retrying.RetryingDependencies;
3334
import com.google.cloud.storage.UnbufferedWritableByteChannelSession.UnbufferedWritableByteChannel;
3435
import com.google.common.annotations.VisibleForTesting;
35-
import com.google.common.collect.ImmutableList;
3636
import com.google.protobuf.ByteString;
3737
import com.google.storage.v2.BidiWriteObjectRequest;
3838
import com.google.storage.v2.BidiWriteObjectResponse;
@@ -297,7 +297,7 @@ public void onNext(BidiWriteObjectResponse value) {
297297
} else {
298298
clientDetectedError(
299299
ResumableSessionFailureScenario.SCENARIO_7.toStorageException(
300-
ImmutableList.of(lastWrittenRequest), value, context, null));
300+
nullSafeList(lastWrittenRequest), value, context, null));
301301
}
302302
} else if (finalizing && value.hasResource()) {
303303
long totalSentBytes = writeCtx.getTotalSentBytes().get();
@@ -308,16 +308,16 @@ public void onNext(BidiWriteObjectResponse value) {
308308
} else if (finalSize < totalSentBytes) {
309309
clientDetectedError(
310310
ResumableSessionFailureScenario.SCENARIO_4_1.toStorageException(
311-
ImmutableList.of(lastWrittenRequest), value, context, null));
311+
nullSafeList(lastWrittenRequest), value, context, null));
312312
} else {
313313
clientDetectedError(
314314
ResumableSessionFailureScenario.SCENARIO_4_2.toStorageException(
315-
ImmutableList.of(lastWrittenRequest), value, context, null));
315+
nullSafeList(lastWrittenRequest), value, context, null));
316316
}
317317
} else if (!finalizing && value.hasResource()) {
318318
clientDetectedError(
319319
ResumableSessionFailureScenario.SCENARIO_1.toStorageException(
320-
ImmutableList.of(lastWrittenRequest), value, context, null));
320+
nullSafeList(lastWrittenRequest), value, context, null));
321321
} else if (finalizing && value.hasPersistedSize()) {
322322
long totalSentBytes = writeCtx.getTotalSentBytes().get();
323323
long persistedSize = value.getPersistedSize();
@@ -329,16 +329,16 @@ public void onNext(BidiWriteObjectResponse value) {
329329
} else if (persistedSize < totalSentBytes) {
330330
clientDetectedError(
331331
ResumableSessionFailureScenario.SCENARIO_3.toStorageException(
332-
ImmutableList.of(lastWrittenRequest), value, context, null));
332+
nullSafeList(lastWrittenRequest), value, context, null));
333333
} else {
334334
clientDetectedError(
335335
ResumableSessionFailureScenario.SCENARIO_2.toStorageException(
336-
ImmutableList.of(lastWrittenRequest), value, context, null));
336+
nullSafeList(lastWrittenRequest), value, context, null));
337337
}
338338
} else {
339339
clientDetectedError(
340340
ResumableSessionFailureScenario.SCENARIO_0.toStorageException(
341-
ImmutableList.of(lastWrittenRequest), value, context, null));
341+
nullSafeList(lastWrittenRequest), value, context, null));
342342
}
343343
}
344344

@@ -352,7 +352,7 @@ public void onError(Throwable t) {
352352
&& ed.getErrorInfo().getReason().equals("GRPC_MISMATCHED_UPLOAD_SIZE"))) {
353353
clientDetectedError(
354354
ResumableSessionFailureScenario.SCENARIO_5.toStorageException(
355-
ImmutableList.of(lastWrittenRequest), null, context, oore));
355+
nullSafeList(lastWrittenRequest), null, context, oore));
356356
return;
357357
}
358358
}
@@ -367,9 +367,7 @@ public void onError(Throwable t) {
367367
tmp.getCode(),
368368
tmp.getMessage(),
369369
tmp.getReason(),
370-
lastWrittenRequest != null
371-
? ImmutableList.of(lastWrittenRequest)
372-
: ImmutableList.of(),
370+
nullSafeList(lastWrittenRequest),
373371
null,
374372
context,
375373
t);
@@ -422,7 +420,7 @@ void await() {
422420
clientDetectedError = null;
423421
previousError = null;
424422
if ((e != null || err != null) && stream != null) {
425-
if (lastWrittenRequest.getFinishWrite()) {
423+
if (lastWrittenRequest != null && lastWrittenRequest.getFinishWrite()) {
426424
stream.onCompleted();
427425
} else {
428426
stream.onError(Status.CANCELLED.asRuntimeException());

0 commit comments

Comments
 (0)