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  
19  package org.apache.hadoop.hbase;
20  
21  import java.io.IOException;
22  import java.net.BindException;
23  import java.net.InetAddress;
24  import java.net.InetSocketAddress;
25  import java.net.ServerSocket;
26  import java.nio.channels.ServerSocketChannel;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.junit.Assert;
31  import org.junit.Test;
32  import org.junit.experimental.categories.Category;
33  
34  /**
35   * This tests whether ServerSocketChannel works over ipv6, which Zookeeper
36   * depends on. On Windows Oracle JDK 6, creating a ServerSocketChannel throws
37   * java.net.SocketException: Address family not supported by protocol family
38   * exception. It is a known JVM bug, seems to be only resolved for JDK7:
39   * http://bugs.sun.com/view_bug.do?bug_id=6230761
40   *
41   * For this test, we check that whether we are effected by this bug, and if so
42   * the test ensures that we are running with java.net.preferIPv4Stack=true, so
43   * that ZK will not fail to bind to ipv6 address using ClientCnxnSocketNIO.
44   */
45  @Category(SmallTests.class)
46  public class TestIPv6NIOServerSocketChannel {
47  
48    private static final Log LOG = LogFactory.getLog(TestIPv6NIOServerSocketChannel.class);
49  
50    /**
51     * Creates and binds a regular ServerSocket.
52     */
53    private void bindServerSocket(InetAddress inetAddr) throws IOException {
54      while(true) {
55        int port = HBaseTestingUtility.randomFreePort();
56        InetSocketAddress addr = new InetSocketAddress(inetAddr, port);
57        ServerSocket serverSocket = null;
58        try {
59          serverSocket = new ServerSocket();
60          serverSocket.bind(addr);
61          break;
62        } catch (BindException ex) {
63          //continue
64        } finally {
65          if (serverSocket != null) {
66            serverSocket.close();
67          }
68        }
69      }
70    }
71  
72    /**
73     * Creates a NIO ServerSocketChannel, and gets the ServerSocket from
74     * there. Then binds the obtained socket.
75     * This fails on Windows with Oracle JDK1.6.0u33, if the passed InetAddress is a
76     * IPv6 address. Works on Oracle JDK 1.7.
77     */
78    private void bindNIOServerSocket(InetAddress inetAddr) throws IOException {
79      while (true) {
80        int port = HBaseTestingUtility.randomFreePort();
81        InetSocketAddress addr = new InetSocketAddress(inetAddr, port);
82        ServerSocketChannel channel = null;
83        ServerSocket serverSocket = null;
84        try {
85          channel = ServerSocketChannel.open();
86          serverSocket = channel.socket();
87          serverSocket.bind(addr); // This does not work
88          break;
89        } catch (BindException ex) {
90          //continue
91        } finally {
92          if (serverSocket != null) {
93            serverSocket.close();
94          }
95          if (channel != null) {
96            channel.close();
97          }
98        }  
99      }
100   }
101 
102   /**
103    * Checks whether we are effected by the JDK issue on windows, and if so
104    * ensures that we are running with preferIPv4Stack=true.
105    */
106   @Test
107   public void testServerSocket() throws IOException {
108     byte[] addr = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
109     InetAddress inetAddr = InetAddress.getByAddress(addr);
110 
111     try {
112       bindServerSocket(inetAddr);
113       bindNIOServerSocket(inetAddr);
114       //if on *nix or windows JDK7, both will pass
115     } catch(java.net.SocketException ex) {
116       //On Windows JDK6, we will get expected exception:
117       //java.net.SocketException: Address family not supported by protocol family
118       //or java.net.SocketException: Protocol family not supported
119       Assert.assertFalse(ex.getClass().isInstance(BindException.class));
120       Assert.assertTrue(ex.getMessage().toLowerCase().contains("protocol family"));
121       LOG.info("Received expected exception:");
122       LOG.info(ex);
123 
124       //if this is the case, ensure that we are running on preferIPv4=true
125       ensurePreferIPv4();
126     }
127   }
128 
129   /**
130    * Checks whether we are running with java.net.preferIPv4Stack=true
131    */
132   public void ensurePreferIPv4() throws IOException {
133     InetAddress[] addrs = InetAddress.getAllByName("localhost");
134     for (InetAddress addr : addrs) {
135       LOG.info("resolved localhost as:" + addr);
136       Assert.assertEquals(4, addr.getAddress().length); //ensure 4 byte ipv4 address
137     }
138   }
139 
140   /**
141    * Tests whether every InetAddress we obtain by resolving can open a
142    * ServerSocketChannel.
143    */
144   @Test
145   public void testServerSocketFromLocalhostResolution() throws IOException {
146     InetAddress[] addrs = InetAddress.getAllByName("localhost");
147     for (InetAddress addr : addrs) {
148       LOG.info("resolved localhost as:" + addr);
149       bindServerSocket(addr);
150       bindNIOServerSocket(addr);
151     }
152   }
153 
154   public static void main(String[] args) throws Exception {
155     TestIPv6NIOServerSocketChannel test = new TestIPv6NIOServerSocketChannel();
156     test.testServerSocket();
157     test.testServerSocketFromLocalhostResolution();
158   }
159 }