From a4c21404e375bd7d0723cb57d4c4fdeead7b54f8 Mon Sep 17 00:00:00 2001 From: Duo Zhang Date: Wed, 15 May 2024 23:43:35 +0800 Subject: [PATCH 1/2] HBASE-28582 ModifyTableProcedure should not reset TRSP on region node when closing unused region replicas --- .../server/master/MasterProcedure.proto | 10 ++ .../master/assignment/AssignmentManager.java | 56 ++++++- .../CloseExcessRegionReplicasProcedure.java | 150 ++++++++++++++++++ .../procedure/ModifyTableProcedure.java | 3 +- 4 files changed, 210 insertions(+), 9 deletions(-) create mode 100644 hbase-server/src/main/java/org/apache/hadoop/hbase/master/procedure/CloseExcessRegionReplicasProcedure.java diff --git a/hbase-protocol-shaded/src/main/protobuf/server/master/MasterProcedure.proto b/hbase-protocol-shaded/src/main/protobuf/server/master/MasterProcedure.proto index c9c9c6357312..b1ca687d6239 100644 --- a/hbase-protocol-shaded/src/main/protobuf/server/master/MasterProcedure.proto +++ b/hbase-protocol-shaded/src/main/protobuf/server/master/MasterProcedure.proto @@ -780,3 +780,13 @@ enum MigrateNamespaceTableProcedureState { message MigrateNamespaceTableProcedureStateData { } + +enum CloseExcessRegionReplicasProcedureState { + CLOSE_EXCESS_REGION_REPLICAS_SCHEDULE = 1; + CLOSE_EXCESS_REGION_REPLICAS_CONFIRM = 2; +} + +message CloseExcessRegionReplicasProcedureStateData { + required TableName table_name = 1; + required uint32 new_replica_count = 2; +} 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 9cee9f87ce2f..bcfa50fe66d5 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 @@ -33,6 +33,7 @@ import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.ReentrantLock; +import java.util.function.Consumer; import java.util.stream.Collectors; import java.util.stream.Stream; import org.apache.hadoop.conf.Configuration; @@ -1084,14 +1085,55 @@ public TransitRegionStateProcedure[] createUnassignProceduresForDisabling(TableN } /** - * Called by ModifyTableProcedures to unassign all the excess region replicas for a table. + * Called by ModifyTableProcedure to unassign all the excess region replicas for a table. Will + * skip submit unassign procedure if the region is in transition, so you may need to call this + * method multiple times. + * @param tableName the table for closing excess region replicas + * @param newReplicaCount the new replica count, should be less than current replica count + * @param submit for submitting procedure + * @return the number of regions in transition that we can not schedule unassign procedures */ - public TransitRegionStateProcedure[] createUnassignProceduresForClosingExcessRegionReplicas( - TableName tableName, int newReplicaCount) { - return regionStates.getTableRegionStateNodes(tableName).stream() - .filter(regionNode -> regionNode.getRegionInfo().getReplicaId() >= newReplicaCount) - .map(this::forceCreateUnssignProcedure).filter(p -> p != null) - .toArray(TransitRegionStateProcedure[]::new); + public int submitUnassignProcedureForClosingExcessRegionReplicas(TableName tableName, + int newReplicaCount, Consumer submit) { + int inTransitionCount = 0; + for (RegionStateNode regionNode : regionStates.getTableRegionStateNodes(tableName)) { + regionNode.lock(); + try { + if (regionNode.getRegionInfo().getReplicaId() >= newReplicaCount) { + if (regionNode.isInTransition()) { + LOG.debug("skip scheduling unassign procedure for {} when closing excess region " + + "replicas since it is in transition", regionNode); + inTransitionCount++; + continue; + } + if (regionNode.isInState(State.OFFLINE, State.CLOSED, State.SPLIT)) { + continue; + } + submit.accept(regionNode.setProcedure(TransitRegionStateProcedure + .unassign(getProcedureEnvironment(), regionNode.getRegionInfo()))); + } + } finally { + regionNode.unlock(); + } + } + return inTransitionCount; + } + + public int numberOfUnclosedExcessRegionReplicas(TableName tableName, int newReplicaCount) { + int unclosed = 0; + for (RegionStateNode regionNode : regionStates.getTableRegionStateNodes(tableName)) { + regionNode.lock(); + try { + if (regionNode.getRegionInfo().getReplicaId() >= newReplicaCount) { + if (!regionNode.isInState(State.OFFLINE, State.CLOSED, State.SPLIT)) { + unclosed++; + } + } + } finally { + regionNode.unlock(); + } + } + return unclosed; } public SplitTableRegionProcedure createSplitProcedure(final RegionInfo regionToSplit, diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/master/procedure/CloseExcessRegionReplicasProcedure.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/master/procedure/CloseExcessRegionReplicasProcedure.java new file mode 100644 index 000000000000..fc73df723fc3 --- /dev/null +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/master/procedure/CloseExcessRegionReplicasProcedure.java @@ -0,0 +1,150 @@ +/* + * 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.procedure; + +import java.io.IOException; +import org.apache.commons.lang3.mutable.MutableBoolean; +import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.procedure2.ProcedureStateSerializer; +import org.apache.hadoop.hbase.procedure2.ProcedureSuspendedException; +import org.apache.hadoop.hbase.procedure2.ProcedureUtil; +import org.apache.hadoop.hbase.procedure2.ProcedureYieldException; +import org.apache.hadoop.hbase.util.RetryCounter; +import org.apache.yetus.audience.InterfaceAudience; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil; +import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProcedureProtos.CloseExcessRegionReplicasProcedureState; +import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProcedureProtos.CloseExcessRegionReplicasProcedureStateData; + +/** + * Procedure for close excess region replicas. + */ +@InterfaceAudience.Private +public class CloseExcessRegionReplicasProcedure + extends AbstractStateMachineTableProcedure { + + private static final Logger LOG = + LoggerFactory.getLogger(CloseExcessRegionReplicasProcedure.class); + + private TableName tableName; + private int newReplicaCount; + + private RetryCounter retryCounter; + + public CloseExcessRegionReplicasProcedure() { + } + + public CloseExcessRegionReplicasProcedure(TableName tableName, int newReplicaCount) { + this.tableName = tableName; + this.newReplicaCount = newReplicaCount; + } + + @Override + public TableName getTableName() { + return tableName; + } + + @Override + public TableOperationType getTableOperationType() { + return TableOperationType.REGION_EDIT; + } + + @Override + protected Flow executeFromState(MasterProcedureEnv env, + CloseExcessRegionReplicasProcedureState state) + throws ProcedureSuspendedException, ProcedureYieldException, InterruptedException { + LOG.trace("{} execute state={}", this, state); + switch (state) { + case CLOSE_EXCESS_REGION_REPLICAS_SCHEDULE: + MutableBoolean submitted = new MutableBoolean(false); + int inTransitionCount = env.getAssignmentManager() + .submitUnassignProcedureForClosingExcessRegionReplicas(tableName, newReplicaCount, p -> { + submitted.setTrue(); + addChildProcedure(p); + }); + if (inTransitionCount > 0 && submitted.isFalse()) { + // we haven't scheduled any unassign procedures and there are still regions in + // transition, sleep for a while and try again + if (retryCounter == null) { + retryCounter = ProcedureUtil.createRetryCounter(env.getMasterConfiguration()); + } + long backoffMillis = retryCounter.getBackoffTimeAndIncrementAttempts(); + LOG.info( + "There are still {} region(s) in transition for table {} when closing excess" + + " region replicas, suspend {}secs and try again later", + inTransitionCount, tableName, backoffMillis / 1000); + suspend(inTransitionCount, true); + } + setNextState(CloseExcessRegionReplicasProcedureState.CLOSE_EXCESS_REGION_REPLICAS_CONFIRM); + return Flow.HAS_MORE_STATE; + case CLOSE_EXCESS_REGION_REPLICAS_CONFIRM: + int unclosedCount = env.getAssignmentManager() + .numberOfUnclosedExcessRegionReplicas(tableName, newReplicaCount); + if (unclosedCount > 0) { + LOG.info("There are still {} unclosed region(s) for table {} when closing excess" + + " region replicas, continue..."); + setNextState( + CloseExcessRegionReplicasProcedureState.CLOSE_EXCESS_REGION_REPLICAS_SCHEDULE); + } else { + return Flow.NO_MORE_STATE; + } + default: + throw new UnsupportedOperationException("unhandled state=" + state); + } + } + + @Override + protected void rollbackState(MasterProcedureEnv env, + CloseExcessRegionReplicasProcedureState state) throws IOException, InterruptedException { + throw new UnsupportedOperationException(); + } + + @Override + protected CloseExcessRegionReplicasProcedureState getState(int stateId) { + return CloseExcessRegionReplicasProcedureState.forNumber(stateId); + } + + @Override + protected int getStateId(CloseExcessRegionReplicasProcedureState state) { + return state.getNumber(); + } + + @Override + protected CloseExcessRegionReplicasProcedureState getInitialState() { + return CloseExcessRegionReplicasProcedureState.CLOSE_EXCESS_REGION_REPLICAS_SCHEDULE; + } + + @Override + protected void serializeStateData(ProcedureStateSerializer serializer) throws IOException { + CloseExcessRegionReplicasProcedureStateData data = CloseExcessRegionReplicasProcedureStateData + .newBuilder().setTableName(ProtobufUtil.toProtoTableName(tableName)) + .setNewReplicaCount(newReplicaCount).build(); + serializer.serialize(data); + } + + @Override + protected void deserializeStateData(ProcedureStateSerializer serializer) throws IOException { + CloseExcessRegionReplicasProcedureStateData data = + serializer.deserialize(CloseExcessRegionReplicasProcedureStateData.class); + tableName = ProtobufUtil.toTableName(data.getTableName()); + newReplicaCount = data.getNewReplicaCount(); + } + +} diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/master/procedure/ModifyTableProcedure.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/master/procedure/ModifyTableProcedure.java index 9a52dbd079dd..45153612259b 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/master/procedure/ModifyTableProcedure.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/master/procedure/ModifyTableProcedure.java @@ -525,8 +525,7 @@ private void closeExcessReplicasIfNeeded(MasterProcedureEnv env) { if (newReplicaCount >= oldReplicaCount) { return; } - addChildProcedure(env.getAssignmentManager() - .createUnassignProceduresForClosingExcessRegionReplicas(getTableName(), newReplicaCount)); + addChildProcedure(new CloseExcessRegionReplicasProcedure(getTableName(), newReplicaCount)); } /** From d6298ee6b35a0bf9a7594915ce3c9a9b9130666e Mon Sep 17 00:00:00 2001 From: Duo Zhang Date: Thu, 16 May 2024 23:15:54 +0800 Subject: [PATCH 2/2] add test --- .../server/master/MasterProcedure.proto | 2 +- .../CloseExcessRegionReplicasProcedure.java | 11 +- ...educeExcessRegionReplicasBlockedByRIT.java | 107 ++++++++++++++++++ .../TestRegionReplicasWithModifyTable.java | 2 +- 4 files changed, 119 insertions(+), 3 deletions(-) create mode 100644 hbase-server/src/test/java/org/apache/hadoop/hbase/master/assignment/TestReduceExcessRegionReplicasBlockedByRIT.java diff --git a/hbase-protocol-shaded/src/main/protobuf/server/master/MasterProcedure.proto b/hbase-protocol-shaded/src/main/protobuf/server/master/MasterProcedure.proto index b1ca687d6239..48d20b6bef27 100644 --- a/hbase-protocol-shaded/src/main/protobuf/server/master/MasterProcedure.proto +++ b/hbase-protocol-shaded/src/main/protobuf/server/master/MasterProcedure.proto @@ -788,5 +788,5 @@ enum CloseExcessRegionReplicasProcedureState { message CloseExcessRegionReplicasProcedureStateData { required TableName table_name = 1; - required uint32 new_replica_count = 2; + required uint32 new_replica_count = 2; } diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/master/procedure/CloseExcessRegionReplicasProcedure.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/master/procedure/CloseExcessRegionReplicasProcedure.java index fc73df723fc3..61e7c0f86075 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/master/procedure/CloseExcessRegionReplicasProcedure.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/master/procedure/CloseExcessRegionReplicasProcedure.java @@ -32,6 +32,7 @@ import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil; import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProcedureProtos.CloseExcessRegionReplicasProcedureState; import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProcedureProtos.CloseExcessRegionReplicasProcedureStateData; +import org.apache.hadoop.hbase.shaded.protobuf.generated.ProcedureProtos; /** * Procedure for close excess region replicas. @@ -90,7 +91,7 @@ protected Flow executeFromState(MasterProcedureEnv env, "There are still {} region(s) in transition for table {} when closing excess" + " region replicas, suspend {}secs and try again later", inTransitionCount, tableName, backoffMillis / 1000); - suspend(inTransitionCount, true); + suspend((int) backoffMillis, true); } setNextState(CloseExcessRegionReplicasProcedureState.CLOSE_EXCESS_REGION_REPLICAS_CONFIRM); return Flow.HAS_MORE_STATE; @@ -102,6 +103,7 @@ protected Flow executeFromState(MasterProcedureEnv env, + " region replicas, continue..."); setNextState( CloseExcessRegionReplicasProcedureState.CLOSE_EXCESS_REGION_REPLICAS_SCHEDULE); + return Flow.HAS_MORE_STATE; } else { return Flow.NO_MORE_STATE; } @@ -110,6 +112,13 @@ protected Flow executeFromState(MasterProcedureEnv env, } } + @Override + protected synchronized boolean setTimeoutFailure(MasterProcedureEnv env) { + setState(ProcedureProtos.ProcedureState.RUNNABLE); + env.getProcedureScheduler().addFront(this); + return false; + } + @Override protected void rollbackState(MasterProcedureEnv env, CloseExcessRegionReplicasProcedureState state) throws IOException, InterruptedException { diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/master/assignment/TestReduceExcessRegionReplicasBlockedByRIT.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/master/assignment/TestReduceExcessRegionReplicasBlockedByRIT.java new file mode 100644 index 000000000000..4cdf1ba3a633 --- /dev/null +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/master/assignment/TestReduceExcessRegionReplicasBlockedByRIT.java @@ -0,0 +1,107 @@ +/* + * 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.assignment; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.util.concurrent.CompletableFuture; +import org.apache.hadoop.hbase.HBaseClassTestRule; +import org.apache.hadoop.hbase.HBaseTestingUtil; +import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; +import org.apache.hadoop.hbase.client.TableDescriptor; +import org.apache.hadoop.hbase.client.TableDescriptorBuilder; +import org.apache.hadoop.hbase.master.RegionState; +import org.apache.hadoop.hbase.master.procedure.CloseExcessRegionReplicasProcedure; +import org.apache.hadoop.hbase.master.procedure.MasterProcedureEnv; +import org.apache.hadoop.hbase.procedure2.ProcedureExecutor; +import org.apache.hadoop.hbase.testclassification.MasterTests; +import org.apache.hadoop.hbase.testclassification.MediumTests; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +/** + * A test to make sure that we will wait for RIT to finish while closing excess region replicas. See + * HBASE-28582 and related issues for more details. + */ +@Category({ MasterTests.class, MediumTests.class }) +public class TestReduceExcessRegionReplicasBlockedByRIT { + + @ClassRule + public static final HBaseClassTestRule CLASS_RULE = + HBaseClassTestRule.forClass(TestReduceExcessRegionReplicasBlockedByRIT.class); + + private static final HBaseTestingUtil UTIL = new HBaseTestingUtil(); + + private static TableDescriptor TD = + TableDescriptorBuilder.newBuilder(TableName.valueOf("CloseExcessRegionReplicas")) + .setColumnFamily(ColumnFamilyDescriptorBuilder.of("family")).setRegionReplication(4).build(); + + @BeforeClass + public static void setUp() throws Exception { + UTIL.startMiniCluster(1); + UTIL.getAdmin().createTable(TD); + UTIL.waitTableAvailable(TD.getTableName()); + UTIL.waitUntilNoRegionsInTransition(); + } + + @AfterClass + public static void tearDown() throws Exception { + UTIL.shutdownMiniCluster(); + } + + @Test + public void testRIT() throws Exception { + RegionStateNode rsn = UTIL.getMiniHBaseCluster().getMaster().getAssignmentManager() + .getRegionStates().getTableRegionStateNodes(TD.getTableName()).stream() + .filter(rn -> rn.getRegionInfo().getReplicaId() > 1).findAny().get(); + // fake a TRSP to block the CloseExcessRegionReplicasProcedure + TransitRegionStateProcedure trsp = new TransitRegionStateProcedure(); + rsn.setProcedure(trsp); + TableDescriptor newTd = TableDescriptorBuilder.newBuilder(TD).setRegionReplication(2).build(); + CompletableFuture future = UTIL.getAsyncConnection().getAdmin().modifyTable(newTd); + ProcedureExecutor procExec = + UTIL.getMiniHBaseCluster().getMaster().getMasterProcedureExecutor(); + UTIL.waitFor(5000, () -> procExec.getProcedures().stream() + .anyMatch(p -> p instanceof CloseExcessRegionReplicasProcedure && !p.isFinished())); + CloseExcessRegionReplicasProcedure proc = + procExec.getProcedures().stream().filter(p -> p instanceof CloseExcessRegionReplicasProcedure) + .map(p -> (CloseExcessRegionReplicasProcedure) p).findFirst().get(); + // make sure that the procedure can not finish + for (int i = 0; i < 5; i++) { + Thread.sleep(3000); + assertFalse(proc.isFinished()); + } + assertTrue(rsn.isInState(RegionState.State.OPEN)); + // unset the procedure, so we could make progress on CloseExcessRegionReplicasProcedure + rsn.unsetProcedure(trsp); + UTIL.waitFor(60000, () -> proc.isFinished()); + + future.get(); + + // the region should be in CLOSED state, and should have been removed from AM + assertTrue(rsn.isInState(RegionState.State.CLOSED)); + // only 2 replicas now + assertEquals(2, UTIL.getMiniHBaseCluster().getRegions(TD.getTableName()).size()); + } +} diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestRegionReplicasWithModifyTable.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestRegionReplicasWithModifyTable.java index ca6a745d91bf..935e33dacdc4 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestRegionReplicasWithModifyTable.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestRegionReplicasWithModifyTable.java @@ -157,7 +157,7 @@ public void testRegionReplicasByEnableTableWhenReplicaCountIsDecreasedWithMultip } @Test - public void testRegionReplicasByEnableTableWhenReplicaCountIsIncreasedWithmultipleRegions() + public void testRegionReplicasByEnableTableWhenReplicaCountIsIncreasedWithMultipleRegions() throws Exception { enableReplicationByModification(true, 2, 3, 15); }