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.hbase.HBaseTestingUtility;
31 import org.apache.hadoop.hbase.testclassification.MediumTests;
32 import org.apache.hadoop.hbase.util.Threads;
33 import org.apache.hadoop.hbase.zookeeper.ZKUtil;
34 import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
35 import org.junit.Ignore;
36 import org.junit.Test;
37 import org.junit.experimental.categories.Category;
38
39
40
41
42 @Category({MediumTests.class})
43 public class TestRegionServerHostname {
44 private static final Log LOG = LogFactory.getLog(TestRegionServerHostname.class);
45 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
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 TEST_UTIL.getConfiguration().set(HRegionServer.RS_HOSTNAME_KEY, invalidHostname);
53 try {
54 TEST_UTIL.startMiniCluster(NUM_MASTERS, NUM_RS);
55 } catch (IOException ioe) {
56 Throwable t1 = ioe.getCause();
57 Throwable t2 = t1.getCause();
58 assertTrue(t1.getMessage() + " - " + t2.getMessage(),
59 t2.getMessage().contains("Failed resolve of " + invalidHostname) ||
60 t2.getMessage().contains("Problem binding to " + invalidHostname));
61 return;
62 } finally {
63 TEST_UTIL.shutdownMiniCluster();
64 }
65 assertTrue("Failed to validate against invalid hostname", false);
66 }
67
68 @Ignore @Test(timeout=120000)
69 public void testRegionServerHostname() throws Exception {
70 final int NUM_MASTERS = 1;
71 final int NUM_RS = 1;
72 Enumeration<NetworkInterface> netInterfaceList = NetworkInterface.getNetworkInterfaces();
73
74 while (netInterfaceList.hasMoreElements()) {
75 NetworkInterface ni = netInterfaceList.nextElement();
76 Enumeration<InetAddress> addrList = ni.getInetAddresses();
77
78 while (addrList.hasMoreElements()) {
79 InetAddress addr = addrList.nextElement();
80 if (addr.isLoopbackAddress() || addr.isLinkLocalAddress() || addr.isMulticastAddress()) {
81 continue;
82 }
83 String hostName = addr.getHostName();
84 LOG.info("Found " + hostName + " on " + ni);
85
86 TEST_UTIL.getConfiguration().set(HRegionServer.RS_HOSTNAME_KEY, hostName);
87 TEST_UTIL.startMiniCluster(NUM_MASTERS, NUM_RS);
88 try {
89 ZooKeeperWatcher zkw = TEST_UTIL.getZooKeeperWatcher();
90 List<String> servers = ZKUtil.listChildrenNoWatch(zkw, zkw.rsZNode);
91 while (servers == null) {
92 Threads.sleep(10);
93 }
94 assertTrue(servers.size() == NUM_RS);
95 for (String server : servers) {
96 assertTrue(server.startsWith(hostName+","));
97 }
98 zkw.close();
99 } finally {
100 TEST_UTIL.shutdownMiniCluster();
101 }
102 }
103 }
104 }
105 }