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

feat: Plumb PartNamingStrategy for Parallel Composite Uploads in Transfer Manager #2547

Merged
merged 3 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -16,6 +16,7 @@

package com.google.cloud.storage.transfermanager;

import com.google.cloud.storage.ParallelCompositeUploadBlobWriteSessionConfig.PartNamingStrategy;
import com.google.cloud.storage.StorageOptions;
import com.google.common.base.MoreObjects;
import java.util.Objects;
Expand All @@ -31,18 +32,22 @@ public final class TransferManagerConfig {
private final boolean allowDivideAndConquerDownload;
private final boolean allowParallelCompositeUpload;

private final PartNamingStrategy partNamingStrategy;

private final StorageOptions storageOptions;

TransferManagerConfig(
int maxWorkers,
int perWorkerBufferSize,
boolean allowDivideAndConquerDownload,
boolean allowParallelCompositeUpload,
PartNamingStrategy partNamingStrategy,
StorageOptions storageOptions) {
this.maxWorkers = maxWorkers;
this.perWorkerBufferSize = perWorkerBufferSize;
this.allowDivideAndConquerDownload = allowDivideAndConquerDownload;
this.allowParallelCompositeUpload = allowParallelCompositeUpload;
this.partNamingStrategy = partNamingStrategy;
this.storageOptions = storageOptions;
}

Expand Down Expand Up @@ -101,6 +106,10 @@ public StorageOptions getStorageOptions() {
return storageOptions;
}

public PartNamingStrategy getPartNamingStrategy() {
BenWhitehead marked this conversation as resolved.
Show resolved Hide resolved
return partNamingStrategy;
}

/** The service object for {@link TransferManager} */
public TransferManager getService() {
return new TransferManagerImpl(this, DefaultQos.of(this));
Expand Down Expand Up @@ -169,13 +178,15 @@ public static class Builder {
private boolean allowParallelCompositeUpload;

private StorageOptions storageOptions;
private PartNamingStrategy partNamingStrategy;

private Builder() {
this.perWorkerBufferSize = 16 * 1024 * 1024;
this.maxWorkers = 2 * Runtime.getRuntime().availableProcessors();
this.allowDivideAndConquerDownload = false;
this.allowParallelCompositeUpload = false;
this.storageOptions = StorageOptions.getDefaultInstance();
this.partNamingStrategy = PartNamingStrategy.noPrefix();
}

/**
Expand Down Expand Up @@ -246,6 +257,12 @@ public Builder setStorageOptions(StorageOptions storageOptions) {
return this;
}

public Builder setParallelCompositeUploadPartNamingStrategy(
BenWhitehead marked this conversation as resolved.
Show resolved Hide resolved
PartNamingStrategy partNamingStrategy) {
this.partNamingStrategy = partNamingStrategy;
BenWhitehead marked this conversation as resolved.
Show resolved Hide resolved
return this;
}

/**
* Creates a TransferManagerConfig object.
*
Expand All @@ -257,6 +274,7 @@ public TransferManagerConfig build() {
perWorkerBufferSize,
allowDivideAndConquerDownload,
allowParallelCompositeUpload,
partNamingStrategy,
storageOptions);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ final class TransferManagerImpl implements TransferManager {
.withBufferAllocationStrategy(
BufferAllocationStrategy.fixedPool(
transferManagerConfig.getMaxWorkers(),
transferManagerConfig.getPerWorkerBufferSize()));
transferManagerConfig.getPerWorkerBufferSize()))
.withPartNamingStrategy(transferManagerConfig.getPartNamingStrategy());
storageOptions = storageOptions.toBuilder().setBlobWriteSessionConfig(pcuConfig).build();
}
this.pcuQueue = new ConcurrentLinkedDeque<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.BucketInfo;
import com.google.cloud.storage.DataGenerator;
import com.google.cloud.storage.ParallelCompositeUploadBlobWriteSessionConfig.PartNamingStrategy;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.Storage.BlobSourceOption;
import com.google.cloud.storage.Storage.BlobWriteOption;
Expand Down Expand Up @@ -141,6 +142,28 @@ public void uploadFiles() throws Exception {
}
}

@Test
public void uploadFilesPartNaming() throws Exception {
TransferManagerConfig config =
TransferManagerConfigTestingInstances.defaults(storage.getOptions())
.toBuilder()
.setAllowParallelCompositeUpload(true)
.setPerWorkerBufferSize(128 * 1024)
.setParallelCompositeUploadPartNamingStrategy(PartNamingStrategy.prefix("not-root"))
.build();
long size = CHUNK_THRESHOLD + 100L;
try (TransferManager transferManager = config.getService();
TmpFile tmpFile = DataGenerator.base64Characters().tempFile(baseDir, size)) {
ParallelUploadConfig parallelUploadConfig =
ParallelUploadConfig.newBuilder().setBucketName(bucket.getName()).build();
UploadJob job =
transferManager.uploadFiles(
Collections.singletonList(tmpFile.getPath()), parallelUploadConfig);
List<UploadResult> uploadResults = job.getUploadResults();
assertThat(uploadResults.get(0).getStatus()).isEqualTo(TransferStatus.SUCCESS);
}
}

@Test
public void uploadFilesWithOpts() throws Exception {
TransferManagerConfig config =
Expand Down
Loading