001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.regionserver; 019 020import static org.junit.Assert.assertEquals; 021import static org.junit.Assert.assertFalse; 022import static org.junit.Assert.assertNull; 023import static org.junit.Assert.assertTrue; 024 025import java.net.InetAddress; 026import java.net.NetworkInterface; 027import java.util.Enumeration; 028import java.util.List; 029import java.util.Locale; 030import org.apache.hadoop.conf.Configuration; 031import org.apache.hadoop.hbase.HBaseClassTestRule; 032import org.apache.hadoop.hbase.HBaseConfiguration; 033import org.apache.hadoop.hbase.HBaseTestingUtility; 034import org.apache.hadoop.hbase.StartMiniClusterOption; 035import org.apache.hadoop.hbase.master.LoadBalancer; 036import org.apache.hadoop.hbase.testclassification.MediumTests; 037import org.apache.hadoop.hbase.testclassification.RegionServerTests; 038import org.apache.hadoop.hbase.util.DNS; 039import org.apache.hadoop.hbase.zookeeper.ZKUtil; 040import org.apache.hadoop.hbase.zookeeper.ZKWatcher; 041import org.junit.After; 042import org.junit.Before; 043import org.junit.ClassRule; 044import org.junit.Test; 045import org.junit.experimental.categories.Category; 046import org.slf4j.Logger; 047import org.slf4j.LoggerFactory; 048 049/** 050 * Tests for the hostname specification by region server 051 */ 052@Category({RegionServerTests.class, MediumTests.class}) 053public class TestRegionServerHostname { 054 055 @ClassRule 056 public static final HBaseClassTestRule CLASS_RULE = 057 HBaseClassTestRule.forClass(TestRegionServerHostname.class); 058 059 private static final Logger LOG = LoggerFactory.getLogger(TestRegionServerHostname.class); 060 061 private HBaseTestingUtility TEST_UTIL; 062 063 private static final int NUM_MASTERS = 1; 064 private static final int NUM_RS = 1; 065 066 @Before 067 public void setup() { 068 Configuration conf = HBaseConfiguration.create(); 069 TEST_UTIL = new HBaseTestingUtility(conf); 070 } 071 072 @After 073 public void teardown() throws Exception { 074 TEST_UTIL.shutdownMiniCluster(); 075 } 076 077 @Test 078 public void testInvalidRegionServerHostnameAbortsServer() throws Exception { 079 String invalidHostname = "hostAddr.invalid"; 080 TEST_UTIL.getConfiguration().set(DNS.UNSAFE_RS_HOSTNAME_KEY, invalidHostname); 081 HRegionServer hrs = null; 082 try { 083 hrs = new HRegionServer(TEST_UTIL.getConfiguration()); 084 } catch (IllegalArgumentException iae) { 085 assertTrue(iae.getMessage(), 086 iae.getMessage().contains("Failed resolve of " + invalidHostname) || 087 iae.getMessage().contains("Problem binding to " + invalidHostname)); 088 } 089 assertNull("Failed to validate against invalid hostname", hrs); 090 } 091 092 @Test 093 public void testRegionServerHostname() throws Exception { 094 Enumeration<NetworkInterface> netInterfaceList = NetworkInterface.getNetworkInterfaces(); 095 while (netInterfaceList.hasMoreElements()) { 096 NetworkInterface ni = netInterfaceList.nextElement(); 097 Enumeration<InetAddress> addrList = ni.getInetAddresses(); 098 // iterate through host addresses and use each as hostname 099 while (addrList.hasMoreElements()) { 100 InetAddress addr = addrList.nextElement(); 101 if (addr.isLoopbackAddress() || addr.isLinkLocalAddress() || addr.isMulticastAddress() || 102 !addr.isSiteLocalAddress()) { 103 continue; 104 } 105 String hostName = addr.getHostName(); 106 LOG.info("Found " + hostName + " on " + ni + ", addr=" + addr); 107 108 TEST_UTIL.getConfiguration().set(DNS.MASTER_HOSTNAME_KEY, hostName); 109 TEST_UTIL.getConfiguration().set(DNS.UNSAFE_RS_HOSTNAME_KEY, hostName); 110 StartMiniClusterOption option = StartMiniClusterOption.builder() 111 .numMasters(NUM_MASTERS).numRegionServers(NUM_RS).numDataNodes(NUM_RS).build(); 112 TEST_UTIL.startMiniCluster(option); 113 try { 114 ZKWatcher zkw = TEST_UTIL.getZooKeeperWatcher(); 115 List<String> servers = ZKUtil.listChildrenNoWatch(zkw, zkw.getZNodePaths().rsZNode); 116 // there would be NUM_RS+1 children - one for the master 117 assertTrue(servers.size() == 118 NUM_RS + (LoadBalancer.isTablesOnMaster(TEST_UTIL.getConfiguration())? 1: 0)); 119 for (String server : servers) { 120 assertTrue("From zookeeper: " + server + " hostname: " + hostName, 121 server.startsWith(hostName.toLowerCase(Locale.ROOT)+",")); 122 } 123 zkw.close(); 124 } finally { 125 TEST_UTIL.shutdownMiniCluster(); 126 } 127 } 128 } 129 } 130 131 @Test 132 public void testDeprecatedConfigs() throws Exception { 133 Configuration conf = TEST_UTIL.getConfiguration(); 134 new HRegionServer(conf); 135 conf.setBoolean(HRegionServer.RS_HOSTNAME_DISABLE_MASTER_REVERSEDNS_KEY, false); 136 assertFalse(conf.getBoolean(HRegionServer.UNSAFE_RS_HOSTNAME_DISABLE_MASTER_REVERSEDNS_KEY, true)); 137 conf.setBoolean(HRegionServer.RS_HOSTNAME_DISABLE_MASTER_REVERSEDNS_KEY, true); 138 assertTrue(conf.getBoolean(HRegionServer.UNSAFE_RS_HOSTNAME_DISABLE_MASTER_REVERSEDNS_KEY, false)); 139 conf.setBoolean(HRegionServer.UNSAFE_RS_HOSTNAME_DISABLE_MASTER_REVERSEDNS_KEY, true); 140 assertTrue(conf.getBoolean(HRegionServer.RS_HOSTNAME_DISABLE_MASTER_REVERSEDNS_KEY, false)); 141 conf.setBoolean(HRegionServer.UNSAFE_RS_HOSTNAME_DISABLE_MASTER_REVERSEDNS_KEY, false); 142 assertFalse(conf.getBoolean(HRegionServer.RS_HOSTNAME_DISABLE_MASTER_REVERSEDNS_KEY, true)); 143 144 conf.setBoolean(DNS.RS_HOSTNAME_KEY, false); 145 assertFalse(conf.getBoolean(DNS.UNSAFE_RS_HOSTNAME_KEY, true)); 146 conf.setBoolean(DNS.RS_HOSTNAME_KEY, true); 147 assertTrue(conf.getBoolean(DNS.UNSAFE_RS_HOSTNAME_KEY, false)); 148 conf.setBoolean(DNS.UNSAFE_RS_HOSTNAME_KEY, true); 149 assertTrue(conf.getBoolean(DNS.RS_HOSTNAME_KEY, false)); 150 conf.setBoolean(DNS.UNSAFE_RS_HOSTNAME_KEY, false); 151 assertFalse(conf.getBoolean(DNS.RS_HOSTNAME_KEY, true)); 152 } 153 154 @Test 155 public void testConflictRegionServerHostnameConfigurationsAbortServer() throws Exception { 156 Enumeration<NetworkInterface> netInterfaceList = NetworkInterface.getNetworkInterfaces(); 157 while (netInterfaceList.hasMoreElements()) { 158 NetworkInterface ni = netInterfaceList.nextElement(); 159 Enumeration<InetAddress> addrList = ni.getInetAddresses(); 160 // iterate through host addresses and use each as hostname 161 while (addrList.hasMoreElements()) { 162 InetAddress addr = addrList.nextElement(); 163 if (addr.isLoopbackAddress() || addr.isLinkLocalAddress() || addr.isMulticastAddress()) { 164 continue; 165 } 166 String hostName = addr.getHostName(); 167 LOG.info("Found " + hostName + " on " + ni); 168 169 TEST_UTIL.getConfiguration().set(DNS.MASTER_HOSTNAME_KEY, hostName); 170 // "hbase.unsafe.regionserver.hostname" and "hbase.unsafe.regionserver.hostname.disable.master.reversedns" 171 // are mutually exclusive. Exception should be thrown if both are used. 172 TEST_UTIL.getConfiguration().set(DNS.UNSAFE_RS_HOSTNAME_KEY, hostName); 173 TEST_UTIL.getConfiguration().setBoolean(HRegionServer.UNSAFE_RS_HOSTNAME_DISABLE_MASTER_REVERSEDNS_KEY, true); 174 try { 175 StartMiniClusterOption option = StartMiniClusterOption.builder() 176 .numMasters(NUM_MASTERS).numRegionServers(NUM_RS).numDataNodes(NUM_RS).build(); 177 TEST_UTIL.startMiniCluster(option); 178 } catch (Exception e) { 179 Throwable t1 = e.getCause(); 180 Throwable t2 = t1.getCause(); 181 assertTrue(t1.getMessage()+" - "+t2.getMessage(), t2.getMessage().contains( 182 HRegionServer.UNSAFE_RS_HOSTNAME_DISABLE_MASTER_REVERSEDNS_KEY + " and " + 183 DNS.UNSAFE_RS_HOSTNAME_KEY + " are mutually exclusive")); 184 return; 185 } finally { 186 TEST_UTIL.shutdownMiniCluster(); 187 } 188 assertTrue("Failed to validate against conflict hostname configurations", false); 189 } 190 } 191 } 192 193 @Test 194 public void testRegionServerHostnameReportedToMaster() throws Exception { 195 TEST_UTIL.getConfiguration().setBoolean(HRegionServer.UNSAFE_RS_HOSTNAME_DISABLE_MASTER_REVERSEDNS_KEY, 196 true); 197 StartMiniClusterOption option = StartMiniClusterOption.builder() 198 .numMasters(NUM_MASTERS).numRegionServers(NUM_RS).numDataNodes(NUM_RS).build(); 199 TEST_UTIL.startMiniCluster(option); 200 boolean tablesOnMaster = LoadBalancer.isTablesOnMaster(TEST_UTIL.getConfiguration()); 201 int expectedRS = NUM_RS + (tablesOnMaster? 1: 0); 202 try (ZKWatcher zkw = TEST_UTIL.getZooKeeperWatcher()) { 203 List<String> servers = ZKUtil.listChildrenNoWatch(zkw, zkw.getZNodePaths().rsZNode); 204 assertEquals(expectedRS, servers.size()); 205 } 206 } 207}