HBASE-24033 Add ut for loading the corrupt recovered hfiles (#1322)
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / client / TestRegionLocationCaching.java
blob276023c9b2d39fb08dde60ab5a7aceee251d7d4b
1 /**
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.client;
20 import static org.junit.Assert.assertNotEquals;
21 import static org.junit.Assert.assertTrue;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.List;
26 import org.apache.hadoop.hbase.HBaseClassTestRule;
27 import org.apache.hadoop.hbase.HBaseTestingUtility;
28 import org.apache.hadoop.hbase.TableName;
29 import org.apache.hadoop.hbase.testclassification.ClientTests;
30 import org.apache.hadoop.hbase.testclassification.MediumTests;
31 import org.apache.hadoop.hbase.util.Bytes;
32 import org.junit.AfterClass;
33 import org.junit.BeforeClass;
34 import org.junit.ClassRule;
35 import org.junit.Test;
36 import org.junit.experimental.categories.Category;
38 @Category({MediumTests.class, ClientTests.class})
39 public class TestRegionLocationCaching {
41 @ClassRule
42 public static final HBaseClassTestRule CLASS_RULE =
43 HBaseClassTestRule.forClass(TestRegionLocationCaching.class);
45 private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
46 private static int SLAVES = 1;
47 private static TableName TABLE_NAME = TableName.valueOf("TestRegionLocationCaching");
48 private static byte[] FAMILY = Bytes.toBytes("testFamily");
49 private static byte[] QUALIFIER = Bytes.toBytes("testQualifier");
51 @BeforeClass
52 public static void setUpBeforeClass() throws Exception {
53 TEST_UTIL.startMiniCluster(SLAVES);
54 TEST_UTIL.createTable(TABLE_NAME, new byte[][] { FAMILY });
55 TEST_UTIL.waitUntilAllRegionsAssigned(TABLE_NAME);
58 @AfterClass
59 public static void tearDownAfterClass() throws Exception {
60 TEST_UTIL.shutdownMiniCluster();
63 @Test
64 public void testCachingForHTableSinglePut() throws Exception {
65 byte[] row = Bytes.toBytes("htable_single_put");
66 byte[] value = Bytes.toBytes("value");
68 Put put = new Put(row);
69 put.addColumn(FAMILY, QUALIFIER, value);
71 try (Table table = TEST_UTIL.getConnection().getTable(TABLE_NAME)) {
72 table.put(put);
75 checkRegionLocationIsCached(TABLE_NAME, TEST_UTIL.getConnection());
76 checkExistence(TABLE_NAME, row, FAMILY, QUALIFIER);
79 @Test
80 public void testCachingForHTableMultiPut() throws Exception {
81 List<Put> multiput = new ArrayList<Put>();
82 for (int i = 0; i < 10; i++) {
83 Put put = new Put(Bytes.toBytes("htable_multi_put" + i));
84 byte[] value = Bytes.toBytes("value_" + i);
85 put.addColumn(FAMILY, QUALIFIER, value);
86 multiput.add(put);
89 try (Table table = TEST_UTIL.getConnection().getTable(TABLE_NAME)) {
90 table.put(multiput);
92 checkRegionLocationIsCached(TABLE_NAME, TEST_UTIL.getConnection());
93 for (int i = 0; i < 10; i++) {
94 checkExistence(TABLE_NAME, Bytes.toBytes("htable_multi_put" + i), FAMILY, QUALIFIER);
98 /**
99 * Method to check whether the cached region location is non-empty for the given table. It repeats
100 * the same check several times as clearing of cache by some async operations may not reflect
101 * immediately.
103 private void checkRegionLocationIsCached(final TableName tableName, final Connection conn)
104 throws InterruptedException, IOException {
105 for (int count = 0; count < 50; count++) {
106 int number = ((AsyncConnectionImpl) conn.toAsyncConnection()).getLocator()
107 .getNumberOfCachedRegionLocations(tableName);
108 assertNotEquals("Expected non-zero number of cached region locations", 0, number);
109 Thread.sleep(100);
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
119 Result r;
120 Get get = new Get(row);
121 get.addColumn(family, qualifier);
122 int nbTry = 0;
123 try (Table table = TEST_UTIL.getConnection().getTable(tableName)) {
124 do {
125 assertTrue("Failed to get row after " + nbTry + " tries", nbTry < 50);
126 nbTry++;
127 Thread.sleep(100);
128 r = table.get(get);
129 } while (r == null || r.getValue(family, qualifier) == null);