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(), iae.getMessage().contains("Failed resolve of " + invalidHostname) 086 || iae.getMessage().contains("Problem binding to " + invalidHostname)); 087 } 088 assertNull("Failed to validate against invalid hostname", hrs); 089 } 090 091 @Test 092 public void testRegionServerHostname() throws Exception { 093 Enumeration<NetworkInterface> netInterfaceList = NetworkInterface.getNetworkInterfaces(); 094 while (netInterfaceList.hasMoreElements()) { 095 NetworkInterface ni = netInterfaceList.nextElement(); 096 Enumeration<InetAddress> addrList = ni.getInetAddresses(); 097 // iterate through host addresses and use each as hostname 098 while (addrList.hasMoreElements()) { 099 InetAddress addr = addrList.nextElement(); 100 if ( 101 addr.isLoopbackAddress() || addr.isLinkLocalAddress() || addr.isMulticastAddress() 102 || !addr.isSiteLocalAddress() 103 ) { 104 continue; 105 } 106 String hostName = addr.getHostName(); 107 LOG.info("Found " + hostName + " on " + ni + ", addr=" + addr); 108 109 TEST_UTIL.getConfiguration().set(DNS.MASTER_HOSTNAME_KEY, hostName); 110 TEST_UTIL.getConfiguration().set(DNS.UNSAFE_RS_HOSTNAME_KEY, hostName); 111 StartMiniClusterOption option = StartMiniClusterOption.builder().numMasters(NUM_MASTERS) 112 .numRegionServers(NUM_RS).numDataNodes(NUM_RS).build(); 113 TEST_UTIL.startMiniCluster(option); 114 try { 115 ZKWatcher zkw = TEST_UTIL.getZooKeeperWatcher(); 116 List<String> servers = ZKUtil.listChildrenNoWatch(zkw, zkw.getZNodePaths().rsZNode); 117 // there would be NUM_RS+1 children - one for the master 118 assertTrue(servers.size() 119 == NUM_RS + (LoadBalancer.isTablesOnMaster(TEST_UTIL.getConfiguration()) ? 1 : 0)); 120 for (String server : servers) { 121 assertTrue("From zookeeper: " + server + " hostname: " + hostName, 122 server.startsWith(hostName.toLowerCase(Locale.ROOT) + ",")); 123 } 124 zkw.close(); 125 } finally { 126 TEST_UTIL.shutdownMiniCluster(); 127 } 128 } 129 } 130 } 131 132 @Test 133 public void testDeprecatedConfigs() throws Exception { 134 Configuration conf = TEST_UTIL.getConfiguration(); 135 new HRegionServer(conf); 136 conf.setBoolean(HRegionServer.RS_HOSTNAME_DISABLE_MASTER_REVERSEDNS_KEY, false); 137 assertFalse( 138 conf.getBoolean(HRegionServer.UNSAFE_RS_HOSTNAME_DISABLE_MASTER_REVERSEDNS_KEY, true)); 139 conf.setBoolean(HRegionServer.RS_HOSTNAME_DISABLE_MASTER_REVERSEDNS_KEY, true); 140 assertTrue( 141 conf.getBoolean(HRegionServer.UNSAFE_RS_HOSTNAME_DISABLE_MASTER_REVERSEDNS_KEY, false)); 142 conf.setBoolean(HRegionServer.UNSAFE_RS_HOSTNAME_DISABLE_MASTER_REVERSEDNS_KEY, true); 143 assertTrue(conf.getBoolean(HRegionServer.RS_HOSTNAME_DISABLE_MASTER_REVERSEDNS_KEY, false)); 144 conf.setBoolean(HRegionServer.UNSAFE_RS_HOSTNAME_DISABLE_MASTER_REVERSEDNS_KEY, false); 145 assertFalse(conf.getBoolean(HRegionServer.RS_HOSTNAME_DISABLE_MASTER_REVERSEDNS_KEY, true)); 146 147 conf.setBoolean(DNS.RS_HOSTNAME_KEY, false); 148 assertFalse(conf.getBoolean(DNS.UNSAFE_RS_HOSTNAME_KEY, true)); 149 conf.setBoolean(DNS.RS_HOSTNAME_KEY, true); 150 assertTrue(conf.getBoolean(DNS.UNSAFE_RS_HOSTNAME_KEY, false)); 151 conf.setBoolean(DNS.UNSAFE_RS_HOSTNAME_KEY, true); 152 assertTrue(conf.getBoolean(DNS.RS_HOSTNAME_KEY, false)); 153 conf.setBoolean(DNS.UNSAFE_RS_HOSTNAME_KEY, false); 154 assertFalse(conf.getBoolean(DNS.RS_HOSTNAME_KEY, true)); 155 } 156 157 @Test 158 public void testConflictRegionServerHostnameConfigurationsAbortServer() throws Exception { 159 Enumeration<NetworkInterface> netInterfaceList = NetworkInterface.getNetworkInterfaces(); 160 while (netInterfaceList.hasMoreElements()) { 161 NetworkInterface ni = netInterfaceList.nextElement(); 162 Enumeration<InetAddress> addrList = ni.getInetAddresses(); 163 // iterate through host addresses and use each as hostname 164 while (addrList.hasMoreElements()) { 165 InetAddress addr = addrList.nextElement(); 166 if (addr.isLoopbackAddress() || addr.isLinkLocalAddress() || addr.isMulticastAddress()) { 167 continue; 168 } 169 String hostName = addr.getHostName(); 170 LOG.info("Found " + hostName + " on " + ni); 171 172 TEST_UTIL.getConfiguration().set(DNS.MASTER_HOSTNAME_KEY, hostName); 173 // "hbase.unsafe.regionserver.hostname" and 174 // "hbase.unsafe.regionserver.hostname.disable.master.reversedns" 175 // are mutually exclusive. Exception should be thrown if both are used. 176 TEST_UTIL.getConfiguration().set(DNS.UNSAFE_RS_HOSTNAME_KEY, hostName); 177 TEST_UTIL.getConfiguration() 178 .setBoolean(HRegionServer.UNSAFE_RS_HOSTNAME_DISABLE_MASTER_REVERSEDNS_KEY, true); 179 try { 180 StartMiniClusterOption option = StartMiniClusterOption.builder().numMasters(NUM_MASTERS) 181 .numRegionServers(NUM_RS).numDataNodes(NUM_RS).build(); 182 TEST_UTIL.startMiniCluster(option); 183 } catch (Exception e) { 184 Throwable t1 = e.getCause(); 185 Throwable t2 = t1.getCause(); 186 assertTrue(t1.getMessage() + " - " + t2.getMessage(), 187 t2.getMessage().contains(HRegionServer.UNSAFE_RS_HOSTNAME_DISABLE_MASTER_REVERSEDNS_KEY 188 + " and " + DNS.UNSAFE_RS_HOSTNAME_KEY + " are mutually exclusive")); 189 return; 190 } finally { 191 TEST_UTIL.shutdownMiniCluster(); 192 } 193 assertTrue("Failed to validate against conflict hostname configurations", false); 194 } 195 } 196 } 197 198 @Test 199 public void testRegionServerHostnameReportedToMaster() throws Exception { 200 TEST_UTIL.getConfiguration() 201 .setBoolean(HRegionServer.UNSAFE_RS_HOSTNAME_DISABLE_MASTER_REVERSEDNS_KEY, true); 202 StartMiniClusterOption option = StartMiniClusterOption.builder().numMasters(NUM_MASTERS) 203 .numRegionServers(NUM_RS).numDataNodes(NUM_RS).build(); 204 TEST_UTIL.startMiniCluster(option); 205 boolean tablesOnMaster = LoadBalancer.isTablesOnMaster(TEST_UTIL.getConfiguration()); 206 int expectedRS = NUM_RS + (tablesOnMaster ? 1 : 0); 207 try (ZKWatcher zkw = TEST_UTIL.getZooKeeperWatcher()) { 208 List<String> servers = ZKUtil.listChildrenNoWatch(zkw, zkw.getZNodePaths().rsZNode); 209 assertEquals(expectedRS, servers.size()); 210 } 211 } 212}