001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.io.hfile;
019
020import static org.apache.hadoop.hbase.HConstants.BUCKET_CACHE_IOENGINE_KEY;
021import static org.apache.hadoop.hbase.HConstants.BUCKET_CACHE_SIZE_KEY;
022import static org.junit.Assert.assertEquals;
023
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.HBaseTestingUtility;
027import org.apache.hadoop.hbase.io.hfile.CombinedBlockCache.CombinedCacheStats;
028import org.apache.hadoop.hbase.testclassification.SmallTests;
029import org.junit.Assert;
030import org.junit.ClassRule;
031import org.junit.Test;
032import org.junit.experimental.categories.Category;
033
034@Category({ SmallTests.class })
035public class TestCombinedBlockCache {
036
037  @ClassRule
038  public static final HBaseClassTestRule CLASS_RULE =
039    HBaseClassTestRule.forClass(TestCombinedBlockCache.class);
040
041  private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
042
043  @Test
044  public void testCombinedCacheStats() {
045    CacheStats lruCacheStats = new CacheStats("lruCacheStats", 2);
046    CacheStats bucketCacheStats = new CacheStats("bucketCacheStats", 2);
047    CombinedCacheStats stats = new CombinedCacheStats(lruCacheStats, bucketCacheStats);
048
049    double delta = 0.01;
050
051    // period 1:
052    // lru cache: 1 hit caching, 1 miss caching
053    // bucket cache: 2 hit non-caching,1 miss non-caching/primary,1 fail insert
054    lruCacheStats.hit(true, true, BlockType.DATA);
055    lruCacheStats.miss(true, false, BlockType.DATA);
056    bucketCacheStats.hit(false, true, BlockType.DATA);
057    bucketCacheStats.hit(false, true, BlockType.DATA);
058    bucketCacheStats.miss(false, true, BlockType.DATA);
059
060    assertEquals(5, stats.getRequestCount());
061    assertEquals(2, stats.getRequestCachingCount());
062    assertEquals(2, stats.getMissCount());
063    assertEquals(1, stats.getPrimaryMissCount());
064    assertEquals(1, stats.getMissCachingCount());
065    assertEquals(3, stats.getHitCount());
066    assertEquals(3, stats.getPrimaryHitCount());
067    assertEquals(1, stats.getHitCachingCount());
068    assertEquals(0.6, stats.getHitRatio(), delta);
069    assertEquals(0.5, stats.getHitCachingRatio(), delta);
070    assertEquals(0.4, stats.getMissRatio(), delta);
071    assertEquals(0.5, stats.getMissCachingRatio(), delta);
072
073    // lru cache: 2 evicted, 1 evict
074    // bucket cache: 1 evict
075    lruCacheStats.evicted(1000, true);
076    lruCacheStats.evicted(1000, false);
077    lruCacheStats.evict();
078    bucketCacheStats.evict();
079    assertEquals(2, stats.getEvictionCount());
080    assertEquals(2, stats.getEvictedCount());
081    assertEquals(1, stats.getPrimaryEvictedCount());
082    assertEquals(1.0, stats.evictedPerEviction(), delta);
083
084    // lru cache: 1 fail insert
085    lruCacheStats.failInsert();
086    assertEquals(1, stats.getFailedInserts());
087
088    // rollMetricsPeriod
089    stats.rollMetricsPeriod();
090    assertEquals(3, stats.getSumHitCountsPastNPeriods());
091    assertEquals(5, stats.getSumRequestCountsPastNPeriods());
092    assertEquals(1, stats.getSumHitCachingCountsPastNPeriods());
093    assertEquals(2, stats.getSumRequestCachingCountsPastNPeriods());
094    assertEquals(0.6, stats.getHitRatioPastNPeriods(), delta);
095    assertEquals(0.5, stats.getHitCachingRatioPastNPeriods(), delta);
096
097    // period 2:
098    // lru cache: 3 hit caching
099    lruCacheStats.hit(true, true, BlockType.DATA);
100    lruCacheStats.hit(true, true, BlockType.DATA);
101    lruCacheStats.hit(true, true, BlockType.DATA);
102    stats.rollMetricsPeriod();
103    assertEquals(6, stats.getSumHitCountsPastNPeriods());
104    assertEquals(8, stats.getSumRequestCountsPastNPeriods());
105    assertEquals(4, stats.getSumHitCachingCountsPastNPeriods());
106    assertEquals(5, stats.getSumRequestCachingCountsPastNPeriods());
107    assertEquals(0.75, stats.getHitRatioPastNPeriods(), delta);
108    assertEquals(0.8, stats.getHitCachingRatioPastNPeriods(), delta);
109  }
110
111  @Test
112  public void testMultiThreadGetAndEvictBlock() throws Exception {
113    Configuration conf = UTIL.getConfiguration();
114    conf.set(BUCKET_CACHE_IOENGINE_KEY, "offheap");
115    conf.setInt(BUCKET_CACHE_SIZE_KEY, 32);
116    BlockCache blockCache = BlockCacheFactory.createBlockCache(conf);
117    Assert.assertTrue(blockCache instanceof CombinedBlockCache);
118    TestLruBlockCache.testMultiThreadGetAndEvictBlockInternal(blockCache);
119  }
120}