Skip to content

Commit 8a8eb57

Browse files
committed
Fix handling of HTTP_GONE response
1 parent 88f276c commit 8a8eb57

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

modules/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageBlobStore.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -398,19 +398,24 @@ public void write(byte[] b, int off, int len) throws IOException {
398398
writeBlob(purpose, blobName, buffer.bytes(), failIfAlreadyExists);
399399
}
400400
return;
401-
} catch (final StorageException se) {
402-
final int errorCode = se.getCode();
403-
if (errorCode == HTTP_GONE) {
404-
logger.warn(() -> format("Retrying broken resumable upload session for blob %s", blobInfo), se);
405-
storageException = ExceptionsHelper.useOrSuppress(storageException, se);
406-
continue;
407-
} else if (failIfAlreadyExists && errorCode == HTTP_PRECON_FAILED) {
408-
throw new FileAlreadyExistsException(blobInfo.getBlobId().getName(), null, se.getMessage());
401+
} catch (IOException ioException) {
402+
// This is for handling exceptions thrown by resumable uploads. BaseStorageWriteChannel.write wraps
403+
// the StorageException in an IOException, so we need to unwrap it.
404+
final StorageException se = (StorageException) ExceptionsHelper.unwrap(ioException, StorageException.class);
405+
if (se != null) {
406+
final int errorCode = se.getCode();
407+
if (errorCode == HTTP_GONE) {
408+
logger.warn(() -> format("Retrying broken resumable upload session for blob %s", blobInfo), se);
409+
storageException = ExceptionsHelper.useOrSuppress(storageException, se);
410+
continue;
411+
} else if (failIfAlreadyExists && errorCode == HTTP_PRECON_FAILED) {
412+
throw new FileAlreadyExistsException(blobInfo.getBlobId().getName(), null, se.getMessage());
413+
}
409414
}
410415
if (storageException != null) {
411-
se.addSuppressed(storageException);
416+
ioException.addSuppressed(storageException);
412417
}
413-
throw se;
418+
throw ioException;
414419
}
415420
}
416421
assert storageException != null;
@@ -466,6 +471,8 @@ private void writeBlobResumable(
466471
stats.trackPutOperation();
467472
return;
468473
} catch (final IOException ioException) {
474+
// This is for handling exceptions thrown by resumable uploads. BaseStorageWriteChannel.write wraps
475+
// the StorageException in an IOException, so we need to unwrap it.
469476
final StorageException se = (StorageException) ExceptionsHelper.unwrap(ioException, StorageException.class);
470477
if (se != null) {
471478
final int errorCode = se.getCode();
@@ -477,10 +484,9 @@ private void writeBlobResumable(
477484
} else if (failIfAlreadyExists && errorCode == HTTP_PRECON_FAILED) {
478485
throw new FileAlreadyExistsException(blobInfo.getBlobId().getName(), null, se.getMessage());
479486
}
480-
if (storageException != null) {
481-
se.addSuppressed(storageException);
482-
}
483-
throw se;
487+
}
488+
if (storageException != null) {
489+
ioException.addSuppressed(storageException);
484490
}
485491
throw ioException;
486492
}

0 commit comments

Comments
 (0)