View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.hadoop.hbase.util;
18  
19  import java.lang.reflect.Method;
20  import java.net.UnknownHostException;
21  
22  import org.apache.hadoop.conf.Configuration;
23  import org.apache.hadoop.hbase.classification.InterfaceAudience;
24  
25  /**
26   * Wrapper around Hadoop's DNS class to hide reflection.
27   */
28  @InterfaceAudience.Private
29  @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="REC_CATCH_EXCEPTION",
30    justification="If exception, presume HAS_NEW_DNS_GET_DEFAULT_HOST_API false")
31  public final class DNS {
32    private static boolean HAS_NEW_DNS_GET_DEFAULT_HOST_API;
33    private static Method GET_DEFAULT_HOST_METHOD;
34  
35    static {
36      try {
37        GET_DEFAULT_HOST_METHOD = org.apache.hadoop.net.DNS.class
38            .getMethod("getDefaultHost", String.class, String.class, boolean.class);
39        HAS_NEW_DNS_GET_DEFAULT_HOST_API = true;
40      } catch (Exception e) {
41        HAS_NEW_DNS_GET_DEFAULT_HOST_API = false; // FindBugs: Causes REC_CATCH_EXCEPTION. Suppressed
42      }
43    }
44  
45    private DNS() {}
46  
47    /**
48     * Wrapper around DNS.getDefaultHost(String, String), calling
49     * DNS.getDefaultHost(String, String, boolean) when available.
50     *
51     * @param strInterface The network interface to query.
52     * @param nameserver The DNS host name.
53     * @return The default host names associated with IPs bound to the network interface.
54     */
55    public static String getDefaultHost(String strInterface, String nameserver)
56        throws UnknownHostException {
57      if (HAS_NEW_DNS_GET_DEFAULT_HOST_API) {
58        try {
59          // Hadoop-2.8 includes a String, String, boolean variant of getDefaultHost
60          // which properly handles multi-homed systems with Kerberos.
61          return (String) GET_DEFAULT_HOST_METHOD.invoke(null, strInterface, nameserver, true);
62        } catch (Exception e) {
63          // If we can't invoke the method as it should exist, throw an exception
64          throw new RuntimeException("Failed to invoke DNS.getDefaultHost via reflection", e);
65        }
66      } else {
67        return org.apache.hadoop.net.DNS.getDefaultHost(strInterface, nameserver);
68      }
69    }
70  
71    public static String getMasterHostname(Configuration conf) throws UnknownHostException {
72      String hostname = conf.get("hbase.master.hostname", "");
73      if (hostname.isEmpty()) {
74        return Strings.domainNamePointerToHostName(getDefaultHost(
75            conf.get("hbase.master.dns.interface", "default"),
76            conf.get("hbase.master.dns.nameserver", "default")));
77      }
78      return hostname;
79    }
80  }