View Javadoc

1   /**
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.zookeeper;
21  
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.net.InetAddress;
25  import java.net.UnknownHostException;
26  import java.util.ArrayList;
27  import java.util.List;
28  import java.util.Properties;
29  import java.util.Map.Entry;
30  
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  import org.apache.hadoop.conf.Configuration;
34  import org.apache.hadoop.hbase.HConstants;
35  import org.apache.hadoop.util.StringUtils;
36  
37  /**
38   * Utility methods for reading, parsing, and building zookeeper configuration.
39   */
40  public class ZKConfig {
41    private static final Log LOG = LogFactory.getLog(ZKConfig.class);
42  
43    private static final String VARIABLE_START = "${";
44    private static final int VARIABLE_START_LENGTH = VARIABLE_START.length();
45    private static final String VARIABLE_END = "}";
46    private static final int VARIABLE_END_LENGTH = VARIABLE_END.length();
47  
48    /**
49     * Make a Properties object holding ZooKeeper config equivalent to zoo.cfg.
50     * If there is a zoo.cfg in the classpath, simply read it in. Otherwise parse
51     * the corresponding config options from the HBase XML configs and generate
52     * the appropriate ZooKeeper properties.
53     * @param conf Configuration to read from.
54     * @return Properties holding mappings representing ZooKeeper zoo.cfg file.
55     */
56    public static Properties makeZKProps(Configuration conf) {
57      // First check if there is a zoo.cfg in the CLASSPATH. If so, simply read
58      // it and grab its configuration properties.
59      ClassLoader cl = HQuorumPeer.class.getClassLoader();
60      final InputStream inputStream =
61        cl.getResourceAsStream(HConstants.ZOOKEEPER_CONFIG_NAME);
62      if (inputStream != null) {
63        try {
64          return parseZooCfg(conf, inputStream);
65        } catch (IOException e) {
66          LOG.warn("Cannot read " + HConstants.ZOOKEEPER_CONFIG_NAME +
67                   ", loading from XML files", e);
68        }
69      }
70  
71      // Otherwise, use the configuration options from HBase's XML files.
72      Properties zkProperties = new Properties();
73  
74      // Directly map all of the hbase.zookeeper.property.KEY properties.
75      for (Entry<String, String> entry : conf) {
76        String key = entry.getKey();
77        if (key.startsWith(HConstants.ZK_CFG_PROPERTY_PREFIX)) {
78          String zkKey = key.substring(HConstants.ZK_CFG_PROPERTY_PREFIX_LEN);
79          String value = entry.getValue();
80          // If the value has variables substitutions, need to do a get.
81          if (value.contains(VARIABLE_START)) {
82            value = conf.get(key);
83          }
84          zkProperties.put(zkKey, value);
85        }
86      }
87  
88      // If clientPort is not set, assign the default.
89      if (zkProperties.getProperty(HConstants.CLIENT_PORT_STR) == null) {
90        zkProperties.put(HConstants.CLIENT_PORT_STR,
91            HConstants.DEFAULT_ZOOKEPER_CLIENT_PORT);
92      }
93  
94      // Create the server.X properties.
95      int peerPort = conf.getInt("hbase.zookeeper.peerport", 2888);
96      int leaderPort = conf.getInt("hbase.zookeeper.leaderport", 3888);
97  
98      final String[] serverHosts = conf.getStrings(HConstants.ZOOKEEPER_QUORUM,
99                                                   HConstants.LOCALHOST);
100     for (int i = 0; i < serverHosts.length; ++i) {
101       String serverHost = serverHosts[i];
102       String address = serverHost + ":" + peerPort + ":" + leaderPort;
103       String key = "server." + i;
104       zkProperties.put(key, address);
105     }
106 
107     return zkProperties;
108   }
109 
110   /**
111    * Parse ZooKeeper's zoo.cfg, injecting HBase Configuration variables in.
112    * This method is used for testing so we can pass our own InputStream.
113    * @param conf HBaseConfiguration to use for injecting variables.
114    * @param inputStream InputStream to read from.
115    * @return Properties parsed from config stream with variables substituted.
116    * @throws IOException if anything goes wrong parsing config
117    */
118   public static Properties parseZooCfg(Configuration conf,
119       InputStream inputStream) throws IOException {
120     Properties properties = new Properties();
121     try {
122       properties.load(inputStream);
123     } catch (IOException e) {
124       final String msg = "fail to read properties from "
125         + HConstants.ZOOKEEPER_CONFIG_NAME;
126       LOG.fatal(msg);
127       throw new IOException(msg, e);
128     }
129     for (Entry<Object, Object> entry : properties.entrySet()) {
130       String value = entry.getValue().toString().trim();
131       String key = entry.getKey().toString().trim();
132       StringBuilder newValue = new StringBuilder();
133       int varStart = value.indexOf(VARIABLE_START);
134       int varEnd = 0;
135       while (varStart != -1) {
136         varEnd = value.indexOf(VARIABLE_END, varStart);
137         if (varEnd == -1) {
138           String msg = "variable at " + varStart + " has no end marker";
139           LOG.fatal(msg);
140           throw new IOException(msg);
141         }
142         String variable = value.substring(varStart + VARIABLE_START_LENGTH, varEnd);
143 
144         String substituteValue = System.getProperty(variable);
145         if (substituteValue == null) {
146           substituteValue = conf.get(variable);
147         }
148         if (substituteValue == null) {
149           String msg = "variable " + variable + " not set in system property "
150                      + "or hbase configs";
151           LOG.fatal(msg);
152           throw new IOException(msg);
153         }
154 
155         newValue.append(substituteValue);
156 
157         varEnd += VARIABLE_END_LENGTH;
158         varStart = value.indexOf(VARIABLE_START, varEnd);
159       }
160       // Special case for 'hbase.cluster.distributed' property being 'true'
161       if (key.startsWith("server.")) {
162         boolean mode = conf.getBoolean(HConstants.CLUSTER_DISTRIBUTED, HConstants.DEFAULT_CLUSTER_DISTRIBUTED);
163         if (mode == HConstants.CLUSTER_IS_DISTRIBUTED && value.startsWith(HConstants.LOCALHOST)) {
164           String msg = "The server in zoo.cfg cannot be set to localhost " +
165               "in a fully-distributed setup because it won't be reachable. " +
166               "See \"Getting Started\" for more information.";
167           LOG.fatal(msg);
168           throw new IOException(msg);
169         }
170       }
171       newValue.append(value.substring(varEnd));
172       properties.setProperty(key, newValue.toString());
173     }
174     return properties;
175   }
176 
177   /**
178    * Return the ZK Quorum servers string given zk properties returned by
179    * makeZKProps
180    * @param properties
181    * @return Quorum servers String
182    */
183   public static String getZKQuorumServersString(Properties properties) {
184     String clientPort = null;
185     List<String> servers = new ArrayList<String>();
186 
187     // The clientPort option may come after the server.X hosts, so we need to
188     // grab everything and then create the final host:port comma separated list.
189     boolean anyValid = false;
190     for (Entry<Object,Object> property : properties.entrySet()) {
191       String key = property.getKey().toString().trim();
192       String value = property.getValue().toString().trim();
193       if (key.equals("clientPort")) {
194         clientPort = value;
195       }
196       else if (key.startsWith("server.")) {
197         String host = value.substring(0, value.indexOf(':'));
198         servers.add(host);
199         anyValid = true;
200       }
201     }
202 
203     if (!anyValid) {
204       LOG.error("no valid quorum servers found in " + HConstants.ZOOKEEPER_CONFIG_NAME);
205       return null;
206     }
207 
208     if (clientPort == null) {
209       LOG.error("no clientPort found in " + HConstants.ZOOKEEPER_CONFIG_NAME);
210       return null;
211     }
212 
213     if (servers.isEmpty()) {
214       LOG.fatal("No server.X lines found in conf/zoo.cfg. HBase must have a " +
215                 "ZooKeeper cluster configured for its operation.");
216       return null;
217     }
218 
219     StringBuilder hostPortBuilder = new StringBuilder();
220     for (int i = 0; i < servers.size(); ++i) {
221       String host = servers.get(i);
222       if (i > 0) {
223         hostPortBuilder.append(',');
224       }
225       hostPortBuilder.append(host);
226       hostPortBuilder.append(':');
227       hostPortBuilder.append(clientPort);
228     }
229 
230     return hostPortBuilder.toString();
231   }
232 
233   /**
234    * Return the ZK Quorum servers string given the specified configuration.
235    * @param conf
236    * @return Quorum servers
237    */
238   public static String getZKQuorumServersString(Configuration conf) {
239     return getZKQuorumServersString(makeZKProps(conf));
240   }
241 }