This is an automated email from the ASF dual-hosted git repository.

lupeng pushed a commit to branch branch-2.5
in repository https://gitbox.apache.org/repos/asf/hbase.git


The following commit(s) were added to refs/heads/branch-2.5 by this push:
     new f17a976dd2b HBASE-29767 Fix IOTimePerHit NaN issue in BucketCacheStats 
(#7542)
f17a976dd2b is described below

commit f17a976dd2bec77716c7979f28958e2cc45c3323
Author: Peng Lu <[email protected]>
AuthorDate: Sun Dec 14 19:14:31 2025 +0800

    HBASE-29767 Fix IOTimePerHit NaN issue in BucketCacheStats (#7542)
    
    Signed-off-by: Duo Zhang <[email protected]>
    Signed-off-by: Dávid Paksy <[email protected]>
    Reviewed-by: Liu Xiao <[email protected]>
---
 .../hbase/io/hfile/bucket/BucketCacheStats.java       |  2 +-
 .../hadoop/hbase/io/hfile/bucket/TestBucketCache.java | 19 +++++++++++++++++++
 2 files changed, 20 insertions(+), 1 deletion(-)

diff --git 
a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/bucket/BucketCacheStats.java
 
b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/bucket/BucketCacheStats.java
index 73ca011004a..0f6e8fd3e25 100644
--- 
a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/bucket/BucketCacheStats.java
+++ 
b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/bucket/BucketCacheStats.java
@@ -63,7 +63,7 @@ public class BucketCacheStats extends CacheStats {
   public double getIOTimePerHit() {
     long time = ioHitTime.sum() / NANO_TIME;
     long count = ioHitCount.sum();
-    return ((float) time / (float) count);
+    return count == 0 ? 0.0 : (double) time / count;
   }
 
   public void reset() {
diff --git 
a/hbase-server/src/test/java/org/apache/hadoop/hbase/io/hfile/bucket/TestBucketCache.java
 
b/hbase-server/src/test/java/org/apache/hadoop/hbase/io/hfile/bucket/TestBucketCache.java
index bbc15c555b0..203927d5949 100644
--- 
a/hbase-server/src/test/java/org/apache/hadoop/hbase/io/hfile/bucket/TestBucketCache.java
+++ 
b/hbase-server/src/test/java/org/apache/hadoop/hbase/io/hfile/bucket/TestBucketCache.java
@@ -27,6 +27,7 @@ import static org.mockito.Mockito.when;
 
 import java.io.File;
 import java.io.IOException;
+import java.lang.reflect.Field;
 import java.nio.ByteBuffer;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -34,6 +35,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.ThreadLocalRandom;
+import java.util.concurrent.atomic.LongAdder;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.fs.Path;
@@ -44,6 +46,7 @@ import org.apache.hadoop.hbase.HConstants;
 import org.apache.hadoop.hbase.io.ByteBuffAllocator;
 import org.apache.hadoop.hbase.io.hfile.BlockCacheKey;
 import org.apache.hadoop.hbase.io.hfile.BlockType;
+import org.apache.hadoop.hbase.io.hfile.CacheStats;
 import org.apache.hadoop.hbase.io.hfile.CacheTestUtils;
 import org.apache.hadoop.hbase.io.hfile.CacheTestUtils.HFileBlockPair;
 import org.apache.hadoop.hbase.io.hfile.Cacheable;
@@ -770,4 +773,20 @@ public class TestBucketCache {
       HBASE_TESTING_UTILITY.cleanupTestDir();
     }
   }
+
+  @Test
+  public void testIOTimePerHitReturnsZeroWhenNoHits()
+    throws NoSuchFieldException, IllegalAccessException {
+    CacheStats cacheStats = cache.getStats();
+    assertTrue(cacheStats instanceof BucketCacheStats);
+    BucketCacheStats bucketCacheStats = (BucketCacheStats) cacheStats;
+
+    Field field = BucketCacheStats.class.getDeclaredField("ioHitCount");
+    field.setAccessible(true);
+    LongAdder ioHitCount = (LongAdder) field.get(bucketCacheStats);
+
+    assertEquals(0, ioHitCount.sum());
+    double ioTimePerHit = bucketCacheStats.getIOTimePerHit();
+    assertEquals(0, ioTimePerHit, 0.0);
+  }
 }

Reply via email to