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.apache.hadoop.hbase.io.ByteBuffAllocator.HEAP;
023import static org.junit.jupiter.api.Assertions.assertEquals;
024import static org.junit.jupiter.api.Assertions.assertTrue;
025
026import java.nio.ByteBuffer;
027import org.apache.hadoop.conf.Configuration;
028import org.apache.hadoop.hbase.HBaseTestingUtil;
029import org.apache.hadoop.hbase.HConstants;
030import org.apache.hadoop.hbase.io.hfile.CombinedBlockCache.CombinedCacheStats;
031import org.apache.hadoop.hbase.nio.ByteBuff;
032import org.apache.hadoop.hbase.testclassification.RegionServerTests;
033import org.apache.hadoop.hbase.testclassification.SmallTests;
034import org.junit.jupiter.api.Tag;
035import org.junit.jupiter.api.Test;
036
037@Tag(SmallTests.TAG)
038@Tag(RegionServerTests.TAG)
039public class TestCombinedBlockCache {
040
041  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
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    BlockCache blockCache = createCombinedBlockCache();
114    TestLruBlockCache.testMultiThreadGetAndEvictBlockInternal(blockCache);
115  }
116
117  @Test
118  public void testCombinedBlockCacheStatsWithDataBlockType() throws Exception {
119    testCombinedBlockCacheStats(BlockType.DATA, 0, 1);
120  }
121
122  @Test
123  public void testCombinedBlockCacheStatsWithMetaBlockType() throws Exception {
124    testCombinedBlockCacheStats(BlockType.META, 1, 0);
125  }
126
127  @Test
128  public void testCombinedBlockCacheStatsWithNoBlockType() throws Exception {
129    testCombinedBlockCacheStats(null, 0, 1);
130  }
131
132  private CombinedBlockCache createCombinedBlockCache() {
133    Configuration conf = UTIL.getConfiguration();
134    conf.set(BUCKET_CACHE_IOENGINE_KEY, "offheap");
135    conf.setInt(BUCKET_CACHE_SIZE_KEY, 32);
136    BlockCache blockCache = BlockCacheFactory.createBlockCache(conf);
137    assertTrue(blockCache instanceof CombinedBlockCache);
138    return (CombinedBlockCache) blockCache;
139  }
140
141  public void testCombinedBlockCacheStats(BlockType type, int expectedL1Miss, int expectedL2Miss)
142    throws Exception {
143    CombinedBlockCache blockCache = createCombinedBlockCache();
144    BlockCacheKey key = new BlockCacheKey("key1", 0, false, type);
145    int size = 100;
146    int length = HConstants.HFILEBLOCK_HEADER_SIZE + size;
147    byte[] byteArr = new byte[length];
148    HFileContext meta = new HFileContextBuilder().build();
149    HFileBlock blk = new HFileBlock(type != null ? type : BlockType.DATA, size, size, -1,
150      ByteBuff.wrap(ByteBuffer.wrap(byteArr, 0, size)), HFileBlock.FILL_HEADER, -1, 52, -1, meta,
151      HEAP);
152    blockCache.cacheBlock(key, blk);
153    blockCache.getBlock(key, true, false, true);
154    assertEquals(0, blockCache.getStats().getMissCount());
155    blockCache.evictBlock(key);
156    blockCache.getBlock(key, true, false, true);
157    assertEquals(1, blockCache.getStats().getMissCount());
158    assertEquals(expectedL1Miss, blockCache.getFirstLevelCache().getStats().getMissCount());
159    assertEquals(expectedL2Miss, blockCache.getSecondLevelCache().getStats().getMissCount());
160  }
161
162}