2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org
.apache
.hadoop
.hbase
.regionserver
;
20 import static org
.junit
.Assert
.assertEquals
;
22 import java
.io
.IOException
;
23 import java
.util
.ArrayList
;
24 import org
.apache
.hadoop
.conf
.Configuration
;
25 import org
.apache
.hadoop
.hbase
.CacheEvictionStats
;
26 import org
.apache
.hadoop
.hbase
.Cell
;
27 import org
.apache
.hadoop
.hbase
.HBaseClassTestRule
;
28 import org
.apache
.hadoop
.hbase
.HBaseTestingUtility
;
29 import org
.apache
.hadoop
.hbase
.HConstants
;
30 import org
.apache
.hadoop
.hbase
.MiniHBaseCluster
;
31 import org
.apache
.hadoop
.hbase
.TableName
;
32 import org
.apache
.hadoop
.hbase
.client
.Admin
;
33 import org
.apache
.hadoop
.hbase
.client
.AsyncAdmin
;
34 import org
.apache
.hadoop
.hbase
.client
.ConnectionFactory
;
35 import org
.apache
.hadoop
.hbase
.client
.Scan
;
36 import org
.apache
.hadoop
.hbase
.client
.Table
;
37 import org
.apache
.hadoop
.hbase
.io
.hfile
.BlockCache
;
38 import org
.apache
.hadoop
.hbase
.testclassification
.LargeTests
;
39 import org
.apache
.hadoop
.hbase
.util
.Bytes
;
40 import org
.junit
.After
;
41 import org
.junit
.Before
;
42 import org
.junit
.ClassRule
;
43 import org
.junit
.Test
;
44 import org
.junit
.experimental
.categories
.Category
;
45 import org
.junit
.runner
.RunWith
;
46 import org
.junit
.runners
.Parameterized
;
47 import org
.slf4j
.Logger
;
48 import org
.slf4j
.LoggerFactory
;
50 @Category(LargeTests
.class)
51 @RunWith(Parameterized
.class)
52 public class TestClearRegionBlockCache
{
55 public static final HBaseClassTestRule CLASS_RULE
=
56 HBaseClassTestRule
.forClass(TestClearRegionBlockCache
.class);
58 private static final Logger LOG
= LoggerFactory
.getLogger(TestClearRegionBlockCache
.class);
59 private static final TableName TABLE_NAME
= TableName
.valueOf("testClearRegionBlockCache");
60 private static final byte[] FAMILY
= Bytes
.toBytes("family");
61 private static final byte[][] SPLIT_KEY
= new byte[][] { Bytes
.toBytes("5") };
62 private static final int NUM_RS
= 2;
64 private final HBaseTestingUtility HTU
= new HBaseTestingUtility();
66 private Configuration CONF
= HTU
.getConfiguration();
68 private HRegionServer rs1
, rs2
;
69 private MiniHBaseCluster cluster
;
71 @Parameterized.Parameter
public String cacheType
;
73 @Parameterized.Parameters(name
= "{index}: {0}")
74 public static Object
[] data() {
75 return new Object
[] { "lru", "bucket" };
79 public void setup() throws Exception
{
80 if (cacheType
.equals("bucket")) {
81 CONF
.set(HConstants
.BUCKET_CACHE_IOENGINE_KEY
, "offheap");
82 CONF
.setInt(HConstants
.BUCKET_CACHE_SIZE_KEY
, 30);
85 cluster
= HTU
.startMiniCluster(NUM_RS
);
86 rs1
= cluster
.getRegionServer(0);
87 rs2
= cluster
.getRegionServer(1);
90 table
= HTU
.createTable(TABLE_NAME
, FAMILY
, SPLIT_KEY
);
92 HTU
.loadNumericRows(table
, FAMILY
, 1, 10);
93 HTU
.flush(TABLE_NAME
);
97 public void teardown() throws Exception
{
98 HTU
.shutdownMiniCluster();
102 public void testClearBlockCache() throws Exception
{
103 BlockCache blockCache1
= rs1
.getBlockCache().get();
104 BlockCache blockCache2
= rs2
.getBlockCache().get();
106 long initialBlockCount1
= blockCache1
.getBlockCount();
107 long initialBlockCount2
= blockCache2
.getBlockCount();
109 // scan will cause blocks to be added in BlockCache
110 scanAllRegionsForRS(rs1
);
111 assertEquals(blockCache1
.getBlockCount() - initialBlockCount1
,
112 HTU
.getNumHFilesForRS(rs1
, TABLE_NAME
, FAMILY
));
113 clearRegionBlockCache(rs1
);
115 scanAllRegionsForRS(rs2
);
116 assertEquals(blockCache2
.getBlockCount() - initialBlockCount2
,
117 HTU
.getNumHFilesForRS(rs2
, TABLE_NAME
, FAMILY
));
118 clearRegionBlockCache(rs2
);
120 assertEquals(initialBlockCount1
, blockCache1
.getBlockCount());
121 assertEquals(initialBlockCount2
, blockCache2
.getBlockCount());
125 public void testClearBlockCacheFromAdmin() throws Exception
{
126 Admin admin
= HTU
.getAdmin();
128 BlockCache blockCache1
= rs1
.getBlockCache().get();
129 BlockCache blockCache2
= rs2
.getBlockCache().get();
130 long initialBlockCount1
= blockCache1
.getBlockCount();
131 long initialBlockCount2
= blockCache2
.getBlockCount();
133 // scan will cause blocks to be added in BlockCache
134 scanAllRegionsForRS(rs1
);
135 assertEquals(blockCache1
.getBlockCount() - initialBlockCount1
,
136 HTU
.getNumHFilesForRS(rs1
, TABLE_NAME
, FAMILY
));
137 scanAllRegionsForRS(rs2
);
138 assertEquals(blockCache2
.getBlockCount() - initialBlockCount2
,
139 HTU
.getNumHFilesForRS(rs2
, TABLE_NAME
, FAMILY
));
141 CacheEvictionStats stats
= admin
.clearBlockCache(TABLE_NAME
);
142 assertEquals(stats
.getEvictedBlocks(), HTU
.getNumHFilesForRS(rs1
, TABLE_NAME
, FAMILY
)
143 + HTU
.getNumHFilesForRS(rs2
, TABLE_NAME
, FAMILY
));
144 assertEquals(initialBlockCount1
, blockCache1
.getBlockCount());
145 assertEquals(initialBlockCount2
, blockCache2
.getBlockCount());
149 public void testClearBlockCacheFromAsyncAdmin() throws Exception
{
151 ConnectionFactory
.createAsyncConnection(HTU
.getConfiguration()).get().getAdmin();
153 BlockCache blockCache1
= rs1
.getBlockCache().get();
154 BlockCache blockCache2
= rs2
.getBlockCache().get();
155 long initialBlockCount1
= blockCache1
.getBlockCount();
156 long initialBlockCount2
= blockCache2
.getBlockCount();
158 // scan will cause blocks to be added in BlockCache
159 scanAllRegionsForRS(rs1
);
160 assertEquals(blockCache1
.getBlockCount() - initialBlockCount1
,
161 HTU
.getNumHFilesForRS(rs1
, TABLE_NAME
, FAMILY
));
162 scanAllRegionsForRS(rs2
);
163 assertEquals(blockCache2
.getBlockCount() - initialBlockCount2
,
164 HTU
.getNumHFilesForRS(rs2
, TABLE_NAME
, FAMILY
));
166 CacheEvictionStats stats
= admin
.clearBlockCache(TABLE_NAME
).get();
167 assertEquals(stats
.getEvictedBlocks(), HTU
.getNumHFilesForRS(rs1
, TABLE_NAME
, FAMILY
) + HTU
168 .getNumHFilesForRS(rs2
, TABLE_NAME
, FAMILY
));
169 assertEquals(initialBlockCount1
, blockCache1
.getBlockCount());
170 assertEquals(initialBlockCount2
, blockCache2
.getBlockCount());
173 private void scanAllRegionsForRS(HRegionServer rs
) throws IOException
{
174 for (Region region
: rs
.getRegions(TABLE_NAME
)) {
175 RegionScanner scanner
= region
.getScanner(new Scan());
176 while (scanner
.next(new ArrayList
<Cell
>()));
180 private void clearRegionBlockCache(HRegionServer rs
) {
181 for (Region region
: rs
.getRegions(TABLE_NAME
)) {
182 rs
.clearRegionBlockCache(region
);