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.client.replication.ReplicationPeerConfigUtil;
022import org.apache.hadoop.hbase.master.MasterCoprocessorHost;
023import org.apache.hadoop.hbase.master.procedure.MasterProcedureEnv;
024import org.apache.hadoop.hbase.procedure2.ProcedureStateSerializer;
025import org.apache.hadoop.hbase.replication.ReplicationException;
026import org.apache.hadoop.hbase.replication.ReplicationPeerConfig;
027import org.apache.yetus.audience.InterfaceAudience;
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030
031import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProcedureProtos.RemovePeerStateData;
032
033/**
034 * The procedure for removing a replication peer.
035 */
036@InterfaceAudience.Private
037public class RemovePeerProcedure extends ModifyPeerProcedure {
038
039  private static final Logger LOG = LoggerFactory.getLogger(RemovePeerProcedure.class);
040
041  private ReplicationPeerConfig peerConfig;
042
043  public RemovePeerProcedure() {
044  }
045
046  public RemovePeerProcedure(String peerId) {
047    super(peerId);
048  }
049
050  @Override
051  public PeerOperationType getPeerOperationType() {
052    return PeerOperationType.REMOVE;
053  }
054
055  @Override
056  protected void prePeerModification(MasterProcedureEnv env) throws IOException {
057    MasterCoprocessorHost cpHost = env.getMasterCoprocessorHost();
058    if (cpHost != null) {
059      cpHost.preRemoveReplicationPeer(peerId);
060    }
061    peerConfig = env.getReplicationPeerManager().preRemovePeer(peerId);
062  }
063
064  @Override
065  protected void updatePeerStorage(MasterProcedureEnv env) throws ReplicationException {
066    env.getReplicationPeerManager().removePeer(peerId);
067  }
068
069  private void removeRemoteWALs(MasterProcedureEnv env) throws IOException {
070    env.getMasterServices().getSyncReplicationReplayWALManager().removePeerRemoteWALs(peerId);
071  }
072
073  @Override  protected void postPeerModification(MasterProcedureEnv env)
074      throws IOException, ReplicationException {
075    if (peerConfig.isSyncReplication()) {
076      removeRemoteWALs(env);
077    }
078    env.getReplicationPeerManager().removeAllQueuesAndHFileRefs(peerId);
079    if (peerConfig.isSerial()) {
080      env.getReplicationPeerManager().removeAllLastPushedSeqIds(peerId);
081    }
082    LOG.info("Successfully removed peer {}", peerId);
083    MasterCoprocessorHost cpHost = env.getMasterCoprocessorHost();
084    if (cpHost != null) {
085      cpHost.postRemoveReplicationPeer(peerId);
086    }
087  }
088
089  @Override
090  protected void serializeStateData(ProcedureStateSerializer serializer) throws IOException {
091    super.serializeStateData(serializer);
092    RemovePeerStateData.Builder builder = RemovePeerStateData.newBuilder();
093    if (peerConfig != null) {
094      builder.setPeerConfig(ReplicationPeerConfigUtil.convert(peerConfig));
095    }
096    serializer.serialize(builder.build());
097  }
098
099  @Override
100  protected void deserializeStateData(ProcedureStateSerializer serializer) throws IOException {
101    super.deserializeStateData(serializer);
102    RemovePeerStateData data = serializer.deserialize(RemovePeerStateData.class);
103    if (data.hasPeerConfig()) {
104      this.peerConfig = ReplicationPeerConfigUtil.convert(data.getPeerConfig());
105    }
106  }
107}