|
18 | 18 |
|
19 | 19 | import static com.google.cloud.storage.ByteSizeConstants._2MiB; |
20 | 20 | import static com.google.cloud.storage.ITObjectReadSessionFakeTest.getReadRange; |
| 21 | +import static com.google.cloud.storage.ITObjectReadSessionFakeTest.read; |
21 | 22 | import static com.google.cloud.storage.PackagePrivateMethodWorkarounds.maybeGetStorageDataClient; |
| 23 | +import static com.google.cloud.storage.TestUtils.xxd; |
22 | 24 | import static com.google.common.truth.Truth.assertThat; |
23 | 25 | import static java.util.concurrent.TimeUnit.SECONDS; |
| 26 | +import static org.junit.Assert.assertThrows; |
24 | 27 |
|
25 | 28 | import com.google.api.gax.grpc.GrpcCallContext; |
26 | 29 | import com.google.cloud.storage.ITObjectReadSessionFakeTest.FakeStorage; |
|
34 | 37 | import com.google.storage.v2.Object; |
35 | 38 | import com.google.storage.v2.ObjectRangeData; |
36 | 39 | import io.grpc.stub.StreamObserver; |
| 40 | +import java.io.IOException; |
| 41 | +import java.io.UncheckedIOException; |
37 | 42 | import java.util.ArrayList; |
| 43 | +import java.util.Arrays; |
38 | 44 | import java.util.Collections; |
39 | 45 | import java.util.List; |
40 | 46 | import java.util.Map; |
| 47 | +import java.util.concurrent.CompletableFuture; |
41 | 48 | import java.util.function.Consumer; |
42 | 49 | import org.junit.Test; |
43 | 50 |
|
@@ -66,6 +73,8 @@ public final class ITFastOpenFakeTest { |
66 | 73 |
|
67 | 74 | private static final ReadAsFutureBytes READ_10_TO_20 = |
68 | 75 | ReadProjectionConfigs.asFutureBytes().withRangeSpec(RangeSpec.of(10, 10)); |
| 76 | + private static final ReadAsFutureBytes READ_20_TO_30 = |
| 77 | + ReadProjectionConfigs.asFutureBytes().withRangeSpec(RangeSpec.of(20, 10)); |
69 | 78 |
|
70 | 79 | /** Every request the fake server received, in order. */ |
71 | 80 | private final List<BidiReadObjectRequest> requestsReceived = |
@@ -104,6 +113,161 @@ public void openRequest_withoutFastOpenReads_isUnchanged() throws Exception { |
104 | 113 | assertThat(requestsReceived).containsExactly(OPEN); |
105 | 114 | } |
106 | 115 |
|
| 116 | + @Test |
| 117 | + public void readAs_withTheFastOpenConfig_doesNotSendAnotherRequest() throws Exception { |
| 118 | + FakeStorage server = |
| 119 | + fakeServer(ImmutableMap.of(OPEN_WITH_READ_OF_10_TO_20, metadataAndBytes(1, 10, 10))); |
| 120 | + |
| 121 | + try (FakeServer fakeServer = FakeServer.of(server); |
| 122 | + Storage storage = fakeServer.getGrpcStorageOptions().getService()) { |
| 123 | + StorageDataClient dataClient = maybeGetStorageDataClient(storage); |
| 124 | + |
| 125 | + try (ObjectReadSession session = |
| 126 | + dataClient.readSession(OPEN, CTX, ImmutableList.of(READ_10_TO_20)).get(5, SECONDS)) { |
| 127 | + byte[] actual = session.readAs(READ_10_TO_20).get(2, SECONDS); |
| 128 | + assertThat(xxd(actual)).isEqualTo(xxd(slice(10, 10))); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + assertThat(requestsReceived).containsExactly(OPEN_WITH_READ_OF_10_TO_20); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + public void readAs_withTheFastOpenConfigAgain_sendsANewRead() throws Exception { |
| 137 | + BidiReadObjectRequest secondReadOf10To20 = read(2, 10, 10); |
| 138 | + FakeStorage server = |
| 139 | + fakeServer( |
| 140 | + ImmutableMap.of( |
| 141 | + OPEN_WITH_READ_OF_10_TO_20, metadataAndBytes(1, 10, 10), |
| 142 | + secondReadOf10To20, bytes(2, 10, 10))); |
| 143 | + |
| 144 | + try (FakeServer fakeServer = FakeServer.of(server); |
| 145 | + Storage storage = fakeServer.getGrpcStorageOptions().getService()) { |
| 146 | + StorageDataClient dataClient = maybeGetStorageDataClient(storage); |
| 147 | + |
| 148 | + try (ObjectReadSession session = |
| 149 | + dataClient.readSession(OPEN, CTX, ImmutableList.of(READ_10_TO_20)).get(5, SECONDS)) { |
| 150 | + byte[] fastOpen = session.readAs(READ_10_TO_20).get(2, SECONDS); |
| 151 | + byte[] second = session.readAs(READ_10_TO_20).get(2, SECONDS); |
| 152 | + assertThat(xxd(fastOpen)).isEqualTo(xxd(slice(10, 10))); |
| 153 | + assertThat(xxd(second)).isEqualTo(xxd(slice(10, 10))); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + assertThat(requestsReceived) |
| 158 | + .containsExactly(OPEN_WITH_READ_OF_10_TO_20, secondReadOf10To20) |
| 159 | + .inOrder(); |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + public void readAs_withADifferentConfig_sendsANewRead_andKeepsTheFastOpenRead() throws Exception { |
| 164 | + BidiReadObjectRequest readOf20To30 = read(2, 20, 10); |
| 165 | + FakeStorage server = |
| 166 | + fakeServer( |
| 167 | + ImmutableMap.of( |
| 168 | + OPEN_WITH_READ_OF_10_TO_20, metadataAndBytes(1, 10, 10), |
| 169 | + readOf20To30, bytes(2, 20, 10))); |
| 170 | + |
| 171 | + try (FakeServer fakeServer = FakeServer.of(server); |
| 172 | + Storage storage = fakeServer.getGrpcStorageOptions().getService()) { |
| 173 | + StorageDataClient dataClient = maybeGetStorageDataClient(storage); |
| 174 | + |
| 175 | + try (ObjectReadSession session = |
| 176 | + dataClient.readSession(OPEN, CTX, ImmutableList.of(READ_10_TO_20)).get(5, SECONDS)) { |
| 177 | + byte[] other = session.readAs(READ_20_TO_30).get(2, SECONDS); |
| 178 | + byte[] fastOpen = session.readAs(READ_10_TO_20).get(2, SECONDS); |
| 179 | + assertThat(xxd(other)).isEqualTo(xxd(slice(20, 10))); |
| 180 | + assertThat(xxd(fastOpen)).isEqualTo(xxd(slice(10, 10))); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + assertThat(requestsReceived) |
| 185 | + .containsExactly(OPEN_WITH_READ_OF_10_TO_20, readOf20To30) |
| 186 | + .inOrder(); |
| 187 | + } |
| 188 | + |
| 189 | + @Test |
| 190 | + public void close_withAnUnclaimedFastOpenRead_closesTheSession() throws Exception { |
| 191 | + FakeStorage server = |
| 192 | + fakeServer(ImmutableMap.of(OPEN_WITH_READ_OF_10_TO_20, metadataAndBytes(1, 10, 10))); |
| 193 | + |
| 194 | + try (FakeServer fakeServer = FakeServer.of(server); |
| 195 | + Storage storage = fakeServer.getGrpcStorageOptions().getService()) { |
| 196 | + StorageDataClient dataClient = maybeGetStorageDataClient(storage); |
| 197 | + |
| 198 | + ObjectReadSession session = |
| 199 | + dataClient.readSession(OPEN, CTX, ImmutableList.of(READ_10_TO_20)).get(5, SECONDS); |
| 200 | + session.close(); |
| 201 | + |
| 202 | + assertThrows(IllegalStateException.class, () -> session.readAs(READ_10_TO_20)); |
| 203 | + } |
| 204 | + |
| 205 | + assertThat(requestsReceived).containsExactly(OPEN_WITH_READ_OF_10_TO_20); |
| 206 | + } |
| 207 | + |
| 208 | + @Test |
| 209 | + public void close_withAnUnclaimedChannelFastOpenRead_doesNotHang() throws Exception { |
| 210 | + BidiReadObjectRequest openWithWholeObjectRead = |
| 211 | + OPEN.toBuilder().addReadRanges(getReadRange(1, 0, 0)).build(); |
| 212 | + FakeStorage server = |
| 213 | + FakeStorage.of( |
| 214 | + ImmutableMap.of( |
| 215 | + openWithWholeObjectRead, |
| 216 | + client -> { |
| 217 | + client.onNext(metadataOnly()); |
| 218 | + // more chunks than a channel read keeps while nobody reads it |
| 219 | + for (int offset = 0; offset < 64; offset += 8) { |
| 220 | + client.onNext( |
| 221 | + BidiReadObjectResponse.newBuilder() |
| 222 | + .addObjectDataRanges( |
| 223 | + ObjectRangeData.newBuilder() |
| 224 | + .setChecksummedData( |
| 225 | + ChecksummedTestContent.of(ALL_OBJECT_BYTES, offset, 8) |
| 226 | + .asChecksummedData()) |
| 227 | + .setReadRange(getReadRange(1, offset, 8))) |
| 228 | + .build()); |
| 229 | + } |
| 230 | + })); |
| 231 | + |
| 232 | + try (FakeServer fakeServer = FakeServer.of(server); |
| 233 | + Storage storage = fakeServer.getGrpcStorageOptions().getService()) { |
| 234 | + StorageDataClient dataClient = maybeGetStorageDataClient(storage); |
| 235 | + ObjectReadSession session = |
| 236 | + dataClient |
| 237 | + .readSession(OPEN, CTX, ImmutableList.of(ReadProjectionConfigs.asChannel())) |
| 238 | + .get(5, SECONDS); |
| 239 | + // let the chunks arrive and fill the unread channel read |
| 240 | + Thread.sleep(500); |
| 241 | + |
| 242 | + CompletableFuture<Void> close = |
| 243 | + CompletableFuture.runAsync( |
| 244 | + () -> { |
| 245 | + try { |
| 246 | + session.close(); |
| 247 | + } catch (IOException e) { |
| 248 | + throw new UncheckedIOException(e); |
| 249 | + } |
| 250 | + }); |
| 251 | + close.get(10, SECONDS); |
| 252 | + } |
| 253 | + } |
| 254 | + |
| 255 | + @Test |
| 256 | + public void readSession_withMoreThanOneFastOpenConfig_isRejected() throws Exception { |
| 257 | + FakeStorage server = fakeServer(ImmutableMap.of()); |
| 258 | + |
| 259 | + try (FakeServer fakeServer = FakeServer.of(server); |
| 260 | + Storage storage = fakeServer.getGrpcStorageOptions().getService()) { |
| 261 | + StorageDataClient dataClient = maybeGetStorageDataClient(storage); |
| 262 | + |
| 263 | + assertThrows( |
| 264 | + IllegalArgumentException.class, |
| 265 | + () -> dataClient.readSession(OPEN, CTX, ImmutableList.of(READ_10_TO_20, READ_20_TO_30))); |
| 266 | + } |
| 267 | + |
| 268 | + assertThat(requestsReceived).isEmpty(); |
| 269 | + } |
| 270 | + |
107 | 271 | /** |
108 | 272 | * A fake server that answers each request in {@code responses} and adds it to {@link |
109 | 273 | * #requestsReceived}. Any other request is answered with an error. |
@@ -138,4 +302,13 @@ private static BidiReadObjectResponse metadataAndBytes(int readId, int offset, i |
138 | 302 | .setRangeEnd(true)) |
139 | 303 | .build(); |
140 | 304 | } |
| 305 | + |
| 306 | + /** The response to a read request of {@code [offset, offset + length)}. */ |
| 307 | + private static BidiReadObjectResponse bytes(int readId, int offset, int length) { |
| 308 | + return metadataAndBytes(readId, offset, length).toBuilder().clearMetadata().build(); |
| 309 | + } |
| 310 | + |
| 311 | + private static byte[] slice(int offset, int length) { |
| 312 | + return Arrays.copyOfRange(ALL_OBJECT_BYTES, offset, offset + length); |
| 313 | + } |
141 | 314 | } |
0 commit comments