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