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 org.apache.hadoop.hbase.classification.InterfaceAudience;
22  import org.apache.hadoop.hbase.classification.InterfaceStability;
23  import org.apache.hadoop.conf.Configuration;
24  import org.apache.hadoop.hbase.HBaseConfiguration;
25  import org.apache.hadoop.hbase.util.Strings;
26  import org.apache.hadoop.net.DNS;
27  import org.apache.hadoop.util.StringUtils;
28  import org.apache.zookeeper.server.ServerConfig;
29  import org.apache.zookeeper.server.ZooKeeperServerMain;
30  import org.apache.zookeeper.server.quorum.QuorumPeerConfig;
31  import org.apache.zookeeper.server.quorum.QuorumPeerMain;
32  
33  import java.io.File;
34  import java.io.IOException;
35  import java.io.PrintWriter;
36  import java.net.InetAddress;
37  import java.net.NetworkInterface;
38  import java.net.UnknownHostException;
39  import java.util.ArrayList;
40  import java.util.Enumeration;
41  import java.util.List;
42  import java.util.Map.Entry;
43  import java.util.Properties;
44  
45  import static org.apache.hadoop.hbase.HConstants.DEFAULT_ZK_SESSION_TIMEOUT;
46  import static org.apache.hadoop.hbase.HConstants.ZK_SESSION_TIMEOUT;
47  
48  /**
49   * HBase's version of ZooKeeper's QuorumPeer. When HBase is set to manage
50   * ZooKeeper, this class is used to start up QuorumPeer instances. By doing
51   * things in here rather than directly calling to ZooKeeper, we have more
52   * control over the process. This class uses {@link ZKConfig} to parse the
53   * zoo.cfg and inject variables from HBase's site.xml configuration in.
54   */
55  @InterfaceAudience.Public
56  @InterfaceStability.Evolving
57  public class HQuorumPeer {
58    
59    /**
60     * Parse ZooKeeper configuration from HBase XML config and run a QuorumPeer.
61     * @param args String[] of command line arguments. Not used.
62     */
63    public static void main(String[] args) {
64      Configuration conf = HBaseConfiguration.create();
65      try {
66        Properties zkProperties = ZKConfig.makeZKProps(conf);
67        writeMyID(zkProperties);
68        QuorumPeerConfig zkConfig = new QuorumPeerConfig();
69        zkConfig.parseProperties(zkProperties);
70  
71        // login the zookeeper server principal (if using security)
72        ZKUtil.loginServer(conf, "hbase.zookeeper.server.keytab.file",
73          "hbase.zookeeper.server.kerberos.principal",
74          zkConfig.getClientPortAddress().getHostName());
75  
76        runZKServer(zkConfig);
77      } catch (Exception e) {
78        e.printStackTrace();
79        System.exit(-1);
80      }
81    }
82  
83    private static void runZKServer(QuorumPeerConfig zkConfig) throws UnknownHostException, IOException {
84      if (zkConfig.isDistributed()) {
85        QuorumPeerMain qp = new QuorumPeerMain();
86        qp.runFromConfig(zkConfig);
87      } else {
88        ZooKeeperServerMain zk = new ZooKeeperServerMain();
89        ServerConfig serverConfig = new ServerConfig();
90        serverConfig.readFrom(zkConfig);
91        zk.runFromConfig(serverConfig);
92      }
93    }
94  
95    private static boolean addressIsLocalHost(String address) {
96      return address.equals("localhost") || address.equals("127.0.0.1");
97    }
98  
99    static void writeMyID(Properties properties) throws IOException {
100     long myId = -1;
101 
102     Configuration conf = HBaseConfiguration.create();
103     String myAddress = Strings.domainNamePointerToHostName(DNS.getDefaultHost(
104         conf.get("hbase.zookeeper.dns.interface","default"),
105         conf.get("hbase.zookeeper.dns.nameserver","default")));
106 
107     List<String> ips = new ArrayList<String>();
108 
109     // Add what could be the best (configured) match
110     ips.add(myAddress.contains(".") ?
111         myAddress :
112         StringUtils.simpleHostname(myAddress));
113 
114     // For all nics get all hostnames and IPs
115     Enumeration<?> nics = NetworkInterface.getNetworkInterfaces();
116     while(nics.hasMoreElements()) {
117       Enumeration<?> rawAdrs =
118           ((NetworkInterface)nics.nextElement()).getInetAddresses();
119       while(rawAdrs.hasMoreElements()) {
120         InetAddress inet = (InetAddress) rawAdrs.nextElement();
121         ips.add(StringUtils.simpleHostname(inet.getHostName()));
122         ips.add(inet.getHostAddress());
123       }
124     }
125 
126     for (Entry<Object, Object> entry : properties.entrySet()) {
127       String key = entry.getKey().toString().trim();
128       String value = entry.getValue().toString().trim();
129       if (key.startsWith("server.")) {
130         int dot = key.indexOf('.');
131         long id = Long.parseLong(key.substring(dot + 1));
132         String[] parts = value.split(":");
133         String address = parts[0];
134         if (addressIsLocalHost(address) || ips.contains(address)) {
135           myId = id;
136           break;
137         }
138       }
139     }
140 
141     // Set the max session timeout from the provided client-side timeout
142     properties.setProperty("maxSessionTimeout",
143       conf.get(ZK_SESSION_TIMEOUT, Integer.toString(DEFAULT_ZK_SESSION_TIMEOUT)));
144 
145     if (myId == -1) {
146       throw new IOException("Could not find my address: " + myAddress +
147                             " in list of ZooKeeper quorum servers");
148     }
149 
150     String dataDirStr = properties.get("dataDir").toString().trim();
151     File dataDir = new File(dataDirStr);
152     if (!dataDir.isDirectory()) {
153       if (!dataDir.mkdirs()) {
154         throw new IOException("Unable to create data dir " + dataDir);
155       }
156     }
157 
158     File myIdFile = new File(dataDir, "myid");
159     PrintWriter w = new PrintWriter(myIdFile);
160     w.println(myId);
161     w.close();
162   }
163 }