View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.zookeeper;
20  
21  import java.util.Map.Entry;
22  import java.util.Properties;
23  
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.hbase.HConstants;
26  import org.apache.hadoop.hbase.classification.InterfaceAudience;
27  
28  /**
29   * Utility methods for reading, and building the ZooKeeper configuration.
30   *
31   * The order and priority for reading the config are as follows:
32   * (1). Property with "hbase.zookeeper.property." prefix from HBase XML
33   * (2). other zookeeper related properties in HBASE XML
34   */
35  @InterfaceAudience.Private
36  public class ZKConfig {
37  
38    private static final String VARIABLE_START = "${";
39  
40    /**
41     * Make a Properties object holding ZooKeeper config.
42     * Parses the corresponding config options from the HBase XML configs
43     * and generates the appropriate ZooKeeper properties.
44     * @param conf Configuration to read from.
45     * @return Properties holding mappings representing ZooKeeper config file.
46     */
47    public static Properties makeZKProps(Configuration conf) {
48      return makeZKPropsFromHbaseConfig(conf);
49    }
50  
51    /**
52     * Make a Properties object holding ZooKeeper config.
53     * Parses the corresponding config options from the HBase XML configs
54     * and generates the appropriate ZooKeeper properties.
55     *
56     * @param conf Configuration to read from.
57     * @return Properties holding mappings representing ZooKeeper config file.
58     */
59    private static Properties makeZKPropsFromHbaseConfig(Configuration conf) {
60      Properties zkProperties = new Properties();
61  
62      // Directly map all of the hbase.zookeeper.property.KEY properties.
63      // Synchronize on conf so no loading of configs while we iterate
64      synchronized (conf) {
65        for (Entry<String, String> entry : conf) {
66          String key = entry.getKey();
67          if (key.startsWith(HConstants.ZK_CFG_PROPERTY_PREFIX)) {
68            String zkKey = key.substring(HConstants.ZK_CFG_PROPERTY_PREFIX_LEN);
69            String value = entry.getValue();
70            // If the value has variables substitutions, need to do a get.
71            if (value.contains(VARIABLE_START)) {
72              value = conf.get(key);
73            }
74            zkProperties.setProperty(zkKey, value);
75          }
76        }
77      }
78  
79      // If clientPort is not set, assign the default.
80      if (zkProperties.getProperty(HConstants.CLIENT_PORT_STR) == null) {
81        zkProperties.put(HConstants.CLIENT_PORT_STR,
82            HConstants.DEFAULT_ZOOKEPER_CLIENT_PORT);
83      }
84  
85      // Create the server.X properties.
86      int peerPort = conf.getInt("hbase.zookeeper.peerport", 2888);
87      int leaderPort = conf.getInt("hbase.zookeeper.leaderport", 3888);
88  
89      final String[] serverHosts = conf.getStrings(HConstants.ZOOKEEPER_QUORUM,
90                                                   HConstants.LOCALHOST);
91      String serverHost;
92      String address;
93      String key;
94      for (int i = 0; i < serverHosts.length; ++i) {
95        if (serverHosts[i].contains(":")) {
96          serverHost = serverHosts[i].substring(0, serverHosts[i].indexOf(':'));
97        } else {
98          serverHost = serverHosts[i];
99        }
100       address = serverHost + ":" + peerPort + ":" + leaderPort;
101       key = "server." + i;
102       zkProperties.put(key, address);
103     }
104 
105     return zkProperties;
106   }
107 
108   /**
109    * Return the ZK Quorum servers string given the specified configuration
110    *
111    * @param conf
112    * @return Quorum servers String
113    */
114   private static String getZKQuorumServersStringFromHbaseConfig(Configuration conf) {
115     String defaultClientPort = Integer.toString(
116         conf.getInt(HConstants.ZOOKEEPER_CLIENT_PORT, HConstants.DEFAULT_ZOOKEPER_CLIENT_PORT));
117 
118     // Build the ZK quorum server string with "server:clientport" list, separated by ','
119     final String[] serverHosts =
120         conf.getStrings(HConstants.ZOOKEEPER_QUORUM, HConstants.LOCALHOST);
121     return buildQuorumServerString(serverHosts, defaultClientPort);
122   }
123 
124   /**
125    * Build the ZK quorum server string with "server:clientport" list, separated by ','
126    *
127    * @param serverHosts a list of servers for ZK quorum
128    * @param clientPort the default client port
129    * @return the string for a list of "server:port" separated by ","
130    */
131   public static String buildQuorumServerString(String[] serverHosts, String clientPort) {
132     StringBuilder quorumStringBuilder = new StringBuilder();
133     String serverHost;
134     for (int i = 0; i < serverHosts.length; ++i) {
135       if (serverHosts[i].contains(":")) {
136         serverHost = serverHosts[i]; // just use the port specified from the input
137       } else {
138         serverHost = serverHosts[i] + ":" + clientPort;
139       }
140       if (i > 0) {
141         quorumStringBuilder.append(',');
142       }
143       quorumStringBuilder.append(serverHost);
144     }
145     return quorumStringBuilder.toString();
146   }
147 
148   /**
149    * Return the ZK Quorum servers string given the specified configuration.
150    * @return Quorum servers
151    */
152   public static String getZKQuorumServersString(Configuration conf) {
153     return getZKQuorumServersStringFromHbaseConfig(conf);
154   }
155 }