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.HBaseTestingUtil;
030import org.apache.hadoop.hbase.SingleProcessHBaseCluster;
031import org.apache.hadoop.hbase.StartTestingClusterOption;
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 HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
065
066  private Configuration conf;
067  Path testDir;
068  MiniZooKeeperCluster zkCluster;
069  SingleProcessHBaseCluster cluster;
070  StartTestingClusterOption option =
071    StartTestingClusterOption.builder().numRegionServers(2).build();
072
073  @Before
074  public void setup() throws Exception {
075    conf = TEST_UTIL.getConfiguration();
076    testDir = TEST_UTIL.getDataTestDir();
077    TEST_UTIL.getTestFileSystem().mkdirs(testDir);
078
079    conf.setBoolean(CacheConfig.PREFETCH_BLOCKS_ON_OPEN_KEY, true);
080    conf.set(BUCKET_CACHE_IOENGINE_KEY, "file:" + testDir + "/bucket.cache");
081    conf.setInt("hbase.bucketcache.size", 400);
082    conf.set("hbase.bucketcache.persistent.path", testDir + "/bucket.persistence");
083    conf.setLong(CacheConfig.BUCKETCACHE_PERSIST_INTERVAL_KEY, 100);
084    conf.setBoolean(CacheConfig.EVICT_BLOCKS_ON_CLOSE_KEY, true);
085    zkCluster = TEST_UTIL.startMiniZKCluster();
086    cluster = TEST_UTIL.startMiniHBaseCluster(option);
087    cluster.setConf(conf);
088  }
089
090  @Test
091  public void testBlockEvictionOnRegionMove() throws Exception {
092    // Write to table and flush
093    TableName tableRegionMove = writeDataToTable("testBlockEvictionOnRegionMove");
094
095    HRegionServer regionServingRS =
096      cluster.getRegionServer(1).getRegions(tableRegionMove).size() == 1
097        ? cluster.getRegionServer(1)
098        : cluster.getRegionServer(0);
099    assertTrue(regionServingRS.getBlockCache().isPresent());
100    long oldUsedCacheSize =
101      regionServingRS.getBlockCache().get().getBlockCaches()[1].getCurrentSize();
102    assertNotEquals(0, regionServingRS.getBlockCache().get().getBlockCaches()[1].getBlockCount());
103
104    Admin admin = TEST_UTIL.getAdmin();
105    RegionInfo regionToMove = regionServingRS.getRegions(tableRegionMove).get(0).getRegionInfo();
106    admin.move(regionToMove.getEncodedNameAsBytes(),
107      TEST_UTIL.getOtherRegionServer(regionServingRS).getServerName());
108    assertEquals(0, regionServingRS.getRegions(tableRegionMove).size());
109
110    long newUsedCacheSize =
111      regionServingRS.getBlockCache().get().getBlockCaches()[1].getCurrentSize();
112    assertTrue(oldUsedCacheSize > newUsedCacheSize);
113    assertEquals(0, regionServingRS.getBlockCache().get().getBlockCaches()[1].getBlockCount());
114  }
115
116  @Test
117  public void testBlockEvictionOnGracefulStop() throws Exception {
118    // Write to table and flush
119    TableName tableRegionClose = writeDataToTable("testBlockEvictionOnGracefulStop");
120
121    HRegionServer regionServingRS =
122      cluster.getRegionServer(1).getRegions(tableRegionClose).size() == 1
123        ? cluster.getRegionServer(1)
124        : cluster.getRegionServer(0);
125
126    assertTrue(regionServingRS.getBlockCache().isPresent());
127    long oldUsedCacheSize =
128      regionServingRS.getBlockCache().get().getBlockCaches()[1].getCurrentSize();
129    assertNotEquals(0, regionServingRS.getBlockCache().get().getBlockCaches()[1].getBlockCount());
130
131    cluster.stopRegionServer(regionServingRS.getServerName());
132    Thread.sleep(500);
133    cluster.startRegionServer();
134    Thread.sleep(500);
135
136    long newUsedCacheSize =
137      regionServingRS.getBlockCache().get().getBlockCaches()[1].getCurrentSize();
138    assertEquals(oldUsedCacheSize, newUsedCacheSize);
139    assertNotEquals(0, regionServingRS.getBlockCache().get().getBlockCaches()[1].getBlockCount());
140  }
141
142  public TableName writeDataToTable(String testName) throws IOException, InterruptedException {
143    TableName tableName = TableName.valueOf(testName + EnvironmentEdgeManager.currentTime());
144    byte[] row0 = Bytes.toBytes("row1");
145    byte[] row1 = Bytes.toBytes("row2");
146    byte[] family = Bytes.toBytes("family");
147    byte[] qf1 = Bytes.toBytes("qf1");
148    byte[] qf2 = Bytes.toBytes("qf2");
149    byte[] value1 = Bytes.toBytes("value1");
150    byte[] value2 = Bytes.toBytes("value2");
151
152    TableDescriptor td = TableDescriptorBuilder.newBuilder(tableName)
153      .setColumnFamily(ColumnFamilyDescriptorBuilder.of(family)).build();
154    Table table = TEST_UTIL.createTable(td, null);
155    try {
156      // put data
157      Put put0 = new Put(row0);
158      put0.addColumn(family, qf1, 1, value1);
159      table.put(put0);
160      Put put1 = new Put(row1);
161      put1.addColumn(family, qf2, 1, value2);
162      table.put(put1);
163      TEST_UTIL.flush(tableName);
164    } finally {
165      Thread.sleep(1000);
166    }
167    assertEquals(1, cluster.getRegions(tableName).size());
168    return tableName;
169  }
170
171  @After
172  public void tearDown() throws Exception {
173    TEST_UTIL.shutdownMiniCluster();
174    TEST_UTIL.cleanupDataTestDirOnTestFS(String.valueOf(testDir));
175    if (zkCluster != null) {
176      zkCluster.shutdown();
177    }
178  }
179}