From f87428e70469fdca8d40728f5d54a908bfc6e2c9 Mon Sep 17 00:00:00 2001 From: binlijin Date: Tue, 10 Dec 2019 19:28:42 +0800 Subject: [PATCH] HBASE-23377 Balancer should skip disabled tables's regions (#908) Signed-off-by: Wellington Chevreuil Signed-off-by: GuangxuCheng Signed-off-by Anoop Sam John Signed-off-by: Reid Chan --- .../apache/hadoop/hbase/master/HMaster.java | 3 +- .../master/assignment/AssignmentManager.java | 3 +- .../hbase/master/assignment/RegionStates.java | 17 ++- .../master/TestBalancerWithDisabledTable.java | 104 ++++++++++++++++++ 4 files changed, 122 insertions(+), 5 deletions(-) create mode 100644 hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestBalancerWithDisabledTable.java diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/master/HMaster.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/master/HMaster.java index cc226bd58c3..87bfb20ff82 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/master/HMaster.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/master/HMaster.java @@ -1796,7 +1796,8 @@ public class HMaster extends HRegionServer implements MasterServices { boolean isByTable = getConfiguration().getBoolean("hbase.master.loadbalance.bytable", false); Map>> assignments = - this.assignmentManager.getRegionStates().getAssignmentsForBalancer(isByTable); + this.assignmentManager.getRegionStates() + .getAssignmentsForBalancer(tableStateManager, isByTable); for (Map> serverMap : assignments.values()) { serverMap.keySet().removeAll(this.serverManager.getDrainingServersList()); } diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/master/assignment/AssignmentManager.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/master/assignment/AssignmentManager.java index d1585c7c1c5..1d0d2bee85d 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/master/assignment/AssignmentManager.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/master/assignment/AssignmentManager.java @@ -581,8 +581,7 @@ public class AssignmentManager { if (!regionNode.isInState(expectedStates)) { throw new DoNotRetryRegionException("Unexpected state for " + regionNode); } - if (getTableStateManager().isTableState(regionNode.getTable(), TableState.State.DISABLING, - TableState.State.DISABLED)) { + if (isTableDisabled(regionNode.getTable())) { throw new DoNotRetryIOException(regionNode.getTable() + " is disabled for " + regionNode); } } diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/master/assignment/RegionStates.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/master/assignment/RegionStates.java index b58de83eb4c..ae0ca9afe6d 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/master/assignment/RegionStates.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/master/assignment/RegionStates.java @@ -37,8 +37,10 @@ import org.apache.hadoop.hbase.HRegionLocation; import org.apache.hadoop.hbase.ServerName; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.RegionInfo; +import org.apache.hadoop.hbase.client.TableState; import org.apache.hadoop.hbase.master.RegionState; import org.apache.hadoop.hbase.master.RegionState.State; +import org.apache.hadoop.hbase.master.TableStateManager; import org.apache.hadoop.hbase.util.Bytes; import org.apache.yetus.audience.InterfaceAudience; import org.slf4j.Logger; @@ -537,10 +539,13 @@ public class RegionStates { * @return A clone of current assignments. */ public Map>> getAssignmentsForBalancer( - boolean isByTable) { + TableStateManager tableStateManager, boolean isByTable) { final Map>> result = new HashMap<>(); if (isByTable) { for (RegionStateNode node : regionsMap.values()) { + if (isTableDisabled(tableStateManager, node.getTable())) { + continue; + } Map> tableResult = result.computeIfAbsent(node.getTable(), t -> new HashMap<>()); final ServerName serverName = node.getRegionLocation(); @@ -561,7 +566,9 @@ public class RegionStates { } else { final HashMap> ensemble = new HashMap<>(serverMap.size()); for (ServerStateNode serverNode : serverMap.values()) { - ensemble.put(serverNode.getServerName(), serverNode.getRegionInfoList()); + ensemble.put(serverNode.getServerName(), serverNode.getRegionInfoList().stream() + .filter(region -> !isTableDisabled(tableStateManager, region.getTable())) + .collect(Collectors.toList())); } // Use a fake table name to represent the whole cluster's assignments result.put(HConstants.ENSEMBLE_TABLE_NAME, ensemble); @@ -569,6 +576,12 @@ public class RegionStates { return result; } + private boolean isTableDisabled(final TableStateManager tableStateManager, + final TableName tableName) { + return tableStateManager + .isTableState(tableName, TableState.State.DISABLED, TableState.State.DISABLING); + } + // ========================================================================== // Region in transition helpers // ========================================================================== diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestBalancerWithDisabledTable.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestBalancerWithDisabledTable.java new file mode 100644 index 00000000000..190b54a84be --- /dev/null +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestBalancerWithDisabledTable.java @@ -0,0 +1,104 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hbase.master; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.hadoop.hbase.HBaseClassTestRule; +import org.apache.hadoop.hbase.HBaseTestingUtility; +import org.apache.hadoop.hbase.HConstants; +import org.apache.hadoop.hbase.ServerName; +import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.client.RegionInfo; +import org.apache.hadoop.hbase.master.assignment.AssignmentManager; +import org.apache.hadoop.hbase.testclassification.LargeTests; +import org.apache.hadoop.hbase.testclassification.MasterTests; +import org.junit.After; +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Rule; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.rules.TestName; + +/** + * Test balancer with disabled table + */ +@Category({ MasterTests.class, LargeTests.class }) +public class TestBalancerWithDisabledTable { + + @ClassRule + public static final HBaseClassTestRule CLASS_RULE = + HBaseClassTestRule.forClass(TestBalancerWithDisabledTable.class); + + private final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); + + @Rule + public TestName name = new TestName(); + + @Before + public void before() throws Exception { + TEST_UTIL.startMiniCluster(); + } + + @After + public void after() throws Exception { + TEST_UTIL.shutdownMiniCluster(); + } + + @Test + public void testAssignmentsForBalancer() throws Exception { + final TableName tableName = TableName.valueOf(name.getMethodName()); + TEST_UTIL.createMultiRegionTable(tableName, HConstants.CATALOG_FAMILY, 10); + // disable table + final TableName disableTableName = TableName.valueOf("testDisableTable"); + TEST_UTIL.createMultiRegionTable(disableTableName, HConstants.CATALOG_FAMILY, 10); + TEST_UTIL.getAdmin().disableTable(disableTableName); + + HMaster master = TEST_UTIL.getMiniHBaseCluster().getMaster(); + AssignmentManager assignmentManager = master.getAssignmentManager(); + TableStateManager tableStateManager = master.getTableStateManager(); + Map>> assignments = + assignmentManager.getRegionStates().getAssignmentsForBalancer(tableStateManager, true); + assertFalse(assignments.containsKey(disableTableName)); + assertTrue(assignments.containsKey(tableName)); + + assignments = + assignmentManager.getRegionStates().getAssignmentsForBalancer(tableStateManager, false); + Map>> tableNameMap = new HashMap<>(); + for (Map.Entry> entry : assignments + .get(HConstants.ENSEMBLE_TABLE_NAME).entrySet()) { + final ServerName serverName = entry.getKey(); + for (RegionInfo regionInfo : entry.getValue()) { + Map> tableResult = + tableNameMap.computeIfAbsent(regionInfo.getTable(), t -> new HashMap<>()); + List serverResult = + tableResult.computeIfAbsent(serverName, s -> new ArrayList<>()); + serverResult.add(regionInfo); + } + } + assertFalse(tableNameMap.containsKey(disableTableName)); + assertTrue(tableNameMap.containsKey(tableName)); + } +}