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.regionserver;
019
020import static org.junit.Assert.assertEquals;
021
022import java.io.IOException;
023import java.util.ArrayList;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.hbase.CacheEvictionStats;
026import org.apache.hadoop.hbase.Cell;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.HBaseTestingUtility;
029import org.apache.hadoop.hbase.HConstants;
030import org.apache.hadoop.hbase.MiniHBaseCluster;
031import org.apache.hadoop.hbase.TableName;
032import org.apache.hadoop.hbase.client.Admin;
033import org.apache.hadoop.hbase.client.AsyncAdmin;
034import org.apache.hadoop.hbase.client.ConnectionFactory;
035import org.apache.hadoop.hbase.client.Scan;
036import org.apache.hadoop.hbase.client.Table;
037import org.apache.hadoop.hbase.io.hfile.BlockCache;
038import org.apache.hadoop.hbase.testclassification.MediumTests;
039import org.apache.hadoop.hbase.util.Bytes;
040import org.junit.After;
041import org.junit.Before;
042import org.junit.ClassRule;
043import org.junit.Test;
044import org.junit.experimental.categories.Category;
045import org.junit.runner.RunWith;
046import org.junit.runners.Parameterized;
047import org.slf4j.Logger;
048import org.slf4j.LoggerFactory;
049
050@Category(MediumTests.class)
051@RunWith(Parameterized.class)
052public class TestClearRegionBlockCache {
053
054  @ClassRule
055  public static final HBaseClassTestRule CLASS_RULE =
056      HBaseClassTestRule.forClass(TestClearRegionBlockCache.class);
057
058  private static final Logger LOG = LoggerFactory.getLogger(TestClearRegionBlockCache.class);
059  private static final TableName TABLE_NAME = TableName.valueOf("testClearRegionBlockCache");
060  private static final byte[] FAMILY = Bytes.toBytes("family");
061  private static final byte[][] SPLIT_KEY = new byte[][] { Bytes.toBytes("5") };
062  private static final int NUM_MASTERS = 1;
063  private static final int NUM_RS = 2;
064
065  private final HBaseTestingUtility HTU = new HBaseTestingUtility();
066
067  private Configuration CONF = HTU.getConfiguration();
068  private Table table;
069  private HRegionServer rs1, rs2;
070  private MiniHBaseCluster cluster;
071
072  @Parameterized.Parameter public String cacheType;
073
074  @Parameterized.Parameters(name = "{index}: {0}")
075  public static Object[] data() {
076    return new Object[] { "lru", "bucket" };
077  }
078
079  @Before
080  public void setup() throws Exception {
081    if (cacheType.equals("bucket")) {
082      CONF.set(HConstants.BUCKET_CACHE_IOENGINE_KEY, "offheap");
083      CONF.setInt(HConstants.BUCKET_CACHE_SIZE_KEY, 30);
084    }
085
086    cluster = HTU.startMiniCluster(NUM_MASTERS, NUM_RS);
087    rs1 = cluster.getRegionServer(0);
088    rs2 = cluster.getRegionServer(1);
089
090    // Create table
091    table = HTU.createTable(TABLE_NAME, FAMILY, SPLIT_KEY);
092
093    HTU.loadNumericRows(table, FAMILY, 1, 10);
094    HTU.flush(TABLE_NAME);
095  }
096
097  @After
098  public void teardown() throws Exception {
099    HTU.shutdownMiniCluster();
100  }
101
102  @Test
103  public void testClearBlockCache() throws Exception {
104    BlockCache blockCache1 = rs1.getCacheConfig().getBlockCache();
105    BlockCache blockCache2 = rs2.getCacheConfig().getBlockCache();
106
107    long initialBlockCount1 = blockCache1.getBlockCount();
108    long initialBlockCount2 = blockCache2.getBlockCount();
109
110    // scan will cause blocks to be added in BlockCache
111    scanAllRegionsForRS(rs1);
112    assertEquals(blockCache1.getBlockCount() - initialBlockCount1,
113      HTU.getNumHFilesForRS(rs1, TABLE_NAME, FAMILY));
114    clearRegionBlockCache(rs1);
115
116    scanAllRegionsForRS(rs2);
117    assertEquals(blockCache2.getBlockCount() - initialBlockCount2,
118      HTU.getNumHFilesForRS(rs2, TABLE_NAME, FAMILY));
119    clearRegionBlockCache(rs2);
120
121    assertEquals(initialBlockCount1, blockCache1.getBlockCount());
122    assertEquals(initialBlockCount2, blockCache2.getBlockCount());
123  }
124
125  @Test
126  public void testClearBlockCacheFromAdmin() throws Exception {
127    Admin admin = HTU.getAdmin();
128
129    // All RS run in a same process, so the block cache is same for rs1 and rs2
130    BlockCache blockCache = rs1.getCacheConfig().getBlockCache();
131    long initialBlockCount = blockCache.getBlockCount();
132
133    // scan will cause blocks to be added in BlockCache
134    scanAllRegionsForRS(rs1);
135    assertEquals(blockCache.getBlockCount() - initialBlockCount,
136      HTU.getNumHFilesForRS(rs1, TABLE_NAME, FAMILY));
137    scanAllRegionsForRS(rs2);
138    assertEquals(blockCache.getBlockCount() - initialBlockCount,
139      HTU.getNumHFilesForRS(rs1, TABLE_NAME, FAMILY)
140          + HTU.getNumHFilesForRS(rs2, TABLE_NAME, FAMILY));
141
142    CacheEvictionStats stats = admin.clearBlockCache(TABLE_NAME);
143    assertEquals(stats.getEvictedBlocks(), HTU.getNumHFilesForRS(rs1, TABLE_NAME, FAMILY)
144        + HTU.getNumHFilesForRS(rs2, TABLE_NAME, FAMILY));
145    assertEquals(initialBlockCount, blockCache.getBlockCount());
146  }
147
148  @Test
149  public void testClearBlockCacheFromAsyncAdmin() throws Exception {
150    AsyncAdmin admin =
151        ConnectionFactory.createAsyncConnection(HTU.getConfiguration()).get().getAdmin();
152
153    // All RS run in a same process, so the block cache is same for rs1 and rs2
154    BlockCache blockCache = rs1.getCacheConfig().getBlockCache();
155    long initialBlockCount = blockCache.getBlockCount();
156
157    // scan will cause blocks to be added in BlockCache
158    scanAllRegionsForRS(rs1);
159    assertEquals(blockCache.getBlockCount() - initialBlockCount,
160      HTU.getNumHFilesForRS(rs1, TABLE_NAME, FAMILY));
161    scanAllRegionsForRS(rs2);
162    assertEquals(blockCache.getBlockCount() - initialBlockCount,
163      HTU.getNumHFilesForRS(rs1, TABLE_NAME, FAMILY)
164          + HTU.getNumHFilesForRS(rs2, TABLE_NAME, FAMILY));
165
166    CacheEvictionStats stats = admin.clearBlockCache(TABLE_NAME).get();
167    assertEquals(stats.getEvictedBlocks(), HTU.getNumHFilesForRS(rs1, TABLE_NAME, FAMILY)
168        + HTU.getNumHFilesForRS(rs2, TABLE_NAME, FAMILY));
169    assertEquals(initialBlockCount, blockCache.getBlockCount());
170  }
171
172  private void scanAllRegionsForRS(HRegionServer rs) throws IOException {
173    for (Region region : rs.getRegions(TABLE_NAME)) {
174      RegionScanner scanner = region.getScanner(new Scan());
175      while (scanner.next(new ArrayList<Cell>()));
176    }
177  }
178
179  private void clearRegionBlockCache(HRegionServer rs) {
180    for (Region region : rs.getRegions(TABLE_NAME)) {
181      rs.clearRegionBlockCache(region);
182    }
183  }
184}