-
Notifications
You must be signed in to change notification settings - Fork 77
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: Add Bidi write feature #2343
Merged
Merged
Changes from 6 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
0a31458
feat: Add Bidi write feature
JesseLovelace ae58937
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 284c719
Merge branch 'main' of github.com:googleapis/java-storage into bidi
JesseLovelace 625927f
Merge branch 'bidi' of github.com:googleapis/java-storage into bidi
JesseLovelace 6290531
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 7e20b44
Merge branch 'main' into bidi
sydney-munro 9c4be0c
add in bidi ssb workload
sydney-munro 9f72af1
WIP
sydney-munro 896c224
Merge branch 'main' into bidi
sydney-munro 53f86ca
Add in bidi fields
sydney-munro 347bb0f
Add in DefaultBlobWriteSession test
sydney-munro f4053e3
fix copyright
sydney-munro b27e3c2
remove bucket creation line
sydney-munro 08d348c
cleanup object created by ssb
sydney-munro 983514a
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 778ce9e
fix up comments
JesseLovelace File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
180 changes: 180 additions & 0 deletions
180
google-cloud-storage/src/main/java/com/google/cloud/storage/BidiBlobWriteSessionConfig.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,180 @@ | ||
/* | ||
* 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.ApiFutures; | ||
import com.google.api.core.BetaApi; | ||
import com.google.api.core.InternalApi; | ||
import com.google.api.gax.grpc.GrpcCallContext; | ||
import com.google.common.base.Preconditions; | ||
import com.google.common.util.concurrent.MoreExecutors; | ||
import com.google.storage.v2.BidiWriteObjectRequest; | ||
import com.google.storage.v2.BidiWriteObjectResponse; | ||
import java.io.IOException; | ||
import java.nio.channels.WritableByteChannel; | ||
import java.time.Clock; | ||
|
||
public class BidiBlobWriteSessionConfig extends BlobWriteSessionConfig | ||
implements BlobWriteSessionConfig.GrpcCompatible { | ||
private static final long serialVersionUID = -903533790705476197L; | ||
|
||
private final int bufferSize; | ||
|
||
@InternalApi | ||
BidiBlobWriteSessionConfig(int bufferSize) { | ||
this.bufferSize = bufferSize; | ||
} | ||
|
||
/** | ||
* The number of bytes to hold in the buffer before each flush | ||
* | ||
* <p><i>Default:</i> {@code 16777216 (16 MiB)} | ||
* | ||
* @see #withBufferSize(int) | ||
* @since 2.26.0 This new api is in preview and is subject to breaking changes. | ||
*/ | ||
public int getBufferSize() { | ||
return bufferSize; | ||
} | ||
|
||
@Override | ||
WriterFactory createFactory(Clock clock) throws IOException { | ||
return new Factory(ByteSizeConstants._16MiB); | ||
} | ||
|
||
@InternalApi | ||
private static final class Factory implements WriterFactory { | ||
private static final Conversions.Decoder<BidiWriteObjectResponse, BlobInfo> | ||
WRITE_OBJECT_RESPONSE_BLOB_INFO_DECODER = | ||
Conversions.grpc().blobInfo().compose(BidiWriteObjectResponse::getResource); | ||
|
||
private final int bufferSize; | ||
|
||
private Factory(int bufferSize) { | ||
this.bufferSize = bufferSize; | ||
} | ||
|
||
@InternalApi | ||
@Override | ||
public WritableByteChannelSession<?, BlobInfo> writeSession( | ||
StorageInternal s, BlobInfo info, UnifiedOpts.Opts<UnifiedOpts.ObjectTargetOpt> opts) { | ||
if (s instanceof GrpcStorageImpl) { | ||
return new DecoratedWritableByteChannelSession<>( | ||
new LazySession<>( | ||
new LazyWriteChannel<>( | ||
() -> { | ||
GrpcStorageImpl grpc = (GrpcStorageImpl) s; | ||
GrpcCallContext grpcCallContext = | ||
opts.grpcMetadataMapper().apply(GrpcCallContext.createDefault()); | ||
BidiWriteObjectRequest req = grpc.getBidiWriteObjectRequest(info, opts); | ||
|
||
ApiFuture<BidiResumableWrite> startResumableWrite = | ||
grpc.startResumableWrite(grpcCallContext, req); | ||
return ResumableMedia.gapic() | ||
.write() | ||
.bidiByteChannel(grpc.storageClient.bidiWriteObjectCallable()) | ||
.setHasher(Hasher.noop()) | ||
.setByteStringStrategy(ByteStringStrategy.copy()) | ||
.resumable() | ||
.withRetryConfig( | ||
grpc.getOptions(), grpc.retryAlgorithmManager.idempotent()) | ||
.buffered(BufferHandle.allocate(bufferSize)) | ||
.setStartAsync(startResumableWrite) | ||
.build(); | ||
})), | ||
WRITE_OBJECT_RESPONSE_BLOB_INFO_DECODER); | ||
} else { | ||
throw new IllegalStateException( | ||
"Unknown Storage implementation: " + s.getClass().getName()); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Create a new instance with the {@code bufferSize} set to the specified value. | ||
* | ||
* <p><i>Default:</i> {@code 16777216 (16 MiB)} | ||
* | ||
* @param bufferSize The number of bytes to hold in the buffer before each flush. Must be >= | ||
* {@code 262144 (256 KiB)} | ||
* @return The new instance | ||
* @see #getBufferSize() | ||
* @since 2.26.0 This new api is in preview and is subject to breaking changes. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm assuming this version needs to be updated to whichever version is used at time of release? (same goes for other occurences). |
||
*/ | ||
@BetaApi | ||
public BidiBlobWriteSessionConfig withBufferSize(int bufferSize) { | ||
Preconditions.checkArgument( | ||
bufferSize >= ByteSizeConstants._256KiB, | ||
"bufferSize must be >= %d", | ||
ByteSizeConstants._256KiB); | ||
return new BidiBlobWriteSessionConfig(bufferSize); | ||
} | ||
|
||
private static final class DecoratedWritableByteChannelSession<WBC extends WritableByteChannel, T> | ||
implements WritableByteChannelSession<WBC, BlobInfo> { | ||
|
||
private final WritableByteChannelSession<WBC, T> delegate; | ||
private final Conversions.Decoder<T, BlobInfo> decoder; | ||
|
||
private DecoratedWritableByteChannelSession( | ||
WritableByteChannelSession<WBC, T> delegate, Conversions.Decoder<T, BlobInfo> decoder) { | ||
this.delegate = delegate; | ||
this.decoder = decoder; | ||
} | ||
|
||
@Override | ||
public WBC open() { | ||
try { | ||
return WritableByteChannelSession.super.open(); | ||
} catch (Exception e) { | ||
throw StorageException.coalesce(e); | ||
} | ||
} | ||
|
||
@Override | ||
public ApiFuture<WBC> openAsync() { | ||
return delegate.openAsync(); | ||
} | ||
|
||
@Override | ||
public ApiFuture<BlobInfo> getResult() { | ||
return ApiFutures.transform( | ||
delegate.getResult(), decoder::decode, MoreExecutors.directExecutor()); | ||
} | ||
} | ||
|
||
private static final class LazySession<R> | ||
implements WritableByteChannelSession< | ||
BufferedWritableByteChannelSession.BufferedWritableByteChannel, R> { | ||
private final LazyWriteChannel<R> lazy; | ||
|
||
private LazySession(LazyWriteChannel<R> lazy) { | ||
this.lazy = lazy; | ||
} | ||
|
||
@Override | ||
public ApiFuture<BufferedWritableByteChannelSession.BufferedWritableByteChannel> openAsync() { | ||
return lazy.getSession().openAsync(); | ||
} | ||
|
||
@Override | ||
public ApiFuture<R> getResult() { | ||
return lazy.getSession().getResult(); | ||
} | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
google-cloud-storage/src/main/java/com/google/cloud/storage/BidiResumableWrite.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,96 @@ | ||
/* | ||
* 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 static com.google.cloud.storage.StorageV2ProtoUtils.fmtProto; | ||
|
||
import com.google.cloud.storage.BidiWriteCtx.BidiWriteObjectRequestBuilderFactory; | ||
import com.google.storage.v2.BidiWriteObjectRequest; | ||
import com.google.storage.v2.StartResumableWriteRequest; | ||
import com.google.storage.v2.StartResumableWriteResponse; | ||
import java.util.Objects; | ||
import java.util.function.Function; | ||
import org.checkerframework.checker.nullness.qual.Nullable; | ||
|
||
final class BidiResumableWrite implements BidiWriteObjectRequestBuilderFactory { | ||
|
||
private final StartResumableWriteRequest req; | ||
private final StartResumableWriteResponse res; | ||
|
||
private final BidiWriteObjectRequest writeRequest; | ||
|
||
public BidiResumableWrite( | ||
StartResumableWriteRequest req, | ||
StartResumableWriteResponse res, | ||
Function<String, BidiWriteObjectRequest> f) { | ||
this.req = req; | ||
this.res = res; | ||
this.writeRequest = f.apply(res.getUploadId()); | ||
} | ||
|
||
public StartResumableWriteRequest getReq() { | ||
return req; | ||
} | ||
|
||
public StartResumableWriteResponse getRes() { | ||
return res; | ||
} | ||
|
||
@Override | ||
public BidiWriteObjectRequest.Builder newBuilder() { | ||
return writeRequest.toBuilder(); | ||
} | ||
|
||
@Override | ||
public @Nullable String bucketName() { | ||
if (req.hasWriteObjectSpec() && req.getWriteObjectSpec().hasResource()) { | ||
return req.getWriteObjectSpec().getResource().getBucket(); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "BidiResumableWrite{" + "req=" + fmtProto(req) + ", res=" + fmtProto(res) + '}'; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (!(o instanceof ResumableWrite)) { | ||
return false; | ||
} | ||
ResumableWrite resumableWrite = (ResumableWrite) o; | ||
return Objects.equals(req, resumableWrite.getReq()) | ||
&& Objects.equals(res, resumableWrite.getRes()); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(req, res); | ||
} | ||
|
||
/** | ||
* Helper function which is more specific than {@link Function#identity()}. Constraining the input | ||
* and output to be exactly {@link BidiResumableWrite}. | ||
*/ | ||
static BidiResumableWrite identity(BidiResumableWrite w) { | ||
return w; | ||
} | ||
} |
119 changes: 119 additions & 0 deletions
119
google-cloud-storage/src/main/java/com/google/cloud/storage/BidiWriteCtx.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,119 @@ | ||
/* | ||
* 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 static com.google.cloud.storage.StorageV2ProtoUtils.fmtProto; | ||
|
||
import com.google.cloud.storage.BidiWriteCtx.BidiWriteObjectRequestBuilderFactory; | ||
import com.google.cloud.storage.Crc32cValue.Crc32cLengthKnown; | ||
import com.google.storage.v2.BidiWriteObjectRequest; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import org.checkerframework.checker.nullness.qual.Nullable; | ||
|
||
final class BidiWriteCtx<RequestFactoryT extends BidiWriteObjectRequestBuilderFactory> { | ||
|
||
private final RequestFactoryT requestFactory; | ||
|
||
private final AtomicLong totalSentBytes; | ||
private final AtomicLong confirmedBytes; | ||
private final AtomicReference<Crc32cLengthKnown> cumulativeCrc32c; | ||
|
||
BidiWriteCtx(RequestFactoryT requestFactory) { | ||
this.requestFactory = requestFactory; | ||
this.totalSentBytes = new AtomicLong(0); | ||
this.confirmedBytes = new AtomicLong(0); | ||
this.cumulativeCrc32c = new AtomicReference<>(); | ||
} | ||
|
||
public RequestFactoryT getRequestFactory() { | ||
return requestFactory; | ||
} | ||
|
||
public BidiWriteObjectRequest.Builder newRequestBuilder() { | ||
return requestFactory.newBuilder(); | ||
} | ||
|
||
public AtomicLong getTotalSentBytes() { | ||
return totalSentBytes; | ||
} | ||
|
||
public AtomicLong getConfirmedBytes() { | ||
return confirmedBytes; | ||
} | ||
|
||
public AtomicReference<Crc32cLengthKnown> getCumulativeCrc32c() { | ||
return cumulativeCrc32c; | ||
} | ||
|
||
// TODO: flush this out more | ||
boolean isDirty() { | ||
return confirmedBytes.get() == totalSentBytes.get(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "ServerState{" | ||
+ "requestFactory=" | ||
+ requestFactory | ||
+ ", totalSentBytes=" | ||
+ totalSentBytes | ||
+ ", confirmedBytes=" | ||
+ confirmedBytes | ||
+ ", totalSentCrc32c=" | ||
+ cumulativeCrc32c | ||
+ '}'; | ||
} | ||
|
||
interface BidiWriteObjectRequestBuilderFactory { | ||
BidiWriteObjectRequest.Builder newBuilder(); | ||
|
||
@Nullable | ||
String bucketName(); | ||
|
||
static BidiSimpleWriteObjectRequestBuilderFactory simple(BidiWriteObjectRequest req) { | ||
return new BidiSimpleWriteObjectRequestBuilderFactory(req); | ||
} | ||
} | ||
|
||
static final class BidiSimpleWriteObjectRequestBuilderFactory | ||
implements BidiWriteObjectRequestBuilderFactory { | ||
private final BidiWriteObjectRequest req; | ||
|
||
private BidiSimpleWriteObjectRequestBuilderFactory(BidiWriteObjectRequest req) { | ||
this.req = req; | ||
} | ||
|
||
@Override | ||
public BidiWriteObjectRequest.Builder newBuilder() { | ||
return req.toBuilder(); | ||
} | ||
|
||
@Override | ||
public @Nullable String bucketName() { | ||
if (req.hasWriteObjectSpec() && req.getWriteObjectSpec().hasResource()) { | ||
return req.getWriteObjectSpec().getResource().getBucket(); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "SimpleBidiWriteObjectRequestBuilderFactory{" + "req=" + fmtProto(req) + '}'; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2024