Skip to content

Commit e9de87e

Browse files
author
Dhriti Chopra
committed
feat(storage): the session gives a fast open read to the matching readAs
Second step of fast open BiDi reads (go/fast-open-bidi-read, b/552263062). ObjectReadSessionImpl keeps the fast open reads sent with the open, keyed by the config each was created from. readAs removes the read for an equal config and returns it, so the bytes that came back with the open are used and no second request is sent; any other readAs sends a new read exactly as before. Closing the session first closes fast open reads that were never asked for. An unclaimed asChannel() read fills its small buffer and blocks the stream's thread, so it must be closed (and its buffer emptied) before the stream, or close() would wait forever. Only one fast open read is supported for now; readSession rejects more than one before anything is sent.
1 parent 19a09bd commit e9de87e

4 files changed

Lines changed: 227 additions & 8 deletions

File tree

‎java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/BaseObjectReadSessionStreamRead.java‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ abstract class BaseObjectReadSessionStreamRead<Projection>
5656
protected final RangeSpec rangeSpec;
5757
protected final RetryContext retryContext;
5858
protected final AtomicLong readOffset;
59-
protected boolean closed;
59+
protected volatile boolean closed;
6060
protected boolean tombstoned;
6161
protected IOAutoCloseable onCloseCallback;
6262

@@ -328,8 +328,10 @@ public ApiFuture<?> fail(Throwable t) {
328328
try {
329329
offer(new SmuggledFailure(t));
330330
failFuture.set(null);
331-
} catch (InterruptedIOException e) {
332-
Thread.currentThread().interrupt();
331+
} catch (IOException e) {
332+
if (e instanceof InterruptedIOException) {
333+
Thread.currentThread().interrupt();
334+
}
333335
failFuture.setException(e);
334336
}
335337
return failFuture;
@@ -433,9 +435,13 @@ public long read(ByteBuffer[] dsts, int offset, int length) throws IOException {
433435
return read;
434436
}
435437

436-
private void offer(Closeable offer) throws InterruptedIOException {
438+
private void offer(Closeable offer) throws IOException {
437439
try {
438440
queue.put(offer);
441+
if (closed) {
442+
// the reader closed while we waited, nobody will take this anymore
443+
drainQueue();
444+
}
439445
} catch (InterruptedException e) {
440446
Thread.currentThread().interrupt();
441447
InterruptedIOException ioe = new InterruptedIOException();
@@ -449,7 +455,14 @@ private void internalCleanup() throws IOException {
449455
if (leftovers != null) {
450456
leftovers.ref.close();
451457
}
452-
GrpcUtils.closeAll(queue);
458+
drainQueue();
459+
}
460+
461+
/** Remove and close everything in the queue, freeing space for a producer waiting to add. */
462+
private void drainQueue() throws IOException {
463+
List<Closeable> drained = new ArrayList<>();
464+
queue.drainTo(drained);
465+
GrpcUtils.closeAll(drained);
453466
}
454467

455468
/**

‎java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/ObjectReadSessionImpl.java‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@
3434
import java.util.Iterator;
3535
import java.util.List;
3636
import java.util.Locale;
37+
import java.util.Map;
3738
import java.util.Map.Entry;
39+
import java.util.concurrent.ConcurrentHashMap;
40+
import java.util.concurrent.ConcurrentMap;
3841
import java.util.concurrent.ExecutionException;
3942
import java.util.concurrent.ScheduledExecutorService;
4043
import java.util.concurrent.locks.ReentrantLock;
@@ -52,21 +55,29 @@ final class ObjectReadSessionImpl implements ObjectReadSession {
5255

5356
private final ConcurrentIdentityMap<ObjectReadSessionStream, ObjectReadSessionState> children;
5457

58+
// Fast open reads: reads sent in the request that opened the stream, keyed by the config each was
59+
// created from. The first readAs with an equal config is given the read instead of sending a new
60+
// one.
61+
private final ConcurrentMap<ReadProjectionConfig<?>, ObjectReadSessionStreamRead<?>>
62+
fastOpenReads;
63+
5564
private volatile boolean open;
5665

5766
ObjectReadSessionImpl(
5867
ScheduledExecutorService executor,
5968
ZeroCopyBidiStreamingCallable<BidiReadObjectRequest, BidiReadObjectResponse> callable,
6069
ObjectReadSessionStream stream,
6170
ObjectReadSessionState state,
62-
RetryContextProvider retryContextProvider) {
71+
RetryContextProvider retryContextProvider,
72+
Map<ReadProjectionConfig<?>, ObjectReadSessionStreamRead<?>> fastOpenReads) {
6373
this.executor = executor;
6474
this.callable = callable;
6575
this.stream = stream;
6676
this.state = state;
6777
this.resource = state.getMetadata();
6878
this.retryContextProvider = retryContextProvider;
6979
this.children = new ConcurrentIdentityMap<>();
80+
this.fastOpenReads = new ConcurrentHashMap<>(fastOpenReads);
7081
this.open = true;
7182
}
7283

@@ -80,6 +91,14 @@ public <Projection> Projection readAs(ReadProjectionConfig<Projection> config) {
8091
checkState(open, "Session already closed");
8192
switch (config.getType()) {
8293
case STREAM_READ:
94+
ObjectReadSessionStreamRead<?> fastOpenRead = fastOpenReads.remove(config);
95+
if (fastOpenRead != null) {
96+
// configs are final classes with a class-exact equals, so an equal config has the same
97+
// Projection type
98+
@SuppressWarnings("unchecked")
99+
Projection projection = (Projection) fastOpenRead.project();
100+
return projection;
101+
}
83102
long readId = state.newReadId();
84103
ObjectReadSessionStreamRead<Projection> read =
85104
config.cast().newRead(readId, retryContextProvider.create());
@@ -104,6 +123,10 @@ public void close() throws IOException {
104123
return;
105124
}
106125
open = false;
126+
// close fast open reads nobody asked for before the stream: an unread channel read holds the
127+
// stream's thread, and a completed read is out of the state, so stream.close() can't reach it
128+
GrpcUtils.closeAll(fastOpenReads.values());
129+
fastOpenReads.clear();
107130
List<ApiFuture<Void>> closing =
108131
children.drainEntries((subStream, subStreamState) -> subStream.closeAsync());
109132
stream.close();

‎java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageDataClient.java‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,14 @@
2727
import com.google.cloud.storage.ReadProjectionConfig.ProjectionType;
2828
import com.google.cloud.storage.RetryContext.RetryContextProvider;
2929
import com.google.common.collect.ImmutableList;
30+
import com.google.common.collect.ImmutableMap;
3031
import com.google.storage.v2.BidiReadObjectRequest;
3132
import com.google.storage.v2.BidiReadObjectResponse;
3233
import java.io.IOException;
3334
import java.time.Duration;
35+
import java.util.HashMap;
3436
import java.util.List;
37+
import java.util.Map;
3538
import java.util.concurrent.ScheduledExecutorService;
3639
import java.util.concurrent.TimeUnit;
3740

@@ -66,6 +69,9 @@ ApiFuture<ObjectReadSession> readSession(BidiReadObjectRequest req, GrpcCallCont
6669
* Open a new {@link ObjectReadSession} using fast open: the ranges in {@code fastOpenConfigs} are
6770
* sent in the same request that opens the session, saving a round trip before the first bytes.
6871
*
72+
* <p>The first {@code readAs} with a config equal to one of these is given that fast open read
73+
* instead of sending a new one.
74+
*
6975
* @param fastOpenConfigs the reads to send with the open, for example {@code
7076
* ReadProjectionConfigs.asFutureBytes().withRangeSpec(RangeSpec.of(0, 1024))}; empty to only
7177
* open the session
@@ -77,6 +83,7 @@ ApiFuture<ObjectReadSession> readSession(
7783
checkArgument(
7884
req.getReadRangesList().isEmpty(),
7985
"ranges included in the initial request are not supported");
86+
checkArgument(fastOpenConfigs.size() <= 1, "at most one fast open read is supported");
8087
ObjectReadSessionState state = new ObjectReadSessionState(ctx, req);
8188

8289
ZeroCopyBidiStreamingCallable<BidiReadObjectRequest, BidiReadObjectResponse> callable =
@@ -86,18 +93,21 @@ ApiFuture<ObjectReadSession> readSession(
8693

8794
// Add each fast open read to the session state. The open request below is built from the
8895
// state, so these reads are sent as part of the open.
96+
Map<ReadProjectionConfig<?>, ObjectReadSessionStreamRead<?>> fastOpenReads = new HashMap<>();
8997
for (ReadProjectionConfig<?> config : fastOpenConfigs) {
9098
long readId = state.newReadId();
9199
ObjectReadSessionStreamRead<?> read =
92100
config.cast().newRead(readId, retryContextProvider.create());
93101
state.putOutstandingRead(readId, read);
102+
fastOpenReads.put(config, read);
94103
}
95104

96105
ApiFuture<ObjectReadSession> objectReadSessionFuture =
97106
ApiFutures.transform(
98107
stream,
99108
nowOpen ->
100-
new ObjectReadSessionImpl(executor, callable, stream, state, retryContextProvider),
109+
new ObjectReadSessionImpl(
110+
executor, callable, stream, state, retryContextProvider, fastOpenReads),
101111
executor);
102112
stream.send(state.getOpenArguments().getReq());
103113
return objectReadSessionFuture;
@@ -132,7 +142,7 @@ <Projection> ApiFuture<FastOpenObjectReadSession<Projection>> fastOpenReadSessio
132142
nowOpen ->
133143
new FastOpenObjectReadSession<>(
134144
new ObjectReadSessionImpl(
135-
executor, callable, stream, state, retryContextProvider),
145+
executor, callable, stream, state, retryContextProvider, ImmutableMap.of()),
136146
read,
137147
stream),
138148
executor);

‎java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/ITFastOpenFakeTest.java‎

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@
1818

1919
import static com.google.cloud.storage.ByteSizeConstants._2MiB;
2020
import static com.google.cloud.storage.ITObjectReadSessionFakeTest.getReadRange;
21+
import static com.google.cloud.storage.ITObjectReadSessionFakeTest.read;
2122
import static com.google.cloud.storage.PackagePrivateMethodWorkarounds.maybeGetStorageDataClient;
23+
import static com.google.cloud.storage.TestUtils.xxd;
2224
import static com.google.common.truth.Truth.assertThat;
2325
import static java.util.concurrent.TimeUnit.SECONDS;
26+
import static org.junit.Assert.assertThrows;
2427

2528
import com.google.api.gax.grpc.GrpcCallContext;
2629
import com.google.cloud.storage.ITObjectReadSessionFakeTest.FakeStorage;
@@ -34,10 +37,14 @@
3437
import com.google.storage.v2.Object;
3538
import com.google.storage.v2.ObjectRangeData;
3639
import io.grpc.stub.StreamObserver;
40+
import java.io.IOException;
41+
import java.io.UncheckedIOException;
3742
import java.util.ArrayList;
43+
import java.util.Arrays;
3844
import java.util.Collections;
3945
import java.util.List;
4046
import java.util.Map;
47+
import java.util.concurrent.CompletableFuture;
4148
import java.util.function.Consumer;
4249
import org.junit.Test;
4350

@@ -66,6 +73,8 @@ public final class ITFastOpenFakeTest {
6673

6774
private static final ReadAsFutureBytes READ_10_TO_20 =
6875
ReadProjectionConfigs.asFutureBytes().withRangeSpec(RangeSpec.of(10, 10));
76+
private static final ReadAsFutureBytes READ_20_TO_30 =
77+
ReadProjectionConfigs.asFutureBytes().withRangeSpec(RangeSpec.of(20, 10));
6978

7079
/** Every request the fake server received, in order. */
7180
private final List<BidiReadObjectRequest> requestsReceived =
@@ -104,6 +113,161 @@ public void openRequest_withoutFastOpenReads_isUnchanged() throws Exception {
104113
assertThat(requestsReceived).containsExactly(OPEN);
105114
}
106115

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+
107271
/**
108272
* A fake server that answers each request in {@code responses} and adds it to {@link
109273
* #requestsReceived}. Any other request is answered with an error.
@@ -138,4 +302,13 @@ private static BidiReadObjectResponse metadataAndBytes(int readId, int offset, i
138302
.setRangeEnd(true))
139303
.build();
140304
}
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+
}
141314
}

0 commit comments

Comments
 (0)