001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to you under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.hadoop.hbase.util;
018
019import edu.umd.cs.findbugs.annotations.NonNull;
020import java.lang.reflect.Method;
021import java.net.UnknownHostException;
022
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.hbase.HBaseInterfaceAudience;
025import org.apache.yetus.audience.InterfaceAudience;
026
027/**
028 * Wrapper around Hadoop's DNS class to hide reflection.
029 */
030@InterfaceAudience.Private
031@edu.umd.cs.findbugs.annotations.SuppressWarnings(value="REC_CATCH_EXCEPTION",
032  justification="If exception, presume HAS_NEW_DNS_GET_DEFAULT_HOST_API false")
033public final class DNS {
034  // key to the config parameter of server hostname
035  // the specification of server hostname is optional. The hostname should be resolvable from
036  // both master and region server
037  @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)
038  public static final String UNSAFE_RS_HOSTNAME_KEY = "hbase.unsafe.regionserver.hostname";
039  @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)
040  public static final String MASTER_HOSTNAME_KEY = "hbase.master.hostname";
041
042  private static boolean HAS_NEW_DNS_GET_DEFAULT_HOST_API;
043  private static Method GET_DEFAULT_HOST_METHOD;
044
045  /**
046   * @deprecated since 2.4.0 and will be removed in 4.0.0.
047   * Use {@link DNS#UNSAFE_RS_HOSTNAME_KEY} instead.
048   * @see <a href="https://issues.apache.org/jira/browse/HBASE-24667">HBASE-24667</a>
049   */
050  @Deprecated
051  @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)
052  public static final String RS_HOSTNAME_KEY = "hbase.regionserver.hostname";
053
054  static {
055    try {
056      GET_DEFAULT_HOST_METHOD = org.apache.hadoop.net.DNS.class
057          .getMethod("getDefaultHost", String.class, String.class, boolean.class);
058      HAS_NEW_DNS_GET_DEFAULT_HOST_API = true;
059    } catch (Exception e) {
060      HAS_NEW_DNS_GET_DEFAULT_HOST_API = false; // FindBugs: Causes REC_CATCH_EXCEPTION. Suppressed
061    }
062    Configuration.addDeprecation(RS_HOSTNAME_KEY, UNSAFE_RS_HOSTNAME_KEY);
063  }
064
065  public enum ServerType {
066    MASTER("master"),
067    REGIONSERVER("regionserver");
068
069    private String name;
070    ServerType(String name) {
071      this.name = name;
072    }
073
074    public String getName() {
075      return name;
076    }
077  }
078
079  private DNS() {}
080
081  /**
082   * Wrapper around DNS.getDefaultHost(String, String), calling
083   * DNS.getDefaultHost(String, String, boolean) when available.
084   *
085   * @param strInterface The network interface to query.
086   * @param nameserver The DNS host name.
087   * @return The default host names associated with IPs bound to the network interface.
088   */
089  public static String getDefaultHost(String strInterface, String nameserver)
090      throws UnknownHostException {
091    if (HAS_NEW_DNS_GET_DEFAULT_HOST_API) {
092      try {
093        // Hadoop-2.8 includes a String, String, boolean variant of getDefaultHost
094        // which properly handles multi-homed systems with Kerberos.
095        return (String) GET_DEFAULT_HOST_METHOD.invoke(null, strInterface, nameserver, true);
096      } catch (Exception e) {
097        // If we can't invoke the method as it should exist, throw an exception
098        throw new RuntimeException("Failed to invoke DNS.getDefaultHost via reflection", e);
099      }
100    } else {
101      return org.apache.hadoop.net.DNS.getDefaultHost(strInterface, nameserver);
102    }
103  }
104
105  /**
106   * Get the configured hostname for a given ServerType. Gets the default hostname if not specified
107   * in the configuration.
108   * @param conf Configuration to look up.
109   * @param serverType ServerType to look up in the configuration for overrides.
110   */
111  public static String getHostname(@NonNull Configuration conf, @NonNull ServerType serverType)
112      throws UnknownHostException {
113    String hostname;
114    switch (serverType) {
115      case MASTER:
116        hostname = conf.get(MASTER_HOSTNAME_KEY);
117        break;
118      case REGIONSERVER:
119        hostname = conf.get(UNSAFE_RS_HOSTNAME_KEY);
120        break;
121      default:
122        hostname = null;
123    }
124    if (hostname == null || hostname.isEmpty()) {
125      return Strings.domainNamePointerToHostName(getDefaultHost(
126          conf.get("hbase." + serverType.getName() + ".dns.interface", "default"),
127          conf.get("hbase." + serverType.getName() + ".dns.nameserver", "default")));
128    } else {
129      return hostname;
130    }
131  }
132}