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