dbtsai commented on code in PR #17236:
URL: https://github.com/apache/iceberg/pull/17236#discussion_r3875263608


##########
azure/src/test/java/org/apache/iceberg/azure/adlsv2/TestADLSInputStream.java:
##########
@@ -123,4 +129,106 @@ void testReadTailClosesTheStream() throws IOException {
 
     verify(inputStream).close();
   }
+
+  @Test
+  void testReadFullyTracksMetrics() throws IOException {
+    byte[] data = new byte[] {1, 2, 3, 4, 5, 6, 7, 8};
+    InputStream byteStream = new ByteArrayInputStream(data);
+    InternalDataLakeFileOpenInputStreamResult openInputStreamResult =
+        new InternalDataLakeFileOpenInputStreamResult(byteStream, mock());
+    when(fileClient.openInputStream(any())).thenReturn(openInputStreamResult);
+
+    CachingMetricsContext metrics = new CachingMetricsContext();
+    Counter readBytes = metrics.counter(FileIOMetricsContext.READ_BYTES, 
MetricsContext.Unit.BYTES);
+    Counter readOperations = 
metrics.counter(FileIOMetricsContext.READ_OPERATIONS);
+
+    try (ADLSInputStream in =
+        new ADLSInputStream(
+            "abfs://[email protected]/path/to/file",
+            fileClient,
+            (long) data.length,
+            mock(),
+            metrics)) {
+      in.readFully(0, new byte[data.length], 0, data.length);
+
+      assertThat(readBytes.value()).isEqualTo(data.length);
+      assertThat(readOperations.value()).isEqualTo(1);
+    }
+  }
+
+  @Test
+  void testReadTailTracksMetrics() throws IOException {
+    byte[] data = new byte[] {1, 2, 3, 4, 5, 6, 7, 8};
+    InputStream byteStream = new ByteArrayInputStream(data);
+    // the constructor's openStream() reads the file size from the stream 
result, so report the
+    // real length; otherwise readTail computes a negative start offset
+    PathProperties properties = mock(PathProperties.class);
+    when(properties.getFileSize()).thenReturn((long) data.length);
+    InternalDataLakeFileOpenInputStreamResult openInputStreamResult =
+        new InternalDataLakeFileOpenInputStreamResult(byteStream, properties);
+    when(fileClient.openInputStream(any())).thenReturn(openInputStreamResult);
+
+    CachingMetricsContext metrics = new CachingMetricsContext();
+    Counter readBytes = metrics.counter(FileIOMetricsContext.READ_BYTES, 
MetricsContext.Unit.BYTES);
+    Counter readOperations = 
metrics.counter(FileIOMetricsContext.READ_OPERATIONS);
+
+    try (ADLSInputStream in =
+        new ADLSInputStream(
+            "abfs://[email protected]/path/to/file",
+            fileClient,
+            (long) data.length,
+            mock(),
+            metrics)) {
+      int tailLength = 4;
+      int bytesRead = in.readTail(new byte[tailLength], 0, tailLength);
+
+      assertThat(bytesRead).isEqualTo(tailLength);
+      assertThat(readBytes.value()).isEqualTo(tailLength);
+      assertThat(readOperations.value()).isEqualTo(1);
+    }
+  }
+
+  @Test
+  void testReadTailEmptyObjectDoesNotCountMetrics() throws IOException {
+    InputStream byteStream = new ByteArrayInputStream(new byte[0]);
+    PathProperties properties = mock(PathProperties.class);
+    when(properties.getFileSize()).thenReturn(0L);
+    InternalDataLakeFileOpenInputStreamResult openInputStreamResult =
+        new InternalDataLakeFileOpenInputStreamResult(byteStream, properties);
+    when(fileClient.openInputStream(any())).thenReturn(openInputStreamResult);
+
+    CachingMetricsContext metrics = new CachingMetricsContext();
+    Counter readBytes = metrics.counter(FileIOMetricsContext.READ_BYTES, 
MetricsContext.Unit.BYTES);
+    Counter readOperations = 
metrics.counter(FileIOMetricsContext.READ_OPERATIONS);
+
+    try (ADLSInputStream in =
+        new ADLSInputStream(
+            "abfs://[email protected]/path/to/file",
+            fileClient,
+            0L,
+            mock(),
+            metrics)) {
+      int bytesRead = in.readTail(new byte[8], 0, 8);

Review Comment:
   Good catch, thanks — fixed in 1884fb8. `readTail` now clamps the start with 
`Math.max(0, fileSize - length)`, matching `GCSInputStream`, so an empty/small 
file no longer produces a negative offset that the Azure SDK `FileRange` 
rejects. `testReadTailEmptyObjectDoesNotCountMetrics` exercises exactly this 
path (empty file, tail of 8) and passes only with the clamp.



##########
aws/src/test/java/org/apache/iceberg/aws/s3/TestAnalyticsAcceleratorInputStreamWrapper.java:
##########
@@ -0,0 +1,89 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iceberg.aws.s3;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.io.IOException;
+import java.util.Map;
+import org.apache.iceberg.io.FileIOMetricsContext;
+import org.apache.iceberg.metrics.Counter;
+import org.apache.iceberg.metrics.DefaultMetricsContext;
+import org.apache.iceberg.metrics.MetricsContext;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.junit.jupiter.api.Test;
+import software.amazon.s3.analyticsaccelerator.S3SeekableInputStream;
+
+public class TestAnalyticsAcceleratorInputStreamWrapper {
+
+  @Test
+  public void testReadTracksMetrics() throws IOException {
+    S3SeekableInputStream delegate = mock(S3SeekableInputStream.class);
+    // first a single-byte read, then a buffered read of 8 bytes, then a 
zero-length read, then EOF
+    when(delegate.read()).thenReturn(1);
+    when(delegate.read(any(byte[].class), anyInt(), anyInt()))
+        .thenReturn(8)
+        .thenReturn(0)
+        .thenReturn(-1);
+
+    CachingMetricsContext metrics = new CachingMetricsContext();
+    Counter readBytes = metrics.counter(FileIOMetricsContext.READ_BYTES, 
MetricsContext.Unit.BYTES);
+    Counter readOperations = 
metrics.counter(FileIOMetricsContext.READ_OPERATIONS);
+
+    try (AnalyticsAcceleratorInputStreamWrapper in =
+        new AnalyticsAcceleratorInputStreamWrapper(delegate, metrics)) {
+      assertThat(in.read()).isEqualTo(1);
+      assertThat(readBytes.value()).isEqualTo(1);
+      assertThat(readOperations.value()).isEqualTo(1);
+
+      assertThat(in.read(new byte[16], 0, 16)).isEqualTo(8);
+      assertThat(readBytes.value()).isEqualTo(9);
+      assertThat(readOperations.value()).isEqualTo(2);
+
+      // a zero-length read counts neither bytes nor an operation
+      assertThat(in.read(new byte[16], 0, 0)).isEqualTo(0);
+      assertThat(readBytes.value()).isEqualTo(9);
+      assertThat(readOperations.value()).isEqualTo(2);
+
+      // an EOF read counts neither bytes nor an operation
+      assertThat(in.read(new byte[16], 0, 16)).isEqualTo(-1);
+      assertThat(readBytes.value()).isEqualTo(9);
+      assertThat(readOperations.value()).isEqualTo(2);
+    }
+  }
+
+  /**
+   * A {@link MetricsContext} that returns the same {@link Counter} instance 
for a given name, so
+   * that tests can observe the counters the stream under test increments. 
{@link
+   * DefaultMetricsContext} allocates a fresh counter on every {@code 
counter(...)} call.
+   */
+  private static class CachingMetricsContext extends DefaultMetricsContext {
+    private final Map<String, org.apache.iceberg.metrics.Counter> counters =
+        Maps.newConcurrentMap();

Review Comment:
   Done in 1884fb8. Dropped Guava here — and while at it, hoisted the 
duplicated helper into a single shared `CachingMetricsContext` under 
`iceberg-api` test artifacts (already on the aws/azure/gcp test classpath), 
backed by JDK `ConcurrentHashMap`. That also removes the four other copies. If 
you'd rather keep the change minimal I'm happy to revert to separate per-module 
helpers that each just use `ConcurrentHashMap`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to