1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.regionserver;
19
20 import static org.junit.Assert.assertTrue;
21
22 import java.io.IOException;
23 import java.net.InetAddress;
24 import java.net.NetworkInterface;
25 import java.util.Enumeration;
26 import java.util.List;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.hadoop.conf.Configuration;
31 import org.apache.hadoop.hbase.HBaseConfiguration;
32 import org.apache.hadoop.hbase.HBaseTestingUtility;
33 import org.apache.hadoop.hbase.testclassification.MediumTests;
34 import org.apache.hadoop.hbase.util.Threads;
35 import org.apache.hadoop.hbase.zookeeper.ZKUtil;
36 import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
37 import org.junit.Test;
38 import org.junit.experimental.categories.Category;
39
40
41
42
43 @Category({MediumTests.class})
44 public class TestRegionServerHostname {
45 private static final Log LOG = LogFactory.getLog(TestRegionServerHostname.class);
46
47 @Test (timeout=30000)
48 public void testInvalidRegionServerHostnameAbortsServer() throws Exception {
49 final int NUM_MASTERS = 1;
50 final int NUM_RS = 1;
51 String invalidHostname = "hostAddr.invalid";
52 Configuration conf = HBaseConfiguration.create();
53 HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(conf);
54 TEST_UTIL.getConfiguration().set(HRegionServer.RS_HOSTNAME_KEY, invalidHostname);
55 try {
56 TEST_UTIL.startMiniCluster(NUM_MASTERS, NUM_RS);
57 } catch (IOException ioe) {
58 Throwable t1 = ioe.getCause();
59 Throwable t2 = t1.getCause();
60 assertTrue(t1.getMessage() + " - " + t2.getMessage(),
61 t2.getMessage().contains("Failed resolve of " + invalidHostname) ||
62 t2.getMessage().contains("Problem binding to " + invalidHostname));
63 return;
64 } finally {
65 TEST_UTIL.shutdownMiniCluster();
66 }
67 assertTrue("Failed to validate against invalid hostname", false);
68 }
69
70 @Test(timeout=120000)
71 public void testRegionServerHostname() throws Exception {
72 final int NUM_MASTERS = 1;
73 final int NUM_RS = 1;
74 Enumeration<NetworkInterface> netInterfaceList = NetworkInterface.getNetworkInterfaces();
75 Configuration conf = HBaseConfiguration.create();
76 HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(conf);
77 while (netInterfaceList.hasMoreElements()) {
78 NetworkInterface ni = netInterfaceList.nextElement();
79 Enumeration<InetAddress> addrList = ni.getInetAddresses();
80
81 while (addrList.hasMoreElements()) {
82 InetAddress addr = addrList.nextElement();
83 if (addr.isLoopbackAddress() || addr.isLinkLocalAddress() || addr.isMulticastAddress()) {
84 continue;
85 }
86 String hostName = addr.getHostName();
87 LOG.info("Found " + hostName + " on " + ni);
88
89 TEST_UTIL.getConfiguration().set(HRegionServer.RS_HOSTNAME_KEY, hostName);
90 TEST_UTIL.startMiniCluster(NUM_MASTERS, NUM_RS);
91 try {
92 ZooKeeperWatcher zkw = TEST_UTIL.getZooKeeperWatcher();
93 List<String> servers = ZKUtil.listChildrenNoWatch(zkw, zkw.rsZNode);
94 while (servers == null) {
95 Threads.sleep(10);
96 servers = ZKUtil.listChildrenNoWatch(zkw, zkw.rsZNode);
97 }
98 assertTrue(servers.size() == NUM_RS);
99 for (String server : servers) {
100 assertTrue(server.startsWith(hostName.toLowerCase()+","));
101 }
102 zkw.close();
103 } finally {
104 TEST_UTIL.shutdownMiniCluster();
105 }
106 }
107 }
108 }
109
110 @Test(timeout=30000)
111 public void testConflictRegionServerHostnameConfigurationsAbortServer() throws Exception {
112 final int NUM_MASTERS = 1;
113 final int NUM_RS = 1;
114 Enumeration<NetworkInterface> netInterfaceList = NetworkInterface.getNetworkInterfaces();
115 Configuration conf = HBaseConfiguration.create();
116 HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(conf);
117 while (netInterfaceList.hasMoreElements()) {
118 NetworkInterface ni = netInterfaceList.nextElement();
119 Enumeration<InetAddress> addrList = ni.getInetAddresses();
120
121 while (addrList.hasMoreElements()) {
122 InetAddress addr = addrList.nextElement();
123 if (addr.isLoopbackAddress() || addr.isLinkLocalAddress() || addr.isMulticastAddress()) {
124 continue;
125 }
126 String hostName = addr.getHostName();
127 LOG.info("Found " + hostName + " on " + ni);
128
129 TEST_UTIL.getConfiguration().set(HRegionServer.MASTER_HOSTNAME_KEY, hostName);
130
131
132 TEST_UTIL.getConfiguration().set(HRegionServer.RS_HOSTNAME_KEY, hostName);
133 TEST_UTIL.getConfiguration().setBoolean(HRegionServer.RS_HOSTNAME_DISABLE_MASTER_REVERSEDNS_KEY, true);
134 try {
135 TEST_UTIL.startMiniCluster(NUM_MASTERS, NUM_RS);
136 } catch (Exception e) {
137 Throwable t1 = e.getCause();
138 Throwable t2 = t1.getCause();
139 assertTrue(t1.getMessage()+" - "+t2.getMessage(), t2.getMessage().contains(
140 HRegionServer.RS_HOSTNAME_DISABLE_MASTER_REVERSEDNS_KEY + " and " + HRegionServer.RS_HOSTNAME_KEY +
141 " are mutually exclusive"));
142 return;
143 } finally {
144 TEST_UTIL.shutdownMiniCluster();
145 }
146 assertTrue("Failed to validate against conflict hostname configurations", false);
147 }
148 }
149 }
150
151 @Test(timeout=30000)
152 public void testRegionServerHostnameReportedToMaster() throws Exception {
153 final int NUM_MASTERS = 1;
154 final int NUM_RS = 1;
155 Configuration conf = HBaseConfiguration.create();
156 HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(conf);
157 TEST_UTIL.getConfiguration().setBoolean(HRegionServer.RS_HOSTNAME_DISABLE_MASTER_REVERSEDNS_KEY, true);
158 TEST_UTIL.startMiniCluster(NUM_MASTERS, NUM_RS);
159 try {
160 ZooKeeperWatcher zkw = TEST_UTIL.getZooKeeperWatcher();
161 List<String> servers = ZKUtil.listChildrenNoWatch(zkw, zkw.rsZNode);
162
163 assertTrue(servers.size() == NUM_RS);
164 zkw.close();
165 } finally {
166 TEST_UTIL.shutdownMiniCluster();
167 }
168 }
169 }