Skip to content

Commit f87428e

Browse files
authored
HBASE-23377 Balancer should skip disabled tables's regions (#908)
Signed-off-by: Wellington Chevreuil <wchevreuil@apache.org> Signed-off-by: GuangxuCheng <guangxucheng@gmail.com> Signed-off-by Anoop Sam John <anoopsamjohn@apache.org> Signed-off-by: Reid Chan <reidchan@apache.org>
1 parent 82e155e commit f87428e

File tree

4 files changed

+122
-5
lines changed

4 files changed

+122
-5
lines changed

hbase-server/src/main/java/org/apache/hadoop/hbase/master/HMaster.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1796,7 +1796,8 @@ public boolean balance(boolean force) throws IOException {
17961796

17971797
boolean isByTable = getConfiguration().getBoolean("hbase.master.loadbalance.bytable", false);
17981798
Map<TableName, Map<ServerName, List<RegionInfo>>> assignments =
1799-
this.assignmentManager.getRegionStates().getAssignmentsForBalancer(isByTable);
1799+
this.assignmentManager.getRegionStates()
1800+
.getAssignmentsForBalancer(tableStateManager, isByTable);
18001801
for (Map<ServerName, List<RegionInfo>> serverMap : assignments.values()) {
18011802
serverMap.keySet().removeAll(this.serverManager.getDrainingServersList());
18021803
}

hbase-server/src/main/java/org/apache/hadoop/hbase/master/assignment/AssignmentManager.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -581,8 +581,7 @@ private void preTransitCheck(RegionStateNode regionNode, RegionState.State[] exp
581581
if (!regionNode.isInState(expectedStates)) {
582582
throw new DoNotRetryRegionException("Unexpected state for " + regionNode);
583583
}
584-
if (getTableStateManager().isTableState(regionNode.getTable(), TableState.State.DISABLING,
585-
TableState.State.DISABLED)) {
584+
if (isTableDisabled(regionNode.getTable())) {
586585
throw new DoNotRetryIOException(regionNode.getTable() + " is disabled for " + regionNode);
587586
}
588587
}

hbase-server/src/main/java/org/apache/hadoop/hbase/master/assignment/RegionStates.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@
3737
import org.apache.hadoop.hbase.ServerName;
3838
import org.apache.hadoop.hbase.TableName;
3939
import org.apache.hadoop.hbase.client.RegionInfo;
40+
import org.apache.hadoop.hbase.client.TableState;
4041
import org.apache.hadoop.hbase.master.RegionState;
4142
import org.apache.hadoop.hbase.master.RegionState.State;
43+
import org.apache.hadoop.hbase.master.TableStateManager;
4244
import org.apache.hadoop.hbase.util.Bytes;
4345
import org.apache.yetus.audience.InterfaceAudience;
4446
import org.slf4j.Logger;
@@ -537,10 +539,13 @@ public ServerName getRegionServerOfRegion(RegionInfo regionInfo) {
537539
* @return A clone of current assignments.
538540
*/
539541
public Map<TableName, Map<ServerName, List<RegionInfo>>> getAssignmentsForBalancer(
540-
boolean isByTable) {
542+
TableStateManager tableStateManager, boolean isByTable) {
541543
final Map<TableName, Map<ServerName, List<RegionInfo>>> result = new HashMap<>();
542544
if (isByTable) {
543545
for (RegionStateNode node : regionsMap.values()) {
546+
if (isTableDisabled(tableStateManager, node.getTable())) {
547+
continue;
548+
}
544549
Map<ServerName, List<RegionInfo>> tableResult =
545550
result.computeIfAbsent(node.getTable(), t -> new HashMap<>());
546551
final ServerName serverName = node.getRegionLocation();
@@ -561,14 +566,22 @@ public Map<TableName, Map<ServerName, List<RegionInfo>>> getAssignmentsForBalanc
561566
} else {
562567
final HashMap<ServerName, List<RegionInfo>> ensemble = new HashMap<>(serverMap.size());
563568
for (ServerStateNode serverNode : serverMap.values()) {
564-
ensemble.put(serverNode.getServerName(), serverNode.getRegionInfoList());
569+
ensemble.put(serverNode.getServerName(), serverNode.getRegionInfoList().stream()
570+
.filter(region -> !isTableDisabled(tableStateManager, region.getTable()))
571+
.collect(Collectors.toList()));
565572
}
566573
// Use a fake table name to represent the whole cluster's assignments
567574
result.put(HConstants.ENSEMBLE_TABLE_NAME, ensemble);
568575
}
569576
return result;
570577
}
571578

579+
private boolean isTableDisabled(final TableStateManager tableStateManager,
580+
final TableName tableName) {
581+
return tableStateManager
582+
.isTableState(tableName, TableState.State.DISABLED, TableState.State.DISABLING);
583+
}
584+
572585
// ==========================================================================
573586
// Region in transition helpers
574587
// ==========================================================================
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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.master;
19+
20+
import static org.junit.Assert.assertFalse;
21+
import static org.junit.Assert.assertTrue;
22+
23+
import java.util.ArrayList;
24+
import java.util.HashMap;
25+
import java.util.List;
26+
import java.util.Map;
27+
28+
import org.apache.hadoop.hbase.HBaseClassTestRule;
29+
import org.apache.hadoop.hbase.HBaseTestingUtility;
30+
import org.apache.hadoop.hbase.HConstants;
31+
import org.apache.hadoop.hbase.ServerName;
32+
import org.apache.hadoop.hbase.TableName;
33+
import org.apache.hadoop.hbase.client.RegionInfo;
34+
import org.apache.hadoop.hbase.master.assignment.AssignmentManager;
35+
import org.apache.hadoop.hbase.testclassification.LargeTests;
36+
import org.apache.hadoop.hbase.testclassification.MasterTests;
37+
import org.junit.After;
38+
import org.junit.Before;
39+
import org.junit.ClassRule;
40+
import org.junit.Rule;
41+
import org.junit.Test;
42+
import org.junit.experimental.categories.Category;
43+
import org.junit.rules.TestName;
44+
45+
/**
46+
* Test balancer with disabled table
47+
*/
48+
@Category({ MasterTests.class, LargeTests.class })
49+
public class TestBalancerWithDisabledTable {
50+
51+
@ClassRule
52+
public static final HBaseClassTestRule CLASS_RULE =
53+
HBaseClassTestRule.forClass(TestBalancerWithDisabledTable.class);
54+
55+
private final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
56+
57+
@Rule
58+
public TestName name = new TestName();
59+
60+
@Before
61+
public void before() throws Exception {
62+
TEST_UTIL.startMiniCluster();
63+
}
64+
65+
@After
66+
public void after() throws Exception {
67+
TEST_UTIL.shutdownMiniCluster();
68+
}
69+
70+
@Test
71+
public void testAssignmentsForBalancer() throws Exception {
72+
final TableName tableName = TableName.valueOf(name.getMethodName());
73+
TEST_UTIL.createMultiRegionTable(tableName, HConstants.CATALOG_FAMILY, 10);
74+
// disable table
75+
final TableName disableTableName = TableName.valueOf("testDisableTable");
76+
TEST_UTIL.createMultiRegionTable(disableTableName, HConstants.CATALOG_FAMILY, 10);
77+
TEST_UTIL.getAdmin().disableTable(disableTableName);
78+
79+
HMaster master = TEST_UTIL.getMiniHBaseCluster().getMaster();
80+
AssignmentManager assignmentManager = master.getAssignmentManager();
81+
TableStateManager tableStateManager = master.getTableStateManager();
82+
Map<TableName, Map<ServerName, List<RegionInfo>>> assignments =
83+
assignmentManager.getRegionStates().getAssignmentsForBalancer(tableStateManager, true);
84+
assertFalse(assignments.containsKey(disableTableName));
85+
assertTrue(assignments.containsKey(tableName));
86+
87+
assignments =
88+
assignmentManager.getRegionStates().getAssignmentsForBalancer(tableStateManager, false);
89+
Map<TableName, Map<ServerName, List<RegionInfo>>> tableNameMap = new HashMap<>();
90+
for (Map.Entry<ServerName, List<RegionInfo>> entry : assignments
91+
.get(HConstants.ENSEMBLE_TABLE_NAME).entrySet()) {
92+
final ServerName serverName = entry.getKey();
93+
for (RegionInfo regionInfo : entry.getValue()) {
94+
Map<ServerName, List<RegionInfo>> tableResult =
95+
tableNameMap.computeIfAbsent(regionInfo.getTable(), t -> new HashMap<>());
96+
List<RegionInfo> serverResult =
97+
tableResult.computeIfAbsent(serverName, s -> new ArrayList<>());
98+
serverResult.add(regionInfo);
99+
}
100+
}
101+
assertFalse(tableNameMap.containsKey(disableTableName));
102+
assertTrue(tableNameMap.containsKey(tableName));
103+
}
104+
}

0 commit comments

Comments
 (0)