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 * @deprecated since 2.4.0 and will be removed in 4.0.0. Use {@link DNS#UNSAFE_RS_HOSTNAME_KEY} 047 * 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.getMethod("getDefaultHost", 057 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 } 063 064 public enum ServerType { 065 MASTER("master"), 066 REGIONSERVER("regionserver"); 067 068 private String name; 069 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 /** 083 * Wrapper around DNS.getDefaultHost(String, String), calling DNS.getDefaultHost(String, String, 084 * boolean) when available. 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( 126 getDefaultHost(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}