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