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 /** 050 * @return The RegionInfo of the region we are operating on. 051 */ 052 public RegionInfo getRegion() { 053 return this.hri; 054 } 055 056 /** 057 * Used when deserializing. Otherwise, DON'T TOUCH IT! 058 */ 059 protected void setRegion(final RegionInfo hri) { 060 this.hri = hri; 061 } 062 063 @Override 064 public TableName getTableName() { 065 return getRegion().getTable(); 066 } 067 068 @Override 069 public abstract TableOperationType getTableOperationType(); 070 071 @Override 072 public void toStringClassDetails(final StringBuilder sb) { 073 super.toStringClassDetails(sb); 074 sb.append(", region=").append(getRegion().getShortNameToLog()); 075 } 076 077 @Override 078 protected boolean holdLock(MasterProcedureEnv env) { 079 return true; 080 } 081 082 @Override 083 protected LockState acquireLock(final MasterProcedureEnv env) { 084 if (env.getProcedureScheduler().waitRegions(this, getTableName(), getRegion())) { 085 return LockState.LOCK_EVENT_WAIT; 086 } 087 return LockState.LOCK_ACQUIRED; 088 } 089 090 @Override 091 protected void releaseLock(final MasterProcedureEnv env) { 092 env.getProcedureScheduler().wakeRegions(this, getTableName(), getRegion()); 093 } 094 095 protected void setFailure(Throwable cause) { 096 super.setFailure(getClass().getSimpleName(), cause); 097 } 098 099 @Override 100 protected void serializeStateData(ProcedureStateSerializer serializer) throws IOException { 101 super.serializeStateData(serializer); 102 serializer.serialize(ProtobufUtil.toRegionInfo(getRegion())); 103 } 104 105 @Override 106 protected void deserializeStateData(ProcedureStateSerializer serializer) throws IOException { 107 super.deserializeStateData(serializer); 108 this.hri = ProtobufUtil.toRegionInfo(serializer.deserialize(HBaseProtos.RegionInfo.class)); 109 } 110}