View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.replication;
20  
21  import com.google.common.base.Splitter;
22  
23  import java.util.Collection;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.TreeMap;
28  
29  import org.apache.hadoop.conf.Configuration;
30  import org.apache.hadoop.hbase.TableName;
31  import org.apache.hadoop.hbase.classification.InterfaceAudience;
32  import org.apache.hadoop.hbase.classification.InterfaceStability;
33  import org.apache.hadoop.hbase.util.Bytes;
34  
35  /**
36   * A configuration for the replication peer cluster.
37   */
38  @InterfaceAudience.Public
39  @InterfaceStability.Evolving
40  public class ReplicationPeerConfig {
41  
42    private String clusterKey;
43    private String replicationEndpointImpl;
44    private final Map<byte[], byte[]> peerData;
45    private final Map<String, String> configuration;
46    private Map<TableName, ? extends Collection<String>> tableCFsMap = null;
47    private long bandwidth = 0;
48  
49    public static final String HBASE_REPLICATION_PEER_BASE_CONFIG =
50      "hbase.replication.peer.base.config";
51  
52    public ReplicationPeerConfig() {
53      this.peerData = new TreeMap<byte[], byte[]>(Bytes.BYTES_COMPARATOR);
54      this.configuration = new HashMap<String, String>(0);
55    }
56  
57    /**
58     * Set the clusterKey which is the concatenation of the slave cluster's:
59     *          hbase.zookeeper.quorum:hbase.zookeeper.property.clientPort:zookeeper.znode.parent
60     */
61    public ReplicationPeerConfig setClusterKey(String clusterKey) {
62      this.clusterKey = clusterKey;
63      return this;
64    }
65  
66    /**
67     * Sets the ReplicationEndpoint plugin class for this peer.
68     * @param replicationEndpointImpl a class implementing ReplicationEndpoint
69     */
70    public ReplicationPeerConfig setReplicationEndpointImpl(String replicationEndpointImpl) {
71      this.replicationEndpointImpl = replicationEndpointImpl;
72      return this;
73    }
74  
75    public String getClusterKey() {
76      return clusterKey;
77    }
78  
79    public String getReplicationEndpointImpl() {
80      return replicationEndpointImpl;
81    }
82  
83    public Map<byte[], byte[]> getPeerData() {
84      return peerData;
85    }
86  
87    public Map<String, String> getConfiguration() {
88      return configuration;
89    }
90  
91    public Map<TableName, List<String>> getTableCFsMap() {
92      return (Map<TableName, List<String>>) tableCFsMap;
93    }
94  
95    public void setTableCFsMap(Map<TableName, ? extends Collection<String>> tableCFsMap) {
96      this.tableCFsMap = tableCFsMap;
97    }
98  
99    public long getBandwidth() {
100     return this.bandwidth;
101   }
102 
103   public ReplicationPeerConfig setBandwidth(long bandwidth) {
104     this.bandwidth = bandwidth;
105     return this;
106   }
107 
108   /**
109    * Helper method to add base peer configs from Configuration to ReplicationPeerConfig
110    * if not present in latter.
111    *
112    * This merges the user supplied peer configuration
113    * {@link org.apache.hadoop.hbase.replication.ReplicationPeerConfig} with peer configs
114    * provided as property hbase.replication.peer.base.configs in hbase configuration.
115    * Expected format for this hbase configuration is "k1=v1;k2=v2,v2_1". Original value
116    * of conf is retained if already present in ReplicationPeerConfig.
117    *
118    * @param conf Configuration
119    */
120   public void addBasePeerConfigsIfNotPresent(Configuration conf) {
121     String basePeerConfigs = conf.get(HBASE_REPLICATION_PEER_BASE_CONFIG, "");
122 
123     if (basePeerConfigs.length() != 0) {
124       Map<String, String> basePeerConfigMap = Splitter.on(';').trimResults().omitEmptyStrings()
125         .withKeyValueSeparator("=").split(basePeerConfigs);
126       for (Map.Entry<String,String> entry : basePeerConfigMap.entrySet()) {
127         String configName = entry.getKey();
128         String configValue = entry.getValue();
129         // Only override if base config does not exist in existing replication peer configs
130         if (!this.getConfiguration().containsKey(configName)) {
131           this.getConfiguration().put(configName, configValue);
132         }
133       }
134     }
135   }
136 
137   @Override
138   public String toString() {
139     StringBuilder builder = new StringBuilder("clusterKey=").append(clusterKey).append(",");
140     builder.append("replicationEndpointImpl=").append(replicationEndpointImpl);
141     if (tableCFsMap != null) {
142       builder.append(tableCFsMap.toString()).append(",");
143     }
144     builder.append("bandwidth=").append(bandwidth);
145     return builder.toString();
146   }
147 }