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.replication.regionserver;
019
020import org.apache.hadoop.hbase.executor.EventType;
021import org.apache.hadoop.hbase.procedure2.BaseRSProcedureCallable;
022import org.apache.yetus.audience.InterfaceAudience;
023import org.slf4j.Logger;
024import org.slf4j.LoggerFactory;
025
026import org.apache.hbase.thirdparty.com.google.protobuf.InvalidProtocolBufferException;
027
028import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProcedureProtos.PeerModificationType;
029import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProcedureProtos.RefreshPeerParameter;
030
031/**
032 * The callable executed at RS side to refresh the peer config/state. <br/>
033 */
034@InterfaceAudience.Private
035public class RefreshPeerCallable extends BaseRSProcedureCallable {
036
037  private static final Logger LOG = LoggerFactory.getLogger(RefreshPeerCallable.class);
038
039  private String peerId;
040
041  private PeerModificationType type;
042
043  private int stage;
044
045  @Override
046  protected void doCall() throws Exception {
047    LOG.info("Received a peer change event, peerId=" + peerId + ", type=" + type);
048    PeerProcedureHandler handler = rs.getReplicationSourceService().getPeerProcedureHandler();
049    switch (type) {
050      case ADD_PEER:
051        handler.addPeer(this.peerId);
052        break;
053      case REMOVE_PEER:
054        handler.removePeer(this.peerId);
055        break;
056      case ENABLE_PEER:
057        handler.enablePeer(this.peerId);
058        break;
059      case DISABLE_PEER:
060        handler.disablePeer(this.peerId);
061        break;
062      case UPDATE_PEER_CONFIG:
063        handler.updatePeerConfig(this.peerId);
064        break;
065      case TRANSIT_SYNC_REPLICATION_STATE:
066        handler.transitSyncReplicationPeerState(peerId, stage, rs);
067        break;
068      default:
069        throw new IllegalArgumentException("Unknown peer modification type: " + type);
070    }
071  }
072
073  @Override
074  protected void initParameter(byte[] parameter) throws InvalidProtocolBufferException {
075    RefreshPeerParameter param = RefreshPeerParameter.parseFrom(parameter);
076    this.peerId = param.getPeerId();
077    this.type = param.getType();
078    this.stage = param.getStage();
079  }
080
081  @Override
082  public EventType getEventType() {
083    return EventType.RS_REFRESH_PEER;
084  }
085}