Commit 3dd46f34 authored by Camille Fournier's avatar Camille Fournier
Browse files

ZOOKEEPER-1666. Avoid Reverse DNS lookup if the hostname in

  connection string is literal IP address. (George Cao via camille)

git-svn-id: 13f79535-47bb-0310-9956-ffa450edef68
parent 6631776e
......@@ -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
......@@ -61,11 +61,25 @@ public final class StaticHostProvider implements HostProvider {
InetAddress resolvedAddresses[] = InetAddress.getAllByName((ia!=null) ? ia.getHostAddress():
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) {
new InetSocketAddress(InetAddress.getByAddress(
} else {
this.serverAddresses.add(new InetSocketAddress(resolvedAddress.getHostAddress(), address.getPort()));
if (this.serverAddresses.isEmpty()) {
throw new IllegalArgumentException(
"A HostProvider may not be empty!");
......@@ -35,10 +35,12 @@ import;
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);
public void testNextGoesRound() throws UnknownHostException {
HostProvider hostProvider = getHostProvider((byte) 2);
......@@ -92,6 +94,36 @@ public class StaticHostProviderTest extends ZKTestCase {
assertNotSame(first, second);
public void testLiteralIPNoReverseNS() throws Exception {
byte size = 30;
HostProvider hostProvider = getHostProviderUnresolved(size);
for (int i = 0; i < size; i++) {
InetSocketAddress next =;
assertTrue(next instanceof InetSocketAddress);
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.
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));
return list;
private StaticHostProvider getHostProvider(byte size)
throws UnknownHostException {
ArrayList<InetSocketAddress> list = new ArrayList<InetSocketAddress>(
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment