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
;
20 import static org
.junit
.Assert
.assertEquals
;
21 import static org
.junit
.Assert
.assertNotNull
;
22 import static org
.junit
.Assert
.assertNull
;
24 import java
.io
.IOException
;
25 import org
.apache
.hadoop
.hbase
.client
.RegionInfo
;
26 import org
.apache
.hadoop
.hbase
.regionserver
.HRegionServer
;
27 import org
.apache
.hadoop
.hbase
.testclassification
.MediumTests
;
28 import org
.apache
.hadoop
.hbase
.testclassification
.MiscTests
;
29 import org
.apache
.hadoop
.hbase
.util
.Bytes
;
30 import org
.apache
.hadoop
.hbase
.zookeeper
.MiniZooKeeperCluster
;
31 import org
.apache
.hbase
.thirdparty
.com
.google
.common
.collect
.Iterables
;
32 import org
.junit
.After
;
33 import org
.junit
.Before
;
34 import org
.junit
.ClassRule
;
35 import org
.junit
.Rule
;
36 import org
.junit
.Test
;
37 import org
.junit
.experimental
.categories
.Category
;
38 import org
.junit
.rules
.TestName
;
41 * Test whether moved region cache is correct
43 @Category({ MiscTests
.class, MediumTests
.class })
44 public class TestMovedRegionCache
{
47 public static final HBaseClassTestRule CLASS_RULE
=
48 HBaseClassTestRule
.forClass(TestMovedRegionCache
.class);
51 public TestName name
= new TestName();
53 private HBaseTestingUtil UTIL
;
54 private MiniZooKeeperCluster zkCluster
;
55 private HRegionServer source
;
56 private HRegionServer dest
;
57 private RegionInfo movedRegionInfo
;
60 public void setup() throws Exception
{
61 UTIL
= new HBaseTestingUtil();
62 zkCluster
= UTIL
.startMiniZKCluster();
63 StartTestingClusterOption option
=
64 StartTestingClusterOption
.builder().numRegionServers(2).build();
65 SingleProcessHBaseCluster cluster
= UTIL
.startMiniHBaseCluster(option
);
66 source
= cluster
.getRegionServer(0);
67 dest
= cluster
.getRegionServer(1);
68 assertEquals(2, cluster
.getRegionServerThreads().size());
69 TableName tableName
= TableName
.valueOf(name
.getMethodName());
70 UTIL
.createTable(tableName
, Bytes
.toBytes("cf"));
71 UTIL
.waitTableAvailable(tableName
, 30_000
);
72 movedRegionInfo
= Iterables
.getOnlyElement(cluster
.getRegions(tableName
)).getRegionInfo();
73 UTIL
.getAdmin().move(movedRegionInfo
.getEncodedNameAsBytes(), source
.getServerName());
74 UTIL
.waitFor(2000, new Waiter
.Predicate
<IOException
>() {
76 public boolean evaluate() throws IOException
{
77 return source
.getOnlineRegion(movedRegionInfo
.getRegionName()) != null;
83 public void after() throws Exception
{
84 UTIL
.shutdownMiniCluster();
85 if (zkCluster
!= null) {
91 public void testMovedRegionsCache() throws IOException
, InterruptedException
{
92 UTIL
.getAdmin().move(movedRegionInfo
.getEncodedNameAsBytes(), dest
.getServerName());
93 UTIL
.waitFor(2000, new Waiter
.Predicate
<IOException
>() {
95 public boolean evaluate() throws IOException
{
96 return dest
.getOnlineRegion(movedRegionInfo
.getRegionName()) != null;
99 assertNotNull("Moved region NOT in the cache!", source
.getMovedRegion(
100 movedRegionInfo
.getEncodedName()));
101 Thread
.sleep(source
.movedRegionCacheExpiredTime());
102 assertNull("Expired moved region exist in the cache!", source
.getMovedRegion(
103 movedRegionInfo
.getEncodedName()));