Skip to content

feat(storage): the session gives a fast open read to the matching readAs - #14832

Open
Dhriti07 wants to merge 7 commits into
fast-open/01-first-reads-in-openfrom
fast-open/02-hand-out-first-read
Open

Dhriti07 wants to merge 7 commits into
fast-open/01-first-reads-in-openfrom
fast-open/02-hand-out-first-read

Conversation

@Dhriti07

Copy link
Copy Markdown
Contributor

2/n of fast open BiDi reads.

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 also closes fast open reads that were never asked for.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request implements a fast-open read mechanism for Google Cloud Storage sessions, allowing reads to be sent alongside the initial session-opening request to save a round trip. The session caches these fast-open reads and returns them on the first matching readAs call, while ensuring any unclaimed reads are properly closed. The review feedback highlights a critical issue where duplicate configurations in the fast-open list would overwrite each other in the map, leading to resource leaks and redundant requests. To resolve this, the reviewer suggests refactoring the map to store a queue of reads per configuration, providing complete code suggestions across the session implementation and client code.

@Dhriti07
Dhriti07 force-pushed the fast-open/02-hand-out-first-read branch from 2e65e0a to b59e5e4 Compare October 29, 2026 08:43
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.
@Dhriti07
Dhriti07 force-pushed the fast-open/02-hand-out-first-read branch from b59e5e4 to e9de87e Compare November 1, 2026 12:01
@Dhriti07
Dhriti07 marked this pull request as ready for review November 9, 2026 06:47
@Dhriti07
Dhriti07 requested review from a team as code owners November 9, 2026 06:47
@Dhriti07
Dhriti07 requested a review from ShreyasSinha November 9, 2026 06:47

@nidhiii-27 nidhiii-27 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please resolve the gemini-code-assist[bot] comments as well.

Comment on lines +128 to +129
GrpcUtils.closeAll(fastOpenReads.values());
fastOpenReads.clear();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If close() races with a concurrent readAs(config) that just passed checkState(open), closing fastOpenReads.values() before removing them from the map could let readAs claim a read whose buffers were just closed. Can we remove each entry from fastOpenReads first before closing (similar to children.drainEntries below)?

List<ObjectReadSessionStreamRead<?>> unclaimed = new ArrayList<>(fastOpenReads.size());
fastOpenReads.keySet().forEach(k -> {
  ObjectReadSessionStreamRead<?> read = fastOpenReads.remove(k);
  if (read != null) {
    unclaimed.add(read);
  }
});
GrpcUtils.closeAll(unclaimed);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched to removing entries from fastOpenReads into a local list before calling GrpcUtils.closeAll.

}

@Test
public void close_withAnUnclaimedFastOpenRead_closesTheSession() throws Exception {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Optional suggestion]
READ_10_TO_20 uses asFutureBytes(), which already closes its ChildRefs inside eof(). Consider also testing an unclaimed asFutureByteString() read here, since ZeroCopyByteStringAccumulatingRead is the case that holds zero-copy buffers open after eof() removes the read from state.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

.readSession(OPEN, CTX, ImmutableList.of(ReadProjectionConfigs.asChannel()))
.get(5, SECONDS);
// let the chunks arrive and fill the unread channel read
Thread.sleep(500);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Can we use a CountDownLatch(3) in the fake server loop instead of Thread.sleep(500) so the test deterministically waits for the 2-slot queue to fill?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried CountDownLatch(3) in the fake server loop, but client.onNext returns as soon as the server buffers the frames without waiting for the client—in 10 runs readOffset on the client was still 0 (nine times) or 8 (once) when the latch opened, before the 2-slot queue filled. Keeping the sleep so the 3rd chunk actually blocks the gRPC thread, though happy to switch to polling readOffset >= 16 plus a brief pause if you prefer.

@Dhriti07

Dhriti07 commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

Replied to the bot, currently we are supporting at most one fast open config for now, marking them resolved for now. Will revisit when we support multiple fast open configs

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants