2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org
.apache
.hadoop
.hbase
.regionserver
;
20 import static org
.junit
.Assert
.assertEquals
;
21 import static org
.junit
.Assert
.assertFalse
;
22 import static org
.junit
.Assert
.assertNull
;
23 import static org
.junit
.Assert
.assertTrue
;
25 import java
.net
.InetAddress
;
26 import java
.net
.NetworkInterface
;
27 import java
.util
.Enumeration
;
28 import java
.util
.List
;
29 import java
.util
.Locale
;
30 import org
.apache
.hadoop
.conf
.Configuration
;
31 import org
.apache
.hadoop
.hbase
.HBaseClassTestRule
;
32 import org
.apache
.hadoop
.hbase
.HBaseConfiguration
;
33 import org
.apache
.hadoop
.hbase
.HBaseTestingUtil
;
34 import org
.apache
.hadoop
.hbase
.StartTestingClusterOption
;
35 import org
.apache
.hadoop
.hbase
.testclassification
.MediumTests
;
36 import org
.apache
.hadoop
.hbase
.testclassification
.RegionServerTests
;
37 import org
.apache
.hadoop
.hbase
.util
.DNS
;
38 import org
.apache
.hadoop
.hbase
.zookeeper
.ZKUtil
;
39 import org
.apache
.hadoop
.hbase
.zookeeper
.ZKWatcher
;
40 import org
.junit
.After
;
41 import org
.junit
.Before
;
42 import org
.junit
.ClassRule
;
43 import org
.junit
.Test
;
44 import org
.junit
.experimental
.categories
.Category
;
45 import org
.slf4j
.Logger
;
46 import org
.slf4j
.LoggerFactory
;
49 * Tests for the hostname specification by region server
51 @Category({RegionServerTests
.class, MediumTests
.class})
52 public class TestRegionServerHostname
{
55 public static final HBaseClassTestRule CLASS_RULE
=
56 HBaseClassTestRule
.forClass(TestRegionServerHostname
.class);
58 private static final Logger LOG
= LoggerFactory
.getLogger(TestRegionServerHostname
.class);
60 private HBaseTestingUtil TEST_UTIL
;
62 private static final int NUM_MASTERS
= 1;
63 private static final int NUM_RS
= 1;
67 Configuration conf
= HBaseConfiguration
.create();
68 TEST_UTIL
= new HBaseTestingUtil(conf
);
72 public void teardown() throws Exception
{
73 TEST_UTIL
.shutdownMiniCluster();
77 public void testInvalidRegionServerHostnameAbortsServer() throws Exception
{
78 String invalidHostname
= "hostAddr.invalid";
79 TEST_UTIL
.getConfiguration().set(DNS
.UNSAFE_RS_HOSTNAME_KEY
, invalidHostname
);
80 HRegionServer hrs
= null;
82 hrs
= new HRegionServer(TEST_UTIL
.getConfiguration());
83 } catch (IllegalArgumentException iae
) {
84 assertTrue(iae
.getMessage(),
85 iae
.getMessage().contains("Failed resolve of " + invalidHostname
) ||
86 iae
.getMessage().contains("Problem binding to " + invalidHostname
));
88 assertNull("Failed to validate against invalid hostname", hrs
);
92 public void testRegionServerHostname() throws Exception
{
93 Enumeration
<NetworkInterface
> netInterfaceList
= NetworkInterface
.getNetworkInterfaces();
94 while (netInterfaceList
.hasMoreElements()) {
95 NetworkInterface ni
= netInterfaceList
.nextElement();
96 Enumeration
<InetAddress
> addrList
= ni
.getInetAddresses();
97 // iterate through host addresses and use each as hostname
98 while (addrList
.hasMoreElements()) {
99 InetAddress addr
= addrList
.nextElement();
100 if (addr
.isLoopbackAddress() || addr
.isLinkLocalAddress() || addr
.isMulticastAddress() ||
101 !addr
.isSiteLocalAddress()) {
104 String hostName
= addr
.getHostName();
105 LOG
.info("Found " + hostName
+ " on " + ni
+ ", addr=" + addr
);
107 TEST_UTIL
.getConfiguration().set(DNS
.MASTER_HOSTNAME_KEY
, hostName
);
108 TEST_UTIL
.getConfiguration().set(DNS
.UNSAFE_RS_HOSTNAME_KEY
, hostName
);
109 StartTestingClusterOption option
= StartTestingClusterOption
.builder()
110 .numMasters(NUM_MASTERS
).numRegionServers(NUM_RS
).numDataNodes(NUM_RS
).build();
111 TEST_UTIL
.startMiniCluster(option
);
113 ZKWatcher zkw
= TEST_UTIL
.getZooKeeperWatcher();
114 List
<String
> servers
= ZKUtil
.listChildrenNoWatch(zkw
, zkw
.getZNodePaths().rsZNode
);
115 assertEquals(NUM_RS
, servers
.size());
116 for (String server
: servers
) {
117 assertTrue("From zookeeper: " + server
+ " hostname: " + hostName
,
118 server
.startsWith(hostName
.toLowerCase(Locale
.ROOT
)+","));
122 TEST_UTIL
.shutdownMiniCluster();
129 public void testDeprecatedConfigs() throws Exception
{
130 Configuration conf
= TEST_UTIL
.getConfiguration();
131 new HRegionServer(conf
);
132 conf
.setBoolean(HRegionServer
.RS_HOSTNAME_DISABLE_MASTER_REVERSEDNS_KEY
, false);
133 assertFalse(conf
.getBoolean(HRegionServer
.UNSAFE_RS_HOSTNAME_DISABLE_MASTER_REVERSEDNS_KEY
, true));
134 conf
.setBoolean(HRegionServer
.RS_HOSTNAME_DISABLE_MASTER_REVERSEDNS_KEY
, true);
135 assertTrue(conf
.getBoolean(HRegionServer
.UNSAFE_RS_HOSTNAME_DISABLE_MASTER_REVERSEDNS_KEY
, false));
136 conf
.setBoolean(HRegionServer
.UNSAFE_RS_HOSTNAME_DISABLE_MASTER_REVERSEDNS_KEY
, true);
137 assertTrue(conf
.getBoolean(HRegionServer
.RS_HOSTNAME_DISABLE_MASTER_REVERSEDNS_KEY
, false));
138 conf
.setBoolean(HRegionServer
.UNSAFE_RS_HOSTNAME_DISABLE_MASTER_REVERSEDNS_KEY
, false);
139 assertFalse(conf
.getBoolean(HRegionServer
.RS_HOSTNAME_DISABLE_MASTER_REVERSEDNS_KEY
, true));
141 conf
.setBoolean(DNS
.RS_HOSTNAME_KEY
, false);
142 assertFalse(conf
.getBoolean(DNS
.UNSAFE_RS_HOSTNAME_KEY
, true));
143 conf
.setBoolean(DNS
.RS_HOSTNAME_KEY
, true);
144 assertTrue(conf
.getBoolean(DNS
.UNSAFE_RS_HOSTNAME_KEY
, false));
145 conf
.setBoolean(DNS
.UNSAFE_RS_HOSTNAME_KEY
, true);
146 assertTrue(conf
.getBoolean(DNS
.RS_HOSTNAME_KEY
, false));
147 conf
.setBoolean(DNS
.UNSAFE_RS_HOSTNAME_KEY
, false);
148 assertFalse(conf
.getBoolean(DNS
.RS_HOSTNAME_KEY
, true));
152 public void testConflictRegionServerHostnameConfigurationsAbortServer() throws Exception
{
153 Enumeration
<NetworkInterface
> netInterfaceList
= NetworkInterface
.getNetworkInterfaces();
154 while (netInterfaceList
.hasMoreElements()) {
155 NetworkInterface ni
= netInterfaceList
.nextElement();
156 Enumeration
<InetAddress
> addrList
= ni
.getInetAddresses();
157 // iterate through host addresses and use each as hostname
158 while (addrList
.hasMoreElements()) {
159 InetAddress addr
= addrList
.nextElement();
160 if (addr
.isLoopbackAddress() || addr
.isLinkLocalAddress() || addr
.isMulticastAddress()) {
163 String hostName
= addr
.getHostName();
164 LOG
.info("Found " + hostName
+ " on " + ni
);
166 TEST_UTIL
.getConfiguration().set(DNS
.MASTER_HOSTNAME_KEY
, hostName
);
167 // "hbase.unsafe.regionserver.hostname" and "hbase.unsafe.regionserver.hostname.disable.master.reversedns"
168 // are mutually exclusive. Exception should be thrown if both are used.
169 TEST_UTIL
.getConfiguration().set(DNS
.UNSAFE_RS_HOSTNAME_KEY
, hostName
);
170 TEST_UTIL
.getConfiguration().setBoolean(HRegionServer
.UNSAFE_RS_HOSTNAME_DISABLE_MASTER_REVERSEDNS_KEY
, true);
172 StartTestingClusterOption option
= StartTestingClusterOption
.builder()
173 .numMasters(NUM_MASTERS
).numRegionServers(NUM_RS
).numDataNodes(NUM_RS
).build();
174 TEST_UTIL
.startMiniCluster(option
);
175 } catch (Exception e
) {
176 Throwable t1
= e
.getCause();
177 Throwable t2
= t1
.getCause();
178 assertTrue(t1
.getMessage()+" - "+t2
.getMessage(), t2
.getMessage().contains(
179 HRegionServer
.UNSAFE_RS_HOSTNAME_DISABLE_MASTER_REVERSEDNS_KEY
+ " and " +
180 DNS
.UNSAFE_RS_HOSTNAME_KEY
+ " are mutually exclusive"));
183 TEST_UTIL
.shutdownMiniCluster();
185 assertTrue("Failed to validate against conflict hostname configurations", false);
191 public void testRegionServerHostnameReportedToMaster() throws Exception
{
192 TEST_UTIL
.getConfiguration().setBoolean(HRegionServer
.UNSAFE_RS_HOSTNAME_DISABLE_MASTER_REVERSEDNS_KEY
,
194 StartTestingClusterOption option
= StartTestingClusterOption
.builder()
195 .numMasters(NUM_MASTERS
).numRegionServers(NUM_RS
).numDataNodes(NUM_RS
).build();
196 TEST_UTIL
.startMiniCluster(option
);
197 int expectedRS
= NUM_RS
;
198 try (ZKWatcher zkw
= TEST_UTIL
.getZooKeeperWatcher()) {
199 List
<String
> servers
= ZKUtil
.listChildrenNoWatch(zkw
, zkw
.getZNodePaths().rsZNode
);
200 assertEquals(expectedRS
, servers
.size());