Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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 @@ -155,6 +155,9 @@ public final int read(ByteBuffer dst) throws IOException {
int read = tmp.read(dst);
if (read != -1) {
byteRangeSpec = byteRangeSpec.withShiftBeginOffset(read);
if (read > 0 && metricsContext != null) {
metricsContext.addResponseBytes(read);
}
}
return read;
} catch (StorageException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,31 @@

import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutures;
import com.google.api.gax.rpc.ApiException;
import com.google.cloud.BaseServiceException;
import com.google.cloud.storage.ZeroCopySupport.DisposableByteString;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.util.concurrent.MoreExecutors;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ScatteringByteChannel;
import java.nio.channels.SeekableByteChannel;
import org.jspecify.annotations.Nullable;

final class BlobReadSessionAdapter implements BlobReadSession {

@VisibleForTesting final ObjectReadSession session;
private final @Nullable StorageMetricsContext metricsContext;
private final @Nullable StorageClientMetrics storageClientMetrics;
private volatile @Nullable Throwable lastOperationError;

BlobReadSessionAdapter(ObjectReadSession session) {
BlobReadSessionAdapter(
ObjectReadSession session,
@Nullable StorageMetricsContext metricsContext,
@Nullable StorageClientMetrics storageClientMetrics) {
this.session = session;
this.metricsContext = metricsContext;
this.storageClientMetrics = storageClientMetrics;
}

@Override
Expand All @@ -40,23 +55,197 @@ public BlobInfo getBlobInfo() {
@SuppressWarnings({"rawtypes", "unchecked"})
@Override
public <Projection> Projection readAs(ReadProjectionConfig<Projection> config) {
Projection projection = session.readAs(config);
Projection projection;
try {
projection = session.readAs(config);
} catch (Throwable t) {
lastOperationError = unwrapIoException(t);
throw t;
}
StorageMetricsContext ctx = this.metricsContext;
if (projection instanceof ApiFuture) {
ApiFuture apiFuture = (ApiFuture) projection;
return (Projection) StorageException.coalesceAsync(apiFuture);
ApiFuture<Object> apiFuture = StorageException.coalesceAsync((ApiFuture) projection);
if (ctx != null) {
ApiFuture<Object> tracked =
ApiFutures.transform(
apiFuture,
result -> {
if (result instanceof byte[]) {
int len = ((byte[]) result).length;
if (len > 0) {
ctx.addResponseBytes(len);
}
} else if (result instanceof DisposableByteString) {
int len = ((DisposableByteString) result).byteString().size();
if (len > 0) {
ctx.addResponseBytes(len);
}
}
return result;
},
MoreExecutors.directExecutor());
apiFuture =
ApiFutures.catchingAsync(
tracked,
Throwable.class,
t -> {
if (!(t instanceof AsyncSessionClosedException)
&& !(t.getCause() instanceof AsyncSessionClosedException)) {
lastOperationError = t;
}
return ApiFutures.immediateFailedFuture(t);
},
MoreExecutors.directExecutor());
}
return (Projection) apiFuture;
}
if (ctx != null && projection instanceof SeekableByteChannel) {
return (Projection) wrapSeekableByteChannel((SeekableByteChannel) projection, ctx);
}
if (ctx != null && projection instanceof ScatteringByteChannel) {
return (Projection) wrapScatteringByteChannel((ScatteringByteChannel) projection, ctx);
}
return projection;
}

@Override
public void close() throws IOException {
session.close();
Throwable priorError = lastOperationError;
Throwable error = null;
try {
session.close();
} catch (Throwable t) {
error = unwrapIoException(t);
throw t;
} finally {
if (metricsContext != null && storageClientMetrics != null) {
metricsContext.recordOperation(storageClientMetrics, error != null ? error : priorError);
}
}
}

static ApiFuture<BlobReadSession> wrap(ApiFuture<ObjectReadSession> session) {
static ApiFuture<BlobReadSession> wrap(
ApiFuture<ObjectReadSession> session,
@Nullable StorageMetricsContext metricsContext,
@Nullable StorageClientMetrics storageClientMetrics) {
ApiFuture<ObjectReadSession> coalesced = StorageException.coalesceAsync(session);
if (metricsContext != null && storageClientMetrics != null) {
coalesced =
ApiFutures.catchingAsync(
coalesced,
Throwable.class,
t -> {
metricsContext.recordOperation(storageClientMetrics, t);
return ApiFutures.immediateFailedFuture(t);
},
MoreExecutors.directExecutor());
}
return ApiFutures.transform(
StorageException.coalesceAsync(session),
BlobReadSessionAdapter::new,
coalesced,
s -> new BlobReadSessionAdapter(s, metricsContext, storageClientMetrics),
MoreExecutors.directExecutor());
}

private SeekableByteChannel wrapSeekableByteChannel(
SeekableByteChannel delegate, StorageMetricsContext ctx) {
return new SeekableByteChannel() {
@Override
public int read(ByteBuffer dst) throws IOException {

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.

These four read overrides repeat the same try/count/capture block. Can we pull that into a small trackRead helper (with a throwing functional interface) so each override is a one-liner?

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

Co-authored by AI Agent

return (int) trackRead(ctx, () -> delegate.read(dst));
}

@Override
public int write(ByteBuffer src) throws IOException {
return delegate.write(src);
}

@Override
public long position() throws IOException {
return delegate.position();
}

@Override
public SeekableByteChannel position(long newPosition) throws IOException {
delegate.position(newPosition);
return this;
}

@Override
public long size() throws IOException {
return delegate.size();
}

@Override
public SeekableByteChannel truncate(long size) throws IOException {
delegate.truncate(size);
return this;
}

@Override
public boolean isOpen() {
return delegate.isOpen();
}

@Override
public void close() throws IOException {
delegate.close();
}
};
}

private ScatteringByteChannel wrapScatteringByteChannel(
ScatteringByteChannel delegate, StorageMetricsContext ctx) {
return new ScatteringByteChannel() {
@Override
public int read(ByteBuffer dst) throws IOException {
return (int) trackRead(ctx, () -> delegate.read(dst));
}

@Override
public long read(ByteBuffer[] dsts, int offset, int length) throws IOException {
return trackRead(ctx, () -> delegate.read(dsts, offset, length));
}

@Override
public long read(ByteBuffer[] dsts) throws IOException {
return trackRead(ctx, () -> delegate.read(dsts));
}

@Override
public boolean isOpen() {
return delegate.isOpen();
}

@Override
public void close() throws IOException {
delegate.close();
}
};
}

private long trackRead(StorageMetricsContext ctx, ReadOp op) throws IOException {
try {
long read = op.read();
if (read > 0) {
ctx.addResponseBytes(read);
}
return read;
} catch (IOException | RuntimeException | Error e) {
lastOperationError = unwrapIoException(e);
throw e;
}
}

private static Throwable unwrapIoException(Throwable t) {
if (t instanceof IOException
&& (t.getCause() instanceof BaseServiceException || t.getCause() instanceof ApiException)) {
return t.getCause();
}
return t;
}

@FunctionalInterface
private interface ReadOp {
long read() throws IOException;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import com.google.api.gax.rpc.NotFoundException;
import com.google.api.gax.rpc.UnaryCallable;
import com.google.cloud.BaseService;
import com.google.cloud.BaseServiceException;
import com.google.cloud.Policy;
import com.google.cloud.WriteChannel;
import com.google.cloud.storage.Acl.Entity;
Expand Down Expand Up @@ -872,13 +873,17 @@ public byte[] readAllBytes(BlobId blob, BlobSourceOption... options) {
WritableByteChannel w = Channels.newChannel(baos)) {
ByteStreams.copy(r, w);
} catch (ApiException | IOException e) {
error = e;
throw StorageException.coalesce(e);
BaseServiceException coalesced = StorageException.coalesce(e);
error = coalesced;
throw coalesced;
} catch (Exception e) {
error = e;
throw e;
} finally {
if (metricsContext != null) {
if (error == null) {
metricsContext.setResponseBytes(baos.size());
}
metricsContext.recordOperation(getOptions().getStorageClientMetrics(), error);
}
}
Expand Down Expand Up @@ -925,11 +930,12 @@ public void downloadTo(BlobId blob, Path path, BlobSourceOption... options) {

Throwable error = null;
try (UnbufferedReadableByteChannel r = session.open();
WritableByteChannel w = Files.newByteChannel(path, WRITE_OPS)) {
WritableByteChannel w = countBytes(Files.newByteChannel(path, WRITE_OPS), metricsContext)) {
ByteStreams.copy(r, w);
} catch (ApiException | IOException e) {
error = e;
throw StorageException.coalesce(e);
BaseServiceException coalesced = StorageException.coalesce(e);
error = coalesced;
throw coalesced;
} catch (Exception e) {
error = e;
throw e;
Expand All @@ -950,11 +956,12 @@ public void downloadTo(BlobId blob, OutputStream outputStream, BlobSourceOption.

Throwable error = null;
try (UnbufferedReadableByteChannel r = session.open();
WritableByteChannel w = Channels.newChannel(outputStream)) {
WritableByteChannel w = countBytes(Channels.newChannel(outputStream), metricsContext)) {
ByteStreams.copy(r, w);
} catch (ApiException | IOException e) {
error = e;
throw StorageException.coalesce(e);
BaseServiceException coalesced = StorageException.coalesce(e);
error = coalesced;
throw coalesced;
} catch (Exception e) {
error = e;
throw e;
Expand Down Expand Up @@ -1810,10 +1817,24 @@ public ApiFuture<BlobReadSession> blobReadSession(BlobId id, BlobSourceOption...
opts.bidiReadObjectRequest().apply(b);
BidiReadObjectRequest req = b.build();

GrpcCallContext context = opts.grpcMetadataMapper().apply(GrpcCallContext.createDefault());
ApiFuture<ObjectReadSession> session = storageDataClient.readSession(req, context);

return BlobReadSessionAdapter.wrap(session);
StorageMetricsContext metricsContext =
StorageClientMetrics.createMultiStepContext(
getOptions().getStorageClientMetrics(), "ReadObject");
GrpcCallContext baseContext =
metricsContext != null
? metricsContext.attach(GrpcCallContext.createDefault())
: GrpcCallContext.createDefault();
GrpcCallContext context = opts.grpcMetadataMapper().apply(baseContext);
try {
ApiFuture<ObjectReadSession> session = storageDataClient.readSession(req, context);
return BlobReadSessionAdapter.wrap(
session, metricsContext, getOptions().getStorageClientMetrics());
} catch (Throwable t) {
if (metricsContext != null) {
metricsContext.recordOperation(getOptions().getStorageClientMetrics(), t);
}
throw t;
}
}

@Override
Expand Down Expand Up @@ -2543,4 +2564,31 @@ private ZeroCopyServerStreamingCallable<ReadObjectRequest, ReadObjectResponse> r
storageClient.readObjectCallable().withDefaultCallContext(grpcCallContext),
responseContentLifecycleManager);
}

private static WritableByteChannel countBytes(
WritableByteChannel delegate, @Nullable StorageMetricsContext metricsContext) {
if (metricsContext == null) {
return delegate;
}
return new WritableByteChannel() {
@Override
public int write(ByteBuffer src) throws IOException {
int written = delegate.write(src);
if (written > 0) {
metricsContext.addResponseBytes(written);
}
return written;
}

@Override
public boolean isOpen() {
return delegate.isOpen();
}

@Override
public void close() throws IOException {
delegate.close();
}
};
}
}
Loading
Loading