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
.HBaseTestingUtility
;
26 import org
.apache
.hadoop
.hbase
.HConstants
;
27 import org
.apache
.hadoop
.hbase
.PleaseHoldException
;
28 import org
.apache
.hadoop
.hbase
.StartMiniClusterOption
;
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
.regionserver
.HRegionServer
;
35 import org
.apache
.hadoop
.hbase
.testclassification
.MasterTests
;
36 import org
.apache
.hadoop
.hbase
.testclassification
.MediumTests
;
37 import org
.apache
.hadoop
.hbase
.util
.Bytes
;
38 import org
.junit
.AfterClass
;
39 import org
.junit
.BeforeClass
;
40 import org
.junit
.ClassRule
;
41 import org
.junit
.Test
;
42 import org
.junit
.experimental
.categories
.Category
;
44 import org
.apache
.hadoop
.hbase
.shaded
.protobuf
.generated
.RegionServerStatusProtos
.RegionStateTransition
.TransitionCode
;
45 import org
.apache
.hadoop
.hbase
.shaded
.protobuf
.generated
.RegionServerStatusProtos
.ReportRegionStateTransitionRequest
;
46 import org
.apache
.hadoop
.hbase
.shaded
.protobuf
.generated
.RegionServerStatusProtos
.ReportRegionStateTransitionResponse
;
48 @Category({ MasterTests
.class, MediumTests
.class })
49 public class TestCloseAnOpeningRegion
{
52 public static final HBaseClassTestRule CLASS_RULE
=
53 HBaseClassTestRule
.forClass(TestCloseAnOpeningRegion
.class);
55 private static final HBaseTestingUtility UTIL
= new HBaseTestingUtility();
57 private static TableName TABLE_NAME
= TableName
.valueOf("race");
59 private static byte[] CF
= Bytes
.toBytes("cf");
61 private static volatile CountDownLatch ARRIVE
;
63 private static volatile CountDownLatch RESUME
;
65 public static final class MockHMaster
extends HMaster
{
67 public MockHMaster(Configuration conf
) throws IOException
{
72 protected AssignmentManager
createAssignmentManager(MasterServices master
) {
73 return new AssignmentManager(master
) {
76 public ReportRegionStateTransitionResponse
reportRegionStateTransition(
77 ReportRegionStateTransitionRequest req
) throws PleaseHoldException
{
78 ReportRegionStateTransitionResponse resp
= super.reportRegionStateTransition(req
);
79 TransitionCode code
= req
.getTransition(0).getTransitionCode();
80 if (code
== TransitionCode
.OPENED
&& ARRIVE
!= null) {
84 } catch (InterruptedException e
) {
85 throw new RuntimeException(e
);
95 public static void setUp() throws Exception
{
96 UTIL
.getConfiguration().setInt(HConstants
.HBASE_RPC_SHORTOPERATION_TIMEOUT_KEY
, 60000);
97 UTIL
.startMiniCluster(
98 StartMiniClusterOption
.builder().numRegionServers(2).masterClass(MockHMaster
.class).build());
99 UTIL
.createTable(TABLE_NAME
, CF
);
100 UTIL
.getAdmin().balancerSwitch(false, true);
104 public static void tearDown() throws Exception
{
105 UTIL
.shutdownMiniCluster();
109 public void test() throws IOException
, InterruptedException
{
110 ARRIVE
= new CountDownLatch(1);
111 RESUME
= new CountDownLatch(1);
112 RegionInfo region
= UTIL
.getAdmin().getRegions(TABLE_NAME
).get(0);
113 HRegionServer src
= UTIL
.getRSForFirstRegionInTable(TABLE_NAME
);
114 HRegionServer dst
= UTIL
.getOtherRegionServer(src
);
115 Thread move0
= new Thread(() -> {
117 UTIL
.getAdmin().move(region
.getEncodedNameAsBytes(), dst
.getServerName());
118 } catch (IOException e
) {
119 throw new UncheckedIOException(e
);
124 Thread move1
= new Thread(() -> {
126 UTIL
.getAdmin().move(region
.getEncodedNameAsBytes(), src
.getServerName());
127 } catch (IOException e
) {
128 throw new UncheckedIOException(e
);
132 // No simple way to determine when it is safe to go on and produce the race so let's sleep for a
138 try (Table table
= UTIL
.getConnection().getTable(TABLE_NAME
)) {
139 // make sure that we can write to the table, which means the region is online
140 table
.put(new Put(Bytes
.toBytes(0)).addColumn(CF
, Bytes
.toBytes("cq"), Bytes
.toBytes(0)));