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.io.IOException;
22  import java.io.InputStream;
23  import java.util.ArrayList;
24  import java.util.List;
25  import java.util.Map.Entry;
26  import java.util.Properties;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.apache.hadoop.conf.Configuration;
31  import org.apache.hadoop.hbase.HConstants;
32  import org.apache.hadoop.hbase.classification.InterfaceAudience;
33  
34  /**
35   * Utility methods for reading, and building the ZooKeeper configuration.
36   */
37  @InterfaceAudience.Private
38  public class ZKConfig {
39    private static final Log LOG = LogFactory.getLog(ZKConfig.class);
40  
41    private static final String VARIABLE_START = "${";
42    private static final int VARIABLE_START_LENGTH = VARIABLE_START.length();
43    private static final String VARIABLE_END = "}";
44    private static final int VARIABLE_END_LENGTH = VARIABLE_END.length();
45  
46    /**
47     * Make a Properties object holding ZooKeeper config.
48     * Parses the corresponding config options from the HBase XML configs
49     * and generates the appropriate ZooKeeper properties.
50     * @param conf Configuration to read from.
51     * @return Properties holding mappings representing ZooKeeper config file.
52     */
53    public static Properties makeZKProps(Configuration conf) {
54      if (conf.getBoolean(HConstants.HBASE_CONFIG_READ_ZOOKEEPER_CONFIG, false)) {
55        LOG.warn(
56            "Parsing ZooKeeper's " + HConstants.ZOOKEEPER_CONFIG_NAME +
57            " file for ZK properties " +
58            "has been deprecated. Please instead place all ZK related HBase " +
59            "configuration under the hbase-site.xml, using prefixes " +
60            "of the form '" + HConstants.ZK_CFG_PROPERTY_PREFIX + "', and " +
61            "set property '" + HConstants.HBASE_CONFIG_READ_ZOOKEEPER_CONFIG +
62            "' to false");
63        // First check if there is a zoo.cfg in the CLASSPATH. If so, simply read
64        // it and grab its configuration properties.
65        ClassLoader cl = HQuorumPeer.class.getClassLoader();
66        final InputStream inputStream =
67          cl.getResourceAsStream(HConstants.ZOOKEEPER_CONFIG_NAME);
68        if (inputStream != null) {
69          try {
70            return parseZooCfg(conf, inputStream);
71          } catch (IOException e) {
72            LOG.warn("Cannot read " + HConstants.ZOOKEEPER_CONFIG_NAME +
73                     ", loading from XML files", e);
74          }
75        }
76      } else {
77        if (LOG.isTraceEnabled()) {
78          LOG.trace("Skipped reading ZK properties file '" + HConstants.ZOOKEEPER_CONFIG_NAME +
79            "' since '" + HConstants.HBASE_CONFIG_READ_ZOOKEEPER_CONFIG + "' was not set to true");
80        }
81      }
82  
83      // Otherwise, use the configuration options from HBase's XML files.
84      Properties zkProperties = new Properties();
85  
86      // Directly map all of the hbase.zookeeper.property.KEY properties.
87      // Synchronize on conf so no loading of configs while we iterate
88      synchronized (conf) {
89        for (Entry<String, String> entry : conf) {
90          String key = entry.getKey();
91          if (key.startsWith(HConstants.ZK_CFG_PROPERTY_PREFIX)) {
92            String zkKey = key.substring(HConstants.ZK_CFG_PROPERTY_PREFIX_LEN);
93            String value = entry.getValue();
94            // If the value has variables substitutions, need to do a get.
95            if (value.contains(VARIABLE_START)) {
96              value = conf.get(key);
97            }
98            zkProperties.put(zkKey, value);
99          }
100       }
101     }
102 
103     // If clientPort is not set, assign the default.
104     if (zkProperties.getProperty(HConstants.CLIENT_PORT_STR) == null) {
105       zkProperties.put(HConstants.CLIENT_PORT_STR,
106           HConstants.DEFAULT_ZOOKEPER_CLIENT_PORT);
107     }
108 
109     // Create the server.X properties.
110     int peerPort = conf.getInt("hbase.zookeeper.peerport", 2888);
111     int leaderPort = conf.getInt("hbase.zookeeper.leaderport", 3888);
112 
113     final String[] serverHosts = conf.getStrings(HConstants.ZOOKEEPER_QUORUM,
114                                                  HConstants.LOCALHOST);
115     for (int i = 0; i < serverHosts.length; ++i) {
116       String serverHost = serverHosts[i];
117       String address = serverHost + ":" + peerPort + ":" + leaderPort;
118       String key = "server." + i;
119       zkProperties.put(key, address);
120     }
121 
122     return zkProperties;
123   }
124 
125   /**
126    * Parse ZooKeeper's zoo.cfg, injecting HBase Configuration variables in.
127    * This method is used for testing so we can pass our own InputStream.
128    * @param conf HBaseConfiguration to use for injecting variables.
129    * @param inputStream InputStream to read from.
130    * @return Properties parsed from config stream with variables substituted.
131    * @throws IOException if anything goes wrong parsing config
132    * @deprecated in 0.96 onwards. HBase will no longer rely on zoo.cfg
133    * availability.
134    */
135   @Deprecated
136   public static Properties parseZooCfg(Configuration conf,
137       InputStream inputStream) throws IOException {
138     Properties properties = new Properties();
139     try {
140       properties.load(inputStream);
141     } catch (IOException e) {
142       final String msg = "fail to read properties from "
143         + HConstants.ZOOKEEPER_CONFIG_NAME;
144       LOG.fatal(msg);
145       throw new IOException(msg, e);
146     }
147     for (Entry<Object, Object> entry : properties.entrySet()) {
148       String value = entry.getValue().toString().trim();
149       String key = entry.getKey().toString().trim();
150       StringBuilder newValue = new StringBuilder();
151       int varStart = value.indexOf(VARIABLE_START);
152       int varEnd = 0;
153       while (varStart != -1) {
154         varEnd = value.indexOf(VARIABLE_END, varStart);
155         if (varEnd == -1) {
156           String msg = "variable at " + varStart + " has no end marker";
157           LOG.fatal(msg);
158           throw new IOException(msg);
159         }
160         String variable = value.substring(varStart + VARIABLE_START_LENGTH, varEnd);
161 
162         String substituteValue = System.getProperty(variable);
163         if (substituteValue == null) {
164           substituteValue = conf.get(variable);
165         }
166         if (substituteValue == null) {
167           String msg = "variable " + variable + " not set in system property "
168                      + "or hbase configs";
169           LOG.fatal(msg);
170           throw new IOException(msg);
171         }
172 
173         newValue.append(substituteValue);
174 
175         varEnd += VARIABLE_END_LENGTH;
176         varStart = value.indexOf(VARIABLE_START, varEnd);
177       }
178       // Special case for 'hbase.cluster.distributed' property being 'true'
179       if (key.startsWith("server.")) {
180         boolean mode = conf.getBoolean(HConstants.CLUSTER_DISTRIBUTED, HConstants.DEFAULT_CLUSTER_DISTRIBUTED);
181         if (mode == HConstants.CLUSTER_IS_DISTRIBUTED && value.startsWith(HConstants.LOCALHOST)) {
182           String msg = "The server in zoo.cfg cannot be set to localhost " +
183               "in a fully-distributed setup because it won't be reachable. " +
184               "See \"Getting Started\" for more information.";
185           LOG.fatal(msg);
186           throw new IOException(msg);
187         }
188       }
189       newValue.append(value.substring(varEnd));
190       properties.setProperty(key, newValue.toString());
191     }
192     return properties;
193   }
194 
195   /**
196    * Return the ZK Quorum servers string given zk properties returned by
197    * makeZKProps
198    * @param properties
199    * @return Quorum servers String
200    */
201   public static String getZKQuorumServersString(Properties properties) {
202     String clientPort = null;
203     List<String> servers = new ArrayList<String>();
204 
205     // The clientPort option may come after the server.X hosts, so we need to
206     // grab everything and then create the final host:port comma separated list.
207     boolean anyValid = false;
208     for (Entry<Object,Object> property : properties.entrySet()) {
209       String key = property.getKey().toString().trim();
210       String value = property.getValue().toString().trim();
211       if (key.equals("clientPort")) {
212         clientPort = value;
213       }
214       else if (key.startsWith("server.")) {
215         String host = value.substring(0, value.indexOf(':'));
216         servers.add(host);
217         anyValid = true;
218       }
219     }
220 
221     if (!anyValid) {
222       LOG.error("no valid quorum servers found in " + HConstants.ZOOKEEPER_CONFIG_NAME);
223       return null;
224     }
225 
226     if (clientPort == null) {
227       LOG.error("no clientPort found in " + HConstants.ZOOKEEPER_CONFIG_NAME);
228       return null;
229     }
230 
231     if (servers.isEmpty()) {
232       LOG.fatal("No servers were found in provided ZooKeeper configuration. " +
233           "HBase must have a ZooKeeper cluster configured for its " +
234           "operation. Ensure that you've configured '" +
235           HConstants.ZOOKEEPER_QUORUM + "' properly.");
236       return null;
237     }
238 
239     StringBuilder hostPortBuilder = new StringBuilder();
240     for (int i = 0; i < servers.size(); ++i) {
241       String host = servers.get(i);
242       if (i > 0) {
243         hostPortBuilder.append(',');
244       }
245       hostPortBuilder.append(host);
246       hostPortBuilder.append(':');
247       hostPortBuilder.append(clientPort);
248     }
249 
250     return hostPortBuilder.toString();
251   }
252 
253   /**
254    * Return the ZK Quorum servers string given the specified configuration.
255    * @param conf
256    * @return Quorum servers
257    */
258   public static String getZKQuorumServersString(Configuration conf) {
259     return getZKQuorumServersString(makeZKProps(conf));
260   }
261 }