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; 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.hfile.CacheConfig.CACHE_BLOCKS_ON_WRITE_KEY; 023import static org.apache.hadoop.hbase.io.hfile.CacheConfig.EVICT_BLOCKS_ON_CLOSE_KEY; 024import static org.apache.hadoop.hbase.io.hfile.CacheConfig.EVICT_BLOCKS_ON_SPLIT_KEY; 025import static org.apache.hadoop.hbase.io.hfile.CacheConfig.PREFETCH_BLOCKS_ON_OPEN_KEY; 026import static org.junit.Assert.assertTrue; 027 028import java.io.IOException; 029import java.util.ArrayList; 030import java.util.Collection; 031import java.util.List; 032import java.util.Map; 033import java.util.function.BiConsumer; 034import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; 035import org.apache.hadoop.hbase.client.Put; 036import org.apache.hadoop.hbase.client.Table; 037import org.apache.hadoop.hbase.client.TableDescriptor; 038import org.apache.hadoop.hbase.client.TableDescriptorBuilder; 039import org.apache.hadoop.hbase.regionserver.HStoreFile; 040import org.apache.hadoop.hbase.regionserver.storefiletracker.StoreFileTrackerFactory; 041import org.apache.hadoop.hbase.testclassification.MediumTests; 042import org.apache.hadoop.hbase.testclassification.MiscTests; 043import org.apache.hadoop.hbase.util.Bytes; 044import org.apache.hadoop.hbase.util.Pair; 045import org.junit.BeforeClass; 046import org.junit.ClassRule; 047import org.junit.Test; 048import org.junit.experimental.categories.Category; 049import org.slf4j.Logger; 050import org.slf4j.LoggerFactory; 051 052@Category({ MiscTests.class, MediumTests.class }) 053public class TestCacheEviction { 054 055 @ClassRule 056 public static final HBaseClassTestRule CLASS_RULE = 057 HBaseClassTestRule.forClass(TestCacheEviction.class); 058 059 private static final Logger LOG = LoggerFactory.getLogger(TestCacheEviction.class); 060 061 private static final HBaseTestingUtil UTIL = new HBaseTestingUtil(); 062 063 @BeforeClass 064 public static void setUp() throws Exception { 065 UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_META_OPERATION_TIMEOUT, 1000); 066 UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 2); 067 UTIL.getConfiguration().setBoolean(CACHE_BLOCKS_ON_WRITE_KEY, true); 068 UTIL.getConfiguration().setBoolean(PREFETCH_BLOCKS_ON_OPEN_KEY, true); 069 UTIL.getConfiguration().set(BUCKET_CACHE_IOENGINE_KEY, "offheap"); 070 UTIL.getConfiguration().setInt(BUCKET_CACHE_SIZE_KEY, 200); 071 UTIL.getConfiguration().set(StoreFileTrackerFactory.TRACKER_IMPL, "FILE"); 072 } 073 074 @Test 075 public void testEvictOnSplit() throws Exception { 076 doTestEvictOnSplit("testEvictOnSplit", true, 077 (f, m) -> Waiter.waitFor(UTIL.getConfiguration(), 1000, () -> m.get(f) != null), 078 (f, m) -> Waiter.waitFor(UTIL.getConfiguration(), 1000, () -> m.get(f) == null)); 079 } 080 081 @Test 082 public void testDoesntEvictOnSplit() throws Exception { 083 doTestEvictOnSplit("testDoesntEvictOnSplit", false, 084 (f, m) -> Waiter.waitFor(UTIL.getConfiguration(), 1000, () -> m.get(f) != null), 085 (f, m) -> Waiter.waitFor(UTIL.getConfiguration(), 1000, () -> m.get(f) != null)); 086 } 087 088 @Test 089 public void testEvictOnClose() throws Exception { 090 doTestEvictOnClose("testEvictOnClose", true, 091 (f, m) -> Waiter.waitFor(UTIL.getConfiguration(), 1000, () -> m.get(f) != null), 092 (f, m) -> Waiter.waitFor(UTIL.getConfiguration(), 1000, () -> m.get(f) == null)); 093 } 094 095 @Test 096 public void testDoesntEvictOnClose() throws Exception { 097 doTestEvictOnClose("testDoesntEvictOnClose", false, 098 (f, m) -> Waiter.waitFor(UTIL.getConfiguration(), 1000, () -> m.get(f) != null), 099 (f, m) -> Waiter.waitFor(UTIL.getConfiguration(), 1000, () -> m.get(f) != null)); 100 } 101 102 private void doTestEvictOnSplit(String table, boolean evictOnSplit, 103 BiConsumer<String, Map<String, Pair<String, Long>>> predicateBeforeSplit, 104 BiConsumer<String, Map<String, Pair<String, Long>>> predicateAfterSplit) throws Exception { 105 UTIL.startMiniCluster(1); 106 try { 107 TableName tableName = TableName.valueOf(table); 108 createTable(tableName, true); 109 Collection<HStoreFile> files = 110 UTIL.getMiniHBaseCluster().getRegions(tableName).get(0).getStores().get(0).getStorefiles(); 111 checkCacheForBlocks(tableName, files, predicateBeforeSplit); 112 UTIL.getMiniHBaseCluster().getRegionServer(0).getConfiguration() 113 .setBoolean(EVICT_BLOCKS_ON_SPLIT_KEY, evictOnSplit); 114 UTIL.getAdmin().split(tableName, Bytes.toBytes("row-500")); 115 Waiter.waitFor(UTIL.getConfiguration(), 30000, 116 () -> UTIL.getMiniHBaseCluster().getRegions(tableName).size() == 2); 117 UTIL.waitUntilNoRegionsInTransition(); 118 checkCacheForBlocks(tableName, files, predicateAfterSplit); 119 } finally { 120 UTIL.shutdownMiniCluster(); 121 } 122 } 123 124 private void doTestEvictOnClose(String table, boolean evictOnClose, 125 BiConsumer<String, Map<String, Pair<String, Long>>> predicateBeforeClose, 126 BiConsumer<String, Map<String, Pair<String, Long>>> predicateAfterClose) throws Exception { 127 UTIL.startMiniCluster(1); 128 try { 129 TableName tableName = TableName.valueOf(table); 130 createTable(tableName, true); 131 Collection<HStoreFile> files = 132 UTIL.getMiniHBaseCluster().getRegions(tableName).get(0).getStores().get(0).getStorefiles(); 133 checkCacheForBlocks(tableName, files, predicateBeforeClose); 134 UTIL.getMiniHBaseCluster().getRegionServer(0).getConfiguration() 135 .setBoolean(EVICT_BLOCKS_ON_CLOSE_KEY, evictOnClose); 136 UTIL.getAdmin().disableTable(tableName); 137 UTIL.waitUntilNoRegionsInTransition(); 138 checkCacheForBlocks(tableName, files, predicateAfterClose); 139 } finally { 140 UTIL.shutdownMiniCluster(); 141 } 142 } 143 144 private void createTable(TableName tableName, boolean shouldFlushTable) 145 throws IOException, InterruptedException { 146 byte[] family = Bytes.toBytes("CF"); 147 TableDescriptor td = TableDescriptorBuilder.newBuilder(tableName) 148 .setColumnFamily(ColumnFamilyDescriptorBuilder.of(family)).build(); 149 UTIL.getAdmin().createTable(td); 150 UTIL.waitTableAvailable(tableName); 151 Table tbl = UTIL.getConnection().getTable(tableName); 152 List<Put> puts = new ArrayList<>(); 153 for (int i = 0; i < 1000; i++) { 154 Put p = new Put(Bytes.toBytes("row-" + i)); 155 p.addColumn(family, Bytes.toBytes(1), Bytes.toBytes("val-" + i)); 156 puts.add(p); 157 } 158 tbl.put(puts); 159 if (shouldFlushTable) { 160 UTIL.getAdmin().flush(tableName); 161 Thread.sleep(5000); 162 } 163 } 164 165 private void checkCacheForBlocks(TableName tableName, Collection<HStoreFile> files, 166 BiConsumer<String, Map<String, Pair<String, Long>>> checker) { 167 files.forEach(f -> { 168 UTIL.getMiniHBaseCluster().getRegionServer(0).getBlockCache().ifPresent(cache -> { 169 cache.getFullyCachedFiles().ifPresent(m -> { 170 checker.accept(f.getPath().getName(), m); 171 }); 172 assertTrue(cache.getFullyCachedFiles().isPresent()); 173 }); 174 }); 175 } 176 177 @Test 178 public void testNoCacheWithoutFlush() throws Exception { 179 UTIL.startMiniCluster(1); 180 try { 181 TableName tableName = TableName.valueOf("tableNoCache"); 182 createTable(tableName, false); 183 checkRegionCached(tableName, false); 184 } finally { 185 UTIL.shutdownMiniCluster(); 186 } 187 } 188 189 @Test 190 public void testCacheWithFlush() throws Exception { 191 UTIL.startMiniCluster(1); 192 try { 193 TableName tableName = TableName.valueOf("tableWithFlush"); 194 createTable(tableName, true); 195 checkRegionCached(tableName, true); 196 } finally { 197 UTIL.shutdownMiniCluster(); 198 } 199 } 200 201 private void checkRegionCached(TableName tableName, boolean isCached) throws IOException { 202 UTIL.getMiniHBaseCluster().getRegions(tableName).forEach(r -> { 203 try { 204 UTIL.getMiniHBaseCluster().getClusterMetrics().getLiveServerMetrics().forEach((sn, sm) -> { 205 for (Map.Entry<byte[], RegionMetrics> rm : sm.getRegionMetrics().entrySet()) { 206 if (rm.getValue().getNameAsString().equals(r.getRegionInfo().getRegionNameAsString())) { 207 assertTrue(isCached == (rm.getValue().getCurrentRegionCachedRatio() > 0.0f)); 208 } 209 } 210 }); 211 } catch (IOException e) { 212 throw new RuntimeException(e); 213 } 214 }); 215 } 216}