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  import org.apache.zookeeper.server.DatadirCleanupManager;
38  import org.slf4j.Logger;
39  import org.slf4j.LoggerFactory;
40  
41  import java.io.File;
42  import java.io.IOException;
43  import java.io.PrintWriter;
44  import java.net.InetAddress;
45  import java.net.NetworkInterface;
46  import java.net.UnknownHostException;
47  import java.nio.charset.StandardCharsets;
48  import java.util.ArrayList;
49  import java.util.Enumeration;
50  import java.util.List;
51  import java.util.Map.Entry;
52  import java.util.Properties;
53  
54  
55  /**
56   * HBase's version of ZooKeeper's QuorumPeer. When HBase is set to manage
57   * ZooKeeper, this class is used to start up QuorumPeer instances. By doing
58   * things in here rather than directly calling to ZooKeeper, we have more
59   * control over the process. This class uses {@link ZKConfig} to parse the
60   * zoo.cfg and inject variables from HBase's site.xml configuration in.
61   */
62  @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.TOOLS)
63  @InterfaceStability.Evolving
64  public class HQuorumPeer {
65  
66    /**
67     * Parse ZooKeeper configuration from HBase XML config and run a QuorumPeer.
68     * @param args String[] of command line arguments. Not used.
69     */
70    public static void main(String[] args) {
71      Configuration conf = HBaseConfiguration.create();
72      try {
73        Properties zkProperties = ZKConfig.makeZKProps(conf);
74        writeMyID(zkProperties);
75        QuorumPeerConfig zkConfig = new QuorumPeerConfig();
76        zkConfig.parseProperties(zkProperties);
77  
78        // login the zookeeper server principal (if using security)
79        ZKUtil.loginServer(conf, HConstants.ZK_SERVER_KEYTAB_FILE,
80          HConstants.ZK_SERVER_KERBEROS_PRINCIPAL,
81          zkConfig.getClientPortAddress().getHostName());
82  
83        runZKServer(zkConfig);
84      } catch (Exception e) {
85        e.printStackTrace();
86        System.exit(-1);
87      }
88    }
89  
90    private static void runZKServer(QuorumPeerConfig zkConfig) throws UnknownHostException, IOException {
91      try {
92        /**
93         *  Start and schedule the purge task
94         *  autopurge.purgeInterval is 0 by default,so in fact the DatadirCleanupManager task will not
95         *  be started to clean the logs by default. Config is recommended only for standalone server.
96         */
97  
98        DatadirCleanupManager purgeMgr=new DatadirCleanupManager(
99          zkConfig.getDataDir(),
100         zkConfig.getDataLogDir(),
101         zkConfig.getSnapRetainCount(),
102         zkConfig.getPurgeInterval());
103       purgeMgr.start();
104 
105       if (zkConfig.isDistributed()) {
106         QuorumPeerMain qp = new QuorumPeerMain();
107         qp.runFromConfig(zkConfig);
108       } else {
109         ZooKeeperServerMain zk = new ZooKeeperServerMain();
110         ServerConfig serverConfig = new ServerConfig();
111         serverConfig.readFrom(zkConfig);
112         zk.runFromConfig(serverConfig);
113       }
114     } catch (UnknownHostException e) {
115       throw e;
116     } catch (Exception e) {
117       // The current signature proposes we throw IOException or
118       // UnknownHostException. ZK 3.6 throws another type of checked
119       // exception, which causes a compilation error. Therefore we catch
120       // that and potentially others, and wrap it into an IOE. Adding a
121       // new checked exception to the signature would cause a compilation 
122       // problem with 3.4.
123       throw new IOException(e);
124     }
125   }
126 
127   private static boolean addressIsLocalHost(String address) {
128     return address.equals("localhost") || address.equals("127.0.0.1");
129   }
130 
131   static void writeMyID(Properties properties) throws IOException {
132     long myId = -1;
133 
134     Configuration conf = HBaseConfiguration.create();
135     String myAddress = Strings.domainNamePointerToHostName(DNS.getDefaultHost(
136         conf.get("hbase.zookeeper.dns.interface","default"),
137         conf.get("hbase.zookeeper.dns.nameserver","default")));
138 
139     List<String> ips = new ArrayList<String>();
140 
141     // Add what could be the best (configured) match
142     ips.add(myAddress.contains(".") ?
143         myAddress :
144         StringUtils.simpleHostname(myAddress));
145 
146     // For all nics get all hostnames and IPs
147     Enumeration<?> nics = NetworkInterface.getNetworkInterfaces();
148     while(nics.hasMoreElements()) {
149       Enumeration<?> rawAdrs =
150           ((NetworkInterface)nics.nextElement()).getInetAddresses();
151       while(rawAdrs.hasMoreElements()) {
152         InetAddress inet = (InetAddress) rawAdrs.nextElement();
153         ips.add(StringUtils.simpleHostname(inet.getHostName()));
154         ips.add(inet.getHostAddress());
155       }
156     }
157 
158     for (Entry<Object, Object> entry : properties.entrySet()) {
159       String key = entry.getKey().toString().trim();
160       String value = entry.getValue().toString().trim();
161       if (key.startsWith("server.")) {
162         int dot = key.indexOf('.');
163         long id = Long.parseLong(key.substring(dot + 1));
164         String[] parts = value.split(":");
165         String address = parts[0];
166         if (addressIsLocalHost(address) || ips.contains(address)) {
167           myId = id;
168           break;
169         }
170       }
171     }
172 
173     // Set the max session timeout from the provided client-side timeout
174     properties.setProperty("maxSessionTimeout",
175       conf.get(ZK_SESSION_TIMEOUT, Integer.toString(DEFAULT_ZK_SESSION_TIMEOUT)));
176 
177     if (myId == -1) {
178       throw new IOException("Could not find my address: " + myAddress +
179                             " in list of ZooKeeper quorum servers");
180     }
181 
182     String dataDirStr = properties.get("dataDir").toString().trim();
183     File dataDir = new File(dataDirStr);
184     if (!dataDir.isDirectory()) {
185       if (!dataDir.mkdirs()) {
186         throw new IOException("Unable to create data dir " + dataDir);
187       }
188     }
189 
190     File myIdFile = new File(dataDir, "myid");
191     PrintWriter w = new PrintWriter(myIdFile, StandardCharsets.UTF_8.name());
192     w.println(myId);
193     w.close();
194   }
195 }