zookeeper-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cami...@apache.org
Subject svn commit: r1541359 - in /zookeeper/branches/branch-3.4: CHANGES.txt src/java/main/org/apache/zookeeper/client/StaticHostProvider.java src/java/test/org/apache/zookeeper/test/StaticHostProviderTest.java
Date Wed, 13 Nov 2013 02:22:39 GMT
Author: camille
Date: Wed Nov 13 02:22:39 2013
New Revision: 1541359

URL: http://svn.apache.org/r1541359
Log:
ZOOKEEPER-1666. Avoid Reverse DNS lookup if the hostname in 
  connection string is literal IP address. (George Cao via camille)

Modified:
    zookeeper/branches/branch-3.4/CHANGES.txt
    zookeeper/branches/branch-3.4/src/java/main/org/apache/zookeeper/client/StaticHostProvider.java
    zookeeper/branches/branch-3.4/src/java/test/org/apache/zookeeper/test/StaticHostProviderTest.java

Modified: zookeeper/branches/branch-3.4/CHANGES.txt
URL: http://svn.apache.org/viewvc/zookeeper/branches/branch-3.4/CHANGES.txt?rev=1541359&r1=1541358&r2=1541359&view=diff
==============================================================================
--- zookeeper/branches/branch-3.4/CHANGES.txt (original)
+++ zookeeper/branches/branch-3.4/CHANGES.txt Wed Nov 13 02:22:39 2013
@@ -186,6 +186,10 @@ IMPROVEMENTS:
 
   ZOOKEEPER-1627. Add org.apache.zookeeper.common to exported packages
   in OSGi MANIFEST (Arnoud Glimmerveen via phunt)
+  
+  ZOOKEEPER-1666. Avoid Reverse DNS lookup if the hostname in 
+  connection string is literal IP address. (George Cao via camille)
+  
 
 Release 3.4.5 - 2012-09-30
 

Modified: zookeeper/branches/branch-3.4/src/java/main/org/apache/zookeeper/client/StaticHostProvider.java
URL: http://svn.apache.org/viewvc/zookeeper/branches/branch-3.4/src/java/main/org/apache/zookeeper/client/StaticHostProvider.java?rev=1541359&r1=1541358&r2=1541359&view=diff
==============================================================================
--- zookeeper/branches/branch-3.4/src/java/main/org/apache/zookeeper/client/StaticHostProvider.java
(original)
+++ zookeeper/branches/branch-3.4/src/java/main/org/apache/zookeeper/client/StaticHostProvider.java
Wed Nov 13 02:22:39 2013
@@ -61,11 +61,25 @@ public final class StaticHostProvider im
             InetAddress resolvedAddresses[] = InetAddress.getAllByName((ia!=null) ? ia.getHostAddress():
                 address.getHostName());
             for (InetAddress resolvedAddress : resolvedAddresses) {
-                this.serverAddresses.add(new InetSocketAddress(resolvedAddress
-                        .getHostAddress(), address.getPort()));
+                // If hostName is null but the address is not, we can tell that
+                // the hostName is an literal IP address. Then we can set the host string
as the hostname
+                // safely to avoid reverse DNS lookup.
+                // As far as i know, the only way to check if the hostName is null is use
toString().
+                // Both the two implementations of InetAddress are final class, so we can
trust the return value of
+                // the toString() method.
+                if (resolvedAddress.toString().startsWith("/") 
+                        && resolvedAddress.getAddress() != null) {
+                    this.serverAddresses.add(
+                            new InetSocketAddress(InetAddress.getByAddress(
+                                    address.getHostName(),
+                                    resolvedAddress.getAddress()), 
+                                    address.getPort()));
+                } else {
+                    this.serverAddresses.add(new InetSocketAddress(resolvedAddress.getHostAddress(),
address.getPort()));
+                }  
             }
         }
-
+        
         if (this.serverAddresses.isEmpty()) {
             throw new IllegalArgumentException(
                     "A HostProvider may not be empty!");

Modified: zookeeper/branches/branch-3.4/src/java/test/org/apache/zookeeper/test/StaticHostProviderTest.java
URL: http://svn.apache.org/viewvc/zookeeper/branches/branch-3.4/src/java/test/org/apache/zookeeper/test/StaticHostProviderTest.java?rev=1541359&r1=1541358&r2=1541359&view=diff
==============================================================================
--- zookeeper/branches/branch-3.4/src/java/test/org/apache/zookeeper/test/StaticHostProviderTest.java
(original)
+++ zookeeper/branches/branch-3.4/src/java/test/org/apache/zookeeper/test/StaticHostProviderTest.java
Wed Nov 13 02:22:39 2013
@@ -35,10 +35,12 @@ import java.net.InetAddress;
 import java.net.InetSocketAddress;
 import java.net.UnknownHostException;
 import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Random;
 
 public class StaticHostProviderTest extends ZKTestCase {
     private static final Logger LOG = LoggerFactory.getLogger(StaticHostProviderTest.class);
-
+    
     @Test
     public void testNextGoesRound() throws UnknownHostException {
         HostProvider hostProvider = getHostProvider((byte) 2);
@@ -92,6 +94,36 @@ public class StaticHostProviderTest exte
         assertNotSame(first, second);
     }
 
+    @Test
+    public void testLiteralIPNoReverseNS() throws Exception {
+        byte size = 30;
+        HostProvider hostProvider = getHostProviderUnresolved(size);
+        for (int i = 0; i < size; i++) {
+            InetSocketAddress next = hostProvider.next(0);
+            assertTrue(next instanceof InetSocketAddress);
+            assertTrue(!next.isUnresolved());
+            assertTrue("Did not match "+ next.toString(), !next.toString().startsWith("/"));
+            // Do NOT trigger the reverse name service lookup.
+            String hostname = next.getHostName();
+            // In this case, the hostname equals literal IP address.
+            hostname.equals(next.getAddress().getHostAddress());
+        }
+    }
+
+    private StaticHostProvider getHostProviderUnresolved(byte size)
+            throws UnknownHostException {
+        return new StaticHostProvider(getUnresolvedServerAddresses(size));
+    }
+
+    private Collection<InetSocketAddress> getUnresolvedServerAddresses(byte size) {
+        ArrayList<InetSocketAddress> list = new ArrayList<InetSocketAddress>(size);
+        while (size > 0) {
+            list.add(InetSocketAddress.createUnresolved("10.10.10." + size, 1234 + size));
+            --size;
+        }
+        return list;
+    }
+    
     private StaticHostProvider getHostProvider(byte size)
             throws UnknownHostException {
         ArrayList<InetSocketAddress> list = new ArrayList<InetSocketAddress>(



Mime
View raw message