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.AddPeerStateData;
032import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProcedureProtos.PeerModificationState;
033
034/**
035 * The procedure for adding a new replication peer.
036 */
037@InterfaceAudience.Private
038public class AddPeerProcedure extends ModifyPeerProcedure {
039
040  private static final Logger LOG = LoggerFactory.getLogger(AddPeerProcedure.class);
041
042  private ReplicationPeerConfig peerConfig;
043
044  private boolean enabled;
045
046  public AddPeerProcedure() {
047  }
048
049  public AddPeerProcedure(String peerId, ReplicationPeerConfig peerConfig, boolean enabled) {
050    super(peerId);
051    this.peerConfig = peerConfig;
052    this.enabled = enabled;
053  }
054
055  @Override
056  public PeerOperationType getPeerOperationType() {
057    return PeerOperationType.ADD;
058  }
059
060  @Override
061  protected PeerModificationState nextStateAfterRefresh() {
062    return peerConfig.isSerial()
063      ? PeerModificationState.SERIAL_PEER_REOPEN_REGIONS
064      : super.nextStateAfterRefresh();
065  }
066
067  @Override
068  protected void updateLastPushedSequenceIdForSerialPeer(MasterProcedureEnv env)
069    throws IOException, ReplicationException {
070    setLastPushedSequenceId(env, peerConfig);
071  }
072
073  @Override
074  protected boolean enablePeerBeforeFinish() {
075    return enabled;
076  }
077
078  @Override
079  protected ReplicationPeerConfig getNewPeerConfig() {
080    return peerConfig;
081  }
082
083  @Override
084  protected void prePeerModification(MasterProcedureEnv env)
085    throws IOException, ReplicationException {
086    MasterCoprocessorHost cpHost = env.getMasterCoprocessorHost();
087    if (cpHost != null) {
088      cpHost.preAddReplicationPeer(peerId, peerConfig);
089    }
090    env.getReplicationPeerManager().preAddPeer(peerId, peerConfig);
091  }
092
093  @Override
094  protected void updatePeerStorage(MasterProcedureEnv env) throws ReplicationException {
095    env.getReplicationPeerManager().addPeer(peerId, peerConfig,
096      peerConfig.isSerial() ? false : enabled);
097  }
098
099  @Override
100  protected void postPeerModification(MasterProcedureEnv env)
101    throws IOException, ReplicationException {
102    LOG.info("Successfully added {} peer {}, config {}", enabled ? "ENABLED" : "DISABLED", peerId,
103      peerConfig);
104    MasterCoprocessorHost cpHost = env.getMasterCoprocessorHost();
105    if (cpHost != null) {
106      env.getMasterCoprocessorHost().postAddReplicationPeer(peerId, peerConfig);
107    }
108  }
109
110  @Override
111  protected void serializeStateData(ProcedureStateSerializer serializer) throws IOException {
112    super.serializeStateData(serializer);
113    serializer.serialize(AddPeerStateData.newBuilder()
114      .setPeerConfig(ReplicationPeerConfigUtil.convert(peerConfig)).setEnabled(enabled).build());
115  }
116
117  @Override
118  protected void deserializeStateData(ProcedureStateSerializer serializer) throws IOException {
119    super.deserializeStateData(serializer);
120    AddPeerStateData data = serializer.deserialize(AddPeerStateData.class);
121    peerConfig = ReplicationPeerConfigUtil.convert(data.getPeerConfig());
122    enabled = data.getEnabled();
123  }
124}