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.junit.Assert.assertEquals; 022import static org.junit.Assert.assertNotEquals; 023import static org.junit.Assert.assertTrue; 024 025import java.io.IOException; 026import org.apache.hadoop.conf.Configuration; 027import org.apache.hadoop.fs.Path; 028import org.apache.hadoop.hbase.HBaseClassTestRule; 029import org.apache.hadoop.hbase.HBaseTestingUtility; 030import org.apache.hadoop.hbase.MiniHBaseCluster; 031import org.apache.hadoop.hbase.StartMiniClusterOption; 032import org.apache.hadoop.hbase.TableName; 033import org.apache.hadoop.hbase.client.Admin; 034import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; 035import org.apache.hadoop.hbase.client.Put; 036import org.apache.hadoop.hbase.client.RegionInfo; 037import org.apache.hadoop.hbase.client.Table; 038import org.apache.hadoop.hbase.client.TableDescriptor; 039import org.apache.hadoop.hbase.client.TableDescriptorBuilder; 040import org.apache.hadoop.hbase.regionserver.HRegionServer; 041import org.apache.hadoop.hbase.testclassification.IOTests; 042import org.apache.hadoop.hbase.testclassification.MediumTests; 043import org.apache.hadoop.hbase.util.Bytes; 044import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; 045import org.apache.hadoop.hbase.zookeeper.MiniZooKeeperCluster; 046import org.junit.After; 047import org.junit.Before; 048import org.junit.ClassRule; 049import org.junit.Test; 050import org.junit.experimental.categories.Category; 051import org.slf4j.Logger; 052import org.slf4j.LoggerFactory; 053 054@Category({ IOTests.class, MediumTests.class }) 055public class TestBlockEvictionOnRegionMovement { 056 057 @ClassRule 058 public static final HBaseClassTestRule CLASS_RULE = 059 HBaseClassTestRule.forClass(TestBlockEvictionOnRegionMovement.class); 060 061 private static final Logger LOG = 062 LoggerFactory.getLogger(TestBlockEvictionOnRegionMovement.class); 063 064 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); 065 066 private Configuration conf; 067 Path testDir; 068 MiniZooKeeperCluster zkCluster; 069 MiniHBaseCluster cluster; 070 StartMiniClusterOption option = StartMiniClusterOption.builder().numRegionServers(2).build(); 071 072 @Before 073 public void setup() throws Exception { 074 conf = TEST_UTIL.getConfiguration(); 075 testDir = TEST_UTIL.getDataTestDir(); 076 TEST_UTIL.getTestFileSystem().mkdirs(testDir); 077 078 conf.setBoolean(CacheConfig.PREFETCH_BLOCKS_ON_OPEN_KEY, true); 079 conf.set(BUCKET_CACHE_IOENGINE_KEY, "file:" + testDir + "/bucket.cache"); 080 conf.setInt("hbase.bucketcache.size", 400); 081 conf.set("hbase.bucketcache.persistent.path", testDir + "/bucket.persistence"); 082 conf.setLong(CacheConfig.BUCKETCACHE_PERSIST_INTERVAL_KEY, 100); 083 conf.setBoolean(CacheConfig.EVICT_BLOCKS_ON_CLOSE_KEY, true); 084 zkCluster = TEST_UTIL.startMiniZKCluster(); 085 cluster = TEST_UTIL.startMiniHBaseCluster(option); 086 cluster.setConf(conf); 087 } 088 089 @Test 090 public void testBlockEvictionOnRegionMove() throws Exception { 091 // Write to table and flush 092 TableName tableRegionMove = writeDataToTable("testBlockEvictionOnRegionMove"); 093 094 HRegionServer regionServingRS = 095 cluster.getRegionServer(1).getRegions(tableRegionMove).size() == 1 096 ? cluster.getRegionServer(1) 097 : cluster.getRegionServer(0); 098 assertTrue(regionServingRS.getBlockCache().isPresent()); 099 long oldUsedCacheSize = 100 regionServingRS.getBlockCache().get().getBlockCaches()[1].getCurrentSize(); 101 assertNotEquals(0, regionServingRS.getBlockCache().get().getBlockCaches()[1].getBlockCount()); 102 103 Admin admin = TEST_UTIL.getAdmin(); 104 RegionInfo regionToMove = regionServingRS.getRegions(tableRegionMove).get(0).getRegionInfo(); 105 admin.move(regionToMove.getEncodedNameAsBytes(), 106 TEST_UTIL.getOtherRegionServer(regionServingRS).getServerName()); 107 assertEquals(0, regionServingRS.getRegions(tableRegionMove).size()); 108 109 long newUsedCacheSize = 110 regionServingRS.getBlockCache().get().getBlockCaches()[1].getCurrentSize(); 111 assertTrue(oldUsedCacheSize > newUsedCacheSize); 112 assertEquals(0, regionServingRS.getBlockCache().get().getBlockCaches()[1].getBlockCount()); 113 } 114 115 @Test 116 public void testBlockEvictionOnGracefulStop() throws Exception { 117 // Write to table and flush 118 TableName tableRegionClose = writeDataToTable("testBlockEvictionOnGracefulStop"); 119 120 HRegionServer regionServingRS = 121 cluster.getRegionServer(1).getRegions(tableRegionClose).size() == 1 122 ? cluster.getRegionServer(1) 123 : cluster.getRegionServer(0); 124 125 assertTrue(regionServingRS.getBlockCache().isPresent()); 126 long oldUsedCacheSize = 127 regionServingRS.getBlockCache().get().getBlockCaches()[1].getCurrentSize(); 128 assertNotEquals(0, regionServingRS.getBlockCache().get().getBlockCaches()[1].getBlockCount()); 129 130 cluster.stopRegionServer(regionServingRS.getServerName()); 131 Thread.sleep(500); 132 cluster.startRegionServer(); 133 Thread.sleep(500); 134 135 long newUsedCacheSize = 136 regionServingRS.getBlockCache().get().getBlockCaches()[1].getCurrentSize(); 137 assertEquals(oldUsedCacheSize, newUsedCacheSize); 138 assertNotEquals(0, regionServingRS.getBlockCache().get().getBlockCaches()[1].getBlockCount()); 139 } 140 141 public TableName writeDataToTable(String testName) throws IOException, InterruptedException { 142 TableName tableName = TableName.valueOf(testName + EnvironmentEdgeManager.currentTime()); 143 byte[] row0 = Bytes.toBytes("row1"); 144 byte[] row1 = Bytes.toBytes("row2"); 145 byte[] family = Bytes.toBytes("family"); 146 byte[] qf1 = Bytes.toBytes("qf1"); 147 byte[] qf2 = Bytes.toBytes("qf2"); 148 byte[] value1 = Bytes.toBytes("value1"); 149 byte[] value2 = Bytes.toBytes("value2"); 150 151 TableDescriptor td = TableDescriptorBuilder.newBuilder(tableName) 152 .setColumnFamily(ColumnFamilyDescriptorBuilder.of(family)).build(); 153 Table table = TEST_UTIL.createTable(td, null); 154 try { 155 // put data 156 Put put0 = new Put(row0); 157 put0.addColumn(family, qf1, 1, value1); 158 table.put(put0); 159 Put put1 = new Put(row1); 160 put1.addColumn(family, qf2, 1, value2); 161 table.put(put1); 162 TEST_UTIL.flush(tableName); 163 } finally { 164 Thread.sleep(1000); 165 } 166 assertEquals(1, cluster.getRegions(tableName).size()); 167 return tableName; 168 } 169 170 @After 171 public void tearDown() throws Exception { 172 TEST_UTIL.shutdownMiniCluster(); 173 TEST_UTIL.cleanupDataTestDirOnTestFS(String.valueOf(testDir)); 174 if (zkCluster != null) { 175 zkCluster.shutdown(); 176 } 177 } 178}