Skip to content

Late-bind DNS resolutions #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions src/main/java/net/spy/memcached/AddrUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

package net.spy.memcached;

import java.net.InetSocketAddress;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -40,19 +39,19 @@ private AddrUtil() {
/**
* Split a string containing whitespace or comma separated host or IP
* addresses and port numbers of the form "host:port host2:port" or
* "host:port, host2:port" into a List of InetSocketAddress instances suitable
* "host:port, host2:port" into a List of HostPort instances suitable
* for instantiating a MemcachedClient.
*
* Note that colon-delimited IPv6 is also supported. For example: ::1:11211
*/
public static List<InetSocketAddress> getAddresses(String s) {
public static List<HostPort> getAddresses(String s) {
if (s == null) {
throw new NullPointerException("Null host list");
}
if (s.trim().equals("")) {
throw new IllegalArgumentException("No hosts in list: ``" + s + "''");
}
ArrayList<InetSocketAddress> addrs = new ArrayList<InetSocketAddress>();
ArrayList<HostPort> addrs = new ArrayList<HostPort>();

for (String hoststuff : s.split("(?:\\s|,)+")) {
if (hoststuff.equals("")) {
Expand All @@ -67,15 +66,14 @@ public static List<InetSocketAddress> getAddresses(String s) {
String hostPart = hoststuff.substring(0, finalColon);
String portNum = hoststuff.substring(finalColon + 1);

addrs.add(new InetSocketAddress(hostPart, Integer.parseInt(portNum)));
addrs.add(new HostPort(hostPart, Integer.parseInt(portNum)));
}
assert !addrs.isEmpty() : "No addrs found";
return addrs;
}

public static List<InetSocketAddress> getAddresses(List<String> servers) {
ArrayList<InetSocketAddress> addrs =
new ArrayList<InetSocketAddress>(servers.size());
public static List<HostPort> getAddresses(List<String> servers) {
ArrayList<HostPort> addrs = new ArrayList<HostPort>(servers.size());
for (String server : servers) {
int finalColon = server.lastIndexOf(':');
if (finalColon < 1) {
Expand All @@ -85,7 +83,7 @@ public static List<InetSocketAddress> getAddresses(List<String> servers) {
String hostPart = server.substring(0, finalColon);
String portNum = server.substring(finalColon + 1);

addrs.add(new InetSocketAddress(hostPart, Integer.parseInt(portNum)));
addrs.add(new HostPort(hostPart, Integer.parseInt(portNum)));
}
if (addrs.isEmpty()) {
// servers was passed in empty, and shouldn't have been
Expand All @@ -94,13 +92,12 @@ public static List<InetSocketAddress> getAddresses(List<String> servers) {
return addrs;
}

public static List<InetSocketAddress>
getAddressesFromURL(List<URL> servers) {
ArrayList<InetSocketAddress> addrs =
new ArrayList<InetSocketAddress>(servers.size());
public static List<HostPort> getAddressesFromURL(List<URL> servers) {
ArrayList<HostPort> addrs = new ArrayList<HostPort>(servers.size());
for (URL server : servers) {
addrs.add(new InetSocketAddress(server.getHost(), server.getPort()));
addrs.add(new HostPort(server.getHost(), server.getPort()));
}
return addrs;
}

}
20 changes: 13 additions & 7 deletions src/main/java/net/spy/memcached/BinaryConnectionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

package net.spy.memcached;

import java.net.SocketAddress;
import java.nio.channels.SocketChannel;

import net.spy.memcached.protocol.binary.BinaryMemcachedNodeImpl;
Expand Down Expand Up @@ -61,16 +60,23 @@ public BinaryConnectionFactory(int len, int bufSize, HashAlgorithm hash) {
}

@Override
public MemcachedNode createMemcachedNode(SocketAddress sa, SocketChannel c,
int bufSize) {
public MemcachedNode createMemcachedNode(HostPort hp, SocketChannel c, int bufSize) {
boolean doAuth = false;
if (getAuthDescriptor() != null) {
doAuth = true;
}
return new BinaryMemcachedNodeImpl(sa, c, bufSize,
createReadOperationQueue(), createWriteOperationQueue(),
createOperationQueue(), getOpQueueMaxBlockTime(), doAuth,
getOperationTimeout(), getAuthWaitTime(), this);
return new BinaryMemcachedNodeImpl(
hp,
c,
bufSize,
createReadOperationQueue(),
createWriteOperationQueue(),
createOperationQueue(),
getOpQueueMaxBlockTime(),
doAuth,
getOperationTimeout(),
getAuthWaitTime(),
this);
}

@Override
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/net/spy/memcached/ConnectionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
package net.spy.memcached;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.channels.SocketChannel;
import java.util.Collection;
import java.util.List;
Expand All @@ -45,20 +43,19 @@
public interface ConnectionFactory {

/**
* Create a MemcachedConnection for the given SocketAddresses.
* Create a MemcachedConnection for the given HostPorts.
*
* @param addrs the addresses of the memcached servers
* @return a new MemcachedConnection connected to those addresses
* @throws IOException for problems initializing the memcached connections
*/
MemcachedConnection createConnection(List<InetSocketAddress> addrs)
MemcachedConnection createConnection(List<HostPort> addrs)
throws IOException;

/**
* Create a new memcached node.
*/
MemcachedNode createMemcachedNode(SocketAddress sa, SocketChannel c,
int bufSize);
MemcachedNode createMemcachedNode(HostPort hp, SocketChannel c,int bufSize);

/**
* Create a BlockingQueue for operations for a connection.
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/net/spy/memcached/ConnectionObserver.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@

package net.spy.memcached;

import java.net.SocketAddress;

/**
* Users of this interface will be notified when changes to the state of
* connections take place.
Expand All @@ -33,16 +31,16 @@ public interface ConnectionObserver {
/**
* A connection has just successfully been established on the given socket.
*
* @param sa the address of the node whose connection was established
* @param hp the address of the node whose connection was established
* @param reconnectCount the number of attempts before the connection was
* established
*/
void connectionEstablished(SocketAddress sa, int reconnectCount);
void connectionEstablished(HostPort hp, int reconnectCount);

/**
* A connection was just lost on the given socket.
*
* @param sa the address of the node whose connection was lost
*/
void connectionLost(SocketAddress sa);
void connectionLost(HostPort sa);
}
19 changes: 10 additions & 9 deletions src/main/java/net/spy/memcached/DefaultConnectionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,14 @@
package net.spy.memcached;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.channels.SocketChannel;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -166,12 +162,14 @@ public DefaultConnectionFactory() {
this(DEFAULT_OP_QUEUE_LEN, DEFAULT_READ_BUFFER_SIZE);
}

public MemcachedNode createMemcachedNode(SocketAddress sa, SocketChannel c,
int bufSize) {
public MemcachedNode createMemcachedNode(HostPort hp, SocketChannel c, int bufSize) {

OperationFactory of = getOperationFactory();
if (of instanceof AsciiOperationFactory) {
return new AsciiMemcachedNodeImpl(sa, c, bufSize,
return new AsciiMemcachedNodeImpl(
hp,
c,
bufSize,
createReadOperationQueue(),
createWriteOperationQueue(),
createOperationQueue(),
Expand All @@ -184,7 +182,10 @@ public MemcachedNode createMemcachedNode(SocketAddress sa, SocketChannel c,
if (getAuthDescriptor() != null) {
doAuth = true;
}
return new BinaryMemcachedNodeImpl(sa, c, bufSize,
return new BinaryMemcachedNodeImpl(
hp,
c,
bufSize,
createReadOperationQueue(),
createWriteOperationQueue(),
createOperationQueue(),
Expand All @@ -203,7 +204,7 @@ public MemcachedNode createMemcachedNode(SocketAddress sa, SocketChannel c,
*
* @see net.spy.memcached.ConnectionFactory#createConnection(java.util.List)
*/
public MemcachedConnection createConnection(List<InetSocketAddress> addrs)
public MemcachedConnection createConnection(List<HostPort> addrs)
throws IOException {
return new MemcachedConnection(getReadBufSize(), this, addrs,
getInitialObservers(), getFailureMode(), getOperationFactory());
Expand Down
79 changes: 79 additions & 0 deletions src/main/java/net/spy/memcached/HostPort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package net.spy.memcached;

import java.net.InetSocketAddress;

/**
* Container for the hostname and port of a Node, capable of resolving them
* into an InetSocketAddress.
*/
public class HostPort {

private final String host;
private final int port;

private volatile InetSocketAddress address;

public HostPort(String host, int port) {
this.host = host;
this.port = port;
}

public String getHostName() {
return host;
}

public int getPort() {
return port;
}

/**
* Get the InetSocketAddress. If it has not already been resolved then
* resolve it.
*/
public InetSocketAddress getAddress() {
if (address == null) {
resolveAddress();
}
return address;
}

/**
* Resolve and return the InetSocketAddress. If it has already been resolved,
* it will be re-resolved.
*/
public InetSocketAddress resolveAddress() {
address = new InetSocketAddress(host, port);
return address;
}

@Override
public String toString() {
if (address == null) {
return InetSocketAddress.createUnresolved(host, port).toString();
}
else {
return address.toString();
}
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

HostPort hostPort = (HostPort) o;

if (port != hostPort.port) return false;
if (host != null ? !host.equals(hostPort.host) : hostPort.host != null) return false;

return true;
}

@Override
public int hashCode() {
int result = host != null ? host.hashCode() : 0;
result = 31 * result + port;
return result;
}

}
Loading