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