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
.master
;
20 import java
.io
.IOException
;
21 import java
.io
.UncheckedIOException
;
22 import java
.util
.concurrent
.CountDownLatch
;
23 import org
.apache
.hadoop
.conf
.Configuration
;
24 import org
.apache
.hadoop
.hbase
.HBaseClassTestRule
;
25 import org
.apache
.hadoop
.hbase
.HBaseTestingUtil
;
26 import org
.apache
.hadoop
.hbase
.HConstants
;
27 import org
.apache
.hadoop
.hbase
.PleaseHoldException
;
28 import org
.apache
.hadoop
.hbase
.StartTestingClusterOption
;
29 import org
.apache
.hadoop
.hbase
.TableName
;
30 import org
.apache
.hadoop
.hbase
.client
.Put
;
31 import org
.apache
.hadoop
.hbase
.client
.RegionInfo
;
32 import org
.apache
.hadoop
.hbase
.client
.Table
;
33 import org
.apache
.hadoop
.hbase
.master
.assignment
.AssignmentManager
;
34 import org
.apache
.hadoop
.hbase
.master
.region
.MasterRegion
;
35 import org
.apache
.hadoop
.hbase
.regionserver
.HRegionServer
;
36 import org
.apache
.hadoop
.hbase
.testclassification
.MasterTests
;
37 import org
.apache
.hadoop
.hbase
.testclassification
.MediumTests
;
38 import org
.apache
.hadoop
.hbase
.util
.Bytes
;
39 import org
.junit
.AfterClass
;
40 import org
.junit
.BeforeClass
;
41 import org
.junit
.ClassRule
;
42 import org
.junit
.Test
;
43 import org
.junit
.experimental
.categories
.Category
;
45 import org
.apache
.hadoop
.hbase
.shaded
.protobuf
.generated
.RegionServerStatusProtos
.RegionStateTransition
.TransitionCode
;
46 import org
.apache
.hadoop
.hbase
.shaded
.protobuf
.generated
.RegionServerStatusProtos
.ReportRegionStateTransitionRequest
;
47 import org
.apache
.hadoop
.hbase
.shaded
.protobuf
.generated
.RegionServerStatusProtos
.ReportRegionStateTransitionResponse
;
49 @Category({ MasterTests
.class, MediumTests
.class })
50 public class TestCloseAnOpeningRegion
{
53 public static final HBaseClassTestRule CLASS_RULE
=
54 HBaseClassTestRule
.forClass(TestCloseAnOpeningRegion
.class);
56 private static final HBaseTestingUtil UTIL
= new HBaseTestingUtil();
58 private static TableName TABLE_NAME
= TableName
.valueOf("race");
60 private static byte[] CF
= Bytes
.toBytes("cf");
62 private static volatile CountDownLatch ARRIVE
;
64 private static volatile CountDownLatch RESUME
;
66 public static final class MockHMaster
extends HMaster
{
68 public MockHMaster(Configuration conf
) throws IOException
{
73 protected AssignmentManager
createAssignmentManager(MasterServices master
,
74 MasterRegion masterRegion
) {
75 return new AssignmentManager(master
, masterRegion
) {
78 public ReportRegionStateTransitionResponse
reportRegionStateTransition(
79 ReportRegionStateTransitionRequest req
) throws PleaseHoldException
{
80 ReportRegionStateTransitionResponse resp
= super.reportRegionStateTransition(req
);
81 TransitionCode code
= req
.getTransition(0).getTransitionCode();
82 if (code
== TransitionCode
.OPENED
&& ARRIVE
!= null) {
86 } catch (InterruptedException e
) {
87 throw new RuntimeException(e
);
97 public static void setUp() throws Exception
{
98 UTIL
.getConfiguration().setInt(HConstants
.HBASE_RPC_SHORTOPERATION_TIMEOUT_KEY
, 60000);
99 UTIL
.startMiniCluster(StartTestingClusterOption
.builder().numRegionServers(2)
100 .masterClass(MockHMaster
.class).build());
101 UTIL
.createTable(TABLE_NAME
, CF
);
102 UTIL
.getAdmin().balancerSwitch(false, true);
106 public static void tearDown() throws Exception
{
107 UTIL
.shutdownMiniCluster();
111 public void test() throws IOException
, InterruptedException
{
112 ARRIVE
= new CountDownLatch(1);
113 RESUME
= new CountDownLatch(1);
114 RegionInfo region
= UTIL
.getAdmin().getRegions(TABLE_NAME
).get(0);
115 HRegionServer src
= UTIL
.getRSForFirstRegionInTable(TABLE_NAME
);
116 HRegionServer dst
= UTIL
.getOtherRegionServer(src
);
117 Thread move0
= new Thread(() -> {
119 UTIL
.getAdmin().move(region
.getEncodedNameAsBytes(), dst
.getServerName());
120 } catch (IOException e
) {
121 throw new UncheckedIOException(e
);
126 Thread move1
= new Thread(() -> {
128 UTIL
.getAdmin().move(region
.getEncodedNameAsBytes(), src
.getServerName());
129 } catch (IOException e
) {
130 throw new UncheckedIOException(e
);
134 // No simple way to determine when it is safe to go on and produce the race so let's sleep for a
140 try (Table table
= UTIL
.getConnection().getTable(TABLE_NAME
)) {
141 // make sure that we can write to the table, which means the region is online
142 table
.put(new Put(Bytes
.toBytes(0)).addColumn(CF
, Bytes
.toBytes("cq"), Bytes
.toBytes(0)));