-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce new BlobWriteSession (#2123)
When writing a new Blob to GCS, there are secondary session related state and actions which can't be represented by WriteChannel. BlobWriteSession provides a new construct to allow retrieving the resultant object which is created after the WritableByteChannel is closed. Along with the new session, configuration for this is now performed at the StorageOptions level where cross session considerations can influence the implementation of the returned session. The configurable option present for this new StorageWriterConfig is chunkSize. In the future new configurations will be added with their corresponding options. For example, in a future release it will be possible to change from in memory buffering to instead buffer to disk thereby reducing heap usage.
- Loading branch information
1 parent
8cce2e0
commit e0191b5
Showing
18 changed files
with
740 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,11 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- see https://www.mojohaus.org/clirr-maven-plugin/examples/ignored-differences.html --> | ||
<differences> | ||
<!-- Not breaking, internal only interface and the new methods have default implementations --> | ||
<!-- Not breaking, new method has a default implementation --> | ||
<difference> | ||
<differenceType>7012</differenceType> | ||
<className>com/google/cloud/storage/UnbufferedWritableByteChannelSession$UnbufferedWritableByteChannel</className> | ||
<method>* write(*)</method> | ||
</difference> | ||
<!-- Allow accessing the underlying Apiary instance --> | ||
<difference> | ||
<differenceType>7012</differenceType> | ||
<className>com/google/cloud/storage/spi/v1/StorageRpc</className> | ||
<method>* getStorage()</method> | ||
</difference> | ||
|
||
<difference> | ||
<differenceType>8001</differenceType> | ||
<className>com/google/cloud/storage/Hasher$ConstantConcatValueHasher</className> | ||
</difference> | ||
|
||
<!-- Not breaking, internal only class --> | ||
<difference> | ||
<differenceType>7002</differenceType> | ||
<className>com/google/cloud/storage/HttpDownloadSessionBuilder$ReadableByteChannelSessionBuilder</className> | ||
<method>com.google.cloud.storage.HttpDownloadSessionBuilder$ReadableByteChannelSessionBuilder setCallback(java.util.function.Consumer)</method> | ||
<className>com/google/cloud/storage/Storage</className> | ||
<method>com.google.cloud.storage.BlobWriteSession blobWriteSession(com.google.cloud.storage.BlobInfo, com.google.cloud.storage.Storage$BlobWriteOption[])</method> | ||
</difference> | ||
|
||
</differences> |
73 changes: 73 additions & 0 deletions
73
google-cloud-storage/src/main/java/com/google/cloud/storage/BlobWriteSession.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright 2023 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.storage; | ||
|
||
import com.google.api.core.ApiFuture; | ||
import com.google.api.core.BetaApi; | ||
import java.io.IOException; | ||
import java.nio.channels.WritableByteChannel; | ||
|
||
/** | ||
* A session to write an object to Google Cloud Storage. | ||
* | ||
* <p>A session can only write a single version of an object. If writing multiple versions of an | ||
* object a new session must be created each time. | ||
* | ||
* <p>Provides an api that allows writing to and retrieving the resulting {@link BlobInfo} after | ||
* write finalization. | ||
* | ||
* <p>The underlying implementation is dictated based upon the specified {@link | ||
* BlobWriteSessionConfig} provided at {@link StorageOptions} creation time. | ||
* | ||
* @see GrpcStorageOptions.Builder#setBlobWriteSessionConfig(BlobWriteSessionConfig) | ||
* @see BlobWriteSessionConfig | ||
* @see BlobWriteSessionConfigs | ||
* @since 2.26.0 This new api is in preview and is subject to breaking changes. | ||
*/ | ||
@BetaApi | ||
public interface BlobWriteSession { | ||
|
||
/** | ||
* Open the {@link WritableByteChannel} for this session. | ||
* | ||
* <p>A session may only be {@code open}ed once. If multiple calls to open are made, an illegal | ||
* state exception will be thrown | ||
* | ||
* <p>Upon calling {@link WritableByteChannel#close()} the object creation will be finalized, and | ||
* {@link #getResult()}s future should resolve. | ||
* | ||
* @throws IOException When creating the {@link WritableByteChannel} if an unrecoverable | ||
* underlying IOException occurs it can be rethrown | ||
* @throws IllegalStateException if open is called more than once | ||
* @since 2.26.0 This new api is in preview and is subject to breaking changes. | ||
*/ | ||
@BetaApi | ||
WritableByteChannel open() throws IOException; | ||
|
||
/** | ||
* Return an {@link ApiFuture}{@code <BlobInfo>} which will represent the state of the object upon | ||
* finalization and success response from Google Cloud Storage. | ||
* | ||
* <p>This future will not resolve until: 1. The object is successfully finalized and created in | ||
* Google Cloud Storage 2. A terminal failure occurs, the terminal failure will become the | ||
* exception result | ||
* | ||
* @since 2.26.0 This new api is in preview and is subject to breaking changes. | ||
*/ | ||
@BetaApi | ||
ApiFuture<BlobInfo> getResult(); | ||
} |
59 changes: 59 additions & 0 deletions
59
google-cloud-storage/src/main/java/com/google/cloud/storage/BlobWriteSessionConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright 2023 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.storage; | ||
|
||
import com.google.api.core.InternalApi; | ||
import com.google.cloud.storage.Conversions.Decoder; | ||
import com.google.cloud.storage.Storage.BlobWriteOption; | ||
import com.google.cloud.storage.UnifiedOpts.ObjectTargetOpt; | ||
import com.google.cloud.storage.UnifiedOpts.Opts; | ||
import com.google.storage.v2.WriteObjectResponse; | ||
import java.io.IOException; | ||
import java.time.Clock; | ||
|
||
/** | ||
* A sealed internal implementation only class which provides the means of configuring a {@link | ||
* BlobWriteSession}. | ||
* | ||
* <p>A {@code BlobWriteSessionConfig} will be used to configure all {@link BlobWriteSession}s | ||
* produced by an instance of {@link Storage}. | ||
* | ||
* @see BlobWriteSessionConfigs | ||
* @see GrpcStorageOptions.Builder#setBlobWriteSessionConfig(BlobWriteSessionConfig) | ||
* @see Storage#blobWriteSession(BlobInfo, BlobWriteOption...) | ||
* @since 2.26.0 This new api is in preview and is subject to breaking changes. | ||
*/ | ||
// When we have java modules, actually seal this to internal extension only | ||
@InternalApi | ||
public abstract class BlobWriteSessionConfig { | ||
|
||
@InternalApi | ||
BlobWriteSessionConfig() {} | ||
|
||
@InternalApi | ||
abstract WriterFactory createFactory(Clock clock) throws IOException; | ||
|
||
@InternalApi | ||
interface WriterFactory { | ||
@InternalApi | ||
WritableByteChannelSession<?, BlobInfo> writeSession( | ||
StorageInternal s, | ||
BlobInfo info, | ||
Opts<ObjectTargetOpt> opts, | ||
Decoder<WriteObjectResponse, BlobInfo> d); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
google-cloud-storage/src/main/java/com/google/cloud/storage/BlobWriteSessionConfigs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2023 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.storage; | ||
|
||
import com.google.api.core.BetaApi; | ||
import com.google.cloud.storage.GrpcStorageOptions.GrpcStorageDefaults; | ||
import com.google.cloud.storage.Storage.BlobWriteOption; | ||
|
||
/** | ||
* Factory class to select and construct {@link BlobWriteSessionConfig}s. | ||
* | ||
* @see BlobWriteSessionConfig | ||
* @see GrpcStorageOptions.Builder#setBlobWriteSessionConfig(BlobWriteSessionConfig) | ||
* @see Storage#blobWriteSession(BlobInfo, BlobWriteOption...) | ||
* @since 2.26.0 This new api is in preview and is subject to breaking changes. | ||
*/ | ||
@BetaApi | ||
public final class BlobWriteSessionConfigs { | ||
|
||
private BlobWriteSessionConfigs() {} | ||
|
||
/** | ||
* Factory to produce the default configuration for uploading an object to Cloud Storage. | ||
* | ||
* <p>Configuration of the chunk size can be performed via {@link | ||
* DefaultBlobWriteSessionConfig#withChunkSize(int)}. | ||
* | ||
* @see GrpcStorageDefaults#getDefaultStorageWriterConfig() | ||
* @since 2.26.0 This new api is in preview and is subject to breaking changes. | ||
*/ | ||
@BetaApi | ||
public static DefaultBlobWriteSessionConfig getDefault() { | ||
return new DefaultBlobWriteSessionConfig(ByteSizeConstants._16MiB); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
google-cloud-storage/src/main/java/com/google/cloud/storage/BlobWriteSessions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright 2023 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.storage; | ||
|
||
import com.google.api.core.ApiFuture; | ||
import java.io.IOException; | ||
import java.nio.channels.WritableByteChannel; | ||
|
||
final class BlobWriteSessions { | ||
|
||
private BlobWriteSessions() {} | ||
|
||
static BlobWriteSession of(WritableByteChannelSession<?, BlobInfo> s) { | ||
return new WritableByteChannelSessionAdapter(s); | ||
} | ||
|
||
static final class WritableByteChannelSessionAdapter implements BlobWriteSession { | ||
private final WritableByteChannelSession<?, BlobInfo> delegate; | ||
|
||
private WritableByteChannelSessionAdapter(WritableByteChannelSession<?, BlobInfo> delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
@Override | ||
public WritableByteChannel open() throws IOException { | ||
return delegate.open(); | ||
} | ||
|
||
@Override | ||
public ApiFuture<BlobInfo> getResult() { | ||
return delegate.getResult(); | ||
} | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
google-cloud-storage/src/main/java/com/google/cloud/storage/CrossTransportUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright 2023 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.storage; | ||
|
||
import com.google.cloud.storage.TransportCompatibility.Transport; | ||
import java.util.Arrays; | ||
import java.util.stream.Collectors; | ||
|
||
final class CrossTransportUtils { | ||
|
||
static <T> T throwHttpJsonOnly(String methodName) { | ||
return throwHttpJsonOnly(Storage.class, methodName); | ||
} | ||
|
||
static <T> T throwHttpJsonOnly(Class<?> clazz, String methodName) { | ||
return throwTransportOnly(clazz, methodName, Transport.HTTP); | ||
} | ||
|
||
static <T> T throwGrpcOnly(String methodName) { | ||
return throwGrpcOnly(Storage.class, methodName); | ||
} | ||
|
||
static <T> T throwGrpcOnly(Class<?> clazz, String methodName) { | ||
return throwTransportOnly(clazz, methodName, Transport.GRPC); | ||
} | ||
|
||
static <T> T throwTransportOnly(Class<?> clazz, String methodName, Transport transport) { | ||
String builder; | ||
switch (transport) { | ||
case HTTP: | ||
builder = "StorageOptions.http()"; | ||
break; | ||
case GRPC: | ||
builder = "StorageOptions.grpc()"; | ||
break; | ||
default: | ||
throw new IllegalStateException( | ||
String.format("Broken Java Enum: %s received value: '%s'", Transport.class, transport)); | ||
} | ||
String message = | ||
String.format( | ||
"%s#%s is only supported for %s transport. Please use %s to construct a compatible instance.", | ||
clazz.getName(), methodName, transport, builder); | ||
throw new UnsupportedOperationException(message); | ||
} | ||
|
||
static String fmtMethodName(String name, Class<?>... args) { | ||
return name | ||
+ "(" | ||
+ Arrays.stream(args).map(Class::getName).collect(Collectors.joining(", ")) | ||
+ ")"; | ||
} | ||
} |
Oops, something went wrong.