Browse Source

ZOOKEEPER-2171 avoid reverse lookups in QuorumCnxManager (rgs via michim)

git-svn-id: https://svn.apache.org/repos/asf/zookeeper/trunk@1678529 13f79535-47bb-0310-9956-ffa450edef68
Michi Mutsuzaki 10 years ago
parent
commit
bd2a1bc422

+ 2 - 0
CHANGES.txt

@@ -132,6 +132,8 @@ IMPROVEMENTS:
   ZOOKEEPER-2153 X509 Authentication Documentation
   (Ian Dimayuga via hdeng)
 
+  ZOOKEEPER-2171 avoid reverse lookups in QuorumCnxManager (rgs via michim)
+
 Release 3.5.0 - 8/4/2014
 
 NEW FEATURES:

+ 4 - 4
src/java/main/org/apache/zookeeper/ClientCnxn.java

@@ -1062,14 +1062,14 @@ public class ClientCnxn {
             }
 
             setName(getName().replaceAll("\\(.*\\)",
-                    "(" + addr.getHostName() + ":" + addr.getPort() + ")"));
+                    "(" + addr.getHostString() + ":" + addr.getPort() + ")"));
             if (ZooKeeperSaslClient.isEnabled()) {
                 try {
                     String principalUserName = System.getProperty(
                             ZK_SASL_CLIENT_USERNAME, "zookeeper");
                     zooKeeperSaslClient =
                         new ZooKeeperSaslClient(
-                                principalUserName+"/"+addr.getHostName());
+                                principalUserName+"/"+addr.getHostString());
                 } catch (LoginException e) {
                     // An authentication error occurred when the SASL client tried to initialize:
                     // for Kerberos this means that the client failed to authenticate with the KDC.
@@ -1257,7 +1257,7 @@ public class ClientCnxn {
             Socket sock = null;
             BufferedReader br = null;
             try {
-                sock = new Socket(addr.getHostName(), addr.getPort());
+                sock = new Socket(addr.getHostString(), addr.getPort());
                 sock.setSoLinger(false, -1);
                 sock.setSoTimeout(1000);
                 sock.setTcpNoDelay(true);
@@ -1296,7 +1296,7 @@ public class ClientCnxn {
                 // connection attempt
                 rwServerAddress = addr;
                 throw new RWServerFoundException("Majority server found at "
-                        + addr.getHostName() + ":" + addr.getPort());
+                        + addr.getHostString() + ":" + addr.getPort());
             }
         }
 

+ 4 - 8
src/java/main/org/apache/zookeeper/client/StaticHostProvider.java

@@ -27,7 +27,6 @@ import java.util.Collections;
 import java.util.List;
 import java.util.Random;
 
-import org.apache.zookeeper.common.HostNameUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -111,14 +110,11 @@ public final class StaticHostProvider implements HostProvider {
         for (InetSocketAddress address : serverAddresses) {
             try {
                 InetAddress ia = address.getAddress();
-                String addr = (ia != null) ? ia.getHostAddress() :
-                                             address.getHostName();
+                String addr = (ia != null) ? ia.getHostAddress() : address.getHostString();
                 InetAddress resolvedAddresses[] = InetAddress.getAllByName(addr);
                 for (InetAddress resolvedAddress : resolvedAddresses) {
-                    tmpList.add(new InetSocketAddress(InetAddress.getByAddress(
-                                    HostNameUtils.getHostString(address),
-                                    resolvedAddress.getAddress()),
-                                    address.getPort()));
+                    InetAddress taddr = InetAddress.getByAddress(address.getHostString(), resolvedAddress.getAddress());
+                    tmpList.add(new InetSocketAddress(taddr, address.getPort()));
                 }
             } catch (UnknownHostException ex) {
                 LOG.warn("No IP address found for server: {}", address, ex);
@@ -188,7 +184,7 @@ public final class StaticHostProvider implements HostProvider {
                     && ((addr.getAddress() != null
                             && myServer.getAddress() != null && addr
                             .getAddress().equals(myServer.getAddress())) || addr
-                            .getHostName().equals(myServer.getHostName()))) {
+                            .getHostString().equals(myServer.getHostString()))) {
                 myServerInNewConfig = true;
                 break;
             }

+ 0 - 59
src/java/main/org/apache/zookeeper/common/HostNameUtils.java

@@ -1,59 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with this
- * work for additional information regarding copyright ownership.  The ASF
- * licenses this file to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
- * License for the specific language governing permissions and limitations under
- * the License.
- */
-package org.apache.zookeeper.common;
-
-import java.net.InetAddress;
-import java.net.InetSocketAddress;
-
-/**
- * A class with hostname related utility methods.
- */
-public class HostNameUtils {
-
-    private HostNameUtils() {
-        // non instantiable and non inheritable
-    }
-
-    /**
-     * Returns the hostname or IP address of {@link java.net.InetSocketAddress}.
-     *
-     * This method returns the IP address if the
-     * {@link java.net.InetSocketAddress} was created with a literal IP address,
-     * and it doesn't perform a reverse DNS lookup. The goal of this method is
-     * to substitute {@link java.net.InetSocketAddress#getHostString()}, which
-     * is only available since Java 7.
-     *
-     * This method checks if the input InetSocketAddress was constructed with a
-     * literal IP address by calling toString() on the underlying
-     * {@link java.net.InetAddress}. It returns a string with the form
-     * "hostname/literal IP address", and the hostname part is empty if the
-     * input {@link java.net.InetSocketAddress} was created with an IP address.
-     * There are 2 implementations of {@link java.net.InetAddress},
-     * {@link java.net.Inet4Address} and {@link java.net.Inet6Address}, and both
-     * classes are final, so we can trust the return value of the toString()
-     * method.
-     *
-     * @return the hostname or IP address of {@link java.net.InetSocketAddress}.
-     * @see java.net.InetSocketAddress#getHostString()
-     */
-    public static String getHostString(InetSocketAddress socketAddress) {
-        InetAddress address = socketAddress.getAddress();
-        return (address != null && address.toString().startsWith("/")) ?
-               address.getHostAddress() :
-               socketAddress.getHostName();
-    }
-}

+ 4 - 5
src/java/main/org/apache/zookeeper/server/quorum/LocalPeerBean.java

@@ -18,7 +18,6 @@
 
 package org.apache.zookeeper.server.quorum;
 
-import org.apache.zookeeper.common.HostNameUtils;
 
 /**
  * Implementation of the local peer MBean interface.
@@ -79,13 +78,13 @@ public class LocalPeerBean extends ServerBean implements LocalPeerMXBean {
     }
 
     public String getElectionAddress() {
-        return HostNameUtils.getHostString(peer.getElectionAddress()) + ":"
-                + peer.getElectionAddress().getPort();
+        return peer.getElectionAddress().getHostString() + ":" +
+            peer.getElectionAddress().getPort();
     }
 
     public String getClientAddress() {
-        return HostNameUtils.getHostString(peer.getClientAddress()) + ":"
-                + peer.getClientAddress().getPort();
+        return peer.getClientAddress().getHostString() + ":" +
+            peer.getClientAddress().getPort();
     }
 
     public String getLearnerType(){

+ 1 - 1
src/java/main/org/apache/zookeeper/server/quorum/QuorumCnxManager.java

@@ -181,7 +181,7 @@ public class QuorumCnxManager {
             // represents protocol version (in other words - message type)
             dout.writeLong(0xffff0000);              
             dout.writeLong(self.getId());
-            String addr = self.getElectionAddress().getHostName() + ":" + self.getElectionAddress().getPort();
+            String addr = self.getElectionAddress().getHostString() + ":" + self.getElectionAddress().getPort();
             byte[] addr_bytes = addr.getBytes();
             dout.writeInt(addr_bytes.length);
             dout.write(addr_bytes);

+ 3 - 4
src/java/main/org/apache/zookeeper/server/quorum/QuorumPeer.java

@@ -45,7 +45,6 @@ import java.util.concurrent.atomic.AtomicInteger;
 
 import org.apache.zookeeper.common.AtomicFileWritingIdiom;
 import org.apache.zookeeper.common.AtomicFileWritingIdiom.WriterStatement;
-import org.apache.zookeeper.common.HostNameUtils;
 import org.apache.zookeeper.common.Time;
 import org.apache.zookeeper.jmx.MBeanRegistry;
 import org.apache.zookeeper.jmx.ZKMBeanInfo;
@@ -160,7 +159,7 @@ public class QuorumPeer extends ZooKeeperThread implements QuorumStats.Provider
                 LOG.warn("Election address has not been initialized");
                 return;
             }
-            String host = HostNameUtils.getHostString(this.addr);
+            String host = this.addr.getHostString();
             InetAddress address = null;
             try {
                 address = InetAddress.getByName(host);
@@ -254,7 +253,7 @@ public class QuorumPeer extends ZooKeeperThread implements QuorumStats.Provider
             StringWriter sw = new StringWriter();            
             //addr should never be null, but just to make sure
             if (addr !=null) { 
-                sw.append(HostNameUtils.getHostString(addr));
+                sw.append(addr.getHostString());
                 sw.append(":");
                 sw.append(String.valueOf(addr.getPort()));
             }
@@ -266,7 +265,7 @@ public class QuorumPeer extends ZooKeeperThread implements QuorumStats.Provider
             else if (type == LearnerType.PARTICIPANT) sw.append(":participant");            
             if (clientAddr!=null){
                 sw.append(";");
-                sw.append(HostNameUtils.getHostString(clientAddr));
+                sw.append(clientAddr.getHostString());
                 sw.append(":");
                 sw.append(String.valueOf(clientAddr.getPort()));
             }

+ 3 - 6
src/java/main/org/apache/zookeeper/server/quorum/RemotePeerBean.java

@@ -18,7 +18,6 @@
 
 package org.apache.zookeeper.server.quorum;
 
-import org.apache.zookeeper.common.HostNameUtils;
 import org.apache.zookeeper.jmx.ZKMBeanInfo;
 
 /**
@@ -44,17 +43,15 @@ public class RemotePeerBean implements RemotePeerMXBean,ZKMBeanInfo {
     }
 
     public String getQuorumAddress() {
-        return peer.addr.getHostName()+":"+peer.addr.getPort();
+        return peer.addr.getHostString()+":"+peer.addr.getPort();
     }
 
     public String getElectionAddress() {
-        return HostNameUtils.getHostString(peer.electionAddr) + ":"
-                + peer.electionAddr.getPort();
+        return peer.electionAddr.getHostString() + ":" + peer.electionAddr.getPort();
     }
 
     public String getClientAddress() {
-        return HostNameUtils.getHostString(peer.clientAddr) + ":"
-                + peer.clientAddr.getPort();
+        return peer.clientAddr.getHostString() + ":" + peer.clientAddr.getPort();
     }
 
     public String getLearnerType() {

+ 1 - 1
src/java/main/org/apache/zookeeper/server/util/ConfigUtils.java

@@ -53,7 +53,7 @@ public class ConfigUtils {
              }
              if (!first) sb.append(",");
              else first = false;
-             sb.append(qs.clientAddr.getHostName() + ":" + qs.clientAddr.getPort());
+             sb.append(qs.clientAddr.getHostString() + ":" + qs.clientAddr.getPort());
         }
         return version + " " + sb.toString();
     }

+ 2 - 2
src/java/systest/org/apache/zookeeper/test/system/QuorumPeerInstance.java

@@ -126,8 +126,8 @@ class QuorumPeerInstance implements Instance {
             } catch(IOException e) {
                 e.printStackTrace();
             }
-            String report = clientAddr.getHostName() + ':' + clientAddr.getPort() +
-            ',' + quorumLeaderAddr.getHostName() + ':' + quorumLeaderAddr.getPort() + ':' + quorumLeaderElectionAddr.getPort();
+            String report = clientAddr.getHostString() + ':' + clientAddr.getPort() +
+            ',' + quorumLeaderAddr.getHostString() + ':' + quorumLeaderAddr.getPort() + ':' + quorumLeaderElectionAddr.getPort();
             try {
                 if (LOG.isDebugEnabled()) {
                     LOG.debug("Reporting " + report);

+ 0 - 97
src/java/test/org/apache/zookeeper/common/HostNameUtilsTest.java

@@ -1,97 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with this
- * work for additional information regarding copyright ownership.  The ASF
- * licenses this file to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
- * License for the specific language governing permissions and limitations under
- * the License.
- */
-package org.apache.zookeeper.common;
-
-import java.net.Inet6Address;
-import java.net.InetAddress;
-import java.net.InetSocketAddress;
-import java.net.UnknownHostException;
-import org.junit.Assert;
-import org.junit.Test;
-
-public class HostNameUtilsTest {
-
-    private String validName = "example.com";
-    private String invalidName = "example.com_invalid";
-    private int port = 123;
-
-    @Test
-    public void testWildcard() {
-        InetSocketAddress socketAddress = new InetSocketAddress(port);
-        Assert.assertEquals("InetSocketAddress with no host. " +
-                            "Expecting 0.0.0.0.",
-                            socketAddress.getAddress().getHostAddress(),
-                            HostNameUtils.getHostString(socketAddress));
-    }
-
-    @Test
-    public void testHostName() {
-        InetSocketAddress socketAddress =
-            new InetSocketAddress(validName, port);
-        Assert.assertEquals("InetSocketAddress with a valid hostname",
-                            validName,
-                            HostNameUtils.getHostString(socketAddress));
-
-        socketAddress = new InetSocketAddress(invalidName, port);
-        Assert.assertEquals("InetSocketAddress with an invalid hostname",
-                            invalidName,
-                            HostNameUtils.getHostString(socketAddress));
-    }
-
-    @Test
-    public void testGetByAddress() {
-        try {
-            byte[] byteAddress = new byte[]{1, 2, 3, 4};
-            InetAddress address = InetAddress.getByAddress(byteAddress);
-            InetSocketAddress socketAddress =
-                new InetSocketAddress(address, port);
-            Assert.assertEquals("getByAddress with byte address only.",
-                                address.getHostAddress(),
-                                HostNameUtils.getHostString(socketAddress));
-
-            address = InetAddress.getByAddress(validName, byteAddress);
-            socketAddress = new InetSocketAddress(address, port);
-            Assert.assertEquals("getByAddress with a valid hostname and byte " +
-                                "address.",
-                                validName,
-                                HostNameUtils.getHostString(socketAddress));
-
-            address = InetAddress.getByAddress(invalidName, byteAddress);
-            socketAddress = new InetSocketAddress(address, port);
-            Assert.assertEquals("getByAddress with an invalid hostname and " +
-                                "byte address.",
-                                invalidName,
-                                HostNameUtils.getHostString(socketAddress));
-        } catch (UnknownHostException ex) {
-            Assert.fail(ex.toString());
-        }
-    }
-
-    @Test
-    public void testGetByName() {
-        try {
-            InetAddress address = InetAddress.getByName(validName);
-            InetSocketAddress socketAddress =
-                new InetSocketAddress(address, port);
-            Assert.assertEquals("getByName with a valid hostname.",
-                                validName,
-                                HostNameUtils.getHostString(socketAddress));
-        } catch (UnknownHostException ex) {
-            Assert.fail(ex.toString());
-        }
-    }
-}

+ 1 - 1
src/java/test/org/apache/zookeeper/test/CnxManagerTest.java

@@ -244,7 +244,7 @@ public class CnxManagerTest extends ZKTestCase {
         DataOutputStream dout = new DataOutputStream(sc.socket().getOutputStream());
         dout.writeLong(0xffff0000);
         dout.writeLong(new Long(2));
-        String addr = otherAddr.getHostName()+ ":" + otherAddr.getPort();
+        String addr = otherAddr.getHostString()+ ":" + otherAddr.getPort();
         byte[] addr_bytes = addr.getBytes();
         dout.writeInt(addr_bytes.length);
         dout.write(addr_bytes);

+ 4 - 4
src/java/test/org/apache/zookeeper/test/ConnectStringParserTest.java

@@ -46,8 +46,8 @@ public class ConnectStringParserTest extends ZKTestCase{
         String servers = "10.10.10.1,10.10.10.2";
         ConnectStringParser parser = new ConnectStringParser(servers);
 
-        Assert.assertEquals("10.10.10.1", parser.getServerAddresses().get(0).getHostName());
-        Assert.assertEquals("10.10.10.2", parser.getServerAddresses().get(1).getHostName());
+        Assert.assertEquals("10.10.10.1", parser.getServerAddresses().get(0).getHostString());
+        Assert.assertEquals("10.10.10.2", parser.getServerAddresses().get(1).getHostString());
     }
 
     @Test
@@ -55,8 +55,8 @@ public class ConnectStringParserTest extends ZKTestCase{
         String servers = "10.10.10.1:112,10.10.10.2:110";
         ConnectStringParser parser = new ConnectStringParser(servers);
 
-        Assert.assertEquals("10.10.10.1", parser.getServerAddresses().get(0).getHostName());
-        Assert.assertEquals("10.10.10.2", parser.getServerAddresses().get(1).getHostName());
+        Assert.assertEquals("10.10.10.1", parser.getServerAddresses().get(0).getHostString());
+        Assert.assertEquals("10.10.10.2", parser.getServerAddresses().get(1).getHostString());
 
         Assert.assertEquals(112, parser.getServerAddresses().get(0).getPort());
         Assert.assertEquals(110, parser.getServerAddresses().get(1).getPort());

+ 6 - 15
src/java/test/org/apache/zookeeper/test/ReconfigTest.java

@@ -34,7 +34,6 @@ import org.apache.zookeeper.ZKTestCase;
 import org.apache.zookeeper.ZooDefs;
 import org.apache.zookeeper.ZooKeeper;
 import org.apache.zookeeper.AsyncCallback.DataCallback;
-import org.apache.zookeeper.common.HostNameUtils;
 import org.apache.zookeeper.data.Stat;
 import org.apache.zookeeper.jmx.CommonNames;
 import org.apache.zookeeper.server.quorum.QuorumPeer;
@@ -640,7 +639,7 @@ public class ReconfigTest extends ZKTestCase implements DataCallback{
     	}
     	String server = "server.0=localhost:" + ports[0] + ":" + ports[1] + ";" + ports[2];
     	QuorumServer qs = new QuorumServer(0, server);
-    	Assert.assertEquals(qs.clientAddr.getHostName(), "0.0.0.0");
+    	Assert.assertEquals(qs.clientAddr.getHostString(), "0.0.0.0");
     	Assert.assertEquals(qs.clientAddr.getPort(), ports[2]);
     }
     
@@ -890,12 +889,10 @@ public class ReconfigTest extends ZKTestCase implements DataCallback{
         Assert.assertEquals("Mismatches LearnerType!", qp.getLearnerType()
                 .name(), JMXEnv.ensureBeanAttribute(beanName, "LearnerType"));
         Assert.assertEquals("Mismatches ClientAddress!",
-                HostNameUtils.getHostString(qp.getClientAddress()) + ":"
-                        + qp.getClientAddress().getPort(),
+                qp.getClientAddress().getHostString() + ":" + qp.getClientAddress().getPort(),
                 JMXEnv.ensureBeanAttribute(beanName, "ClientAddress"));
         Assert.assertEquals("Mismatches LearnerType!",
-                HostNameUtils.getHostString(qp.getElectionAddress()) + ":"
-                        + qp.getElectionAddress().getPort(),
+                qp.getElectionAddress().getHostString() + ":" + qp.getElectionAddress().getPort(),
                 JMXEnv.ensureBeanAttribute(beanName, "ElectionAddress"));
         Assert.assertEquals("Mismatches PartOfEnsemble!", isPartOfEnsemble,
                 JMXEnv.ensureBeanAttribute(beanName, "PartOfEnsemble"));
@@ -930,19 +927,13 @@ public class ReconfigTest extends ZKTestCase implements DataCallback{
         Assert.assertEquals("Mismatches LearnerType!", qs.type.name(),
                 JMXEnv.ensureBeanAttribute(beanName, "LearnerType"));
         Assert.assertEquals("Mismatches ClientAddress!",
-                getNumericalAddrPort(
-                        HostNameUtils.getHostString(qs.clientAddr) + ":"
-                        + qs.clientAddr.getPort() ),
+                getNumericalAddrPort(qs.clientAddr.getHostString() + ":" + qs.clientAddr.getPort()),
                 getAddrPortFromBean(beanName, "ClientAddress") );
         Assert.assertEquals("Mismatches ElectionAddress!",
-                getNumericalAddrPort(
-                        HostNameUtils.getHostString(qs.electionAddr) + ":"
-                        + qs.electionAddr.getPort() ),
+                getNumericalAddrPort(qs.electionAddr.getHostString() + ":" + qs.electionAddr.getPort()),
                 getAddrPortFromBean(beanName, "ElectionAddress") );
         Assert.assertEquals("Mismatches QuorumAddress!",
-                getNumericalAddrPort(
-                        qs.addr.getHostName() + ":"
-                        + qs.addr.getPort() ),
+                getNumericalAddrPort(qs.addr.getHostString() + ":" + qs.addr.getPort()),
                 getAddrPortFromBean(beanName, "QuorumAddress") );
     }
 }

+ 1 - 1
src/java/test/org/apache/zookeeper/test/StaticHostProviderTest.java

@@ -497,7 +497,7 @@ public class StaticHostProviderTest extends ZKTestCase {
             assertTrue(!next.isUnresolved());
             assertTrue(!next.toString().startsWith("/"));
             // Do NOT trigger the reverse name service lookup.
-            String hostname = next.getHostName();
+            String hostname = next.getHostString();
             // In this case, the hostname equals literal IP address.
             hostname.equals(next.getAddress().getHostAddress());
         }