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 static org
.junit
.Assert
.assertEquals
;
21 import static org
.junit
.Assert
.assertNotNull
;
22 import static org
.junit
.Assert
.assertTrue
;
24 import java
.util
.List
;
26 import org
.apache
.hadoop
.hbase
.HBaseClassTestRule
;
27 import org
.apache
.hadoop
.hbase
.HConstants
;
28 import org
.apache
.hadoop
.hbase
.ServerName
;
29 import org
.apache
.hadoop
.hbase
.SingleProcessHBaseCluster
;
30 import org
.apache
.hadoop
.hbase
.TableName
;
31 import org
.apache
.hadoop
.hbase
.client
.RegionInfo
;
32 import org
.apache
.hadoop
.hbase
.testclassification
.MasterTests
;
33 import org
.apache
.hadoop
.hbase
.testclassification
.MediumTests
;
34 import org
.apache
.hadoop
.hbase
.util
.JVMClusterUtil
;
35 import org
.junit
.ClassRule
;
36 import org
.junit
.Test
;
37 import org
.junit
.experimental
.categories
.Category
;
38 import org
.slf4j
.Logger
;
39 import org
.slf4j
.LoggerFactory
;
41 @Category({ MasterTests
.class, MediumTests
.class })
42 public class TestRoundRobinAssignmentOnRestart
extends AbstractTestRestartCluster
{
45 public static final HBaseClassTestRule CLASS_RULE
=
46 HBaseClassTestRule
.forClass(TestRoundRobinAssignmentOnRestart
.class);
48 private static final Logger LOG
=
49 LoggerFactory
.getLogger(TestRoundRobinAssignmentOnRestart
.class);
52 protected boolean splitWALCoordinatedByZk() {
56 private final int regionNum
= 10;
57 private final int rsNum
= 2;
60 * This tests retaining assignments on a cluster restart
63 public void test() throws Exception
{
64 UTIL
.startMiniCluster(rsNum
);
66 UTIL
.getMiniHBaseCluster().getMaster().getMasterRpcServices().synchronousBalanceSwitch(false);
67 LOG
.info("\n\nCreating tables");
68 for (TableName TABLE
: TABLES
) {
69 UTIL
.createMultiRegionTable(TABLE
, FAMILY
, regionNum
);
71 // Wait until all regions are assigned
72 for (TableName TABLE
: TABLES
) {
73 UTIL
.waitTableEnabled(TABLE
);
75 UTIL
.waitUntilNoRegionsInTransition(60000);
77 SingleProcessHBaseCluster cluster
= UTIL
.getHBaseCluster();
78 List
<JVMClusterUtil
.RegionServerThread
> threads
= cluster
.getLiveRegionServerThreads();
79 assertEquals(2, threads
.size());
81 ServerName testServer
= threads
.get(0).getRegionServer().getServerName();
82 int port
= testServer
.getPort();
83 List
<RegionInfo
> regionInfos
=
84 cluster
.getMaster().getAssignmentManager().getRegionsOnServer(testServer
);
85 LOG
.debug("RegionServer {} has {} regions", testServer
, regionInfos
.size());
86 assertTrue(regionInfos
.size() >= (TABLES
.length
* regionNum
/ rsNum
));
88 // Restart 1 regionserver
89 cluster
.stopRegionServer(testServer
);
90 cluster
.waitForRegionServerToStop(testServer
, 60000);
91 cluster
.getConf().setInt(HConstants
.REGIONSERVER_PORT
, port
);
92 cluster
.startRegionServer();
94 HMaster master
= UTIL
.getMiniHBaseCluster().getMaster();
95 List
<ServerName
> localServers
= master
.getServerManager().getOnlineServersList();
96 ServerName newTestServer
= null;
97 for (ServerName serverName
: localServers
) {
98 if (serverName
.getAddress().equals(testServer
.getAddress())) {
99 newTestServer
= serverName
;
103 assertNotNull(newTestServer
);
105 // Wait until all regions are assigned
106 for (TableName TABLE
: TABLES
) {
107 UTIL
.waitTableAvailable(TABLE
);
109 UTIL
.waitUntilNoRegionsInTransition(60000);
111 List
<RegionInfo
> newRegionInfos
=
112 cluster
.getMaster().getAssignmentManager().getRegionsOnServer(newTestServer
);
113 LOG
.debug("RegionServer {} has {} regions", newTestServer
, newRegionInfos
.size());
114 assertTrue("Should not retain all regions when restart",
115 newRegionInfos
.size() < regionInfos
.size());