Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update UploadCallable to use createFrom to avoid NPE trying to resolve resulting object #2086

Merged
merged 5 commits into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private ParallelUploadConfig(
this.prefix = prefix;
this.bucketName = bucketName;
this.targetOptsPerRequest = targetOptsPerRequest;
this.writeOptsPerRequest = writeOptsPerRequest;
this.writeOptsPerRequest = applySkipIfExists(skipIfExists, writeOptsPerRequest);
}

/** If a corresponding object already exists skip uploading the object */
Expand Down Expand Up @@ -115,6 +115,17 @@ public static Builder newBuilder() {
return new Builder();
}

private static List<BlobWriteOption> applySkipIfExists(
boolean skipIfExists, List<BlobWriteOption> writeOptsPerRequest) {
if (skipIfExists) {
return writeOptsPerRequest.isEmpty()
sydney-munro marked this conversation as resolved.
Show resolved Hide resolved
? ImmutableList.of(BlobWriteOption.generationMatch(0))
: ImmutableList.copyOf(
BlobWriteOption.dedupe(writeOptsPerRequest, BlobWriteOption.generationMatch(0L)));
}
return writeOptsPerRequest;
}

@BetaApi
public static final class Builder {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,14 @@ private UploadResult uploadWithoutChunking() {
long bytesCopied = -1L;
try {
Optional<BlobInfo> newBlob;
try (FileChannel r = FileChannel.open(sourceFile, StandardOpenOption.READ);
WriteChannel w = storage.writer(originalBlob, opts)) {
WriteChannel w = storage.writer(originalBlob, opts);
sydney-munro marked this conversation as resolved.
Show resolved Hide resolved
try (FileChannel r = FileChannel.open(sourceFile, StandardOpenOption.READ)) {
w.setChunkSize(transferManagerConfig.getPerWorkerBufferSize());
bytesCopied = ByteStreams.copy(r, w);
newBlob = PackagePrivateMethodWorkarounds.maybeGetBlobInfoFunction().apply(w);
} finally {
w.close();
}
newBlob = PackagePrivateMethodWorkarounds.maybeGetBlobInfoFunction().apply(w);
return UploadResult.newBuilder(originalBlob, TransferStatus.SUCCESS)
.setUploadedBlob(newBlob.get())
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ public void uploadFiles() throws Exception {
UploadJob job = transferManager.uploadFiles(files, parallelUploadConfig);
List<UploadResult> uploadResults = job.getUploadResults();
assertThat(uploadResults).hasSize(3);
assertThat(
uploadResults.stream()
.filter(result -> result.getStatus() == TransferStatus.SUCCESS)
.collect(Collectors.toList()))
.hasSize(3);
}
}

Expand All @@ -153,6 +158,11 @@ public void uploadFilesWithOpts() throws Exception {
UploadJob job = transferManager.uploadFiles(files, parallelUploadConfig);
List<UploadResult> uploadResults = job.getUploadResults();
assertThat(uploadResults).hasSize(3);
assertThat(
uploadResults.stream()
.filter(result -> result.getStatus() == TransferStatus.SUCCESS)
.collect(Collectors.toList()))
.hasSize(3);
}
}

Expand Down Expand Up @@ -181,6 +191,11 @@ public void uploadFilesOneFailure() throws Exception {
.filter(x -> x.getStatus() == TransferStatus.FAILED_TO_START)
.collect(Collectors.toList()))
.hasSize(1);
assertThat(
uploadResults.stream()
.filter(result -> result.getStatus() == TransferStatus.SUCCESS)
.collect(Collectors.toList()))
.hasSize(3);
}
}

Expand Down Expand Up @@ -217,6 +232,28 @@ public void uploadNonexistentFile() throws Exception {
}
}

@Test
public void uploadFailsSkipIfExists() throws Exception {
TransferManagerConfig config =
TransferManagerConfigTestingInstances.defaults(storage.getOptions()).toBuilder().build();
String bucketName = bucket.getName();
try (TransferManager transferManager = config.getService();
TmpFile tmpFile = DataGenerator.base64Characters().tempFile(baseDir, objectContentSize)) {
ParallelUploadConfig parallelUploadConfig =
ParallelUploadConfig.newBuilder().setBucketName(bucketName).setSkipIfExists(true).build();
UploadJob jobInitUpload =
transferManager.uploadFiles(ImmutableList.of(tmpFile.getPath()), parallelUploadConfig);
List<UploadResult> uploadResults = jobInitUpload.getUploadResults();
assertThat(uploadResults.get(0).getStatus()).isEqualTo(TransferStatus.SUCCESS);
UploadJob failedSecondUpload =
transferManager.uploadFiles(ImmutableList.of(tmpFile.getPath()), parallelUploadConfig);
List<UploadResult> failedResult = failedSecondUpload.getUploadResults();
assertThat(failedResult.get(0).getStatus()).isEqualTo(TransferStatus.FAILED_TO_FINISH);
assertThat(failedResult.get(0).getException()).isInstanceOf(StorageException.class);
assertThat(failedResult.get(0).getException().getMessage()).contains("Precondition Failed");
}
}

sydney-munro marked this conversation as resolved.
Show resolved Hide resolved
@Test
public void downloadBlobs() throws Exception {
TransferManagerConfig config =
Expand Down