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