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