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.replication; 019 020import java.io.IOException; 021import org.apache.hadoop.hbase.master.procedure.MasterProcedureEnv; 022import org.apache.hadoop.hbase.master.procedure.PeerProcedureInterface; 023import org.apache.hadoop.hbase.master.procedure.ProcedurePrepareLatch; 024import org.apache.hadoop.hbase.procedure2.ProcedureStateSerializer; 025import org.apache.hadoop.hbase.procedure2.StateMachineProcedure; 026import org.apache.yetus.audience.InterfaceAudience; 027 028import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProcedureProtos.PeerProcedureStateData; 029 030/** 031 * The base class for all replication peer related procedure. 032 */ 033@InterfaceAudience.Private 034public abstract class AbstractPeerProcedure<TState> 035 extends StateMachineProcedure<MasterProcedureEnv, TState> implements PeerProcedureInterface { 036 037 protected String peerId; 038 039 // used to keep compatible with old client where we can only return after updateStorage. 040 protected ProcedurePrepareLatch latch; 041 042 protected AbstractPeerProcedure() { 043 } 044 045 protected AbstractPeerProcedure(String peerId) { 046 this.peerId = peerId; 047 this.latch = ProcedurePrepareLatch.createLatch(2, 0); 048 } 049 050 public ProcedurePrepareLatch getLatch() { 051 return latch; 052 } 053 054 @Override 055 public String getPeerId() { 056 return peerId; 057 } 058 059 @Override 060 protected boolean waitInitialized(MasterProcedureEnv env) { 061 return env.waitInitialized(this); 062 } 063 064 @Override 065 protected LockState acquireLock(MasterProcedureEnv env) { 066 if (env.getProcedureScheduler().waitPeerExclusiveLock(this, peerId)) { 067 return LockState.LOCK_EVENT_WAIT; 068 } 069 return LockState.LOCK_ACQUIRED; 070 } 071 072 @Override 073 protected void releaseLock(MasterProcedureEnv env) { 074 env.getProcedureScheduler().wakePeerExclusiveLock(this, peerId); 075 } 076 077 @Override 078 protected boolean holdLock(MasterProcedureEnv env) { 079 return true; 080 } 081 082 @Override 083 protected void serializeStateData(ProcedureStateSerializer serializer) throws IOException { 084 super.serializeStateData(serializer); 085 serializer.serialize(PeerProcedureStateData.newBuilder().setPeerId(peerId).build()); 086 } 087 088 @Override 089 protected void deserializeStateData(ProcedureStateSerializer serializer) throws IOException { 090 super.deserializeStateData(serializer); 091 peerId = serializer.deserialize(PeerProcedureStateData.class).getPeerId(); 092 } 093 094 protected final void checkPeerModificationEnabled(MasterProcedureEnv env) throws IOException { 095 if (!env.getMasterServices().isReplicationPeerModificationEnabled()) { 096 throw new IOException("Replication peer modification disabled"); 097 } 098 } 099}