Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplifying gRPC buffered read flow (#853) #859

Open
wants to merge 3 commits into
base: branch-2.2.x
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@ public class GoogleCloudStorageGrpcReadChannel implements SeekableByteChannel {
// the most recently received content and a reference to how much of it we've returned so far.
@Nullable private ByteString bufferedContent;

private int bufferedContentReadOffset;

// InputStream that backs bufferedContent. This needs to be closed when bufferedContent is no
// longer needed.
@Nullable private InputStream streamForBufferedContent;
Expand Down Expand Up @@ -365,23 +363,25 @@ private static void put(ByteString source, int offset, int size, ByteBuffer dest
private int readBufferedContentInto(ByteBuffer byteBuffer) {
// Handle skipping forward through the buffer for a seek.
long bytesToSkip = positionForNextRead - positionInGrpcStream;
long bufferSkip = min(bufferedContent.size() - bufferedContentReadOffset, bytesToSkip);
bufferSkip = max(0, bufferSkip);
bufferedContentReadOffset += bufferSkip;
positionInGrpcStream += bufferSkip;
int remainingBufferedBytes = bufferedContent.size() - bufferedContentReadOffset;

boolean remainingBufferedContentLargerThanByteBuffer =
remainingBufferedBytes > byteBuffer.remaining();
int bytesToWrite =
remainingBufferedContentLargerThanByteBuffer
? byteBuffer.remaining()
: remainingBufferedBytes;
put(bufferedContent, bufferedContentReadOffset, bytesToWrite, byteBuffer);

if (bytesToSkip >= bufferedContent.size()) {
positionInGrpcStream += bufferedContent.size();
invalidateBufferedContent();
return 0;
}

if (bytesToSkip > 0) {
positionInGrpcStream += bytesToSkip;
bufferedContent = bufferedContent.substring(Math.toIntExact(bytesToSkip));
}

int bytesToWrite = Math.min(byteBuffer.remaining(), bufferedContent.size());
put(bufferedContent, 0, bytesToWrite, byteBuffer);
positionInGrpcStream += bytesToWrite;
positionForNextRead += bytesToWrite;
if (remainingBufferedContentLargerThanByteBuffer) {
bufferedContentReadOffset += bytesToWrite;

if (bytesToWrite < bufferedContent.size()) {
bufferedContent = bufferedContent.substring(bytesToWrite);
} else {
invalidateBufferedContent();
}
Expand Down Expand Up @@ -547,8 +547,7 @@ private int readObjectContentFromGCS(ByteBuffer byteBuffer) throws IOException {
positionForNextRead += bytesToWrite;
if (responseSizeLargerThanRemainingBuffer) {
invalidateBufferedContent();
bufferedContent = content;
bufferedContentReadOffset = bytesToWrite;
bufferedContent = content.substring(bytesToWrite);
// This is to keep the stream alive for the message backed by this.
streamForBufferedContent = stream;
stream = null;
Expand Down Expand Up @@ -800,7 +799,6 @@ public String toString() {

private void invalidateBufferedContent() {
bufferedContent = null;
bufferedContentReadOffset = 0;
if (streamForBufferedContent != null) {
try {
streamForBufferedContent.close();
Expand Down