Skip to content

Commit bdcfd6a

Browse files
authored
[RSGroup] Forward-port HBASE-22658 to master branch and branch-2.x (#1326)
Signed-off-by: stack <stack@apache.org>
1 parent afc1746 commit bdcfd6a

2 files changed

Lines changed: 161 additions & 2 deletions

File tree

hbase-server/src/main/java/org/apache/hadoop/hbase/util/RegionMover.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.nio.file.Files;
3232
import java.nio.file.Paths;
3333
import java.util.ArrayList;
34+
import java.util.Collection;
3435
import java.util.Collections;
3536
import java.util.EnumSet;
3637
import java.util.Iterator;
@@ -60,6 +61,8 @@
6061
import org.apache.hadoop.hbase.client.Scan;
6162
import org.apache.hadoop.hbase.client.Table;
6263
import org.apache.hadoop.hbase.filter.FirstKeyOnlyFilter;
64+
import org.apache.hadoop.hbase.net.Address;
65+
import org.apache.hadoop.hbase.rsgroup.RSGroupInfo;
6366
import org.apache.yetus.audience.InterfaceAudience;
6467
import org.slf4j.Logger;
6568
import org.slf4j.LoggerFactory;
@@ -416,7 +419,9 @@ public boolean unload() throws InterruptedException, ExecutionException, Timeout
416419
try {
417420
// Get Online RegionServers
418421
List<ServerName> regionServers = new ArrayList<>();
419-
regionServers.addAll(admin.getRegionServers());
422+
RSGroupInfo rsgroup = admin.getRSGroup(Address.fromParts(hostname, port));
423+
LOG.info("{} belongs to {}", hostname, rsgroup.getName());
424+
regionServers.addAll(filterRSGroupServers(rsgroup, admin.getRegionServers()));
420425
// Remove the host Region server from target Region Servers list
421426
ServerName server = stripServer(regionServers, hostname, port);
422427
if (server == null) {
@@ -431,6 +436,8 @@ public boolean unload() throws InterruptedException, ExecutionException, Timeout
431436
if (regionServers.isEmpty()) {
432437
LOG.warn("No Regions were moved - no servers available");
433438
return false;
439+
} else {
440+
LOG.info("Available servers {}", regionServers);
434441
}
435442
unloadRegions(server, regionServers, movedRegions);
436443
} catch (Exception e) {
@@ -446,6 +453,22 @@ public boolean unload() throws InterruptedException, ExecutionException, Timeout
446453
return waitTaskToFinish(unloadPool, unloadTask, "unloading");
447454
}
448455

456+
@VisibleForTesting
457+
Collection<ServerName> filterRSGroupServers(RSGroupInfo rsgroup,
458+
Collection<ServerName> onlineServers) {
459+
if (rsgroup.getName().equals(RSGroupInfo.DEFAULT_GROUP)) {
460+
return onlineServers;
461+
}
462+
List<ServerName> serverLists = new ArrayList<>(rsgroup.getServers().size());
463+
for (ServerName server : onlineServers) {
464+
Address address = Address.fromParts(server.getHostname(), server.getPort());
465+
if (rsgroup.containsServer(address)) {
466+
serverLists.add(server);
467+
}
468+
}
469+
return serverLists;
470+
}
471+
449472
private void unloadRegions(ServerName server, List<ServerName> regionServers,
450473
List<RegionInfo> movedRegions) throws Exception {
451474
while (true) {
@@ -790,4 +813,4 @@ public static void main(String[] args) {
790813
mover.doStaticMain(args);
791814
}
792815
}
793-
}
816+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/**
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
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
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.
17+
*/
18+
package org.apache.hadoop.hbase.util;
19+
20+
import java.util.ArrayList;
21+
import java.util.Collection;
22+
import java.util.HashSet;
23+
import java.util.List;
24+
import org.apache.hadoop.fs.Path;
25+
import org.apache.hadoop.hbase.HBaseClassTestRule;
26+
import org.apache.hadoop.hbase.HBaseTestingUtility;
27+
import org.apache.hadoop.hbase.ServerName;
28+
import org.apache.hadoop.hbase.TableName;
29+
import org.apache.hadoop.hbase.client.Admin;
30+
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
31+
import org.apache.hadoop.hbase.client.TableDescriptor;
32+
import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
33+
import org.apache.hadoop.hbase.net.Address;
34+
import org.apache.hadoop.hbase.regionserver.HRegionServer;
35+
import org.apache.hadoop.hbase.rsgroup.RSGroupInfo;
36+
import org.apache.hadoop.hbase.rsgroup.RSGroupUtil;
37+
import org.apache.hadoop.hbase.testclassification.MediumTests;
38+
import org.apache.hadoop.hbase.testclassification.MiscTests;
39+
import org.apache.hadoop.hbase.util.RegionMover.RegionMoverBuilder;
40+
import org.junit.AfterClass;
41+
import org.junit.Before;
42+
import org.junit.BeforeClass;
43+
import org.junit.ClassRule;
44+
import org.junit.Test;
45+
import org.junit.experimental.categories.Category;
46+
import org.slf4j.Logger;
47+
import org.slf4j.LoggerFactory;
48+
import static org.junit.Assert.assertEquals;
49+
50+
/**
51+
* Test for rsgroup enable, unloaded regions from decommissoned host of a rsgroup
52+
* should be assigned to those regionservers belonging to the same rsgroup.
53+
*/
54+
@Category({ MiscTests.class, MediumTests.class })
55+
public class TestRegionMoverWithRSGroupEnable {
56+
57+
@ClassRule
58+
public static final HBaseClassTestRule CLASS_RULE =
59+
HBaseClassTestRule.forClass(TestRegionMoverWithRSGroupEnable.class);
60+
61+
private static final Logger LOG = LoggerFactory.getLogger(TestRegionMoverWithRSGroupEnable.class);
62+
private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
63+
private static final String TEST_RSGROUP = "test";
64+
65+
66+
@BeforeClass
67+
public static void setUpBeforeClass() throws Exception {
68+
RSGroupUtil.enableRSGroup(TEST_UTIL.getConfiguration());
69+
TEST_UTIL.startMiniCluster(5);
70+
}
71+
72+
@AfterClass
73+
public static void tearDownAfterClass() throws Exception {
74+
TEST_UTIL.shutdownMiniCluster();
75+
}
76+
77+
private final List<Address> rsservers = new ArrayList<>(2);
78+
79+
@Before
80+
public void setUp() throws Exception {
81+
Admin admin = TEST_UTIL.getAdmin();
82+
83+
// Add a new rsgroup and assign two servers to it.
84+
admin.addRSGroup(TEST_RSGROUP);
85+
Collection<ServerName> allServers = admin.getRegionServers();
86+
// Remove rs contains hbase:meta, otherwise test looks unstable and buggy in test env.
87+
ServerName rsContainMeta = TEST_UTIL.getMiniHBaseCluster().getRegionServerThreads().stream()
88+
.map(t -> t.getRegionServer())
89+
.filter(rs -> rs.getRegions(TableName.META_TABLE_NAME).size() > 0).findFirst().get()
90+
.getServerName();
91+
LOG.info("{} contains hbase:meta", rsContainMeta);
92+
List<ServerName> modifiable = new ArrayList<>(allServers);
93+
modifiable.remove(rsContainMeta);
94+
int i = 0;
95+
for (ServerName server : modifiable) {
96+
if (i == 2) break;
97+
rsservers.add(Address.fromParts(server.getHostname(), server.getPort()));
98+
i++;
99+
}
100+
admin.moveServersToRSGroup(new HashSet<>(rsservers), TEST_RSGROUP);
101+
LOG.info("Servers in {} are {}", TEST_RSGROUP, rsservers);
102+
assertEquals(3, admin.getRSGroup(RSGroupInfo.DEFAULT_GROUP).getServers().size());
103+
assertEquals(2, admin.getRSGroup(TEST_RSGROUP).getServers().size());
104+
105+
// Create a pre-split table in test rsgroup
106+
TableName tableName = TableName.valueOf("testRegionMoverWithRSGroupEnable");
107+
if (admin.tableExists(tableName)) {
108+
TEST_UTIL.deleteTable(tableName);
109+
}
110+
TableDescriptor tableDesc = TableDescriptorBuilder.newBuilder(tableName)
111+
.setColumnFamily(ColumnFamilyDescriptorBuilder.of("f"))
112+
.setRegionServerGroup(TEST_RSGROUP)
113+
.build();
114+
String startKey = "a";
115+
String endKey = "z";
116+
admin.createTable(tableDesc, Bytes.toBytes(startKey), Bytes.toBytes(endKey), 9);
117+
}
118+
119+
@Test
120+
public void testUnloadRegions() throws Exception {
121+
Address decommission = rsservers.get(0);
122+
Address online = rsservers.get(1);
123+
String filename = new Path(TEST_UTIL.getDataTestDir(), "testRSGroupUnload").toString();
124+
RegionMoverBuilder builder =
125+
new RegionMoverBuilder(decommission.toString(), TEST_UTIL.getConfiguration());
126+
try (RegionMover rm = builder.filename(filename).ack(true).build()) {
127+
LOG.info("Unloading " + decommission.getHostname());
128+
rm.unload();
129+
}
130+
HRegionServer onlineRS = TEST_UTIL.getMiniHBaseCluster().getRegionServerThreads().stream()
131+
.map(JVMClusterUtil.RegionServerThread::getRegionServer)
132+
.filter(rs -> rs.getServerName().getAddress().equals(online)).findFirst().get();
133+
assertEquals(9, onlineRS.getNumberOfOnlineRegions());
134+
}
135+
136+
}

0 commit comments

Comments
 (0)