Skip to content

Commit 19a09bd

Browse files
author
Dhriti Chopra
committed
feat(storage): readSession can send fast open reads in the open request
First step of fast open BiDi reads (go/fast-open-bidi-read, b/552263062). Today a read takes two round trips: one to open the session, one to send the first range. StorageDataClient.readSession(req, ctx, fastOpenConfigs) creates a read for each config and registers it in the session state before the stream is opened. The open request is now built from the session state (ObjectReadSessionState.getOpenArguments(), already used when the stream restarts), which adds the range of every registered read, so the fast open reads are part of the open request and the bytes the server returns with the open are delivered to them. The existing two-argument method opens with no fast open reads and sends exactly the same request as before. The session does not hand these reads out yet; that is the next change, which also replaces the single-range prototype fastOpenReadSession.
1 parent 123f97c commit 19a09bd

2 files changed

Lines changed: 168 additions & 1 deletion

File tree

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@
2626
import com.google.cloud.storage.ObjectReadSessionState.OpenArguments;
2727
import com.google.cloud.storage.ReadProjectionConfig.ProjectionType;
2828
import com.google.cloud.storage.RetryContext.RetryContextProvider;
29+
import com.google.common.collect.ImmutableList;
2930
import com.google.storage.v2.BidiReadObjectRequest;
3031
import com.google.storage.v2.BidiReadObjectResponse;
3132
import java.io.IOException;
3233
import java.time.Duration;
34+
import java.util.List;
3335
import java.util.concurrent.ScheduledExecutorService;
3436
import java.util.concurrent.TimeUnit;
3537

@@ -57,6 +59,21 @@ private StorageDataClient(
5759
}
5860

5961
ApiFuture<ObjectReadSession> readSession(BidiReadObjectRequest req, GrpcCallContext ctx) {
62+
return readSession(req, ctx, ImmutableList.of());
63+
}
64+
65+
/**
66+
* Open a new {@link ObjectReadSession} using fast open: the ranges in {@code fastOpenConfigs} are
67+
* sent in the same request that opens the session, saving a round trip before the first bytes.
68+
*
69+
* @param fastOpenConfigs the reads to send with the open, for example {@code
70+
* ReadProjectionConfigs.asFutureBytes().withRangeSpec(RangeSpec.of(0, 1024))}; empty to only
71+
* open the session
72+
*/
73+
ApiFuture<ObjectReadSession> readSession(
74+
BidiReadObjectRequest req,
75+
GrpcCallContext ctx,
76+
List<ReadProjectionConfig<?>> fastOpenConfigs) {
6077
checkArgument(
6178
req.getReadRangesList().isEmpty(),
6279
"ranges included in the initial request are not supported");
@@ -67,13 +84,22 @@ ApiFuture<ObjectReadSession> readSession(BidiReadObjectRequest req, GrpcCallCont
6784
ObjectReadSessionStream stream =
6885
ObjectReadSessionStream.create(executor, callable, state, retryContextProvider.create());
6986

87+
// Add each fast open read to the session state. The open request below is built from the
88+
// state, so these reads are sent as part of the open.
89+
for (ReadProjectionConfig<?> config : fastOpenConfigs) {
90+
long readId = state.newReadId();
91+
ObjectReadSessionStreamRead<?> read =
92+
config.cast().newRead(readId, retryContextProvider.create());
93+
state.putOutstandingRead(readId, read);
94+
}
95+
7096
ApiFuture<ObjectReadSession> objectReadSessionFuture =
7197
ApiFutures.transform(
7298
stream,
7399
nowOpen ->
74100
new ObjectReadSessionImpl(executor, callable, stream, state, retryContextProvider),
75101
executor);
76-
stream.send(req);
102+
stream.send(state.getOpenArguments().getReq());
77103
return objectReadSessionFuture;
78104
}
79105

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/*
2+
* Copyright 2026 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.storage;
18+
19+
import static com.google.cloud.storage.ByteSizeConstants._2MiB;
20+
import static com.google.cloud.storage.ITObjectReadSessionFakeTest.getReadRange;
21+
import static com.google.cloud.storage.PackagePrivateMethodWorkarounds.maybeGetStorageDataClient;
22+
import static com.google.common.truth.Truth.assertThat;
23+
import static java.util.concurrent.TimeUnit.SECONDS;
24+
25+
import com.google.api.gax.grpc.GrpcCallContext;
26+
import com.google.cloud.storage.ITObjectReadSessionFakeTest.FakeStorage;
27+
import com.google.cloud.storage.it.ChecksummedTestContent;
28+
import com.google.common.collect.ImmutableList;
29+
import com.google.common.collect.ImmutableMap;
30+
import com.google.storage.v2.BidiReadObjectRequest;
31+
import com.google.storage.v2.BidiReadObjectResponse;
32+
import com.google.storage.v2.BidiReadObjectSpec;
33+
import com.google.storage.v2.BucketName;
34+
import com.google.storage.v2.Object;
35+
import com.google.storage.v2.ObjectRangeData;
36+
import io.grpc.stub.StreamObserver;
37+
import java.util.ArrayList;
38+
import java.util.Collections;
39+
import java.util.List;
40+
import java.util.Map;
41+
import java.util.function.Consumer;
42+
import org.junit.Test;
43+
44+
public final class ITFastOpenFakeTest {
45+
46+
private static final byte[] ALL_OBJECT_BYTES = DataGenerator.base64Characters().genBytes(64);
47+
private static final Object METADATA =
48+
Object.newBuilder()
49+
.setBucket(BucketName.format("_", "b"))
50+
.setName("o")
51+
.setGeneration(1)
52+
.setSize(_2MiB)
53+
.build();
54+
private static final GrpcCallContext CTX = GrpcCallContext.createDefault();
55+
56+
private static final BidiReadObjectRequest OPEN =
57+
BidiReadObjectRequest.newBuilder()
58+
.setReadObjectSpec(
59+
BidiReadObjectSpec.newBuilder()
60+
.setBucket(METADATA.getBucket())
61+
.setObject(METADATA.getName())
62+
.build())
63+
.build();
64+
private static final BidiReadObjectRequest OPEN_WITH_READ_OF_10_TO_20 =
65+
OPEN.toBuilder().addReadRanges(getReadRange(1, 10, 10)).build();
66+
67+
private static final ReadAsFutureBytes READ_10_TO_20 =
68+
ReadProjectionConfigs.asFutureBytes().withRangeSpec(RangeSpec.of(10, 10));
69+
70+
/** Every request the fake server received, in order. */
71+
private final List<BidiReadObjectRequest> requestsReceived =
72+
Collections.synchronizedList(new ArrayList<>());
73+
74+
@Test
75+
public void openRequest_includesTheFastOpenRead() throws Exception {
76+
FakeStorage server =
77+
fakeServer(ImmutableMap.of(OPEN_WITH_READ_OF_10_TO_20, metadataAndBytes(1, 10, 10)));
78+
79+
try (FakeServer fakeServer = FakeServer.of(server);
80+
Storage storage = fakeServer.getGrpcStorageOptions().getService()) {
81+
StorageDataClient dataClient = maybeGetStorageDataClient(storage);
82+
83+
ObjectReadSession session =
84+
dataClient.readSession(OPEN, CTX, ImmutableList.of(READ_10_TO_20)).get(5, SECONDS);
85+
session.close();
86+
}
87+
88+
assertThat(requestsReceived).containsExactly(OPEN_WITH_READ_OF_10_TO_20);
89+
}
90+
91+
@Test
92+
public void openRequest_withoutFastOpenReads_isUnchanged() throws Exception {
93+
FakeStorage server = fakeServer(ImmutableMap.of(OPEN, metadataOnly()));
94+
95+
try (FakeServer fakeServer = FakeServer.of(server);
96+
Storage storage = fakeServer.getGrpcStorageOptions().getService()) {
97+
StorageDataClient dataClient = maybeGetStorageDataClient(storage);
98+
99+
ObjectReadSession session =
100+
dataClient.readSession(OPEN, CTX, ImmutableList.of()).get(5, SECONDS);
101+
session.close();
102+
}
103+
104+
assertThat(requestsReceived).containsExactly(OPEN);
105+
}
106+
107+
/**
108+
* A fake server that answers each request in {@code responses} and adds it to {@link
109+
* #requestsReceived}. Any other request is answered with an error.
110+
*/
111+
private FakeStorage fakeServer(Map<BidiReadObjectRequest, BidiReadObjectResponse> responses) {
112+
ImmutableMap.Builder<BidiReadObjectRequest, Consumer<StreamObserver<BidiReadObjectResponse>>>
113+
behaviour = ImmutableMap.builder();
114+
responses.forEach(
115+
(request, response) ->
116+
behaviour.put(
117+
request,
118+
client -> {
119+
requestsReceived.add(request);
120+
client.onNext(response);
121+
}));
122+
return FakeStorage.of(behaviour.buildOrThrow());
123+
}
124+
125+
private static BidiReadObjectResponse metadataOnly() {
126+
return BidiReadObjectResponse.newBuilder().setMetadata(METADATA).build();
127+
}
128+
129+
/** The response to an open request that has a read of {@code [offset, offset + length)}. */
130+
private static BidiReadObjectResponse metadataAndBytes(int readId, int offset, int length) {
131+
return BidiReadObjectResponse.newBuilder()
132+
.setMetadata(METADATA)
133+
.addObjectDataRanges(
134+
ObjectRangeData.newBuilder()
135+
.setChecksummedData(
136+
ChecksummedTestContent.of(ALL_OBJECT_BYTES, offset, length).asChecksummedData())
137+
.setReadRange(getReadRange(readId, offset, length))
138+
.setRangeEnd(true))
139+
.build();
140+
}
141+
}

0 commit comments

Comments
 (0)