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