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