001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.master.procedure;
019
020import java.io.IOException;
021import java.util.function.Consumer;
022import org.apache.hadoop.hbase.TableName;
023import org.apache.hadoop.hbase.master.assignment.TransitRegionStateProcedure;
024import org.apache.hadoop.hbase.procedure2.ProcedureStateSerializer;
025import org.apache.yetus.audience.InterfaceAudience;
026
027import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
028import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProcedureProtos.CloseExcessRegionReplicasProcedureState;
029import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProcedureProtos.CloseExcessRegionReplicasProcedureStateData;
030
031/**
032 * Procedure for close excess region replicas.
033 */
034@InterfaceAudience.Private
035public class CloseExcessRegionReplicasProcedure
036  extends AbstractCloseTableRegionsProcedure<CloseExcessRegionReplicasProcedureState> {
037
038  private int newReplicaCount;
039
040  public CloseExcessRegionReplicasProcedure() {
041  }
042
043  public CloseExcessRegionReplicasProcedure(TableName tableName, int newReplicaCount) {
044    super(tableName);
045    this.newReplicaCount = newReplicaCount;
046  }
047
048  @Override
049  protected CloseExcessRegionReplicasProcedureState getState(int stateId) {
050    return CloseExcessRegionReplicasProcedureState.forNumber(stateId);
051  }
052
053  @Override
054  protected int getStateId(CloseExcessRegionReplicasProcedureState state) {
055    return state.getNumber();
056  }
057
058  @Override
059  protected CloseExcessRegionReplicasProcedureState getInitialState() {
060    return CloseExcessRegionReplicasProcedureState.CLOSE_EXCESS_REGION_REPLICAS_SCHEDULE;
061  }
062
063  @Override
064  protected void serializeStateData(ProcedureStateSerializer serializer) throws IOException {
065    super.serializeStateData(serializer);
066    CloseExcessRegionReplicasProcedureStateData data = CloseExcessRegionReplicasProcedureStateData
067      .newBuilder().setTableName(ProtobufUtil.toProtoTableName(tableName))
068      .setNewReplicaCount(newReplicaCount).build();
069    serializer.serialize(data);
070  }
071
072  @Override
073  protected void deserializeStateData(ProcedureStateSerializer serializer) throws IOException {
074    super.deserializeStateData(serializer);
075    CloseExcessRegionReplicasProcedureStateData data =
076      serializer.deserialize(CloseExcessRegionReplicasProcedureStateData.class);
077    tableName = ProtobufUtil.toTableName(data.getTableName());
078    newReplicaCount = data.getNewReplicaCount();
079  }
080
081  @Override
082  protected CloseExcessRegionReplicasProcedureState getConfirmState() {
083    return CloseExcessRegionReplicasProcedureState.CLOSE_EXCESS_REGION_REPLICAS_CONFIRM;
084  }
085
086  @Override
087  protected int submitUnassignProcedure(MasterProcedureEnv env,
088    Consumer<TransitRegionStateProcedure> submit) {
089    return env.getAssignmentManager()
090      .submitUnassignProcedureForClosingExcessRegionReplicas(tableName, newReplicaCount, submit);
091  }
092
093  @Override
094  protected int numberOfUnclosedRegions(MasterProcedureEnv env) {
095    return env.getAssignmentManager().numberOfUnclosedExcessRegionReplicas(tableName,
096      newReplicaCount);
097  }
098
099}