View Javadoc

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