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 {
Comment thread
nidhiii-27 marked this conversation as resolved.
Resolved
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 @@ -863,7 +863,7 @@ public byte[] readAllBytes(String bucket, String blob, BlobSourceOption... optio
@Override
public byte[] readAllBytes(BlobId blob, BlobSourceOption... options) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
readToChannel(blob, () -> Channels.newChannel(baos), options);
readToChannel(blob, () -> Channels.newChannel(baos), false, options);
return baos.toByteArray();
}

Expand Down Expand Up @@ -899,26 +899,36 @@ public GrpcBlobReadChannel reader(BlobId blob, BlobSourceOption... options) {

@Override
public void downloadTo(BlobId blob, Path path, BlobSourceOption... options) {
readToChannel(blob, () -> Files.newByteChannel(path, WRITE_OPS), options);
readToChannel(blob, () -> Files.newByteChannel(path, WRITE_OPS), true, options);
}

@Override
public void downloadTo(BlobId blob, OutputStream outputStream, BlobSourceOption... options) {
readToChannel(blob, () -> Channels.newChannel(outputStream), options);
readToChannel(blob, () -> Channels.newChannel(outputStream), true, options);
}

private void readToChannel(
BlobId blob, WritableChannelSupplier targetSupplier, BlobSourceOption... options) {
BlobId blob,
WritableChannelSupplier targetSupplier,
boolean recordPartialBytesOnError,
BlobSourceOption... options) {
StorageMetricsContext metricsContext =
StorageClientMetrics.createMultiStepContext(
getOptions().getStorageClientMetrics(), "ReadObject");
Throwable error = null;
try {
UnbufferedReadableByteChannelSession<Object> session =
unbufferedReadSession(blob, options, metricsContext);
long copied;
try (UnbufferedReadableByteChannel r = session.open();
WritableByteChannel w = targetSupplier.get()) {
ByteStreams.copy(r, w);
WritableByteChannel w =
recordPartialBytesOnError
? countBytes(targetSupplier.get(), metricsContext)
: targetSupplier.get()) {
copied = ByteStreams.copy(r, w);
}
if (!recordPartialBytesOnError && metricsContext != null) {
metricsContext.setResponseBytes(copied);
}
} catch (ApiException | IOException e) {
BaseServiceException se = StorageException.coalesce(e);
Expand Down Expand Up @@ -1782,10 +1792,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 @@ -2541,4 +2565,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