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