1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.hadoop.hbase.util;
18
19 import java.lang.reflect.Method;
20 import java.net.UnknownHostException;
21
22 import org.apache.hadoop.conf.Configuration;
23 import org.apache.hadoop.hbase.classification.InterfaceAudience;
24
25
26
27
28 @InterfaceAudience.Private
29 @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="REC_CATCH_EXCEPTION",
30 justification="If exception, presume HAS_NEW_DNS_GET_DEFAULT_HOST_API false")
31 public final class DNS {
32 private static boolean HAS_NEW_DNS_GET_DEFAULT_HOST_API;
33 private static Method GET_DEFAULT_HOST_METHOD;
34
35 static {
36 try {
37 GET_DEFAULT_HOST_METHOD = org.apache.hadoop.net.DNS.class
38 .getMethod("getDefaultHost", String.class, String.class, boolean.class);
39 HAS_NEW_DNS_GET_DEFAULT_HOST_API = true;
40 } catch (Exception e) {
41 HAS_NEW_DNS_GET_DEFAULT_HOST_API = false;
42 }
43 }
44
45 private DNS() {}
46
47
48
49
50
51
52
53
54
55 public static String getDefaultHost(String strInterface, String nameserver)
56 throws UnknownHostException {
57 if (HAS_NEW_DNS_GET_DEFAULT_HOST_API) {
58 try {
59
60
61 return (String) GET_DEFAULT_HOST_METHOD.invoke(null, strInterface, nameserver, true);
62 } catch (Exception e) {
63
64 throw new RuntimeException("Failed to invoke DNS.getDefaultHost via reflection", e);
65 }
66 } else {
67 return org.apache.hadoop.net.DNS.getDefaultHost(strInterface, nameserver);
68 }
69 }
70
71 public static String getMasterHostname(Configuration conf) throws UnknownHostException {
72 String hostname = conf.get("hbase.master.hostname", "");
73 if (hostname.isEmpty()) {
74 return Strings.domainNamePointerToHostName(getDefaultHost(
75 conf.get("hbase.master.dns.interface", "default"),
76 conf.get("hbase.master.dns.nameserver", "default")));
77 }
78 return hostname;
79 }
80 }