diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/CacheStats.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/CacheStats.java index 2dae66fc30b..fff6585aea5 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/CacheStats.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/CacheStats.java @@ -260,13 +260,14 @@ public class CacheStats { } public double getHitRatioPastNPeriods() { - double ratio = ((double)sum(hitCounts)/(double)sum(requestCounts)); + double ratio = ((double)getSumHitCountsPastNPeriods() / + (double)getSumRequestCountsPastNPeriods()); return Double.isNaN(ratio) ? 0 : ratio; } public double getHitCachingRatioPastNPeriods() { - double ratio = - ((double)sum(hitCachingCounts)/(double)sum(requestCachingCounts)); + double ratio = ((double)getSumHitCachingCountsPastNPeriods() / + (double)getSumRequestCachingCountsPastNPeriods()); return Double.isNaN(ratio) ? 0 : ratio; } diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/CombinedBlockCache.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/CombinedBlockCache.java index 3f6582331b2..22bffee6bb8 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/CombinedBlockCache.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/CombinedBlockCache.java @@ -203,22 +203,38 @@ public class CombinedBlockCache implements ResizableBlockCache, HeapSize { } @Override - public double getHitRatioPastNPeriods() { - double ratio = ((double) (lruCacheStats.getSumHitCountsPastNPeriods() + bucketCacheStats - .getSumHitCountsPastNPeriods()) / (double) (lruCacheStats - .getSumRequestCountsPastNPeriods() + bucketCacheStats - .getSumRequestCountsPastNPeriods())); - return Double.isNaN(ratio) ? 0 : ratio; + public void rollMetricsPeriod() { + lruCacheStats.rollMetricsPeriod(); + bucketCacheStats.rollMetricsPeriod(); + } + + @Override + public long getFailedInserts() { + return lruCacheStats.getFailedInserts() + bucketCacheStats.getFailedInserts(); } @Override - public double getHitCachingRatioPastNPeriods() { - double ratio = ((double) (lruCacheStats - .getSumHitCachingCountsPastNPeriods() + bucketCacheStats - .getSumHitCachingCountsPastNPeriods()) / (double) (lruCacheStats - .getSumRequestCachingCountsPastNPeriods() + bucketCacheStats - .getSumRequestCachingCountsPastNPeriods())); - return Double.isNaN(ratio) ? 0 : ratio; + public long getSumHitCountsPastNPeriods() { + return lruCacheStats.getSumHitCountsPastNPeriods() + + bucketCacheStats.getSumHitCountsPastNPeriods(); + } + + @Override + public long getSumRequestCountsPastNPeriods() { + return lruCacheStats.getSumRequestCountsPastNPeriods() + + bucketCacheStats.getSumRequestCountsPastNPeriods(); + } + + @Override + public long getSumHitCachingCountsPastNPeriods() { + return lruCacheStats.getSumHitCachingCountsPastNPeriods() + + bucketCacheStats.getSumHitCachingCountsPastNPeriods(); + } + + @Override + public long getSumRequestCachingCountsPastNPeriods() { + return lruCacheStats.getSumRequestCachingCountsPastNPeriods() + + bucketCacheStats.getSumRequestCachingCountsPastNPeriods(); } } diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/io/hfile/TestCombinedBlockCache.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/io/hfile/TestCombinedBlockCache.java new file mode 100644 index 00000000000..50bf33132ce --- /dev/null +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/io/hfile/TestCombinedBlockCache.java @@ -0,0 +1,98 @@ +/** + * + * 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.hadoop.hbase.io.hfile; + +import static org.junit.Assert.assertEquals; +import org.apache.hadoop.hbase.io.hfile.CombinedBlockCache.CombinedCacheStats; +import org.apache.hadoop.hbase.testclassification.SmallTests; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +@Category({SmallTests.class}) +public class TestCombinedBlockCache { + @Test + public void testCombinedCacheStats() { + CacheStats lruCacheStats = new CacheStats("lruCacheStats", 2); + CacheStats bucketCacheStats = new CacheStats("bucketCacheStats", 2); + CombinedCacheStats stats = + new CombinedCacheStats(lruCacheStats, bucketCacheStats); + + double delta = 0.01; + + // period 1: + // lru cache: 1 hit caching, 1 miss caching + // bucket cache: 2 hit non-caching,1 miss non-caching/primary,1 fail insert + lruCacheStats.hit(true); + lruCacheStats.miss(true, false); + bucketCacheStats.hit(false); + bucketCacheStats.hit(false); + bucketCacheStats.miss(false, true); + + assertEquals(5, stats.getRequestCount()); + assertEquals(2, stats.getRequestCachingCount()); + assertEquals(2, stats.getMissCount()); + assertEquals(1, stats.getPrimaryMissCount()); + assertEquals(1, stats.getMissCachingCount()); + assertEquals(3, stats.getHitCount()); + assertEquals(3, stats.getPrimaryHitCount()); + assertEquals(1, stats.getHitCachingCount()); + assertEquals(0.6, stats.getHitRatio(), delta); + assertEquals(0.5, stats.getHitCachingRatio(), delta); + assertEquals(0.4, stats.getMissRatio(), delta); + assertEquals(0.5, stats.getMissCachingRatio(), delta); + + + // lru cache: 2 evicted, 1 evict + // bucket cache: 1 evict + lruCacheStats.evicted(1000, true); + lruCacheStats.evicted(1000, false); + lruCacheStats.evict(); + bucketCacheStats.evict(); + assertEquals(2, stats.getEvictionCount()); + assertEquals(2, stats.getEvictedCount()); + assertEquals(1, stats.getPrimaryEvictedCount()); + assertEquals(1.0, stats.evictedPerEviction(), delta); + + // lru cache: 1 fail insert + lruCacheStats.failInsert(); + assertEquals(1, stats.getFailedInserts()); + + // rollMetricsPeriod + stats.rollMetricsPeriod(); + assertEquals(3, stats.getSumHitCountsPastNPeriods()); + assertEquals(5, stats.getSumRequestCountsPastNPeriods()); + assertEquals(1, stats.getSumHitCachingCountsPastNPeriods()); + assertEquals(2, stats.getSumRequestCachingCountsPastNPeriods()); + assertEquals(0.6, stats.getHitRatioPastNPeriods(), delta); + assertEquals(0.5, stats.getHitCachingRatioPastNPeriods(), delta); + + // period 2: + // lru cache: 3 hit caching + lruCacheStats.hit(true); + lruCacheStats.hit(true); + lruCacheStats.hit(true); + stats.rollMetricsPeriod(); + assertEquals(6, stats.getSumHitCountsPastNPeriods()); + assertEquals(8, stats.getSumRequestCountsPastNPeriods()); + assertEquals(4, stats.getSumHitCachingCountsPastNPeriods()); + assertEquals(5, stats.getSumRequestCachingCountsPastNPeriods()); + assertEquals(0.75, stats.getHitRatioPastNPeriods(), delta); + assertEquals(0.8, stats.getHitCachingRatioPastNPeriods(), delta); + } +}