Skip to content

Commit 49d3976

Browse files
author
Dhriti Chopra
committed
feat(storage): serve the fast open read through wrapped configs
Sixth step of fast open BiDi reads (go/fast-open-bidi-read, b/552263062). With OpenTelemetry tracing on, readAs wraps the config in OtelReadProjectionConfig, so it never equals the fast open config and the fast open read is never handed out. ReadProjectionConfig gets two package-private hooks: unwrap(), which a wrapper uses to return the config it wraps, and onFastOpenReadClaimed(), which is called with the projection when readAs is served from the fast open read. ObjectReadSessionImpl looks up the fast open read with config.unwrap(). OtelReadProjectionConfig unwraps to its delegate. On a hit it ends its readAs span when the read finishes, recording the error if the read fails; for a channel it ends the span right away.
1 parent 668abf7 commit 49d3976

4 files changed

Lines changed: 103 additions & 1 deletion

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,13 @@ public <Projection> Projection readAs(ReadProjectionConfig<Projection> config) {
9191
checkState(open, "Session already closed");
9292
switch (config.getType()) {
9393
case STREAM_READ:
94-
ObjectReadSessionStreamRead<?> fastOpenRead = fastOpenReads.remove(config);
94+
ObjectReadSessionStreamRead<?> fastOpenRead = fastOpenReads.remove(config.unwrap());
9595
if (fastOpenRead != null) {
9696
// configs are final classes with a class-exact equals, so an equal config has the same
9797
// Projection type
9898
@SuppressWarnings("unchecked")
9999
Projection projection = (Projection) fastOpenRead.project();
100+
config.onFastOpenReadClaimed(projection);
100101
return projection;
101102
}
102103
long readId = state.newReadId();

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static java.util.Objects.requireNonNull;
2020

2121
import com.google.api.core.ApiFuture;
22+
import com.google.api.core.ApiFutureCallback;
2223
import com.google.api.core.ApiFutures;
2324
import com.google.api.core.BetaApi;
2425
import com.google.api.gax.paging.Page;
@@ -1892,6 +1893,36 @@ public ProjectionType getType() {
18921893
return delegate.getType();
18931894
}
18941895

1896+
@Override
1897+
ReadProjectionConfig<Projection> unwrap() {
1898+
return delegate.unwrap();
1899+
}
1900+
1901+
@Override
1902+
void onFastOpenReadClaimed(Projection projection) {
1903+
if (!(projection instanceof ApiFuture)) {
1904+
parentSpan.end();
1905+
return;
1906+
}
1907+
// the fast open read may still be in progress or may fail, so end the span when it's done
1908+
ApiFutures.addCallback(
1909+
(ApiFuture<?>) projection,
1910+
new ApiFutureCallback<Object>() {
1911+
@Override
1912+
public void onFailure(Throwable t) {
1913+
parentSpan.recordException(t);
1914+
parentSpan.setStatus(StatusCode.ERROR, t.getClass().getSimpleName());
1915+
parentSpan.end();
1916+
}
1917+
1918+
@Override
1919+
public void onSuccess(Object result) {
1920+
parentSpan.end();
1921+
}
1922+
},
1923+
MoreExecutors.directExecutor());
1924+
}
1925+
18951926
@Override
18961927
Projection project(ObjectReadSession session, IOAutoCloseable closeAlongWith) {
18971928
try {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ public abstract class ReadProjectionConfig<Projection> {
4343

4444
abstract ProjectionType getType();
4545

46+
/** The config this one wraps, or {@code this}. Used to match a fast open read. */
47+
ReadProjectionConfig<Projection> unwrap() {
48+
return this;
49+
}
50+
51+
/** Called when {@code readAs} with this config is served from the fast open read. */
52+
void onFastOpenReadClaimed(Projection projection) {}
53+
4654
Projection project(ObjectReadSession session, IOAutoCloseable closeAlongWith) {
4755
throw new UnsupportedOperationException(
4856
String.format(Locale.US, "%s#project()", this.getClass().getName()));

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.google.api.gax.grpc.GrpcCallContext;
3232
import com.google.api.gax.rpc.OutOfRangeException;
3333
import com.google.cloud.storage.ITObjectReadSessionFakeTest.FakeStorage;
34+
import com.google.cloud.storage.ReadProjectionConfigs.BaseConfig;
3435
import com.google.cloud.storage.it.ChecksummedTestContent;
3536
import com.google.common.base.Throwables;
3637
import com.google.common.collect.ImmutableList;
@@ -198,6 +199,37 @@ OPEN_WITH_READ_OF_10_TO_20, metadataAndBytes(1, 10, 10),
198199
.inOrder();
199200
}
200201

202+
@Test
203+
public void readAs_withAWrappedFastOpenConfig_isServedFromTheFastOpenRead() throws Exception {
204+
BidiReadObjectRequest secondReadOf10To20 = read(2, 10, 10);
205+
FakeStorage server =
206+
fakeServer(
207+
ImmutableMap.of(
208+
OPEN_WITH_READ_OF_10_TO_20, metadataAndBytes(1, 10, 10),
209+
secondReadOf10To20, bytes(2, 10, 10)));
210+
WrappedConfig wrapped = new WrappedConfig(READ_10_TO_20);
211+
212+
try (FakeServer fakeServer = FakeServer.of(server);
213+
Storage storage = fakeServer.getGrpcStorageOptions().getService()) {
214+
StorageDataClient dataClient = maybeGetStorageDataClient(storage);
215+
216+
try (ObjectReadSession session =
217+
dataClient.readSession(OPEN, CTX, ImmutableList.of(READ_10_TO_20)).get(5, SECONDS)) {
218+
ApiFuture<byte[]> fastOpenFuture = session.readAs(wrapped);
219+
assertThat(wrapped.claimed).containsExactly(fastOpenFuture);
220+
byte[] fastOpen = fastOpenFuture.get(2, SECONDS);
221+
byte[] second = session.readAs(wrapped).get(2, SECONDS);
222+
assertThat(wrapped.claimed).containsExactly(fastOpenFuture);
223+
assertThat(xxd(fastOpen)).isEqualTo(xxd(slice(10, 10)));
224+
assertThat(xxd(second)).isEqualTo(xxd(slice(10, 10)));
225+
}
226+
}
227+
228+
assertThat(requestsReceived)
229+
.containsExactly(OPEN_WITH_READ_OF_10_TO_20, secondReadOf10To20)
230+
.inOrder();
231+
}
232+
201233
@Test
202234
public void close_withAnUnclaimedFastOpenRead_closesTheSession() throws Exception {
203235
FakeStorage server =
@@ -504,4 +536,34 @@ private static BidiReadObjectResponse bytes(int readId, int offset, int length)
504536
private static byte[] slice(int offset, int length) {
505537
return Arrays.copyOfRange(ALL_OBJECT_BYTES, offset, offset + length);
506538
}
539+
540+
/** Wraps a config the way tracing does, and records each fast open hit. */
541+
private static final class WrappedConfig extends ReadProjectionConfig<ApiFuture<byte[]>> {
542+
private final ReadAsFutureBytes delegate;
543+
private final List<ApiFuture<byte[]>> claimed = new ArrayList<>();
544+
545+
private WrappedConfig(ReadAsFutureBytes delegate) {
546+
this.delegate = delegate;
547+
}
548+
549+
@Override
550+
BaseConfig<ApiFuture<byte[]>, ?> cast() {
551+
return delegate.cast();
552+
}
553+
554+
@Override
555+
ProjectionType getType() {
556+
return delegate.getType();
557+
}
558+
559+
@Override
560+
ReadProjectionConfig<ApiFuture<byte[]>> unwrap() {
561+
return delegate;
562+
}
563+
564+
@Override
565+
void onFastOpenReadClaimed(ApiFuture<byte[]> projection) {
566+
claimed.add(projection);
567+
}
568+
}
507569
}

0 commit comments

Comments
 (0)