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 = 048 new CombinedCacheStats(lruCacheStats, bucketCacheStats); 049 050 double delta = 0.01; 051 052 // period 1: 053 // lru cache: 1 hit caching, 1 miss caching 054 // bucket cache: 2 hit non-caching,1 miss non-caching/primary,1 fail insert 055 lruCacheStats.hit(true, true, BlockType.DATA); 056 lruCacheStats.miss(true, false, BlockType.DATA); 057 bucketCacheStats.hit(false,true, BlockType.DATA); 058 bucketCacheStats.hit(false,true, BlockType.DATA); 059 bucketCacheStats.miss(false, true, BlockType.DATA); 060 061 assertEquals(5, stats.getRequestCount()); 062 assertEquals(2, stats.getRequestCachingCount()); 063 assertEquals(2, stats.getMissCount()); 064 assertEquals(1, stats.getPrimaryMissCount()); 065 assertEquals(1, stats.getMissCachingCount()); 066 assertEquals(3, stats.getHitCount()); 067 assertEquals(3, stats.getPrimaryHitCount()); 068 assertEquals(1, stats.getHitCachingCount()); 069 assertEquals(0.6, stats.getHitRatio(), delta); 070 assertEquals(0.5, stats.getHitCachingRatio(), delta); 071 assertEquals(0.4, stats.getMissRatio(), delta); 072 assertEquals(0.5, stats.getMissCachingRatio(), delta); 073 074 075 // lru cache: 2 evicted, 1 evict 076 // bucket cache: 1 evict 077 lruCacheStats.evicted(1000, true); 078 lruCacheStats.evicted(1000, false); 079 lruCacheStats.evict(); 080 bucketCacheStats.evict(); 081 assertEquals(2, stats.getEvictionCount()); 082 assertEquals(2, stats.getEvictedCount()); 083 assertEquals(1, stats.getPrimaryEvictedCount()); 084 assertEquals(1.0, stats.evictedPerEviction(), delta); 085 086 // lru cache: 1 fail insert 087 lruCacheStats.failInsert(); 088 assertEquals(1, stats.getFailedInserts()); 089 090 // rollMetricsPeriod 091 stats.rollMetricsPeriod(); 092 assertEquals(3, stats.getSumHitCountsPastNPeriods()); 093 assertEquals(5, stats.getSumRequestCountsPastNPeriods()); 094 assertEquals(1, stats.getSumHitCachingCountsPastNPeriods()); 095 assertEquals(2, stats.getSumRequestCachingCountsPastNPeriods()); 096 assertEquals(0.6, stats.getHitRatioPastNPeriods(), delta); 097 assertEquals(0.5, stats.getHitCachingRatioPastNPeriods(), delta); 098 099 // period 2: 100 // lru cache: 3 hit caching 101 lruCacheStats.hit(true, true, BlockType.DATA); 102 lruCacheStats.hit(true, true, BlockType.DATA); 103 lruCacheStats.hit(true, true, BlockType.DATA); 104 stats.rollMetricsPeriod(); 105 assertEquals(6, stats.getSumHitCountsPastNPeriods()); 106 assertEquals(8, stats.getSumRequestCountsPastNPeriods()); 107 assertEquals(4, stats.getSumHitCachingCountsPastNPeriods()); 108 assertEquals(5, stats.getSumRequestCachingCountsPastNPeriods()); 109 assertEquals(0.75, stats.getHitRatioPastNPeriods(), delta); 110 assertEquals(0.8, stats.getHitCachingRatioPastNPeriods(), delta); 111 } 112 113 @Test 114 public void testMultiThreadGetAndEvictBlock() throws Exception { 115 Configuration conf = UTIL.getConfiguration(); 116 conf.set(BUCKET_CACHE_IOENGINE_KEY, "offheap"); 117 conf.setInt(BUCKET_CACHE_SIZE_KEY, 32); 118 BlockCache blockCache = BlockCacheFactory.createBlockCache(conf); 119 Assert.assertTrue(blockCache instanceof CombinedBlockCache); 120 TestLruBlockCache.testMultiThreadGetAndEvictBlockInternal(blockCache); 121 } 122}