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 org
.junit
.Assert
.assertEquals
;
21 import static org
.junit
.Assert
.assertFalse
;
22 import static org
.junit
.Assert
.assertTrue
;
24 import java
.io
.IOException
;
25 import java
.util
.ArrayList
;
26 import java
.util
.List
;
27 import org
.apache
.hadoop
.hbase
.HBaseClassTestRule
;
28 import org
.apache
.hadoop
.hbase
.HBaseTestingUtil
;
29 import org
.apache
.hadoop
.hbase
.SingleProcessHBaseCluster
;
30 import org
.apache
.hadoop
.hbase
.TableName
;
31 import org
.apache
.hadoop
.hbase
.client
.Admin
;
32 import org
.apache
.hadoop
.hbase
.client
.ColumnFamilyDescriptorBuilder
;
33 import org
.apache
.hadoop
.hbase
.client
.Put
;
34 import org
.apache
.hadoop
.hbase
.client
.RegionInfo
;
35 import org
.apache
.hadoop
.hbase
.client
.RegionLocator
;
36 import org
.apache
.hadoop
.hbase
.client
.Result
;
37 import org
.apache
.hadoop
.hbase
.client
.ResultScanner
;
38 import org
.apache
.hadoop
.hbase
.client
.Scan
;
39 import org
.apache
.hadoop
.hbase
.client
.Table
;
40 import org
.apache
.hadoop
.hbase
.client
.TableDescriptor
;
41 import org
.apache
.hadoop
.hbase
.client
.TableDescriptorBuilder
;
42 import org
.apache
.hadoop
.hbase
.master
.HMaster
;
43 import org
.apache
.hadoop
.hbase
.testclassification
.MediumTests
;
44 import org
.apache
.hadoop
.hbase
.testclassification
.RegionServerTests
;
45 import org
.apache
.hadoop
.hbase
.util
.Bytes
;
46 import org
.junit
.ClassRule
;
47 import org
.junit
.Rule
;
48 import org
.junit
.Test
;
49 import org
.junit
.experimental
.categories
.Category
;
50 import org
.junit
.rules
.TestName
;
51 import org
.slf4j
.Logger
;
52 import org
.slf4j
.LoggerFactory
;
55 * Tests that need to spin up a cluster testing an {@link HRegion}. Use
56 * {@link TestHRegion} if you don't need a cluster, if you can test w/ a
57 * standalone {@link HRegion}.
59 @Category({RegionServerTests
.class, MediumTests
.class})
60 public class TestHRegionOnCluster
{
63 public static final HBaseClassTestRule CLASS_RULE
=
64 HBaseClassTestRule
.forClass(TestHRegionOnCluster
.class);
66 private static final Logger LOG
= LoggerFactory
.getLogger(TestHRegionOnCluster
.class);
67 private static final HBaseTestingUtil TEST_UTIL
= new HBaseTestingUtil();
70 public TestName name
= new TestName();
73 public void testDataCorrectnessReplayingRecoveredEdits() throws Exception
{
75 Admin hbaseAdmin
= null;
76 TEST_UTIL
.startMiniCluster(NUM_RS
);
79 final TableName tableName
= TableName
.valueOf(name
.getMethodName());
80 final byte[] FAMILY
= Bytes
.toBytes("family");
81 SingleProcessHBaseCluster cluster
= TEST_UTIL
.getHBaseCluster();
82 HMaster master
= cluster
.getMaster();
85 TableDescriptor tableDescriptor
= TableDescriptorBuilder
.newBuilder(tableName
)
86 .setColumnFamily(ColumnFamilyDescriptorBuilder
.of(FAMILY
)).build();
87 hbaseAdmin
= master
.getConnection().getAdmin();
88 hbaseAdmin
.createTable(tableDescriptor
);
90 assertTrue(hbaseAdmin
.isTableAvailable(tableName
));
93 LOG
.info("Loading r1 to v1 into " + tableName
);
94 Table table
= TEST_UTIL
.getConnection().getTable(tableName
);
95 putDataAndVerify(table
, "r1", FAMILY
, "v1", 1);
97 TEST_UTIL
.waitUntilAllRegionsAssigned(table
.getName());
98 // Move region to target server
100 RegionInfo regionInfo
;
101 try (RegionLocator locator
= TEST_UTIL
.getConnection().getRegionLocator(tableName
)) {
102 regionInfo
= locator
.getRegionLocation(Bytes
.toBytes("r1")).getRegion();
105 int originServerNum
= cluster
.getServerWith(regionInfo
.getRegionName());
106 HRegionServer originServer
= cluster
.getRegionServer(originServerNum
);
107 int targetServerNum
= (originServerNum
+ 1) % NUM_RS
;
108 HRegionServer targetServer
= cluster
.getRegionServer(targetServerNum
);
109 assertFalse(originServer
.equals(targetServer
));
111 TEST_UTIL
.waitUntilAllRegionsAssigned(table
.getName());
112 LOG
.info("Moving " + regionInfo
.getEncodedName() + " to " + targetServer
.getServerName());
113 hbaseAdmin
.move(regionInfo
.getEncodedNameAsBytes(), targetServer
.getServerName());
116 } while (cluster
.getServerWith(regionInfo
.getRegionName()) == originServerNum
);
119 LOG
.info("Loading r2 to v2 into " + tableName
);
120 putDataAndVerify(table
, "r2", FAMILY
, "v2", 2);
122 TEST_UTIL
.waitUntilAllRegionsAssigned(table
.getName());
123 // Move region to origin server
124 LOG
.info("Moving " + regionInfo
.getEncodedName() + " to " + originServer
.getServerName());
125 hbaseAdmin
.move(regionInfo
.getEncodedNameAsBytes(), originServer
.getServerName());
128 } while (cluster
.getServerWith(regionInfo
.getRegionName()) == targetServerNum
);
131 LOG
.info("Loading r3 to v3 into " + tableName
);
132 putDataAndVerify(table
, "r3", FAMILY
, "v3", 3);
134 // Kill target server
135 LOG
.info("Killing target server " + targetServer
.getServerName());
137 cluster
.getRegionServerThreads().get(targetServerNum
).join();
138 // Wait until finish processing of shutdown
139 while (master
.getServerManager().areDeadServersInProgress()) {
142 // Kill origin server
143 LOG
.info("Killing origin server " + targetServer
.getServerName());
145 cluster
.getRegionServerThreads().get(originServerNum
).join();
148 LOG
.info("Loading r4 to v4 into " + tableName
);
149 putDataAndVerify(table
, "r4", FAMILY
, "v4", 4);
152 if (hbaseAdmin
!= null) hbaseAdmin
.close();
153 TEST_UTIL
.shutdownMiniCluster();
157 private void putDataAndVerify(Table table
, String row
, byte[] family
,
158 String value
, int verifyNum
) throws IOException
{
159 System
.out
.println("=========Putting data :" + row
);
160 Put put
= new Put(Bytes
.toBytes(row
));
161 put
.addColumn(family
, Bytes
.toBytes("q1"), Bytes
.toBytes(value
));
163 ResultScanner resultScanner
= table
.getScanner(new Scan());
164 List
<Result
> results
= new ArrayList
<>();
166 Result r
= resultScanner
.next();
171 resultScanner
.close();
172 if (results
.size() != verifyNum
) {
173 System
.out
.println(results
);
175 assertEquals(verifyNum
, results
.size());