diff --git a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/BaseStorageReadChannel.java b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/BaseStorageReadChannel.java index 09965205cf5e..a670f57866b4 100644 --- a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/BaseStorageReadChannel.java +++ b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/BaseStorageReadChannel.java @@ -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) { diff --git a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobReadSessionAdapter.java b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobReadSessionAdapter.java index 1823a13283a1..6ec40e08ea6c 100644 --- a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobReadSessionAdapter.java +++ b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobReadSessionAdapter.java @@ -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 @@ -40,23 +55,197 @@ public BlobInfo getBlobInfo() { @SuppressWarnings({"rawtypes", "unchecked"}) @Override public Projection readAs(ReadProjectionConfig 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 apiFuture = StorageException.coalesceAsync((ApiFuture) projection); + if (ctx != null) { + ApiFuture 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 wrap(ApiFuture session) { + static ApiFuture wrap( + ApiFuture session, + @Nullable StorageMetricsContext metricsContext, + @Nullable StorageClientMetrics storageClientMetrics) { + ApiFuture 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 { + 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; + } } diff --git a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/GrpcStorageImpl.java b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/GrpcStorageImpl.java index d8ae74b4331f..f59bc5cf54db 100644 --- a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/GrpcStorageImpl.java +++ b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/GrpcStorageImpl.java @@ -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; @@ -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); } } @@ -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; @@ -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; @@ -1810,10 +1817,24 @@ public ApiFuture blobReadSession(BlobId id, BlobSourceOption... opts.bidiReadObjectRequest().apply(b); BidiReadObjectRequest req = b.build(); - GrpcCallContext context = opts.grpcMetadataMapper().apply(GrpcCallContext.createDefault()); - ApiFuture 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 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 @@ -2543,4 +2564,31 @@ private ZeroCopyServerStreamingCallable 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(); + } + }; + } } diff --git a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java index 34c4d83ac8ff..095d1cbf9f1f 100644 --- a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java +++ b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java @@ -757,11 +757,19 @@ public byte[] readAllBytes(BlobId blob, BlobSourceOption... options) { ImmutableMap optionsMap = resolve.getRpcOptions(); ResultRetryAlgorithm algorithm = retryAlgorithmManager.getForObjectsGet(storageObject, optionsMap); - return run( - "ReadObject", + StorageMetricsContext metricsContext = + StorageClientMetrics.createContext(getOptions().getStorageClientMetrics(), "ReadObject"); + return retrier.run( algorithm, - () -> storageRpc.load(storageObject, optionsMap), - Decoder.identity()); + () -> { + byte[] bytes = storageRpc.load(storageObject, optionsMap); + if (metricsContext != null && bytes != null) { + metricsContext.setResponseBytes(bytes.length); + } + return bytes; + }, + Decoder.identity(), + metricsContext); } @Override @@ -802,15 +810,23 @@ public void downloadTo(BlobId blob, OutputStream outputStream, BlobSourceOption. ImmutableMap optionsMap = Opts.unwrap(options).resolveFrom(blob).getRpcOptions(); ResultRetryAlgorithm algorithm = retryAlgorithmManager.getForObjectsGet(pb, optionsMap); - run( - "ReadObject", + StorageMetricsContext metricsContext = + StorageClientMetrics.createContext(getOptions().getStorageClientMetrics(), "ReadObject"); + retrier.run( algorithm, callable( () -> { - storageRpc.read( - pb, optionsMap, countingOutputStream.getCount(), countingOutputStream); + try { + storageRpc.read( + pb, optionsMap, countingOutputStream.getCount(), countingOutputStream); + } finally { + if (metricsContext != null) { + metricsContext.setResponseBytes(countingOutputStream.getCount()); + } + } }), - Decoder.identity()); + Decoder.identity(), + metricsContext); } @Override diff --git a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageMetricsContext.java b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageMetricsContext.java index 92dc47972967..188195d69eb5 100644 --- a/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageMetricsContext.java +++ b/java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageMetricsContext.java @@ -221,6 +221,7 @@ void recordOperation( } recordRequestBodySize(metrics, bodySizeAttributes); + recordResponseBodySize(metrics, bodySizeAttributes); } } @@ -234,6 +235,16 @@ private void recordRequestBodySize(StorageClientMetrics metrics, Attributes body } } + private void recordResponseBodySize(StorageClientMetrics metrics, Attributes bodySizeAttributes) { + if (metrics.getResponseBodySize() == null) { + return; + } + long respBytes = responseBytes.get(); + if ("ReadObject".equals(operationName) && respBytes >= 0) { + metrics.getResponseBodySize().record(respBytes, bodySizeAttributes, otelContext); + } + } + void recordAttemptSuccess() { recordAttemptSuccess(this.metrics); } @@ -407,6 +418,10 @@ long addResponseBytes(long bytes) { return responseBytes.addAndGet(bytes); } + void setResponseBytes(long bytes) { + responseBytes.set(bytes); + } + long getResponseBytes() { return responseBytes.get(); } diff --git a/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageMetricsContextTest.java b/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageMetricsContextTest.java index f510eb23bc47..dde1e3397412 100644 --- a/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageMetricsContextTest.java +++ b/java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageMetricsContextTest.java @@ -19,6 +19,9 @@ import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertThrows; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.ArgumentMatchers.anyMap; +import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -35,22 +38,36 @@ import com.google.api.core.ApiFutures; import com.google.api.gax.grpc.GrpcCallContext; import com.google.api.gax.grpc.GrpcStatusCode; +import com.google.api.gax.retrying.RetrySettings; import com.google.api.gax.rpc.ApiException; import com.google.api.gax.rpc.StatusCode; import com.google.api.services.storage.model.StorageObject; import com.google.cloud.NoCredentials; +import com.google.cloud.ReadChannel; import com.google.cloud.RestorableState; import com.google.cloud.ServiceOptions; import com.google.cloud.WriteChannel; +import com.google.cloud.storage.BufferedReadableByteChannelSession.BufferedReadableByteChannel; import com.google.cloud.storage.BufferedWritableByteChannelSession.BufferedWritableByteChannel; import com.google.cloud.storage.Conversions.Decoder; import com.google.cloud.storage.UnifiedOpts.Opts; +import com.google.cloud.storage.ZeroCopySupport.DisposableByteString; +import com.google.cloud.storage.it.ChecksummedTestContent; import com.google.cloud.storage.spi.StorageRpcFactory; import com.google.cloud.storage.spi.v1.StorageRpc; import com.google.common.io.ByteStreams; +import com.google.protobuf.ByteString; +import com.google.storage.v2.BidiReadObjectRequest; +import com.google.storage.v2.BidiReadObjectResponse; +import com.google.storage.v2.BucketName; +import com.google.storage.v2.ObjectRangeData; +import com.google.storage.v2.ReadObjectRequest; +import com.google.storage.v2.ReadObjectResponse; +import com.google.storage.v2.ReadRange; import com.google.storage.v2.StartResumableWriteRequest; import com.google.storage.v2.StartResumableWriteResponse; import com.google.storage.v2.StorageGrpc; +import com.google.storage.v2.StorageGrpc.StorageImplBase; import com.google.storage.v2.WriteObjectRequest; import com.google.storage.v2.WriteObjectResponse; import io.grpc.CallOptions; @@ -69,17 +86,30 @@ import io.opentelemetry.sdk.metrics.data.MetricData; import io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader; import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; +import java.io.UncheckedIOException; import java.lang.reflect.Field; import java.net.InetSocketAddress; import java.nio.ByteBuffer; +import java.nio.channels.Channels; +import java.nio.channels.FileChannel; +import java.nio.channels.ReadableByteChannel; +import java.nio.channels.ScatteringByteChannel; +import java.nio.channels.SeekableByteChannel; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.NoSuchFileException; import java.nio.file.Path; +import java.nio.file.StandardOpenOption; +import java.time.Duration; import java.util.Collection; import java.util.Locale; +import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; import org.jspecify.annotations.Nullable; import org.junit.Before; @@ -1202,6 +1232,714 @@ public void onCompleted() { } } + @Test + public void recordOperation_recordsResponseBodySize_whenReadObjectAndBytesGreaterThanZero() { + StorageMetricsContext context = metrics.createContext("ReadObject"); + context.setResponseBytes(4096L); + context.recordOperation(metrics, null); + + Collection collected = reader.collectAllMetrics(); + HistogramPointData point = + getSingleHistogramPoint( + collected, StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_RESPONSE_BODY_SIZE); + assertThat(point.getCount()).isEqualTo(1L); + assertThat(point.getSum()).isEqualTo(4096.0); + Attributes attrs = point.getAttributes(); + assertThat(attrs.asMap().keySet()) + .containsExactly( + StorageClientMetrics.KEY_GCP_CLIENT_SERVICE, + StorageClientMetrics.KEY_GCP_CLIENT_VERSION, + StorageClientMetrics.KEY_GCP_CLIENT_ARTIFACT, + StorageClientMetrics.KEY_GCP_STORAGE_OPERATION, + StorageClientMetrics.KEY_GCP_CLIENT_METHOD); + assertThat(attrs.get(StorageClientMetrics.KEY_GCP_STORAGE_OPERATION)).isEqualTo("ReadObject"); + assertThat(attrs.get(StorageClientMetrics.KEY_GCP_CLIENT_METHOD)).isEqualTo("ReadObject"); + } + + @Test + public void recordOperation_doesNotRecordResponseBodySize_whenNotReadObject() { + StorageMetricsContext context = metrics.createContext("WriteObject"); + context.setResponseBytes(4096L); + context.recordOperation(metrics, null); + + Collection collected = reader.collectAllMetrics(); + assertThat( + findMetric( + collected, StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_RESPONSE_BODY_SIZE)) + .isNull(); + } + + @Test + public void baseStorageReadChannel_recordsResponseBodySizeOnClose_fullRead() throws IOException { + StorageMetricsContext context = metrics.createMultiStepContext("ReadObject"); + + byte[] payload = "0123456789abcdef0123456789abcdef".getBytes(StandardCharsets.UTF_8); + try (StorageReadChannel channel = new FakeStorageReadChannel(payload, null, context, metrics)) { + ByteBuffer buf = ByteBuffer.allocate(10); + int total = 0; + int r; + while ((r = channel.read(buf)) != -1) { + total += r; + buf.clear(); + } + assertThat(total).isEqualTo(payload.length); + } + + Collection collected = reader.collectAllMetrics(); + HistogramPointData point = + getSingleHistogramPoint( + collected, StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_RESPONSE_BODY_SIZE); + assertThat(point.getCount()).isEqualTo(1L); + assertThat(point.getSum()).isEqualTo((double) payload.length); + LongPointData opPoint = + getSinglePoint(collected, StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_OPERATIONS); + assertThat(opPoint.getAttributes().get(StorageClientMetrics.KEY_ERROR_TYPE)) + .isEqualTo(StorageErrorTypeMapper.STATUS_OK); + } + + @Test + public void baseStorageReadChannel_recordsResponseBodySizeOnClose_partialReadEarlyClose() + throws IOException { + StorageMetricsContext context = metrics.createMultiStepContext("ReadObject"); + + byte[] payload = new byte[100]; + try (StorageReadChannel channel = new FakeStorageReadChannel(payload, null, context, metrics)) { + ByteBuffer buf = ByteBuffer.allocate(12); + int read = channel.read(buf); + assertThat(read).isEqualTo(12); + } + + Collection collected = reader.collectAllMetrics(); + HistogramPointData point = + getSingleHistogramPoint( + collected, StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_RESPONSE_BODY_SIZE); + assertThat(point.getCount()).isEqualTo(1L); + assertThat(point.getSum()).isEqualTo(12.0); + } + + @Test + public void baseStorageReadChannel_recordsResponseBodySize_whenClosedWithoutReading() { + StorageMetricsContext context = metrics.createMultiStepContext("ReadObject"); + + byte[] payload = new byte[100]; + try (StorageReadChannel ignored = new FakeStorageReadChannel(payload, null, context, metrics)) { + // Close immediately without reading any bytes + } + + Collection collected = reader.collectAllMetrics(); + HistogramPointData point = + getSingleHistogramPoint( + collected, StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_RESPONSE_BODY_SIZE); + assertThat(point.getCount()).isEqualTo(1L); + assertThat(point.getSum()).isEqualTo(0.0); + assertThat(findMetric(collected, StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_OPERATIONS)) + .isNotNull(); + } + + @Test + public void baseStorageReadChannel_recordsPartialBytesAndError_whenMidStreamErrorOccurs() { + StorageMetricsContext context = metrics.createMultiStepContext("ReadObject"); + + byte[] payload = new byte[64]; + StorageException midStreamError = new StorageException(503, "Service Unavailable"); + IOException thrown = + assertThrows( + IOException.class, + () -> { + try (StorageReadChannel channel = + new FakeStorageReadChannel(payload, midStreamError, context, metrics)) { + ByteBuffer buf = ByteBuffer.allocate(16); + assertThat(channel.read(buf)).isEqualTo(16); + buf.clear(); + channel.read(buf); + } + }); + assertThat(thrown).hasCauseThat().isSameInstanceAs(midStreamError); + + Collection collected = reader.collectAllMetrics(); + HistogramPointData point = + getSingleHistogramPoint( + collected, StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_RESPONSE_BODY_SIZE); + assertThat(point.getCount()).isEqualTo(1L); + assertThat(point.getSum()).isEqualTo(16.0); + + LongPointData opPoint = + getSinglePoint(collected, StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_OPERATIONS); + assertThat(opPoint.getAttributes().get(StorageClientMetrics.KEY_ERROR_TYPE)) + .isEqualTo(StorageErrorTypeMapper.STATUS_UNAVAILABLE); + } + + @Test + public void storageImpl_readAllBytes_recordsResponseBodySize() { + StorageRpc storageRpc = mock(StorageRpc.class); + StorageRpcFactory rpcFactory = options -> storageRpc; + + byte[] payload = "response-body-bytes".getBytes(StandardCharsets.UTF_8); + when(storageRpc.load(any(StorageObject.class), anyMap())) + .thenAnswer( + inv -> { + StorageObject obj = inv.getArgument(0); + if ("fail".equals(obj.getName())) { + throw new StorageException(403, "Forbidden"); + } + return payload; + }); + + HttpStorageOptions options = + HttpStorageOptions.newBuilder() + .setProjectId("test-proj") + .setCredentials(NoCredentials.getInstance()) + .setServiceRpcFactory(rpcFactory) + .setRetrySettings(ServiceOptions.getNoRetrySettings()) + .setMeterProvider(provider) + .setEnableOtelMetrics(true) + .build(); + Storage storage = options.getService(); + + byte[] actual = storage.readAllBytes(BlobId.of("b", "ok")); + assertThat(actual).isEqualTo(payload); + + HistogramPointData point = + getSingleHistogramPoint( + reader.collectAllMetrics(), + StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_RESPONSE_BODY_SIZE); + assertThat(point.getCount()).isEqualTo(1L); + assertThat(point.getSum()).isEqualTo((double) payload.length); + + assertThrows(StorageException.class, () -> storage.readAllBytes(BlobId.of("b", "fail"))); + HistogramPointData afterFailPoint = + getSingleHistogramPoint( + reader.collectAllMetrics(), + StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_RESPONSE_BODY_SIZE); + assertThat(afterFailPoint.getCount()).isEqualTo(2L); + assertThat(afterFailPoint.getSum()).isEqualTo((double) payload.length); + } + + @Test + public void storageImpl_downloadTo_recordsResponseBodySize_acrossRetriesWithoutDoubleCounting() { + StorageRpc storageRpc = mock(StorageRpc.class); + StorageRpcFactory rpcFactory = options -> storageRpc; + + byte[] payload = + "0123456789abcdefghij0123456789abcdefghij0123456789".getBytes(StandardCharsets.UTF_8); + AtomicInteger callCount = new AtomicInteger(0); + doAnswer( + invocation -> { + long position = invocation.getArgument(2); + OutputStream out = invocation.getArgument(3); + int attempt = callCount.incrementAndGet(); + if (attempt == 1) { + out.write(payload, 0, 20); + throw new StorageException(503, "Transient failure after 20 bytes"); + } + out.write(payload, (int) position, payload.length - (int) position); + return (long) (payload.length - (int) position); + }) + .when(storageRpc) + .read(any(StorageObject.class), anyMap(), anyLong(), any(OutputStream.class)); + + HttpStorageOptions options = + HttpStorageOptions.newBuilder() + .setProjectId("test-proj") + .setCredentials(NoCredentials.getInstance()) + .setServiceRpcFactory(rpcFactory) + .setRetrySettings( + RetrySettings.newBuilder() + .setMaxAttempts(3) + .setInitialRetryDelayDuration(Duration.ofMillis(1)) + .setMaxRetryDelayDuration(Duration.ofMillis(5)) + .setRetryDelayMultiplier(1.0) + .build()) + .setMeterProvider(provider) + .setEnableOtelMetrics(true) + .build(); + Storage storage = options.getService(); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + storage.downloadTo(BlobId.of("b", "o"), out); + assertThat(out.toByteArray()).isEqualTo(payload); + assertThat(callCount.get()).isEqualTo(2); + + Collection collected = reader.collectAllMetrics(); + HistogramPointData point = + getSingleHistogramPoint( + collected, StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_RESPONSE_BODY_SIZE); + assertThat(point.getCount()).isEqualTo(1L); + assertThat(point.getSum()).isEqualTo((double) payload.length); + } + + @Test + public void blobReadSessionAdapter_recordsResponseBodySizeOnClose_acrossProjections() + throws Exception { + StorageMetricsContext context = metrics.createMultiStepContext("ReadObject"); + + byte[] futureBytesPayload = new byte[25]; + ByteString byteStringPayload = ByteString.copyFrom(new byte[15]); + byte[] channelPayload = new byte[40]; + byte[] seekablePayload = new byte[30]; + Path seekableFile = tempFolder.newFile("seekable.bin").toPath(); + Files.write(seekableFile, seekablePayload); + + ObjectReadSession fakeSession = + new ObjectReadSession() { + @Override + public com.google.storage.v2.Object getResource() { + return com.google.storage.v2.Object.getDefaultInstance(); + } + + @SuppressWarnings("unchecked") + @Override + public Projection readAs(ReadProjectionConfig config) { + if (config instanceof ReadAsFutureBytes) { + return (Projection) ApiFutures.immediateFuture(futureBytesPayload); + } + if (config instanceof ReadAsFutureByteString) { + DisposableByteString dbs = + new DisposableByteString() { + @Override + public ByteString byteString() { + return byteStringPayload; + } + + @Override + public void close() {} + }; + return (Projection) ApiFutures.immediateFuture(dbs); + } + if (config instanceof ReadAsChannel) { + return (Projection) + StorageByteChannels.readable() + .asScatteringByteChannel( + Channels.newChannel(new ByteArrayInputStream(channelPayload))); + } + if (config instanceof ReadAsSeekableChannel) { + try { + return (Projection) FileChannel.open(seekableFile, StandardOpenOption.READ); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } + throw new UnsupportedOperationException("Unexpected config: " + config); + } + + @Override + public void close() {} + }; + + ApiFuture wrapped = + BlobReadSessionAdapter.wrap(ApiFutures.immediateFuture(fakeSession), context, metrics); + try (BlobReadSession session = wrapped.get()) { + byte[] b1 = session.readAs(ReadProjectionConfigs.asFutureBytes()).get(); + assertThat(b1).hasLength(25); + + try (DisposableByteString dbs = + session.readAs(ReadProjectionConfigs.asFutureByteString()).get()) { + assertThat(dbs.byteString().size()).isEqualTo(15); + } + + // Partial read from ScatteringByteChannel across all 3 read overloads (4 + 3 + 3 = 10 of 40 + // bytes) + try (ScatteringByteChannel ch = session.readAs(ReadProjectionConfigs.asChannel())) { + assertThat(ch.read(ByteBuffer.allocate(4))).isEqualTo(4); + assertThat(ch.read(new ByteBuffer[] {ByteBuffer.allocate(3)})).isEqualTo(3L); + assertThat(ch.read(new ByteBuffer[] {ByteBuffer.allocate(3)}, 0, 1)).isEqualTo(3L); + } + + // Read 12 bytes from SeekableByteChannel + try (SeekableByteChannel seekable = + session.readAs(ReadProjectionConfigs.asSeekableChannel())) { + ByteBuffer dst = ByteBuffer.allocate(12); + assertThat(seekable.read(dst)).isEqualTo(12); + } + } + + Collection collected = reader.collectAllMetrics(); + HistogramPointData point = + getSingleHistogramPoint( + collected, StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_RESPONSE_BODY_SIZE); + assertThat(point.getCount()).isEqualTo(1L); + assertThat(point.getSum()).isEqualTo(25.0 + 15.0 + 10.0 + 12.0); + } + + @Test + public void blobReadSessionAdapter_recordsZeroResponseBodySize_whenOpenFails() { + StorageMetricsContext context = metrics.createMultiStepContext("ReadObject"); + + ApiFuture failedOpen = + ApiFutures.immediateFailedFuture(new StorageException(404, "Not Found")); + ApiFuture wrapped = BlobReadSessionAdapter.wrap(failedOpen, context, metrics); + + assertThrows(ExecutionException.class, wrapped::get); + + Collection collected = reader.collectAllMetrics(); + HistogramPointData point = + getSingleHistogramPoint( + collected, StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_RESPONSE_BODY_SIZE); + assertThat(point.getCount()).isEqualTo(1L); + assertThat(point.getSum()).isEqualTo(0.0); + LongPointData opPoint = + getSinglePoint(collected, StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_OPERATIONS); + assertThat(opPoint.getAttributes().get(StorageClientMetrics.KEY_ERROR_TYPE)) + .isEqualTo(StorageErrorTypeMapper.STATUS_NOT_FOUND); + } + + @Test + public void + blobReadSessionAdapter_recordsPartialBytesAndErrorType_whenProjectionFailsBeforeClose() + throws Exception { + // 1. Async projection (asFutureBytes) fails after an earlier successful read + StorageMetricsContext asyncContext = metrics.createMultiStepContext("ReadObject"); + AtomicInteger futureCalls = new AtomicInteger(0); + ObjectReadSession failingFutureSession = + new ObjectReadSession() { + @Override + public com.google.storage.v2.Object getResource() { + return com.google.storage.v2.Object.getDefaultInstance(); + } + + @SuppressWarnings("unchecked") + @Override + public Projection readAs(ReadProjectionConfig config) { + if (config instanceof ReadAsFutureBytes) { + if (futureCalls.incrementAndGet() == 1) { + return (Projection) ApiFutures.immediateFuture(new byte[25]); + } + return (Projection) + ApiFutures.immediateFailedFuture(Status.PERMISSION_DENIED.asRuntimeException()); + } + throw new UnsupportedOperationException("Unexpected config: " + config); + } + + @Override + public void close() {} + }; + + try (BlobReadSession session = + BlobReadSessionAdapter.wrap( + ApiFutures.immediateFuture(failingFutureSession), asyncContext, metrics) + .get()) { + assertThat(session.readAs(ReadProjectionConfigs.asFutureBytes()).get()).hasLength(25); + ApiFuture failedRead = session.readAs(ReadProjectionConfigs.asFutureBytes()); + assertThrows(ExecutionException.class, failedRead::get); + } + + Collection collected = reader.collectAllMetrics(); + HistogramPointData asyncBodyPoint = + getSingleHistogramPoint( + collected, StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_RESPONSE_BODY_SIZE); + assertThat(asyncBodyPoint.getCount()).isEqualTo(1L); + assertThat(asyncBodyPoint.getSum()).isEqualTo(25.0); + LongPointData asyncOpPoint = + getSinglePoint(collected, StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_OPERATIONS); + assertThat(asyncOpPoint.getAttributes().get(StorageClientMetrics.KEY_ERROR_TYPE)) + .isEqualTo("PERMISSION_DENIED"); + HistogramPointData asyncDurationPoint = + getSingleHistogramPoint(collected, StorageClientMetrics.METRIC_GCP_CLIENT_REQUEST_DURATION); + assertThat(asyncDurationPoint.getAttributes().get(StorageClientMetrics.KEY_ERROR_TYPE)) + .isEqualTo("PERMISSION_DENIED"); + + // 2. Channel projection throws IOException wrapping coalesced StorageException mid-stream + resetMetrics(); + StorageMetricsContext channelContext = metrics.createMultiStepContext("ReadObject"); + ObjectReadSession failingChannelSession = + new ObjectReadSession() { + @Override + public com.google.storage.v2.Object getResource() { + return com.google.storage.v2.Object.getDefaultInstance(); + } + + @SuppressWarnings("unchecked") + @Override + public Projection readAs(ReadProjectionConfig config) { + if (config instanceof ReadAsChannel) { + ReadableByteChannel failingChannel = + new ReadableByteChannel() { + private boolean firstRead = true; + private boolean open = true; + + @Override + public int read(ByteBuffer dst) throws IOException { + if (firstRead) { + firstRead = false; + int toCopy = Math.min(dst.remaining(), 18); + dst.put(new byte[toCopy]); + return toCopy; + } + throw new IOException( + StorageException.coalesce(Status.UNAVAILABLE.asRuntimeException())); + } + + @Override + public boolean isOpen() { + return open; + } + + @Override + public void close() { + open = false; + } + }; + return (Projection) + StorageByteChannels.readable().asScatteringByteChannel(failingChannel); + } + throw new UnsupportedOperationException("Unexpected config: " + config); + } + + @Override + public void close() {} + }; + + try (BlobReadSession session = + BlobReadSessionAdapter.wrap( + ApiFutures.immediateFuture(failingChannelSession), channelContext, metrics) + .get()) { + try (ScatteringByteChannel ch = session.readAs(ReadProjectionConfigs.asChannel())) { + assertThat(ch.read(ByteBuffer.allocate(32))).isEqualTo(18); + assertThrows(IOException.class, () -> ch.read(ByteBuffer.allocate(32))); + } + } + + Collection channelCollected = reader.collectAllMetrics(); + HistogramPointData channelBodyPoint = + getSingleHistogramPoint( + channelCollected, StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_RESPONSE_BODY_SIZE); + assertThat(channelBodyPoint.getCount()).isEqualTo(1L); + assertThat(channelBodyPoint.getSum()).isEqualTo(18.0); + LongPointData channelOpPoint = + getSinglePoint(channelCollected, StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_OPERATIONS); + assertThat(channelOpPoint.getAttributes().get(StorageClientMetrics.KEY_ERROR_TYPE)) + .isEqualTo(StorageErrorTypeMapper.STATUS_UNAVAILABLE); + } + + @Test + public void grpcStorageImpl_readAllBytes_recordsResponseBodySizeAndZeroOnMidStreamError() + throws Exception { + // chunk1 (16 KiB) exceeds ByteStreams.copy's 8 KiB buffer so partial bytes are flushed into + // baos before chunk2 fails mid-stream. + int chunk1Len = 16 * 1024; + int chunk2Len = 4 * 1024; + byte[] payload = new byte[chunk1Len + chunk2Len]; + for (int i = 0; i < payload.length; i++) { + payload[i] = (byte) (i & 0x7f); + } + ChecksummedTestContent chunk1 = ChecksummedTestContent.of(payload, 0, chunk1Len); + ChecksummedTestContent chunk2 = ChecksummedTestContent.of(payload, chunk1Len, chunk2Len); + com.google.storage.v2.Object meta = + com.google.storage.v2.Object.newBuilder() + .setBucket(BucketName.format("_", "b")) + .setName("ok") + .setGeneration(1L) + .setSize(payload.length) + .build(); + + StorageImplBase fakeService = + new StorageImplBase() { + @Override + public void readObject( + ReadObjectRequest request, StreamObserver responseObserver) { + if ("ok".equals(request.getObject())) { + responseObserver.onNext( + ReadObjectResponse.newBuilder() + .setMetadata(meta) + .setChecksummedData(chunk1.asChecksummedData()) + .build()); + responseObserver.onNext( + ReadObjectResponse.newBuilder() + .setChecksummedData(chunk2.asChecksummedData()) + .build()); + responseObserver.onCompleted(); + } else { + responseObserver.onNext( + ReadObjectResponse.newBuilder() + .setMetadata(meta.toBuilder().setName("fail").build()) + .setChecksummedData(chunk1.asChecksummedData()) + .build()); + responseObserver.onError(Status.PERMISSION_DENIED.asRuntimeException()); + } + } + }; + + try (FakeServer fakeServer = FakeServer.of(fakeService); + Storage storage = + fakeServer.getGrpcStorageOptions().toBuilder() + .setRetrySettings(RetrySettings.newBuilder().setMaxAttempts(1).build()) + .setMeterProvider(provider) + .setEnableOtelMetrics(true) + .build() + .getService()) { + byte[] actual = storage.readAllBytes(BlobId.of("b", "ok")); + assertThat(actual).isEqualTo(payload); + + HistogramPointData okPoint = + getSingleHistogramPoint( + reader.collectAllMetrics(), + StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_RESPONSE_BODY_SIZE); + assertThat(okPoint.getCount()).isEqualTo(1L); + assertThat(okPoint.getSum()).isEqualTo((double) payload.length); + + assertThrows(StorageException.class, () -> storage.readAllBytes(BlobId.of("b", "fail"))); + + Collection collected = reader.collectAllMetrics(); + HistogramPointData cumulativePoint = + getSingleHistogramPoint( + collected, StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_RESPONSE_BODY_SIZE); + assertThat(cumulativePoint.getCount()).isEqualTo(2L); + assertThat(cumulativePoint.getSum()).isEqualTo((double) payload.length); + + MetricData opsMetric = + findMetric(collected, StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_OPERATIONS); + assertThat(opsMetric).isNotNull(); + assertThat( + opsMetric.getLongSumData().getPoints().stream() + .filter( + p -> + "PERMISSION_DENIED" + .equals(p.getAttributes().get(StorageClientMetrics.KEY_ERROR_TYPE))) + .mapToLong(LongPointData::getValue) + .sum()) + .isEqualTo(1L); + } + } + + @Test + public void grpcStorageImpl_downloadToAndBlobReadSession_recordResponseBodySize() + throws Exception { + byte[] payload = "0123456789abcdefghij0123456789".getBytes(StandardCharsets.UTF_8); + ChecksummedTestContent content = ChecksummedTestContent.of(payload); + byte[] partialChunkBytes = new byte[16 * 1024]; + ChecksummedTestContent partialChunk = ChecksummedTestContent.of(partialChunkBytes); + com.google.storage.v2.Object meta = + com.google.storage.v2.Object.newBuilder() + .setBucket(BucketName.format("_", "b")) + .setName("o") + .setGeneration(1L) + .setSize(payload.length) + .build(); + + StorageImplBase fakeService = + new StorageImplBase() { + @Override + public void readObject( + ReadObjectRequest request, StreamObserver responseObserver) { + if ("fail".equals(request.getObject())) { + responseObserver.onNext( + ReadObjectResponse.newBuilder() + .setMetadata(meta.toBuilder().setName("fail").setSize(20 * 1024).build()) + .setChecksummedData(partialChunk.asChecksummedData()) + .build()); + responseObserver.onError(Status.PERMISSION_DENIED.asRuntimeException()); + return; + } + responseObserver.onNext( + ReadObjectResponse.newBuilder() + .setMetadata(meta) + .setChecksummedData(content.asChecksummedData()) + .build()); + responseObserver.onCompleted(); + } + + @Override + public StreamObserver bidiReadObject( + StreamObserver responseObserver) { + return new StreamObserver() { + @Override + public void onNext(BidiReadObjectRequest req) { + if (req.getReadRangesList().isEmpty()) { + responseObserver.onNext( + BidiReadObjectResponse.newBuilder().setMetadata(meta).build()); + } else { + ReadRange rr = req.getReadRangesList().get(0); + responseObserver.onNext( + BidiReadObjectResponse.newBuilder() + .addObjectDataRanges( + ObjectRangeData.newBuilder() + .setReadRange( + ReadRange.newBuilder() + .setReadId(rr.getReadId()) + .setReadOffset(0) + .setReadLength(payload.length) + .build()) + .setRangeEnd(true) + .setChecksummedData(content.asChecksummedData()) + .build()) + .build()); + } + } + + @Override + public void onError(Throwable t) { + responseObserver.onError(t); + } + + @Override + public void onCompleted() { + responseObserver.onCompleted(); + } + }; + } + }; + + try (FakeServer fakeServer = FakeServer.of(fakeService); + Storage storage = + fakeServer.getGrpcStorageOptions().toBuilder() + .setRetrySettings(RetrySettings.newBuilder().setMaxAttempts(1).build()) + .setMeterProvider(provider) + .setEnableOtelMetrics(true) + .build() + .getService()) { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + storage.downloadTo(BlobId.of("b", "o"), baos); + assertThat(baos.toByteArray()).isEqualTo(payload); + + HistogramPointData downloadPoint = + getSingleHistogramPoint( + reader.collectAllMetrics(), + StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_RESPONSE_BODY_SIZE); + assertThat(downloadPoint.getCount()).isEqualTo(1L); + assertThat(downloadPoint.getSum()).isEqualTo((double) payload.length); + + try (BlobReadSession session = + storage.blobReadSession(BlobId.of("b", "o")).get(5, TimeUnit.SECONDS)) { + byte[] sessionBytes = + session.readAs(ReadProjectionConfigs.asFutureBytes()).get(5, TimeUnit.SECONDS); + assertThat(sessionBytes).isEqualTo(payload); + } + + HistogramPointData sessionPoint = + getSingleHistogramPoint( + reader.collectAllMetrics(), + StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_RESPONSE_BODY_SIZE); + assertThat(sessionPoint.getCount()).isEqualTo(2L); + assertThat(sessionPoint.getSum()).isEqualTo(2.0 * payload.length); + + ByteArrayOutputStream partialOut = new ByteArrayOutputStream(); + assertThrows( + StorageException.class, () -> storage.downloadTo(BlobId.of("b", "fail"), partialOut)); + assertThat(partialOut.size()).isEqualTo(partialChunkBytes.length); + + Collection collected = reader.collectAllMetrics(); + HistogramPointData afterFailPoint = + getSingleHistogramPoint( + collected, StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_RESPONSE_BODY_SIZE); + assertThat(afterFailPoint.getCount()).isEqualTo(3L); + assertThat(afterFailPoint.getSum()) + .isEqualTo(2.0 * payload.length + partialChunkBytes.length); + + MetricData opsMetric = + findMetric(collected, StorageClientMetrics.METRIC_GCP_STORAGE_CLIENT_OPERATIONS); + assertThat(opsMetric).isNotNull(); + assertThat( + opsMetric.getLongSumData().getPoints().stream() + .filter( + p -> + "PERMISSION_DENIED" + .equals(p.getAttributes().get(StorageClientMetrics.KEY_ERROR_TYPE))) + .mapToLong(LongPointData::getValue) + .sum()) + .isEqualTo(1L); + } + } + private static @Nullable MetricData findMetric( Collection metrics, String metricName) { return metrics.stream().filter(m -> m.getName().equals(metricName)).findFirst().orElse(null); @@ -1281,4 +2019,71 @@ protected LazyWriteChannel newLazyWriteChannel() { } }; } + + private static final class FakeStorageReadChannel extends BaseStorageReadChannel { + private final byte[] data; + @Nullable private final StorageException errorAfterFirstRead; + + FakeStorageReadChannel( + byte[] data, + @Nullable StorageException errorAfterFirstRead, + @Nullable StorageMetricsContext metricsContext, + @Nullable StorageClientMetrics storageClientMetrics) { + super(s -> BlobInfo.newBuilder("b", "o").build(), metricsContext, storageClientMetrics); + this.data = data; + this.errorAfterFirstRead = errorAfterFirstRead; + } + + @Override + protected LazyReadChannel newLazyReadChannel() { + return new LazyReadChannel<>( + () -> + new BufferedReadableByteChannelSession() { + @Override + public ApiFuture openAsync() { + return ApiFutures.immediateFuture( + new BufferedReadableByteChannel() { + private int offset = 0; + private boolean open = true; + private int reads = 0; + + @Override + public int read(ByteBuffer dst) throws IOException { + reads++; + if (reads > 1 && errorAfterFirstRead != null) { + throw errorAfterFirstRead; + } + if (offset >= data.length) { + return -1; + } + int toCopy = Math.min(dst.remaining(), data.length - offset); + dst.put(data, offset, toCopy); + offset += toCopy; + return toCopy; + } + + @Override + public boolean isOpen() { + return open; + } + + @Override + public void close() { + open = false; + } + }); + } + + @Override + public ApiFuture getResult() { + return ApiFutures.immediateFuture("ok"); + } + }); + } + + @Override + public RestorableState capture() { + throw new UnsupportedOperationException(); + } + } }