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 org.apache.hadoop.hbase.TableName;
022import org.apache.hadoop.hbase.client.RegionInfo;
023import org.apache.hadoop.hbase.procedure2.ProcedureStateSerializer;
024import org.apache.yetus.audience.InterfaceAudience;
025
026import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
027import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos;
028
029/**
030 * Base class for all the Region procedures that want to use a StateMachine. It provides some basic
031 * helpers like basic locking, sync latch, and toStringClassDetails(). Defaults to holding the lock
032 * for the life of the procedure.
033 */
034@InterfaceAudience.Private
035public abstract class AbstractStateMachineRegionProcedure<TState>
036  extends AbstractStateMachineTableProcedure<TState> {
037  private RegionInfo hri;
038
039  protected AbstractStateMachineRegionProcedure(MasterProcedureEnv env, RegionInfo hri) {
040    super(env);
041    this.hri = hri;
042  }
043
044  protected AbstractStateMachineRegionProcedure() {
045    // Required by the Procedure framework to create the procedure on replay
046    super();
047  }
048
049  /** Returns The RegionInfo of the region we are operating on. */
050  public RegionInfo getRegion() {
051    return this.hri;
052  }
053
054  /**
055   * Used when deserializing. Otherwise, DON'T TOUCH IT!
056   */
057  protected void setRegion(final RegionInfo hri) {
058    this.hri = hri;
059  }
060
061  @Override
062  public TableName getTableName() {
063    return getRegion().getTable();
064  }
065
066  @Override
067  public abstract TableOperationType getTableOperationType();
068
069  @Override
070  public void toStringClassDetails(final StringBuilder sb) {
071    super.toStringClassDetails(sb);
072    sb.append(", region=").append(getRegion().getShortNameToLog());
073  }
074
075  @Override
076  protected boolean holdLock(MasterProcedureEnv env) {
077    return true;
078  }
079
080  @Override
081  protected LockState acquireLock(final MasterProcedureEnv env) {
082    if (env.getProcedureScheduler().waitRegions(this, getTableName(), getRegion())) {
083      return LockState.LOCK_EVENT_WAIT;
084    }
085    return LockState.LOCK_ACQUIRED;
086  }
087
088  @Override
089  protected void releaseLock(final MasterProcedureEnv env) {
090    env.getProcedureScheduler().wakeRegions(this, getTableName(), getRegion());
091  }
092
093  protected void setFailure(Throwable cause) {
094    super.setFailure(getClass().getSimpleName(), cause);
095  }
096
097  @Override
098  protected void serializeStateData(ProcedureStateSerializer serializer) throws IOException {
099    super.serializeStateData(serializer);
100    serializer.serialize(ProtobufUtil.toRegionInfo(getRegion()));
101  }
102
103  @Override
104  protected void deserializeStateData(ProcedureStateSerializer serializer) throws IOException {
105    super.deserializeStateData(serializer);
106    this.hri = ProtobufUtil.toRegionInfo(serializer.deserialize(HBaseProtos.RegionInfo.class));
107  }
108}