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;
019
020import java.util.ArrayList;
021import java.util.List;
022import java.util.Map;
023import java.util.Set;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.hbase.TableName;
026import org.apache.hadoop.hbase.conf.ConfigurationObserver;
027import org.apache.yetus.audience.InterfaceAudience;
028
029@InterfaceAudience.Private
030public class ReplicationPeerImpl implements ReplicationPeer, ConfigurationObserver {
031
032  private volatile Configuration conf;
033
034  private final String id;
035
036  private volatile ReplicationPeerConfig peerConfig;
037
038  private volatile PeerState peerState;
039
040  private final List<ReplicationPeerConfigListener> peerConfigListeners;
041
042  /**
043   * Constructor that takes all the objects required to communicate with the specified peer, except
044   * for the region server addresses.
045   * @param conf       configuration object to this peer
046   * @param id         string representation of this peer's identifier
047   * @param peerConfig configuration for the replication peer
048   */
049  public ReplicationPeerImpl(Configuration conf, String id, boolean peerState,
050    ReplicationPeerConfig peerConfig) {
051    this.conf = conf;
052    this.id = id;
053    setPeerState(peerState);
054    this.peerConfig = peerConfig;
055    this.peerConfigListeners = new ArrayList<>();
056  }
057
058  public void setPeerState(boolean enabled) {
059    this.peerState = enabled ? PeerState.ENABLED : PeerState.DISABLED;
060  }
061
062  public void setPeerConfig(ReplicationPeerConfig peerConfig) {
063    this.peerConfig = peerConfig;
064    peerConfigListeners.forEach(listener -> listener.peerConfigUpdated(peerConfig));
065  }
066
067  /**
068   * Get the identifier of this peer
069   * @return string representation of the id (short)
070   */
071  @Override
072  public String getId() {
073    return id;
074  }
075
076  @Override
077  public PeerState getPeerState() {
078    return peerState;
079  }
080
081  /**
082   * Get the peer config object
083   * @return the ReplicationPeerConfig for this peer
084   */
085  @Override
086  public ReplicationPeerConfig getPeerConfig() {
087    return peerConfig;
088  }
089
090  /**
091   * Get the configuration object required to communicate with this peer
092   * @return configuration object
093   */
094  @Override
095  public Configuration getConfiguration() {
096    return conf;
097  }
098
099  /**
100   * Get replicable (table, cf-list) map of this peer
101   * @return the replicable (table, cf-list) map
102   */
103  @Override
104  public Map<TableName, List<String>> getTableCFs() {
105    return this.peerConfig.getTableCFsMap();
106  }
107
108  /**
109   * Get replicable namespace set of this peer
110   * @return the replicable namespaces set
111   */
112  @Override
113  public Set<String> getNamespaces() {
114    return this.peerConfig.getNamespaces();
115  }
116
117  @Override
118  public long getPeerBandwidth() {
119    return this.peerConfig.getBandwidth();
120  }
121
122  @Override
123  public void registerPeerConfigListener(ReplicationPeerConfigListener listener) {
124    this.peerConfigListeners.add(listener);
125  }
126
127  @Override
128  public void onConfigurationChange(Configuration conf) {
129    this.conf = conf;
130  }
131}