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(MasterProcedureEnv env, RegionInfo hri, 045 ProcedurePrepareLatch latch) { 046 super(env, latch); 047 this.hri = hri; 048 } 049 050 protected AbstractStateMachineRegionProcedure() { 051 // Required by the Procedure framework to create the procedure on replay 052 super(); 053 } 054 055 /** Returns The RegionInfo of the region we are operating on. */ 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 @Override 082 protected boolean holdLock(MasterProcedureEnv env) { 083 return true; 084 } 085 086 @Override 087 protected LockState acquireLock(final MasterProcedureEnv env) { 088 if (env.getProcedureScheduler().waitRegions(this, getTableName(), getRegion())) { 089 return LockState.LOCK_EVENT_WAIT; 090 } 091 return LockState.LOCK_ACQUIRED; 092 } 093 094 @Override 095 protected void releaseLock(final MasterProcedureEnv env) { 096 env.getProcedureScheduler().wakeRegions(this, getTableName(), getRegion()); 097 } 098 099 protected void setFailure(Throwable cause) { 100 super.setFailure(getClass().getSimpleName(), cause); 101 } 102 103 @Override 104 protected void serializeStateData(ProcedureStateSerializer serializer) throws IOException { 105 super.serializeStateData(serializer); 106 serializer.serialize(ProtobufUtil.toRegionInfo(getRegion())); 107 } 108 109 @Override 110 protected void deserializeStateData(ProcedureStateSerializer serializer) throws IOException { 111 super.deserializeStateData(serializer); 112 this.hri = ProtobufUtil.toRegionInfo(serializer.deserialize(HBaseProtos.RegionInfo.class)); 113 } 114}