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  public AbstractStateMachineRegionProcedure(final MasterProcedureEnv env,
044      final RegionInfo hri) {
045    super(env);
046    this.hri = hri;
047  }
048
049  public AbstractStateMachineRegionProcedure() {
050    // Required by the Procedure framework to create the procedure on replay
051    super();
052  }
053
054  /**
055   * @return The RegionInfo of the region we are operating on.
056   */
057  protected RegionInfo getRegion() {
058    return this.hri;
059  }
060
061  /**
062   * Used when deserializing. Otherwise, DON'T TOUCH IT!
063   */
064  protected void setRegion(final RegionInfo hri) {
065    this.hri = hri;
066  }
067
068  @Override
069  public TableName getTableName() {
070    return getRegion().getTable();
071  }
072
073  @Override
074  public abstract TableOperationType getTableOperationType();
075
076  @Override
077  public void toStringClassDetails(final StringBuilder sb) {
078    super.toStringClassDetails(sb);
079    sb.append(", region=").append(getRegion().getShortNameToLog());
080  }
081
082  /**
083   * Check whether a table is modifiable - exists and either offline or online with config set
084   * @param env MasterProcedureEnv
085   * @throws IOException
086   */
087  @Override
088  protected void checkTableModifiable(final MasterProcedureEnv env) throws IOException {
089    // Checks whether the table exists
090    if (!MetaTableAccessor.tableExists(env.getMasterServices().getConnection(), getTableName())) {
091      throw new TableNotFoundException(getTableName());
092    }
093  }
094
095  @Override
096  protected boolean holdLock(MasterProcedureEnv env) {
097    return true;
098  }
099
100  @Override
101  protected LockState acquireLock(final MasterProcedureEnv env) {
102    if (env.getProcedureScheduler().waitRegions(this, getTableName(), getRegion())) {
103      return LockState.LOCK_EVENT_WAIT;
104    }
105    return LockState.LOCK_ACQUIRED;
106  }
107
108  @Override
109  protected void releaseLock(final MasterProcedureEnv env) {
110    env.getProcedureScheduler().wakeRegions(this, getTableName(), getRegion());
111  }
112
113  protected void setFailure(Throwable cause) {
114    super.setFailure(getClass().getSimpleName(), cause);
115  }
116
117  @Override
118  protected void serializeStateData(ProcedureStateSerializer serializer)
119      throws IOException {
120    super.serializeStateData(serializer);
121    serializer.serialize(ProtobufUtil.toRegionInfo(getRegion()));
122  }
123
124  @Override
125  protected void deserializeStateData(ProcedureStateSerializer serializer)
126      throws IOException {
127    super.deserializeStateData(serializer);
128    this.hri = ProtobufUtil.toRegionInfo(serializer.deserialize(HBaseProtos.RegionInfo.class));
129  }
130}