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