zookeeper-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cami...@apache.org
Subject svn commit: r1606254 - in /zookeeper/trunk: CHANGES.txt src/java/main/org/apache/zookeeper/client/HostProvider.java src/java/main/org/apache/zookeeper/client/StaticHostProvider.java src/java/test/org/apache/zookeeper/test/StaticHostProviderTest.java
Date Fri, 27 Jun 2014 21:47:50 GMT
Author: camille
Date: Fri Jun 27 21:47:49 2014
New Revision: 1606254

URL: http://svn.apache.org/r1606254
Log:
ZOOKEEPER-1576. Zookeeper cluster - failed to connect to cluster if one of the 
  provided IPs causes java.net.UnknownHostException (Edward Ribeiro via camille)

Modified:
    zookeeper/trunk/CHANGES.txt
    zookeeper/trunk/src/java/main/org/apache/zookeeper/client/HostProvider.java
    zookeeper/trunk/src/java/main/org/apache/zookeeper/client/StaticHostProvider.java
    zookeeper/trunk/src/java/test/org/apache/zookeeper/test/StaticHostProviderTest.java

Modified: zookeeper/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/zookeeper/trunk/CHANGES.txt?rev=1606254&r1=1606253&r2=1606254&view=diff
==============================================================================
--- zookeeper/trunk/CHANGES.txt (original)
+++ zookeeper/trunk/CHANGES.txt Fri Jun 27 21:47:49 2014
@@ -944,6 +944,9 @@ IMPROVEMENTS:
 
   ZOOKEEPER-1938. bump version in the C library as we prepare for
   3.5.0 release (Raul Gutierrez Segales via phunt)
+  
+  ZOOKEEPER-1576. Zookeeper cluster - failed to connect to cluster if one of the 
+  provided IPs causes java.net.UnknownHostException (Edward Ribeiro via camille)
 
 headers
 

Modified: zookeeper/trunk/src/java/main/org/apache/zookeeper/client/HostProvider.java
URL: http://svn.apache.org/viewvc/zookeeper/trunk/src/java/main/org/apache/zookeeper/client/HostProvider.java?rev=1606254&r1=1606253&r2=1606254&view=diff
==============================================================================
--- zookeeper/trunk/src/java/main/org/apache/zookeeper/client/HostProvider.java (original)
+++ zookeeper/trunk/src/java/main/org/apache/zookeeper/client/HostProvider.java Fri Jun 27
21:47:49 2014
@@ -67,6 +67,6 @@ public interface HostProvider {
      * @param currentHost the host to which this client is currently connected
      * @return true if changing connections is necessary for load-balancing, false otherwise
 
      */
-	boolean updateServerList(Collection<InetSocketAddress> serverAddresses, InetSocketAddress
currentHost)
-			throws UnknownHostException;
+    boolean updateServerList(Collection<InetSocketAddress> serverAddresses,
+        InetSocketAddress currentHost);
 }

Modified: zookeeper/trunk/src/java/main/org/apache/zookeeper/client/StaticHostProvider.java
URL: http://svn.apache.org/viewvc/zookeeper/trunk/src/java/main/org/apache/zookeeper/client/StaticHostProvider.java?rev=1606254&r1=1606253&r2=1606254&view=diff
==============================================================================
--- zookeeper/trunk/src/java/main/org/apache/zookeeper/client/StaticHostProvider.java (original)
+++ zookeeper/trunk/src/java/main/org/apache/zookeeper/client/StaticHostProvider.java Fri
Jun 27 21:47:49 2014
@@ -68,12 +68,10 @@ public final class StaticHostProvider im
      * 
      * @param serverAddresses
      *            possibly unresolved ZooKeeper server addresses
-     * @throws UnknownHostException
      * @throws IllegalArgumentException
      *             if serverAddresses is empty or resolves to an empty list
      */
-    public StaticHostProvider(Collection<InetSocketAddress> serverAddresses)
-            throws UnknownHostException {
+    public StaticHostProvider(Collection<InetSocketAddress> serverAddresses) {
        sourceOfRandomness = new Random(System.currentTimeMillis() ^ this.hashCode());
 
         this.serverAddresses = resolveAndShuffle(serverAddresses);
@@ -92,12 +90,11 @@ public final class StaticHostProvider im
      * @param serverAddresses
      *            possibly unresolved ZooKeeper server addresses
      * @param randomnessSeed a seed used to initialize sourceOfRandomnes
-     * @throws UnknownHostException
      * @throws IllegalArgumentException
      *             if serverAddresses is empty or resolves to an empty list
      */
-    public StaticHostProvider(Collection<InetSocketAddress> serverAddresses, long randomnessSeed)
-            throws UnknownHostException {
+    public StaticHostProvider(Collection<InetSocketAddress> serverAddresses,
+        long randomnessSeed) {
         sourceOfRandomness = new Random(randomnessSeed);
 
         this.serverAddresses = resolveAndShuffle(serverAddresses);
@@ -109,18 +106,22 @@ public final class StaticHostProvider im
         lastIndex = -1;              
     }
 
-    private List<InetSocketAddress> resolveAndShuffle(Collection<InetSocketAddress>
serverAddresses)
-            throws UnknownHostException {
+    private List<InetSocketAddress> resolveAndShuffle(Collection<InetSocketAddress>
serverAddresses) {
         List<InetSocketAddress> tmpList = new ArrayList<InetSocketAddress>(serverAddresses.size());
      
         for (InetSocketAddress address : serverAddresses) {
-            InetAddress ia = address.getAddress();
-            InetAddress resolvedAddresses[] = InetAddress.getAllByName((ia!=null) ? ia.getHostAddress():
-                    address.getHostName());
-            for (InetAddress resolvedAddress : resolvedAddresses) {
-                tmpList.add(new InetSocketAddress(InetAddress.getByAddress(
-                                HostNameUtils.getHostString(address),
-                                resolvedAddress.getAddress()),
-                                address.getPort()));
+            try {
+                InetAddress ia = address.getAddress();
+                String addr = (ia != null) ? ia.getHostAddress() :
+                                             address.getHostName();
+                InetAddress resolvedAddresses[] = InetAddress.getAllByName(addr);
+                for (InetAddress resolvedAddress : resolvedAddresses) {
+                    tmpList.add(new InetSocketAddress(InetAddress.getByAddress(
+                                    HostNameUtils.getHostString(address),
+                                    resolvedAddress.getAddress()),
+                                    address.getPort()));
+                }
+            } catch (UnknownHostException ex) {
+                LOG.warn("No IP address found for server: {}", address, ex);
             }
         }
         Collections.shuffle(tmpList, sourceOfRandomness);
@@ -151,7 +152,8 @@ public final class StaticHostProvider im
 
 
     @Override
-    public boolean updateServerList(Collection<InetSocketAddress> serverAddresses,
InetSocketAddress currentHost) throws UnknownHostException {        
+    public boolean updateServerList(Collection<InetSocketAddress> serverAddresses,

+        InetSocketAddress currentHost) {
         // Resolve server addresses and shuffle them
         List<InetSocketAddress> resolvedList = resolveAndShuffle(serverAddresses);
         if (resolvedList.isEmpty()) {

Modified: zookeeper/trunk/src/java/test/org/apache/zookeeper/test/StaticHostProviderTest.java
URL: http://svn.apache.org/viewvc/zookeeper/trunk/src/java/test/org/apache/zookeeper/test/StaticHostProviderTest.java?rev=1606254&r1=1606253&r2=1606254&view=diff
==============================================================================
--- zookeeper/trunk/src/java/test/org/apache/zookeeper/test/StaticHostProviderTest.java (original)
+++ zookeeper/trunk/src/java/test/org/apache/zookeeper/test/StaticHostProviderTest.java Fri
Jun 27 21:47:49 2014
@@ -39,7 +39,7 @@ public class StaticHostProviderTest exte
     private Random r = new Random(1);
 
     @Test
-    public void testNextGoesRound() throws UnknownHostException {
+    public void testNextGoesRound() {
         HostProvider hostProvider = getHostProvider((byte) 2);
         InetSocketAddress first = hostProvider.next(0);
         assertTrue(first != null);
@@ -48,7 +48,7 @@ public class StaticHostProviderTest exte
     }
 
     @Test
-    public void testNextGoesRoundAndSleeps() throws UnknownHostException {
+    public void testNextGoesRoundAndSleeps() {
         byte size = 2;
         HostProvider hostProvider = getHostProvider(size);
         while (size > 0) {
@@ -62,7 +62,7 @@ public class StaticHostProviderTest exte
     }
 
     @Test
-    public void testNextDoesNotSleepForZero() throws UnknownHostException {
+    public void testNextDoesNotSleepForZero() {
         byte size = 2;
         HostProvider hostProvider = getHostProvider(size);
         while (size > 0) {
@@ -75,15 +75,34 @@ public class StaticHostProviderTest exte
         assertTrue(5 > stop - start);
     }
 
+    @Test(expected = IllegalArgumentException.class)
+    public void testTwoInvalidHostAddresses() {
+        ArrayList<InetSocketAddress> list = new ArrayList<InetSocketAddress>();
+        list.add(new InetSocketAddress("a", 2181));
+        list.add(new InetSocketAddress("b", 2181));
+        new StaticHostProvider(list);
+    }
+
     @Test
-    public void testTwoConsequitiveCallsToNextReturnDifferentElement()
-            throws UnknownHostException {
+    public void testOneInvalidHostAddresses() {
+        Collection<InetSocketAddress> addr = getServerAddresses((byte) 1);
+        addr.add(new InetSocketAddress("a", 2181));
+
+        StaticHostProvider sp = new StaticHostProvider(addr);
+        InetSocketAddress n1 = sp.next(0);
+        InetSocketAddress n2 = sp.next(0);
+
+        assertEquals(n2, n1);
+    }
+
+    @Test
+    public void testTwoConsequitiveCallsToNextReturnDifferentElement() {
         HostProvider hostProvider = getHostProvider((byte) 2);
         assertNotSame(hostProvider.next(0), hostProvider.next(0));
     }
 
     @Test
-    public void testOnConnectDoesNotReset() throws UnknownHostException {
+    public void testOnConnectDoesNotReset() {
         HostProvider hostProvider = getHostProvider((byte) 2);
         InetSocketAddress first = hostProvider.next(0);
         hostProvider.onConnected();
@@ -301,8 +320,7 @@ public class StaticHostProviderTest exte
         }
     }
 
-    private StaticHostProvider getHostProvider(byte size)
-            throws UnknownHostException {
+    private StaticHostProvider getHostProvider(byte size) {
         return new StaticHostProvider(getServerAddresses(size), r.nextLong());
     }
 



Mime
View raw message