Skip to content

Commit

Permalink
chore: rename BufferStrategy to BufferAllocationStrategy (#2244)
Browse files Browse the repository at this point in the history
Use a more clear name to signal the configuration is for the allocation of buffers, not the buffering itself.
  • Loading branch information
BenWhitehead authored Oct 5, 2023
1 parent dc6c080 commit 338dd18
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,19 +121,19 @@ public final class ParallelCompositeUploadBlobWriteSessionConfig extends BlobWri
private static final int MAX_PARTS_PER_COMPOSE = 32;
private final int maxPartsPerCompose;
private final ExecutorSupplier executorSupplier;
private final BufferStrategy bufferStrategy;
private final BufferAllocationStrategy bufferAllocationStrategy;
private final PartNamingStrategy partNamingStrategy;
private final PartCleanupStrategy partCleanupStrategy;

private ParallelCompositeUploadBlobWriteSessionConfig(
int maxPartsPerCompose,
ExecutorSupplier executorSupplier,
BufferStrategy bufferStrategy,
BufferAllocationStrategy bufferAllocationStrategy,
PartNamingStrategy partNamingStrategy,
PartCleanupStrategy partCleanupStrategy) {
this.maxPartsPerCompose = maxPartsPerCompose;
this.executorSupplier = executorSupplier;
this.bufferStrategy = bufferStrategy;
this.bufferAllocationStrategy = bufferAllocationStrategy;
this.partNamingStrategy = partNamingStrategy;
this.partCleanupStrategy = partCleanupStrategy;
}
Expand All @@ -147,7 +147,7 @@ ParallelCompositeUploadBlobWriteSessionConfig withMaxPartsPerCompose(int maxPart
return new ParallelCompositeUploadBlobWriteSessionConfig(
maxPartsPerCompose,
executorSupplier,
bufferStrategy,
bufferAllocationStrategy,
partNamingStrategy,
partCleanupStrategy);
}
Expand All @@ -167,7 +167,7 @@ public ParallelCompositeUploadBlobWriteSessionConfig withExecutorSupplier(
return new ParallelCompositeUploadBlobWriteSessionConfig(
maxPartsPerCompose,
executorSupplier,
bufferStrategy,
bufferAllocationStrategy,
partNamingStrategy,
partCleanupStrategy);
}
Expand All @@ -176,18 +176,19 @@ public ParallelCompositeUploadBlobWriteSessionConfig withExecutorSupplier(
* Specify a specific buffering strategy which will dictate how buffers are allocated and used
* when performing a parallel composite upload.
*
* <p><i>Default: </i> {@link BufferStrategy#simple(int) BufferStrategy#simple(16MiB)}
* <p><i>Default: </i> {@link BufferAllocationStrategy#simple(int)
* BufferAllocationStrategy#simple(16MiB)}
*
* @since 2.28.0 This new api is in preview and is subject to breaking changes.
*/
@BetaApi
public ParallelCompositeUploadBlobWriteSessionConfig withBufferStrategy(
BufferStrategy bufferStrategy) {
checkNotNull(bufferStrategy, "bufferStrategy must be non null");
public ParallelCompositeUploadBlobWriteSessionConfig withBufferAllocationStrategy(
BufferAllocationStrategy bufferAllocationStrategy) {
checkNotNull(bufferAllocationStrategy, "bufferAllocationStrategy must be non null");
return new ParallelCompositeUploadBlobWriteSessionConfig(
maxPartsPerCompose,
executorSupplier,
bufferStrategy,
bufferAllocationStrategy,
partNamingStrategy,
partCleanupStrategy);
}
Expand All @@ -207,7 +208,7 @@ public ParallelCompositeUploadBlobWriteSessionConfig withPartNamingStrategy(
return new ParallelCompositeUploadBlobWriteSessionConfig(
maxPartsPerCompose,
executorSupplier,
bufferStrategy,
bufferAllocationStrategy,
partNamingStrategy,
partCleanupStrategy);
}
Expand All @@ -227,7 +228,7 @@ public ParallelCompositeUploadBlobWriteSessionConfig withPartCleanupStrategy(
return new ParallelCompositeUploadBlobWriteSessionConfig(
maxPartsPerCompose,
executorSupplier,
bufferStrategy,
bufferAllocationStrategy,
partNamingStrategy,
partCleanupStrategy);
}
Expand All @@ -237,7 +238,7 @@ static ParallelCompositeUploadBlobWriteSessionConfig withDefaults() {
return new ParallelCompositeUploadBlobWriteSessionConfig(
MAX_PARTS_PER_COMPOSE,
ExecutorSupplier.cachedPool(),
BufferStrategy.simple(ByteSizeConstants._16MiB),
BufferAllocationStrategy.simple(ByteSizeConstants._16MiB),
PartNamingStrategy.noPrefix(),
PartCleanupStrategy.always());
}
Expand All @@ -246,7 +247,7 @@ static ParallelCompositeUploadBlobWriteSessionConfig withDefaults() {
@Override
WriterFactory createFactory(Clock clock) throws IOException {
Executor executor = executorSupplier.get();
BufferHandlePool bufferHandlePool = bufferStrategy.get();
BufferHandlePool bufferHandlePool = bufferAllocationStrategy.get();
return new ParallelCompositeUploadWriterFactory(clock, executor, bufferHandlePool);
}

Expand All @@ -255,27 +256,27 @@ WriterFactory createFactory(Clock clock) throws IOException {
* will apply to all instances of {@link BlobWriteSession} created from a single instance of
* {@link Storage}.
*
* @see #withBufferStrategy(BufferStrategy)
* @see #withBufferAllocationStrategy(BufferAllocationStrategy)
* @since 2.28.0 This new api is in preview and is subject to breaking changes.
*/
@BetaApi
@Immutable
public abstract static class BufferStrategy extends Factory<BufferHandlePool>
public abstract static class BufferAllocationStrategy extends Factory<BufferHandlePool>
implements Serializable {

private BufferStrategy() {}
private BufferAllocationStrategy() {}

/**
* Create a buffer strategy which will rely upon standard garbage collection. Each buffer will
* be used once and then garbage collected.
*
* @param capacity the number of bytes each buffer should be
* @see #withBufferStrategy(BufferStrategy)
* @see #withBufferAllocationStrategy(BufferAllocationStrategy)
* @since 2.28.0 This new api is in preview and is subject to breaking changes.
*/
@BetaApi
public static BufferStrategy simple(int capacity) {
return new SimpleBufferStrategy(capacity);
public static BufferAllocationStrategy simple(int capacity) {
return new SimpleBufferAllocationStrategy(capacity);
}

/**
Expand All @@ -284,20 +285,20 @@ public static BufferStrategy simple(int capacity) {
*
* @param bufferCount the number of buffers the pool will be
* @param bufferCapacity the number of bytes each buffer should be
* @see #withBufferStrategy(BufferStrategy)
* @see #withBufferAllocationStrategy(BufferAllocationStrategy)
* @since 2.28.0 This new api is in preview and is subject to breaking changes.
*/
@BetaApi
public static BufferStrategy fixedPool(int bufferCount, int bufferCapacity) {
return new FixedBufferStrategy(bufferCount, bufferCapacity);
public static BufferAllocationStrategy fixedPool(int bufferCount, int bufferCapacity) {
return new FixedPoolBufferAllocationStrategy(bufferCount, bufferCapacity);
}

private static class SimpleBufferStrategy extends BufferStrategy {
private static class SimpleBufferAllocationStrategy extends BufferAllocationStrategy {
private static final long serialVersionUID = 8884826090481043434L;

private final int capacity;

private SimpleBufferStrategy(int capacity) {
private SimpleBufferAllocationStrategy(int capacity) {
this.capacity = capacity;
}

Expand All @@ -307,13 +308,13 @@ BufferHandlePool get() {
}
}

private static class FixedBufferStrategy extends BufferStrategy {
private static class FixedPoolBufferAllocationStrategy extends BufferAllocationStrategy {
private static final long serialVersionUID = 3288902741819257066L;

private final int bufferCount;
private final int bufferCapacity;

private FixedBufferStrategy(int bufferCount, int bufferCapacity) {
private FixedPoolBufferAllocationStrategy(int bufferCount, int bufferCapacity) {
this.bufferCount = bufferCount;
this.bufferCapacity = bufferCapacity;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import com.google.cloud.storage.BlobReadChannelV2.BlobReadChannelContext;
import com.google.cloud.storage.BlobReadChannelV2.BlobReadChannelV2State;
import com.google.cloud.storage.BlobWriteChannelV2.BlobWriteChannelV2State;
import com.google.cloud.storage.ParallelCompositeUploadBlobWriteSessionConfig.BufferStrategy;
import com.google.cloud.storage.ParallelCompositeUploadBlobWriteSessionConfig.BufferAllocationStrategy;
import com.google.cloud.storage.ParallelCompositeUploadBlobWriteSessionConfig.ExecutorSupplier;
import com.google.cloud.storage.ParallelCompositeUploadBlobWriteSessionConfig.PartCleanupStrategy;
import com.google.cloud.storage.ParallelCompositeUploadBlobWriteSessionConfig.PartNamingStrategy;
Expand Down Expand Up @@ -385,7 +385,7 @@ public void blobWriteSessionConfig_pcu() throws IOException, ClassNotFoundExcept

ParallelCompositeUploadBlobWriteSessionConfig pcu2 =
BlobWriteSessionConfigs.parallelCompositeUpload()
.withBufferStrategy(BufferStrategy.fixedPool(1, 3))
.withBufferAllocationStrategy(BufferAllocationStrategy.fixedPool(1, 3))
.withPartCleanupStrategy(PartCleanupStrategy.never())
.withPartNamingStrategy(PartNamingStrategy.prefix("prefix"))
.withExecutorSupplier(ExecutorSupplier.fixedPool(5));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import com.google.cloud.storage.BucketInfo;
import com.google.cloud.storage.DataGenerator;
import com.google.cloud.storage.GrpcStorageOptions;
import com.google.cloud.storage.ParallelCompositeUploadBlobWriteSessionConfig.BufferStrategy;
import com.google.cloud.storage.ParallelCompositeUploadBlobWriteSessionConfig.BufferAllocationStrategy;
import com.google.cloud.storage.ParallelCompositeUploadBlobWriteSessionConfig.ExecutorSupplier;
import com.google.cloud.storage.ParallelCompositeUploadBlobWriteSessionConfig.PartCleanupStrategy;
import com.google.cloud.storage.ParallelCompositeUploadBlobWriteSessionConfig.PartNamingStrategy;
Expand Down Expand Up @@ -105,8 +105,8 @@ public void setUp() throws Exception {
.setBlobWriteSessionConfig(
BlobWriteSessionConfigs.parallelCompositeUpload()
.withExecutorSupplier(ExecutorSupplier.useExecutor(exec))
// deinfe a max part size that is fairly small to aid in test speed
.withBufferStrategy(BufferStrategy.simple(_1MiB))
// define a max part size that is fairly small to aid in test speed
.withBufferAllocationStrategy(BufferAllocationStrategy.simple(_1MiB))
.withPartNamingStrategy(PartNamingStrategy.prefix("prefix-a"))
// let our fixtures take care of cleaning things up
.withPartCleanupStrategy(PartCleanupStrategy.never()))
Expand Down

0 comments on commit 338dd18

Please sign in to comment.