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
.regionserver
;
20 import static junit
.framework
.TestCase
.fail
;
21 import static org
.junit
.Assert
.assertTrue
;
23 import java
.io
.IOException
;
24 import java
.util
.List
;
25 import java
.util
.stream
.Collectors
;
26 import org
.apache
.hadoop
.conf
.Configuration
;
27 import org
.apache
.hadoop
.hbase
.DoNotRetryIOException
;
28 import org
.apache
.hadoop
.hbase
.HBaseClassTestRule
;
29 import org
.apache
.hadoop
.hbase
.HBaseTestingUtil
;
30 import org
.apache
.hadoop
.hbase
.TableName
;
31 import org
.apache
.hadoop
.hbase
.client
.Admin
;
32 import org
.apache
.hadoop
.hbase
.client
.DoNotRetryRegionException
;
33 import org
.apache
.hadoop
.hbase
.client
.Put
;
34 import org
.apache
.hadoop
.hbase
.client
.RegionInfo
;
35 import org
.apache
.hadoop
.hbase
.client
.Table
;
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
.Before
;
40 import org
.junit
.BeforeClass
;
41 import org
.junit
.ClassRule
;
42 import org
.junit
.Rule
;
43 import org
.junit
.Test
;
44 import org
.junit
.experimental
.categories
.Category
;
45 import org
.junit
.rules
.ExpectedException
;
46 import org
.junit
.rules
.TestName
;
49 * Test move fails when table disabled
51 @Category({MediumTests
.class})
52 public class TestRegionMove
{
55 public static final HBaseClassTestRule CLASS_RULE
=
56 HBaseClassTestRule
.forClass(TestRegionMove
.class);
59 public ExpectedException thrown
= ExpectedException
.none();
62 public TestName name
= new TestName();
63 private static final HBaseTestingUtil TEST_UTIL
= new HBaseTestingUtil();
64 public static Configuration CONF
;
65 protected static final String F1
= "f1";
68 protected TableName tableName
;
69 protected String method
;
72 public static void startCluster() throws Exception
{
73 TEST_UTIL
.startMiniCluster(2);
77 public static void stopCluster() throws Exception
{
78 TEST_UTIL
.shutdownMiniCluster();
82 public void setup() throws IOException
{
83 CONF
= TEST_UTIL
.getConfiguration();
84 method
= name
.getMethodName();
85 tableName
= TableName
.valueOf(method
);
89 public void testDisableAndMove() throws Exception
{
90 Admin admin
= TEST_UTIL
.getAdmin();
92 // Create a table with more than one region
93 Table t
= TEST_UTIL
.createMultiRegionTable(tableName
, Bytes
.toBytes(F1
), 10);
94 TEST_UTIL
.waitUntilAllRegionsAssigned(tableName
);
96 // Write an update to each region
97 for (RegionInfo regionInfo
: admin
.getRegions(tableName
)) {
98 byte[] startKey
= regionInfo
.getStartKey();
99 // The startKey of the first region is "empty", which would throw an error if we try to
101 byte[] rowKey
= org
.apache
.hbase
.thirdparty
.com
.google
.common
.primitives
.Bytes
.concat(
102 startKey
, Bytes
.toBytes("1"));
103 Put p
= new Put(rowKey
);
104 p
.addColumn(Bytes
.toBytes(F1
), Bytes
.toBytes("q1"), Bytes
.toBytes("value"));
108 // Get a Region which is on the first RS
109 HRegionServer rs1
= TEST_UTIL
.getRSForFirstRegionInTable(tableName
);
110 HRegionServer rs2
= TEST_UTIL
.getOtherRegionServer(rs1
);
111 List
<RegionInfo
> regionsOnRS1ForTable
= admin
.getRegions(rs1
.getServerName()).stream()
112 .filter((regionInfo
) -> regionInfo
.getTable().equals(tableName
))
113 .collect(Collectors
.toList());
115 "Expected to find at least one region for " + tableName
+ " on " + rs1
.getServerName()
116 + ", but found none", !regionsOnRS1ForTable
.isEmpty());
117 final RegionInfo regionToMove
= regionsOnRS1ForTable
.get(0);
119 // Offline the region and then try to move it. Should fail.
120 admin
.unassign(regionToMove
.getRegionName(), true);
122 admin
.move(regionToMove
.getEncodedNameAsBytes(), rs2
.getServerName());
124 } catch (DoNotRetryRegionException e
) {
125 // We got expected exception
127 // Reassign for next stage of test.
128 admin
.assign(regionToMove
.getRegionName());
131 admin
.disableTable(tableName
);
134 // Move the region to the other RS -- should fail
135 admin
.move(regionToMove
.getEncodedNameAsBytes(), rs2
.getServerName());
137 } catch (DoNotRetryIOException e
) {
138 // We got expected exception