001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019package org.apache.hadoop.hbase.zookeeper;
020
021import java.io.IOException;
022import java.util.Map.Entry;
023import java.util.Properties;
024
025import org.apache.hadoop.conf.Configuration;
026import org.apache.hadoop.hbase.HConstants;
027import org.apache.hadoop.util.StringUtils;
028import org.apache.yetus.audience.InterfaceAudience;
029
030/**
031 * Utility methods for reading, and building the ZooKeeper configuration.
032 *
033 * The order and priority for reading the config are as follows:
034 * (1). Property with "hbase.zookeeper.property." prefix from HBase XML
035 * (2). other zookeeper related properties in HBASE XML
036 */
037@InterfaceAudience.Private
038public final class ZKConfig {
039
040  private static final String VARIABLE_START = "${";
041
042  private ZKConfig() {
043  }
044
045  /**
046   * Make a Properties object holding ZooKeeper config.
047   * Parses the corresponding config options from the HBase XML configs
048   * and generates the appropriate ZooKeeper properties.
049   * @param conf Configuration to read from.
050   * @return Properties holding mappings representing ZooKeeper config file.
051   */
052  public static Properties makeZKProps(Configuration conf) {
053    return makeZKPropsFromHbaseConfig(conf);
054  }
055
056  /**
057   * Make a Properties object holding ZooKeeper config.
058   * Parses the corresponding config options from the HBase XML configs
059   * and generates the appropriate ZooKeeper properties.
060   *
061   * @param conf Configuration to read from.
062   * @return Properties holding mappings representing ZooKeeper config file.
063   */
064  private static Properties makeZKPropsFromHbaseConfig(Configuration conf) {
065    Properties zkProperties = new Properties();
066
067    // Directly map all of the hbase.zookeeper.property.KEY properties.
068    // Synchronize on conf so no loading of configs while we iterate
069    synchronized (conf) {
070      for (Entry<String, String> entry : conf) {
071        String key = entry.getKey();
072        if (key.startsWith(HConstants.ZK_CFG_PROPERTY_PREFIX)) {
073          String zkKey = key.substring(HConstants.ZK_CFG_PROPERTY_PREFIX_LEN);
074          String value = entry.getValue();
075          // If the value has variables substitutions, need to do a get.
076          if (value.contains(VARIABLE_START)) {
077            value = conf.get(key);
078          }
079          zkProperties.setProperty(zkKey, value);
080        }
081      }
082    }
083
084    // If clientPort is not set, assign the default.
085    if (zkProperties.getProperty(HConstants.CLIENT_PORT_STR) == null) {
086      zkProperties.put(HConstants.CLIENT_PORT_STR,
087          HConstants.DEFAULT_ZOOKEEPER_CLIENT_PORT);
088    }
089
090    // Create the server.X properties.
091    int peerPort = conf.getInt("hbase.zookeeper.peerport", 2888);
092    int leaderPort = conf.getInt("hbase.zookeeper.leaderport", 3888);
093
094    final String[] serverHosts = conf.getStrings(HConstants.ZOOKEEPER_QUORUM,
095                                                 HConstants.LOCALHOST);
096    String serverHost;
097    String address;
098    String key;
099    for (int i = 0; i < serverHosts.length; ++i) {
100      if (serverHosts[i].contains(":")) {
101        serverHost = serverHosts[i].substring(0, serverHosts[i].indexOf(':'));
102      } else {
103        serverHost = serverHosts[i];
104      }
105      address = serverHost + ":" + peerPort + ":" + leaderPort;
106      key = "server." + i;
107      zkProperties.put(key, address);
108    }
109
110    return zkProperties;
111  }
112
113  /**
114   * Return the ZK Quorum servers string given the specified configuration
115   *
116   * @param conf
117   * @return Quorum servers String
118   */
119  private static String getZKQuorumServersStringFromHbaseConfig(Configuration conf) {
120    String defaultClientPort = Integer.toString(
121        conf.getInt(HConstants.ZOOKEEPER_CLIENT_PORT, HConstants.DEFAULT_ZOOKEEPER_CLIENT_PORT));
122
123    // Build the ZK quorum server string with "server:clientport" list, separated by ','
124    final String[] serverHosts =
125        conf.getStrings(HConstants.ZOOKEEPER_QUORUM, HConstants.LOCALHOST);
126    return buildZKQuorumServerString(serverHosts, defaultClientPort);
127  }
128
129  /**
130   * Return the ZK Quorum servers string given the specified configuration.
131   * @return Quorum servers
132   */
133  public static String getZKQuorumServersString(Configuration conf) {
134    return getZKQuorumServersStringFromHbaseConfig(conf);
135  }
136
137  /**
138   * Build the ZK quorum server string with "server:clientport" list, separated by ','
139   *
140   * @param serverHosts a list of servers for ZK quorum
141   * @param clientPort the default client port
142   * @return the string for a list of "server:port" separated by ","
143   */
144  public static String buildZKQuorumServerString(String[] serverHosts, String clientPort) {
145    StringBuilder quorumStringBuilder = new StringBuilder();
146    String serverHost;
147    for (int i = 0; i < serverHosts.length; ++i) {
148      if (serverHosts[i].contains(":")) {
149        serverHost = serverHosts[i]; // just use the port specified from the input
150      } else {
151        serverHost = serverHosts[i] + ":" + clientPort;
152      }
153      if (i > 0) {
154        quorumStringBuilder.append(',');
155      }
156      quorumStringBuilder.append(serverHost);
157    }
158    return quorumStringBuilder.toString();
159  }
160
161  /**
162   * Verifies that the given key matches the expected format for a ZooKeeper cluster key.
163   * The Quorum for the ZK cluster can have one the following formats (see examples below):
164   *
165   * <ol>
166   *   <li>s1,s2,s3 (no client port in the list, the client port could be obtained from
167   *       clientPort)</li>
168   *   <li>s1:p1,s2:p2,s3:p3 (with client port, which could be same or different for each server,
169   *       in this case, the clientPort would be ignored)</li>
170   *   <li>s1:p1,s2,s3:p3 (mix of (1) and (2) - if port is not specified in a server, it would use
171   *       the clientPort; otherwise, it would use the specified port)</li>
172   * </ol>
173   *
174   * @param key the cluster key to validate
175   * @throws IOException if the key could not be parsed
176   */
177  public static void validateClusterKey(String key) throws IOException {
178    transformClusterKey(key);
179  }
180
181  /**
182   * Separate the given key into the three configurations it should contain:
183   * hbase.zookeeper.quorum, hbase.zookeeper.client.port
184   * and zookeeper.znode.parent
185   * @param key
186   * @return the three configuration in the described order
187   * @throws IOException
188   */
189  public static ZKClusterKey transformClusterKey(String key) throws IOException {
190    String[] parts = key.split(":");
191
192    if (parts.length == 3) {
193      if (!parts[2].matches("/.*[^/]")) {
194        throw new IOException("Cluster key passed " + key + " is invalid, the format should be:" +
195            HConstants.ZOOKEEPER_QUORUM + ":" + HConstants.ZOOKEEPER_CLIENT_PORT + ":"
196            + HConstants.ZOOKEEPER_ZNODE_PARENT);
197      }
198      return new ZKClusterKey(parts [0], Integer.parseInt(parts [1]), parts [2]);
199    }
200
201    if (parts.length > 3) {
202      // The quorum could contain client port in server:clientport format, try to transform more.
203      String zNodeParent = parts [parts.length - 1];
204      if (!zNodeParent.matches("/.*[^/]")) {
205        throw new IOException("Cluster key passed " + key + " is invalid, the format should be:"
206            + HConstants.ZOOKEEPER_QUORUM + ":" + HConstants.ZOOKEEPER_CLIENT_PORT + ":"
207            + HConstants.ZOOKEEPER_ZNODE_PARENT);
208      }
209
210      String clientPort = parts [parts.length - 2];
211
212      // The first part length is the total length minus the lengths of other parts and minus 2 ":"
213      int endQuorumIndex = key.length() - zNodeParent.length() - clientPort.length() - 2;
214      String quorumStringInput = key.substring(0, endQuorumIndex);
215      String[] serverHosts = quorumStringInput.split(",");
216
217      // The common case is that every server has its own client port specified - this means
218      // that (total parts - the ZNodeParent part - the ClientPort part) is equal to
219      // (the number of "," + 1) - "+ 1" because the last server has no ",".
220      if ((parts.length - 2) == (serverHosts.length + 1)) {
221        return new ZKClusterKey(quorumStringInput, Integer.parseInt(clientPort), zNodeParent);
222      }
223
224      // For the uncommon case that some servers has no port specified, we need to build the
225      // server:clientport list using default client port for servers without specified port.
226      return new ZKClusterKey(
227          buildZKQuorumServerString(serverHosts, clientPort),
228          Integer.parseInt(clientPort),
229          zNodeParent);
230    }
231
232    throw new IOException("Cluster key passed " + key + " is invalid, the format should be:" +
233        HConstants.ZOOKEEPER_QUORUM + ":" + HConstants.ZOOKEEPER_CLIENT_PORT + ":"
234        + HConstants.ZOOKEEPER_ZNODE_PARENT);
235  }
236
237  /**
238   * Get the key to the ZK ensemble for this configuration without
239   * adding a name at the end
240   * @param conf Configuration to use to build the key
241   * @return ensemble key without a name
242   */
243  public static String getZooKeeperClusterKey(Configuration conf) {
244    return getZooKeeperClusterKey(conf, null);
245  }
246
247  /**
248   * Get the key to the ZK ensemble for this configuration and append
249   * a name at the end
250   * @param conf Configuration to use to build the key
251   * @param name Name that should be appended at the end if not empty or null
252   * @return ensemble key with a name (if any)
253   */
254  public static String getZooKeeperClusterKey(Configuration conf, String name) {
255    String ensemble = conf.get(HConstants.ZOOKEEPER_QUORUM).replaceAll(
256        "[\\t\\n\\x0B\\f\\r]", "");
257    StringBuilder builder = new StringBuilder(ensemble);
258    builder.append(":");
259    builder.append(conf.get(HConstants.ZOOKEEPER_CLIENT_PORT));
260    builder.append(":");
261    builder.append(conf.get(HConstants.ZOOKEEPER_ZNODE_PARENT));
262    if (name != null && !name.isEmpty()) {
263      builder.append(",");
264      builder.append(name);
265    }
266    return builder.toString();
267  }
268
269  /**
270   * Standardize the ZK quorum string: make it a "server:clientport" list, separated by ','
271   * @param quorumStringInput a string contains a list of servers for ZK quorum
272   * @param clientPort the default client port
273   * @return the string for a list of "server:port" separated by ","
274   */
275  public static String standardizeZKQuorumServerString(String quorumStringInput,
276      String clientPort) {
277    String[] serverHosts = quorumStringInput.split(",");
278    return buildZKQuorumServerString(serverHosts, clientPort);
279  }
280
281  // The Quorum for the ZK cluster can have one the following format (see examples below):
282  // (1). s1,s2,s3 (no client port in the list, the client port could be obtained from clientPort)
283  // (2). s1:p1,s2:p2,s3:p3 (with client port, which could be same or different for each server,
284  //      in this case, the clientPort would be ignored)
285  // (3). s1:p1,s2,s3:p3 (mix of (1) and (2) - if port is not specified in a server, it would use
286  //      the clientPort; otherwise, it would use the specified port)
287  public static class ZKClusterKey {
288    private String quorumString;
289    private int clientPort;
290    private String znodeParent;
291
292    ZKClusterKey(String quorumString, int clientPort, String znodeParent) {
293      this.quorumString = quorumString;
294      this.clientPort = clientPort;
295      this.znodeParent = znodeParent;
296    }
297
298    public String getQuorumString() {
299      return quorumString;
300    }
301
302    public int getClientPort() {
303      return clientPort;
304    }
305
306    public String getZnodeParent() {
307      return znodeParent;
308    }
309  }
310
311  /**
312   * Get the client ZK Quorum servers string
313   * @param conf the configuration to read
314   * @return Client quorum servers, or null if not specified
315   */
316  public static String getClientZKQuorumServersString(Configuration conf) {
317    String clientQuromServers = conf.get(HConstants.CLIENT_ZOOKEEPER_QUORUM);
318    if (clientQuromServers == null) {
319      return null;
320    }
321    int defaultClientPort =
322        conf.getInt(HConstants.ZOOKEEPER_CLIENT_PORT, HConstants.DEFAULT_ZOOKEEPER_CLIENT_PORT);
323    String clientZkClientPort =
324        Integer.toString(conf.getInt(HConstants.CLIENT_ZOOKEEPER_CLIENT_PORT, defaultClientPort));
325    // Build the ZK quorum server string with "server:clientport" list, separated by ','
326    final String[] serverHosts = StringUtils.getStrings(clientQuromServers);
327    return buildZKQuorumServerString(serverHosts, clientZkClientPort);
328  }
329}