-
Notifications
You must be signed in to change notification settings - Fork 85
fix: fix appendable upload finalization race condition #3295
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
Merged
+211
−79
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
This file contains hidden or 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
This file contains hidden or 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
121 changes: 121 additions & 0 deletions
121
...c/test/java/com/google/cloud/storage/BidiAppendableUnbufferedWritableByteChannelTest.java
This file contains hidden or 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,121 @@ | ||
/* | ||
* Copyright 2025 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.common.truth.Truth.assertThat; | ||
|
||
import com.google.api.core.SettableApiFuture; | ||
import com.google.api.gax.grpc.GrpcCallContext; | ||
import com.google.cloud.storage.BidiUploadState.AppendableUploadState; | ||
import com.google.cloud.storage.ITAppendableUploadFakeTest.FakeStorage; | ||
import com.google.cloud.storage.it.ChecksummedTestContent; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.storage.v2.BidiWriteObjectResponse; | ||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
import java.time.OffsetDateTime; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.TestName; | ||
|
||
public final class BidiAppendableUnbufferedWritableByteChannelTest { | ||
@Rule public final TestName testName = new TestName(); | ||
|
||
@Test | ||
public void appendAndFinalizeOnlyPerformedIfAllBytesConsumed() throws IOException { | ||
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. The new test to verify the fix |
||
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); | ||
ChecksummedTestContent ctc = ChecksummedTestContent.gen(27); | ||
AppendableUploadState state = | ||
BidiUploadState.appendableNew( | ||
BidiUploadTest.appendRequestNew, | ||
GrpcCallContext::createDefault, | ||
16, | ||
SettableApiFuture.create(), | ||
Crc32cValue.zero()); | ||
AtomicLong finishWriteOffset = new AtomicLong(-1); | ||
BidiUploadStreamingStream stream = | ||
new BidiUploadStreamingStream( | ||
state, | ||
executor, | ||
BidiUploadTestUtils.adaptOnlySend( | ||
respond -> | ||
request -> { | ||
if (request.getFinishWrite()) { | ||
finishWriteOffset.set( | ||
request.getWriteOffset() | ||
+ request.getChecksummedData().getContent().size()); | ||
} | ||
executor.submit( | ||
() -> { | ||
switch ((int) request.getWriteOffset()) { | ||
case 0: | ||
respond.onResponse(BidiUploadTest.resourceWithSize(0)); | ||
break; | ||
case 4: | ||
case 8: | ||
// do not ack any bytes until we receive 16, this simulates | ||
// latency on the bytes being ack'd. | ||
break; | ||
case 12: | ||
respond.onResponse(BidiUploadTestUtils.incremental(8)); | ||
break; | ||
case 16: | ||
respond.onResponse(BidiUploadTestUtils.incremental(12)); | ||
break; | ||
case 20: | ||
respond.onResponse(BidiUploadTestUtils.incremental(16)); | ||
break; | ||
case 24: | ||
BidiWriteObjectResponse.Builder b = | ||
BidiUploadTest.resourceFor(ctc).toBuilder(); | ||
b.getResourceBuilder() | ||
.setFinalizeTime( | ||
Conversions.grpc() | ||
.timestampCodec | ||
.encode(OffsetDateTime.now())); | ||
respond.onResponse(b.build()); | ||
break; | ||
default: | ||
respond.onError( | ||
FakeStorage.unexpectedRequest(request, ImmutableList.of())); | ||
break; | ||
} | ||
}); | ||
}), | ||
3, | ||
RetryContext.neverRetry()); | ||
ChunkSegmenter chunkSegmenter = | ||
new ChunkSegmenter(Hasher.enabled(), ByteStringStrategy.copy(), 4, 2); | ||
BidiAppendableUnbufferedWritableByteChannel channel = | ||
new BidiAppendableUnbufferedWritableByteChannel(stream, chunkSegmenter, 4, 0); | ||
|
||
ByteBuffer buf = ctc.asByteBuffer(); | ||
int written1 = channel.write(buf); | ||
// fill up the outbound queue | ||
assertThat(written1).isEqualTo(16); | ||
|
||
// asynchronously bytes will be ack'd 4 at a time, eventually there will be enough space in the | ||
// outbound queue to allow writeAndClose to start consuming bytes. | ||
channel.nextWriteShouldFinalize(); | ||
int written2 = channel.writeAndClose(buf); | ||
assertThat(written2).isEqualTo(11); | ||
assertThat(finishWriteOffset.get()).isEqualTo(ctc.length()); | ||
} | ||
} |
This file contains hidden or 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 |
---|---|---|
|
@@ -17,6 +17,8 @@ | |
package com.google.cloud.storage; | ||
|
||
import static com.google.cloud.storage.BidiUploadState.appendableNew; | ||
import static com.google.cloud.storage.BidiUploadTestUtils.adaptOnlySend; | ||
import static com.google.cloud.storage.BidiUploadTestUtils.alwaysErrorBidiStreamingCallable; | ||
import static com.google.cloud.storage.BidiUploadTestUtils.createSegment; | ||
import static com.google.cloud.storage.BidiUploadTestUtils.finishAt; | ||
import static com.google.cloud.storage.BidiUploadTestUtils.incremental; | ||
|
@@ -39,11 +41,7 @@ | |
import com.google.api.gax.grpc.GrpcCallContext; | ||
import com.google.api.gax.grpc.GrpcStatusCode; | ||
import com.google.api.gax.rpc.AbortedException; | ||
import com.google.api.gax.rpc.ApiCallContext; | ||
import com.google.api.gax.rpc.ApiExceptionFactory; | ||
import com.google.api.gax.rpc.BidiStreamingCallable; | ||
import com.google.api.gax.rpc.ClientStream; | ||
import com.google.api.gax.rpc.ClientStreamReadyObserver; | ||
import com.google.api.gax.rpc.ErrorDetails; | ||
import com.google.api.gax.rpc.ResponseObserver; | ||
import com.google.api.gax.rpc.StreamController; | ||
|
@@ -64,7 +62,6 @@ | |
import com.google.common.collect.Range; | ||
import com.google.protobuf.Any; | ||
import com.google.protobuf.ByteString; | ||
import com.google.protobuf.Message; | ||
import com.google.protobuf.TextFormat; | ||
import com.google.rpc.Code; | ||
import com.google.storage.v2.AppendObjectSpec; | ||
|
@@ -1840,59 +1837,6 @@ public <T extends Throwable> void recordError( | |
exec1.shutdownNow(); | ||
} | ||
} | ||
|
||
private static BidiStreamingCallable<BidiWriteObjectRequest, BidiWriteObjectResponse> | ||
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. Moved into BidiUploadTestUtils.java for sharing |
||
alwaysErrorBidiStreamingCallable(Status status) { | ||
return adaptOnlySend(respond -> request -> respond.onError(status.asRuntimeException())); | ||
} | ||
|
||
private static <ReqT extends Message, ResT> BidiStreamingCallable<ReqT, ResT> adaptOnlySend( | ||
Function<ResponseObserver<ResT>, OnlySendClientStream<ReqT>> func) { | ||
return adapt(func::apply); | ||
} | ||
|
||
private static <ReqT extends Message, ResT> BidiStreamingCallable<ReqT, ResT> adapt( | ||
Function<ResponseObserver<ResT>, ClientStream<ReqT>> func) { | ||
return adapt( | ||
(respond, onReady, context) -> { | ||
ClientStream<ReqT> clientStream = func.apply(respond); | ||
StreamController controller = TestUtils.nullStreamController(); | ||
respond.onStart(controller); | ||
return clientStream; | ||
}); | ||
} | ||
|
||
/** | ||
* BidiStreamingCallable isn't functional even though it's a single abstract method. | ||
* | ||
* <p>Define a method that can adapt a TriFunc as the required implementation of {@link | ||
* BidiStreamingCallable#internalCall(ResponseObserver, ClientStreamReadyObserver, | ||
* ApiCallContext)}. | ||
* | ||
* <p>Saves several lines of boilerplate in each test. | ||
*/ | ||
private static <ReqT, ResT> BidiStreamingCallable<ReqT, ResT> adapt( | ||
StreamingStreamTest.TriFunc< | ||
ResponseObserver<ResT>, | ||
ClientStreamReadyObserver<ReqT>, | ||
ApiCallContext, | ||
ClientStream<ReqT>> | ||
func) { | ||
return new BidiStreamingCallable<ReqT, ResT>() { | ||
@Override | ||
public ClientStream<ReqT> internalCall( | ||
ResponseObserver<ResT> respond, | ||
ClientStreamReadyObserver<ReqT> onReady, | ||
ApiCallContext context) { | ||
return func.apply(respond, onReady, context); | ||
} | ||
}; | ||
} | ||
|
||
@FunctionalInterface | ||
interface TriFunc<A, B, C, R> { | ||
R apply(A a, B b, C c); | ||
} | ||
} | ||
|
||
public static final class BidiUploadStreamingStreamResponseObserverTest { | ||
|
@@ -2289,18 +2233,4 @@ static BidiWriteObjectRequest flushOffset(long offset) { | |
} | ||
return BidiWriteObjectResponse.newBuilder().setResource(f.apply(b)).build(); | ||
} | ||
|
||
@FunctionalInterface | ||
private interface OnlySendClientStream<ReqT> extends ClientStream<ReqT> { | ||
@Override | ||
default void closeSendWithError(Throwable t) {} | ||
|
||
@Override | ||
default void closeSend() {} | ||
|
||
@Override | ||
default boolean isSendReady() { | ||
return true; | ||
} | ||
} | ||
} |
This file contains hidden or 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
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.
These two lines are the actual fix