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 @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG) 042 public static final String UNSAFE_THRIFT_HOSTNAME_KEY = "hbase.unsafe.thrift.hostname"; 043 044 private static boolean HAS_NEW_DNS_GET_DEFAULT_HOST_API; 045 private static Method GET_DEFAULT_HOST_METHOD; 046 047 /** 048 * Hostname configuration key 049 * @deprecated since 2.4.0 and will be removed in 4.0.0. Use {@link DNS#UNSAFE_RS_HOSTNAME_KEY} 050 * instead. 051 * @see <a href="https://issues.apache.org/jira/browse/HBASE-24667">HBASE-24667</a> 052 */ 053 @Deprecated 054 @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG) 055 public static final String RS_HOSTNAME_KEY = "hbase.regionserver.hostname"; 056 057 static { 058 try { 059 GET_DEFAULT_HOST_METHOD = org.apache.hadoop.net.DNS.class.getMethod("getDefaultHost", 060 String.class, String.class, boolean.class); 061 HAS_NEW_DNS_GET_DEFAULT_HOST_API = true; 062 } catch (Exception e) { 063 HAS_NEW_DNS_GET_DEFAULT_HOST_API = false; // FindBugs: Causes REC_CATCH_EXCEPTION. Suppressed 064 } 065 } 066 067 public enum ServerType { 068 MASTER("master"), 069 REGIONSERVER("regionserver"), 070 THRIFT("thrift"); 071 072 private final String name; 073 074 private ServerType(String name) { 075 this.name = name; 076 } 077 078 public String getName() { 079 return name; 080 } 081 } 082 083 private DNS() { 084 } 085 086 /** 087 * Wrapper around DNS.getDefaultHost(String, String), calling DNS.getDefaultHost(String, String, 088 * boolean) when available. 089 * @param strInterface The network interface to query. 090 * @param nameserver The DNS host name. 091 * @return The default host names associated with IPs bound to the network interface. 092 */ 093 public static String getDefaultHost(String strInterface, String nameserver) 094 throws UnknownHostException { 095 if (HAS_NEW_DNS_GET_DEFAULT_HOST_API) { 096 try { 097 // Hadoop-2.8 includes a String, String, boolean variant of getDefaultHost 098 // which properly handles multi-homed systems with Kerberos. 099 return (String) GET_DEFAULT_HOST_METHOD.invoke(null, strInterface, nameserver, true); 100 } catch (Exception e) { 101 // If we can't invoke the method as it should exist, throw an exception 102 throw new RuntimeException("Failed to invoke DNS.getDefaultHost via reflection", e); 103 } 104 } else { 105 return org.apache.hadoop.net.DNS.getDefaultHost(strInterface, nameserver); 106 } 107 } 108 109 /** 110 * Get the configured hostname for a given ServerType. Gets the default hostname if not specified 111 * in the configuration. 112 * @param conf Configuration to look up. 113 * @param serverType ServerType to look up in the configuration for overrides. 114 */ 115 public static String getHostname(@NonNull Configuration conf, @NonNull ServerType serverType) 116 throws UnknownHostException { 117 String hostname; 118 switch (serverType) { 119 case MASTER: 120 hostname = conf.get(MASTER_HOSTNAME_KEY); 121 break; 122 case REGIONSERVER: 123 hostname = conf.get(UNSAFE_RS_HOSTNAME_KEY); 124 break; 125 case THRIFT: 126 hostname = conf.get(UNSAFE_THRIFT_HOSTNAME_KEY); 127 break; 128 default: 129 hostname = null; 130 } 131 if (hostname == null || hostname.isEmpty()) { 132 return Strings.domainNamePointerToHostName( 133 getDefaultHost(conf.get("hbase." + serverType.getName() + ".dns.interface", "default"), 134 conf.get("hbase." + serverType.getName() + ".dns.nameserver", "default"))); 135 } else { 136 return hostname; 137 } 138 } 139}