-
Notifications
You must be signed in to change notification settings - Fork 84
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
fix: update grpc finalize on close resumable uploads to validate ack'd object size #2572
Merged
+478
−110
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
4be8230
fix: update grpc finalize on close resumable uploads to validate ack'…
BenWhitehead df96b01
fix: update grpc finalize on close resumable uploads to validate ack'…
BenWhitehead 2213c1d
fix: update grpc finalize on close resumable uploads to validate ack'…
BenWhitehead 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 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 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
329 changes: 329 additions & 0 deletions
329
...oogle/cloud/storage/ITGapicUnbufferedFinalizeOnCloseResumableWritableByteChannelTest.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,329 @@ | ||
/* | ||
* Copyright 2024 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.ByteSizeConstants._256KiB; | ||
import static com.google.cloud.storage.ByteSizeConstants._512KiB; | ||
import static com.google.cloud.storage.ByteSizeConstants._768KiB; | ||
import static com.google.cloud.storage.TestUtils.assertAll; | ||
import static com.google.common.truth.Truth.assertThat; | ||
import static org.junit.Assert.assertThrows; | ||
|
||
import com.google.api.core.SettableApiFuture; | ||
import com.google.cloud.storage.ITGapicUnbufferedWritableByteChannelTest.DirectWriteService; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.google.storage.v2.Object; | ||
import com.google.storage.v2.StartResumableWriteRequest; | ||
import com.google.storage.v2.StartResumableWriteResponse; | ||
import com.google.storage.v2.StorageClient; | ||
import com.google.storage.v2.WriteObjectRequest; | ||
import com.google.storage.v2.WriteObjectResponse; | ||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
import org.checkerframework.checker.nullness.qual.NonNull; | ||
import org.junit.Test; | ||
|
||
public final class ITGapicUnbufferedFinalizeOnCloseResumableWritableByteChannelTest { | ||
|
||
private static final ChunkSegmenter CHUNK_SEGMENTER = | ||
new ChunkSegmenter(Hasher.noop(), ByteStringStrategy.copy(), _256KiB, _256KiB); | ||
|
||
@Test | ||
public void incrementalResponseForFinalizingRequest() throws Exception { | ||
String uploadId = "uploadId"; | ||
WriteObjectRequest req1 = | ||
WriteObjectRequest.newBuilder() | ||
.setUploadId(uploadId) | ||
.setWriteOffset(_512KiB) | ||
.setFinishWrite(true) | ||
.build(); | ||
WriteObjectResponse resp1 = WriteObjectResponse.newBuilder().setPersistedSize(_256KiB).build(); | ||
|
||
ImmutableMap<List<WriteObjectRequest>, WriteObjectResponse> map = | ||
ImmutableMap.of(ImmutableList.of(req1), resp1); | ||
DirectWriteService service1 = new DirectWriteService(map); | ||
|
||
try (FakeServer fakeServer = FakeServer.of(service1); | ||
GrpcStorageImpl storage = | ||
(GrpcStorageImpl) fakeServer.getGrpcStorageOptions().getService()) { | ||
StorageClient storageClient = storage.storageClient; | ||
|
||
SettableApiFuture<WriteObjectResponse> done = SettableApiFuture.create(); | ||
ResumableWrite resumableWrite = getResumableWrite(uploadId); | ||
WriteCtx<ResumableWrite> writeCtx = new WriteCtx<>(resumableWrite); | ||
writeCtx.getTotalSentBytes().set(_512KiB); | ||
writeCtx.getConfirmedBytes().set(0); | ||
|
||
//noinspection resource | ||
GapicUnbufferedFinalizeOnCloseResumableWritableByteChannel channel = | ||
new GapicUnbufferedFinalizeOnCloseResumableWritableByteChannel( | ||
done, CHUNK_SEGMENTER, storageClient.writeObjectCallable(), writeCtx); | ||
|
||
StorageException se = assertThrows(StorageException.class, channel::close); | ||
se.printStackTrace(System.out); | ||
assertAll( | ||
() -> assertThat(se.getCode()).isEqualTo(0), | ||
() -> assertThat(se.getReason()).isEqualTo("invalid"), | ||
() -> assertThat(writeCtx.getTotalSentBytes().get()).isEqualTo(_512KiB), | ||
() -> assertThat(writeCtx.getConfirmedBytes().get()).isEqualTo(0), | ||
() -> assertThat(channel.isOpen()).isFalse()); | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* | ||
* <h4>S.4</h4> | ||
* | ||
* Attempting to finalize an already finalized session | ||
* | ||
* <table> | ||
* <caption></caption> | ||
* <tr> | ||
* <td>server state</td> | ||
* <td><pre> | ||
* resource = {name = obj1, size = 262144} | ||
* </pre></td> | ||
* </tr> | ||
* <tr> | ||
* <td>client state</td> | ||
* <td><pre> | ||
* write_offset = 262144, finish = true | ||
* </pre></td> | ||
* </tr> | ||
* <tr> | ||
* <td>request</td> | ||
* <td><pre> | ||
* writeObjectRequest{ upload_id = $UPLOAD_ID, write_offset = 262144, finish_write = true} | ||
* </pre></td> | ||
* </tr> | ||
* <tr> | ||
* <td>response</td> | ||
* <td><pre> | ||
* onNext(WriteObjectResponse{ resources = {name = obj, size = 262144 } }) | ||
* </pre></td> | ||
* </tr> | ||
* </table> | ||
*/ | ||
@Test | ||
public void scenario4() throws Exception { | ||
String uploadId = "uploadId"; | ||
WriteObjectRequest req1 = | ||
WriteObjectRequest.newBuilder() | ||
.setUploadId(uploadId) | ||
.setWriteOffset(_256KiB) | ||
.setFinishWrite(true) | ||
.build(); | ||
WriteObjectResponse resp1 = | ||
WriteObjectResponse.newBuilder() | ||
.setResource(Object.newBuilder().setName("name").setSize(_256KiB).build()) | ||
.build(); | ||
|
||
ImmutableMap<List<WriteObjectRequest>, WriteObjectResponse> map = | ||
ImmutableMap.of(ImmutableList.of(req1), resp1); | ||
DirectWriteService service1 = new DirectWriteService(map); | ||
|
||
try (FakeServer fakeServer = FakeServer.of(service1); | ||
GrpcStorageImpl storage = | ||
(GrpcStorageImpl) fakeServer.getGrpcStorageOptions().getService()) { | ||
StorageClient storageClient = storage.storageClient; | ||
|
||
SettableApiFuture<WriteObjectResponse> done = SettableApiFuture.create(); | ||
ResumableWrite resumableWrite = getResumableWrite(uploadId); | ||
WriteCtx<ResumableWrite> writeCtx = new WriteCtx<>(resumableWrite); | ||
writeCtx.getTotalSentBytes().set(_256KiB); | ||
writeCtx.getConfirmedBytes().set(0); | ||
|
||
GapicUnbufferedFinalizeOnCloseResumableWritableByteChannel channel = | ||
new GapicUnbufferedFinalizeOnCloseResumableWritableByteChannel( | ||
done, CHUNK_SEGMENTER, storageClient.writeObjectCallable(), writeCtx); | ||
|
||
channel.close(); | ||
|
||
WriteObjectResponse writeObjectResponse = done.get(2, TimeUnit.SECONDS); | ||
assertThat(writeObjectResponse).isEqualTo(resp1); | ||
assertThat(writeCtx.getConfirmedBytes().get()).isEqualTo(_256KiB); | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* | ||
* <h4>S.4.1</h4> | ||
* | ||
* Attempting to finalize an already finalized session (ack < expected) | ||
* | ||
* <table> | ||
* <caption></caption> | ||
* <tr> | ||
* <td>server state</td> | ||
* <td><pre> | ||
* resource = {name = obj1, size = 262144} | ||
* </pre></td> | ||
* </tr> | ||
* <tr> | ||
* <td>client state</td> | ||
* <td><pre> | ||
* write_offset = 524288, finish = true | ||
* </pre></td> | ||
* </tr> | ||
* <tr> | ||
* <td>request</td> | ||
* <td><pre> | ||
* writeObjectRequest{ upload_id = $UPLOAD_ID, write_offset = 524288, finish_write = true} | ||
* </pre></td> | ||
* </tr> | ||
* <tr> | ||
* <td>response</td> | ||
* <td><pre> | ||
* onNext(WriteObjectResponse{ resources = {name = obj, size = 262144 } }) | ||
* </pre></td> | ||
* </tr> | ||
* </table> | ||
*/ | ||
@Test | ||
public void scenario4_1() throws Exception { | ||
String uploadId = "uploadId"; | ||
WriteObjectRequest req1 = | ||
WriteObjectRequest.newBuilder() | ||
.setUploadId(uploadId) | ||
.setWriteOffset(_512KiB) | ||
.setFinishWrite(true) | ||
.build(); | ||
WriteObjectResponse resp1 = | ||
WriteObjectResponse.newBuilder() | ||
.setResource(Object.newBuilder().setName("name").setSize(_256KiB).build()) | ||
.build(); | ||
|
||
ImmutableMap<List<WriteObjectRequest>, WriteObjectResponse> map = | ||
ImmutableMap.of(ImmutableList.of(req1), resp1); | ||
DirectWriteService service1 = new DirectWriteService(map); | ||
|
||
try (FakeServer fakeServer = FakeServer.of(service1); | ||
GrpcStorageImpl storage = | ||
(GrpcStorageImpl) fakeServer.getGrpcStorageOptions().getService()) { | ||
StorageClient storageClient = storage.storageClient; | ||
|
||
SettableApiFuture<WriteObjectResponse> done = SettableApiFuture.create(); | ||
ResumableWrite resumableWrite = getResumableWrite(uploadId); | ||
WriteCtx<ResumableWrite> writeCtx = new WriteCtx<>(resumableWrite); | ||
writeCtx.getTotalSentBytes().set(_512KiB); | ||
writeCtx.getConfirmedBytes().set(0); | ||
|
||
//noinspection resource | ||
GapicUnbufferedFinalizeOnCloseResumableWritableByteChannel channel = | ||
new GapicUnbufferedFinalizeOnCloseResumableWritableByteChannel( | ||
done, CHUNK_SEGMENTER, storageClient.writeObjectCallable(), writeCtx); | ||
|
||
StorageException se = assertThrows(StorageException.class, channel::close); | ||
se.printStackTrace(System.out); | ||
assertAll( | ||
() -> assertThat(se.getCode()).isEqualTo(0), | ||
() -> assertThat(se.getReason()).isEqualTo("dataLoss"), | ||
() -> assertThat(writeCtx.getConfirmedBytes().get()).isEqualTo(0), | ||
() -> assertThat(channel.isOpen()).isFalse()); | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* | ||
* <h4>S.4.2</h4> | ||
* | ||
* Attempting to finalize an already finalized session (ack > expected) | ||
* | ||
* <table> | ||
* <caption></caption> | ||
* <tr> | ||
* <td>server state</td> | ||
* <td><pre> | ||
* resource = {name = obj1, size = 786432} | ||
* </pre></td> | ||
* </tr> | ||
* <tr> | ||
* <td>client state</td> | ||
* <td><pre> | ||
* write_offset = 524288, finish = true | ||
* </pre></td> | ||
* </tr> | ||
* <tr> | ||
* <td>request</td> | ||
* <td><pre> | ||
* writeObjectRequest{ upload_id = $UPLOAD_ID, write_offset = 524288, finish_write = true} | ||
* </pre></td> | ||
* </tr> | ||
* <tr> | ||
* <td>response</td> | ||
* <td><pre> | ||
* onNext(WriteObjectResponse{ resources = {name = obj, size = 786432 } }) | ||
* </pre></td> | ||
* </tr> | ||
* </table> | ||
*/ | ||
@Test | ||
public void scenario4_2() throws Exception { | ||
String uploadId = "uploadId"; | ||
WriteObjectRequest req1 = | ||
WriteObjectRequest.newBuilder() | ||
.setUploadId(uploadId) | ||
.setWriteOffset(_512KiB) | ||
.setFinishWrite(true) | ||
.build(); | ||
WriteObjectResponse resp1 = | ||
WriteObjectResponse.newBuilder() | ||
.setResource(Object.newBuilder().setName("name").setSize(_768KiB).build()) | ||
.build(); | ||
|
||
ImmutableMap<List<WriteObjectRequest>, WriteObjectResponse> map = | ||
ImmutableMap.of(ImmutableList.of(req1), resp1); | ||
DirectWriteService service1 = new DirectWriteService(map); | ||
|
||
try (FakeServer fakeServer = FakeServer.of(service1); | ||
GrpcStorageImpl storage = | ||
(GrpcStorageImpl) fakeServer.getGrpcStorageOptions().getService()) { | ||
StorageClient storageClient = storage.storageClient; | ||
|
||
SettableApiFuture<WriteObjectResponse> done = SettableApiFuture.create(); | ||
ResumableWrite resumableWrite = getResumableWrite(uploadId); | ||
WriteCtx<ResumableWrite> writeCtx = new WriteCtx<>(resumableWrite); | ||
writeCtx.getTotalSentBytes().set(_512KiB); | ||
writeCtx.getConfirmedBytes().set(0); | ||
|
||
//noinspection resource | ||
GapicUnbufferedFinalizeOnCloseResumableWritableByteChannel channel = | ||
new GapicUnbufferedFinalizeOnCloseResumableWritableByteChannel( | ||
done, CHUNK_SEGMENTER, storageClient.writeObjectCallable(), writeCtx); | ||
|
||
StorageException se = assertThrows(StorageException.class, channel::close); | ||
se.printStackTrace(System.out); | ||
assertAll( | ||
() -> assertThat(se.getCode()).isEqualTo(0), | ||
() -> assertThat(se.getReason()).isEqualTo("dataLoss"), | ||
() -> assertThat(writeCtx.getConfirmedBytes().get()).isEqualTo(0), | ||
() -> assertThat(channel.isOpen()).isFalse()); | ||
} | ||
} | ||
|
||
private static @NonNull ResumableWrite getResumableWrite(String uploadId) { | ||
StartResumableWriteRequest req = StartResumableWriteRequest.getDefaultInstance(); | ||
StartResumableWriteResponse resp = | ||
StartResumableWriteResponse.newBuilder().setUploadId(uploadId).build(); | ||
return new ResumableWrite( | ||
req, resp, id -> WriteObjectRequest.newBuilder().setUploadId(id).build()); | ||
} | ||
} |
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
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.
left in commented out code
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.
Intentionally; The finalizing has to be true given the previous branches check, but I left it here for more easy readability