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