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

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


The following commit(s) were added to refs/heads/master by this push:
     new 168bd520926 HBASE-29767 Fix IOTimePerHit NaN issue in BucketCacheStats 
(#7535)
168bd520926 is described below

commit 168bd520926ebaf28f1f3e49ccb2df2355ae284f
Author: Peng Lu <[email protected]>
AuthorDate: Fri Dec 12 22:36:25 2025 +0800

    HBASE-29767 Fix IOTimePerHit NaN issue in BucketCacheStats (#7535)
    
    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 7d0f4d78ea0..d270750abb7 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
@@ -62,7 +62,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 5d86ccdf2b5..fc42009d4f9 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
@@ -41,6 +41,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;
@@ -51,6 +52,7 @@ import java.util.Map;
 import java.util.Random;
 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;
@@ -63,6 +65,7 @@ import org.apache.hadoop.hbase.io.ByteBuffAllocator;
 import org.apache.hadoop.hbase.io.hfile.BlockCacheKey;
 import org.apache.hadoop.hbase.io.hfile.BlockPriority;
 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;
@@ -1158,4 +1161,20 @@ public class TestBucketCache {
     cache.getBlock(block.getBlockName(), true, false, true);
     assertEquals(cache.backingMap.get(block.getBlockName()).getPriority(), 
BlockPriority.MULTI);
   }
+
+  @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