HBASE-19936 Introduce a new base class for replication peer procedure

This commit is contained in:
zhangduo 2018-02-05 16:14:25 +08:00
parent 7f7f2b2de5
commit ad3a1ba495
3 changed files with 102 additions and 64 deletions

View File

@ -398,7 +398,7 @@ message RefreshPeerParameter {
required ServerName target_server = 3;
}
message ModifyPeerStateData {
message PeerProcedureStateData {
required string peer_id = 1;
}

View File

@ -0,0 +1,97 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.hbase.master.replication;
import java.io.IOException;
import org.apache.hadoop.hbase.master.procedure.MasterProcedureEnv;
import org.apache.hadoop.hbase.master.procedure.PeerProcedureInterface;
import org.apache.hadoop.hbase.master.procedure.ProcedurePrepareLatch;
import org.apache.hadoop.hbase.procedure2.ProcedureStateSerializer;
import org.apache.hadoop.hbase.procedure2.StateMachineProcedure;
import org.apache.yetus.audience.InterfaceAudience;
import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProcedureProtos.PeerProcedureStateData;
/**
* The base class for all replication peer related procedure.
*/
@InterfaceAudience.Private
public abstract class AbstractPeerProcedure<TState>
extends StateMachineProcedure<MasterProcedureEnv, TState> implements PeerProcedureInterface {
protected String peerId;
private volatile boolean locked;
// used to keep compatible with old client where we can only returns after updateStorage.
protected ProcedurePrepareLatch latch;
protected AbstractPeerProcedure() {
}
protected AbstractPeerProcedure(String peerId) {
this.peerId = peerId;
this.latch = ProcedurePrepareLatch.createLatch(2, 0);
}
public ProcedurePrepareLatch getLatch() {
return latch;
}
@Override
public String getPeerId() {
return peerId;
}
@Override
protected LockState acquireLock(MasterProcedureEnv env) {
if (env.getProcedureScheduler().waitPeerExclusiveLock(this, peerId)) {
return LockState.LOCK_EVENT_WAIT;
}
locked = true;
return LockState.LOCK_ACQUIRED;
}
@Override
protected void releaseLock(MasterProcedureEnv env) {
locked = false;
env.getProcedureScheduler().wakePeerExclusiveLock(this, peerId);
}
@Override
protected boolean holdLock(MasterProcedureEnv env) {
return true;
}
@Override
protected boolean hasLock(MasterProcedureEnv env) {
return locked;
}
@Override
protected void serializeStateData(ProcedureStateSerializer serializer) throws IOException {
super.serializeStateData(serializer);
serializer.serialize(PeerProcedureStateData.newBuilder().setPeerId(peerId).build());
}
@Override
protected void deserializeStateData(ProcedureStateSerializer serializer) throws IOException {
super.deserializeStateData(serializer);
peerId = serializer.deserialize(PeerProcedureStateData.class).getPeerId();
}
}

View File

@ -19,52 +19,30 @@ package org.apache.hadoop.hbase.master.replication;
import java.io.IOException;
import org.apache.hadoop.hbase.master.procedure.MasterProcedureEnv;
import org.apache.hadoop.hbase.master.procedure.PeerProcedureInterface;
import org.apache.hadoop.hbase.master.procedure.ProcedurePrepareLatch;
import org.apache.hadoop.hbase.procedure2.ProcedureStateSerializer;
import org.apache.hadoop.hbase.procedure2.ProcedureSuspendedException;
import org.apache.hadoop.hbase.procedure2.ProcedureYieldException;
import org.apache.hadoop.hbase.procedure2.StateMachineProcedure;
import org.apache.hadoop.hbase.replication.ReplicationException;
import org.apache.yetus.audience.InterfaceAudience;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProcedureProtos.ModifyPeerStateData;
import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProcedureProtos.PeerModificationState;
/**
* The base class for all replication peer related procedure.
* The base class for all replication peer related procedure except sync replication state
* transition.
*/
@InterfaceAudience.Private
public abstract class ModifyPeerProcedure
extends StateMachineProcedure<MasterProcedureEnv, PeerModificationState>
implements PeerProcedureInterface {
public abstract class ModifyPeerProcedure extends AbstractPeerProcedure<PeerModificationState> {
private static final Logger LOG = LoggerFactory.getLogger(ModifyPeerProcedure.class);
protected String peerId;
private volatile boolean locked;
// used to keep compatible with old client where we can only returns after updateStorage.
protected ProcedurePrepareLatch latch;
protected ModifyPeerProcedure() {
}
protected ModifyPeerProcedure(String peerId) {
this.peerId = peerId;
this.latch = ProcedurePrepareLatch.createLatch(2, 0);
}
public ProcedurePrepareLatch getLatch() {
return latch;
}
@Override
public String getPeerId() {
return peerId;
super(peerId);
}
/**
@ -149,31 +127,6 @@ public abstract class ModifyPeerProcedure
}
}
@Override
protected LockState acquireLock(MasterProcedureEnv env) {
if (env.getProcedureScheduler().waitPeerExclusiveLock(this, peerId)) {
return LockState.LOCK_EVENT_WAIT;
}
locked = true;
return LockState.LOCK_ACQUIRED;
}
@Override
protected void releaseLock(MasterProcedureEnv env) {
locked = false;
env.getProcedureScheduler().wakePeerExclusiveLock(this, peerId);
}
@Override
protected boolean holdLock(MasterProcedureEnv env) {
return true;
}
@Override
protected boolean hasLock(MasterProcedureEnv env) {
return locked;
}
@Override
protected void rollbackState(MasterProcedureEnv env, PeerModificationState state)
throws IOException, InterruptedException {
@ -199,16 +152,4 @@ public abstract class ModifyPeerProcedure
protected PeerModificationState getInitialState() {
return PeerModificationState.PRE_PEER_MODIFICATION;
}
@Override
protected void serializeStateData(ProcedureStateSerializer serializer) throws IOException {
super.serializeStateData(serializer);
serializer.serialize(ModifyPeerStateData.newBuilder().setPeerId(peerId).build());
}
@Override
protected void deserializeStateData(ProcedureStateSerializer serializer) throws IOException {
super.deserializeStateData(serializer);
peerId = serializer.deserialize(ModifyPeerStateData.class).getPeerId();
}
}