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