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.
19 package org
.apache
.hadoop
.hbase
.client
;
21 import static org
.junit
.Assert
.assertNotEquals
;
22 import static org
.junit
.Assert
.assertTrue
;
24 import java
.io
.IOException
;
25 import java
.util
.ArrayList
;
26 import java
.util
.List
;
27 import org
.apache
.hadoop
.hbase
.HBaseClassTestRule
;
28 import org
.apache
.hadoop
.hbase
.HBaseTestingUtility
;
29 import org
.apache
.hadoop
.hbase
.TableName
;
30 import org
.apache
.hadoop
.hbase
.testclassification
.ClientTests
;
31 import org
.apache
.hadoop
.hbase
.testclassification
.MediumTests
;
32 import org
.apache
.hadoop
.hbase
.util
.Bytes
;
33 import org
.junit
.AfterClass
;
34 import org
.junit
.BeforeClass
;
35 import org
.junit
.ClassRule
;
36 import org
.junit
.Test
;
37 import org
.junit
.experimental
.categories
.Category
;
39 @Category({MediumTests
.class, ClientTests
.class})
40 public class TestRegionLocationCaching
{
43 public static final HBaseClassTestRule CLASS_RULE
=
44 HBaseClassTestRule
.forClass(TestRegionLocationCaching
.class);
46 private final static HBaseTestingUtility TEST_UTIL
= new HBaseTestingUtility();
47 private static int SLAVES
= 1;
48 private static TableName TABLE_NAME
= TableName
.valueOf("TestRegionLocationCaching");
49 private static byte[] FAMILY
= Bytes
.toBytes("testFamily");
50 private static byte[] QUALIFIER
= Bytes
.toBytes("testQualifier");
53 public static void setUpBeforeClass() throws Exception
{
54 TEST_UTIL
.startMiniCluster(SLAVES
);
55 TEST_UTIL
.createTable(TABLE_NAME
, new byte[][] { FAMILY
});
56 TEST_UTIL
.waitUntilAllRegionsAssigned(TABLE_NAME
);
60 public static void tearDownAfterClass() throws Exception
{
61 TEST_UTIL
.shutdownMiniCluster();
65 public void testCachingForHTableSinglePut() throws Exception
{
66 byte[] row
= Bytes
.toBytes("htable_single_put");
67 byte[] value
= Bytes
.toBytes("value");
69 Put put
= new Put(row
);
70 put
.addColumn(FAMILY
, QUALIFIER
, value
);
72 try (Table table
= TEST_UTIL
.getConnection().getTable(TABLE_NAME
)) {
76 checkRegionLocationIsCached(TABLE_NAME
, TEST_UTIL
.getConnection());
77 checkExistence(TABLE_NAME
, row
, FAMILY
, QUALIFIER
);
81 public void testCachingForHTableMultiPut() throws Exception
{
82 List
<Put
> multiput
= new ArrayList
<Put
>();
83 for (int i
= 0; i
< 10; i
++) {
84 Put put
= new Put(Bytes
.toBytes("htable_multi_put" + i
));
85 byte[] value
= Bytes
.toBytes("value_" + i
);
86 put
.addColumn(FAMILY
, QUALIFIER
, value
);
90 try (Table table
= TEST_UTIL
.getConnection().getTable(TABLE_NAME
)) {
93 checkRegionLocationIsCached(TABLE_NAME
, TEST_UTIL
.getConnection());
94 for (int i
= 0; i
< 10; i
++) {
95 checkExistence(TABLE_NAME
, Bytes
.toBytes("htable_multi_put" + i
), FAMILY
, QUALIFIER
);
100 * Method to check whether the cached region location is non-empty for the given table. It repeats
101 * the same check several times as clearing of cache by some async operations may not reflect
104 private void checkRegionLocationIsCached(final TableName tableName
, final Connection conn
)
105 throws InterruptedException
, IOException
{
106 for (int count
= 0; count
< 50; count
++) {
107 int number
= ((ConnectionImplementation
) conn
).getNumberOfCachedRegionLocations(tableName
);
108 assertNotEquals("Expected non-zero number of cached region locations", 0, number
);
114 * Method to check whether the passed row exists in the given table
116 private static void checkExistence(final TableName tableName
, final byte[] row
,
117 final byte[] family
, final byte[] qualifier
) throws Exception
{
118 // verify that the row exists
120 Get get
= new Get(row
);
121 get
.addColumn(family
, qualifier
);
123 try (Table table
= TEST_UTIL
.getConnection().getTable(tableName
)) {
125 assertTrue("Failed to get row after " + nbTry
+ " tries", nbTry
< 50);
129 } while (r
== null || r
.getValue(family
, qualifier
) == null);