diff --git a/src/main/java/net/spy/memcached/AddrUtil.java b/src/main/java/net/spy/memcached/AddrUtil.java index 5a2e73cca..f19940df5 100644 --- a/src/main/java/net/spy/memcached/AddrUtil.java +++ b/src/main/java/net/spy/memcached/AddrUtil.java @@ -23,7 +23,6 @@ package net.spy.memcached; -import java.net.InetSocketAddress; import java.net.URL; import java.util.ArrayList; import java.util.List; @@ -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 getAddresses(String s) { + public static List 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 addrs = new ArrayList(); + ArrayList addrs = new ArrayList(); for (String hoststuff : s.split("(?:\\s|,)+")) { if (hoststuff.equals("")) { @@ -67,15 +66,14 @@ public static List 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 getAddresses(List servers) { - ArrayList addrs = - new ArrayList(servers.size()); + public static List getAddresses(List servers) { + ArrayList addrs = new ArrayList(servers.size()); for (String server : servers) { int finalColon = server.lastIndexOf(':'); if (finalColon < 1) { @@ -85,7 +83,7 @@ public static List getAddresses(List 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 @@ -94,13 +92,12 @@ public static List getAddresses(List servers) { return addrs; } - public static List - getAddressesFromURL(List servers) { - ArrayList addrs = - new ArrayList(servers.size()); + public static List getAddressesFromURL(List servers) { + ArrayList addrs = new ArrayList(servers.size()); for (URL server : servers) { - addrs.add(new InetSocketAddress(server.getHost(), server.getPort())); + addrs.add(new HostPort(server.getHost(), server.getPort())); } return addrs; } + } diff --git a/src/main/java/net/spy/memcached/BinaryConnectionFactory.java b/src/main/java/net/spy/memcached/BinaryConnectionFactory.java index f5eef17bc..9e130105d 100644 --- a/src/main/java/net/spy/memcached/BinaryConnectionFactory.java +++ b/src/main/java/net/spy/memcached/BinaryConnectionFactory.java @@ -23,7 +23,6 @@ package net.spy.memcached; -import java.net.SocketAddress; import java.nio.channels.SocketChannel; import net.spy.memcached.protocol.binary.BinaryMemcachedNodeImpl; @@ -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 diff --git a/src/main/java/net/spy/memcached/ConnectionFactory.java b/src/main/java/net/spy/memcached/ConnectionFactory.java index 0c0cde78e..984a0d830 100644 --- a/src/main/java/net/spy/memcached/ConnectionFactory.java +++ b/src/main/java/net/spy/memcached/ConnectionFactory.java @@ -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; @@ -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 addrs) + MemcachedConnection createConnection(List 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. diff --git a/src/main/java/net/spy/memcached/ConnectionObserver.java b/src/main/java/net/spy/memcached/ConnectionObserver.java index cbb145e5d..1161d77b8 100644 --- a/src/main/java/net/spy/memcached/ConnectionObserver.java +++ b/src/main/java/net/spy/memcached/ConnectionObserver.java @@ -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. @@ -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); } diff --git a/src/main/java/net/spy/memcached/DefaultConnectionFactory.java b/src/main/java/net/spy/memcached/DefaultConnectionFactory.java index bee0c4e30..12843c506 100644 --- a/src/main/java/net/spy/memcached/DefaultConnectionFactory.java +++ b/src/main/java/net/spy/memcached/DefaultConnectionFactory.java @@ -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.Collections; @@ -33,9 +31,7 @@ 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; @@ -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(), @@ -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(), @@ -203,7 +204,7 @@ public MemcachedNode createMemcachedNode(SocketAddress sa, SocketChannel c, * * @see net.spy.memcached.ConnectionFactory#createConnection(java.util.List) */ - public MemcachedConnection createConnection(List addrs) + public MemcachedConnection createConnection(List addrs) throws IOException { return new MemcachedConnection(getReadBufSize(), this, addrs, getInitialObservers(), getFailureMode(), getOperationFactory()); diff --git a/src/main/java/net/spy/memcached/HostPort.java b/src/main/java/net/spy/memcached/HostPort.java new file mode 100644 index 000000000..fae9d8e2c --- /dev/null +++ b/src/main/java/net/spy/memcached/HostPort.java @@ -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; + } + +} diff --git a/src/main/java/net/spy/memcached/MemcachedClient.java b/src/main/java/net/spy/memcached/MemcachedClient.java index 9d376d13a..b81136ad3 100644 --- a/src/main/java/net/spy/memcached/MemcachedClient.java +++ b/src/main/java/net/spy/memcached/MemcachedClient.java @@ -24,8 +24,6 @@ package net.spy.memcached; import java.io.IOException; -import java.net.InetSocketAddress; -import java.net.SocketAddress; import java.text.MessageFormat; import java.util.ArrayList; import java.util.Arrays; @@ -74,7 +72,6 @@ import net.spy.memcached.ops.StoreOperation; import net.spy.memcached.ops.StoreType; import net.spy.memcached.ops.TimedOutOperationStatus; -import net.spy.memcached.protocol.ascii.AsciiOperationFactory; import net.spy.memcached.protocol.binary.BinaryOperationFactory; import net.spy.memcached.transcoders.TranscodeService; import net.spy.memcached.transcoders.Transcoder; @@ -87,7 +84,7 @@ * *
  * MemcachedClient c = new MemcachedClient(
- *    new InetSocketAddress("hostname", portNum));
+ *    new NostPort("hostname", portNum));
  *
  * // Store a value (async) for one hour
  * c.set("someKey", 3600, someObject);
@@ -163,20 +160,20 @@ public class MemcachedClient extends SpyObject implements MemcachedClientIF,
   /**
    * Get a memcache client operating on the specified memcached locations.
    *
-   * @param ia the memcached locations
+   * @param hp the memcached locations
    * @throws IOException if connections cannot be established
    */
-  public MemcachedClient(InetSocketAddress... ia) throws IOException {
-    this(new DefaultConnectionFactory(), Arrays.asList(ia));
+  public MemcachedClient(HostPort... hp) throws IOException {
+    this(new DefaultConnectionFactory(), Arrays.asList(hp));
   }
 
   /**
    * Get a memcache client over the specified memcached locations.
    *
-   * @param addrs the socket addrs
+   * @param addrs the addresses
    * @throws IOException if connections cannot be established
    */
-  public MemcachedClient(List addrs) throws IOException {
+  public MemcachedClient(List addrs) throws IOException {
     this(new DefaultConnectionFactory(), addrs);
   }
 
@@ -184,10 +181,10 @@ public MemcachedClient(List addrs) throws IOException {
    * Get a memcache client over the specified memcached locations.
    *
    * @param cf the connection factory to configure connections for this client
-   * @param addrs the socket addresses
+   * @param addrs the addresses
    * @throws IOException if connections cannot be established
    */
-  public MemcachedClient(ConnectionFactory cf, List addrs)
+  public MemcachedClient(ConnectionFactory cf, List addrs)
     throws IOException {
     if (cf == null) {
       throw new NullPointerException("Connection factory required");
@@ -229,11 +226,11 @@ public MemcachedClient(ConnectionFactory cf, List addrs)
    * @return point-in-time view of currently available servers
    */
   @Override
-  public Collection getAvailableServers() {
-    ArrayList rv = new ArrayList();
+  public Collection getAvailableServers() {
+    ArrayList rv = new ArrayList();
     for (MemcachedNode node : mconn.getLocator().getAll()) {
       if (node.isActive()) {
-        rv.add(node.getSocketAddress());
+        rv.add(node.getHostPort());
       }
     }
     return rv;
@@ -251,11 +248,11 @@ public Collection getAvailableServers() {
    * @return point-in-time view of currently available servers
    */
   @Override
-  public Collection getUnavailableServers() {
-    ArrayList rv = new ArrayList();
+  public Collection getUnavailableServers() {
+    ArrayList rv = new ArrayList();
     for (MemcachedNode node : mconn.getLocator().getAll()) {
       if (!node.isActive()) {
-        rv.add(node.getSocketAddress());
+        rv.add(node.getHostPort());
       }
     }
     return rv;
@@ -1652,20 +1649,20 @@ public Map getBulk(String... keys) {
   /**
    * Get the versions of all of the connected memcacheds.
    *
-   * @return a Map of SocketAddress to String for connected servers
+   * @return a Map of HostPort to String for connected servers
    * @throws IllegalStateException in the rare circumstance where queue is too
    *           full to accept any more requests
    */
   @Override
-  public Map getVersions() {
-    final Map rv =
-        new ConcurrentHashMap();
+  public Map getVersions() {
+    final Map rv =
+        new ConcurrentHashMap();
 
     CountDownLatch blatch = broadcastOp(new BroadcastOpFactory() {
       @Override
       public Operation newOp(final MemcachedNode n,
           final CountDownLatch latch) {
-        final SocketAddress sa = n.getSocketAddress();
+        final HostPort sa = n.getHostPort();
         return opFact.version(new OperationCallback() {
           @Override
           public void receivedStatus(OperationStatus s) {
@@ -1690,12 +1687,12 @@ public void complete() {
   /**
    * Get all of the stats from all of the connections.
    *
-   * @return a Map of a Map of stats replies by SocketAddress
+   * @return a Map of a Map of stats replies by HostPort
    * @throws IllegalStateException in the rare circumstance where queue is too
    *           full to accept any more requests
    */
   @Override
-  public Map> getStats() {
+  public Map> getStats() {
     return getStats(null);
   }
 
@@ -1703,21 +1700,21 @@ public Map> getStats() {
    * Get a set of stats from all connections.
    *
    * @param arg which stats to get
-   * @return a Map of the server SocketAddress to a map of String stat keys to
+   * @return a Map of the server HostPort to a map of String stat keys to
    *         String stat values.
    * @throws IllegalStateException in the rare circumstance where queue is too
    *           full to accept any more requests
    */
   @Override
-  public Map> getStats(final String arg) {
-    final Map> rv =
-        new HashMap>();
+  public Map> getStats(final String arg) {
+    final Map> rv =
+        new HashMap>();
 
     CountDownLatch blatch = broadcastOp(new BroadcastOpFactory() {
       @Override
       public Operation newOp(final MemcachedNode n,
           final CountDownLatch latch) {
-        final SocketAddress sa = n.getSocketAddress();
+        final HostPort sa = n.getHostPort();
         rv.put(sa, new HashMap());
         return opFact.stats(arg, new StatsOperation.Callback() {
           @Override
@@ -2575,7 +2572,7 @@ public boolean addObserver(ConnectionObserver obs) {
     if (rv) {
       for (MemcachedNode node : mconn.getLocator().getAll()) {
         if (node.isActive()) {
-          obs.connectionEstablished(node.getSocketAddress(), -1);
+          obs.connectionEstablished(node.getHostPort(), -1);
         }
       }
     }
@@ -2594,23 +2591,23 @@ public boolean removeObserver(ConnectionObserver obs) {
   }
 
   @Override
-  public void connectionEstablished(SocketAddress sa, int reconnectCount) {
+  public void connectionEstablished(HostPort hp, int reconnectCount) {
     if (authDescriptor != null) {
       if (authDescriptor.authThresholdReached()) {
         shutdown();
       }
-      authMonitor.authConnection(mconn, opFact, authDescriptor, findNode(sa));
+      authMonitor.authConnection(mconn, opFact, authDescriptor, findNode(hp));
     }
   }
 
-  private MemcachedNode findNode(SocketAddress sa) {
+  private MemcachedNode findNode(HostPort hp) {
     MemcachedNode node = null;
     for (MemcachedNode n : mconn.getLocator().getAll()) {
-      if (n.getSocketAddress().equals(sa)) {
+      if (n.getHostPort().equals(hp)) {
         node = n;
       }
     }
-    assert node != null : "Couldn't find node connected to " + sa;
+    assert node != null : "Couldn't find node connected to " + hp;
     return node;
   }
 
@@ -2624,7 +2621,7 @@ private String buildTimeoutMessage(long timeWaited, TimeUnit unit) {
   }
 
   @Override
-  public void connectionLost(SocketAddress sa) {
+  public void connectionLost(HostPort sa) {
     // Don't care.
   }
 
diff --git a/src/main/java/net/spy/memcached/MemcachedClientIF.java b/src/main/java/net/spy/memcached/MemcachedClientIF.java
index e74c7c32a..86f03d10f 100644
--- a/src/main/java/net/spy/memcached/MemcachedClientIF.java
+++ b/src/main/java/net/spy/memcached/MemcachedClientIF.java
@@ -23,7 +23,6 @@
 
 package net.spy.memcached;
 
-import java.net.SocketAddress;
 import java.util.Collection;
 import java.util.Iterator;
 import java.util.Map;
@@ -46,9 +45,9 @@ public interface MemcachedClientIF {
    */
   int MAX_KEY_LENGTH = 250;
 
-  Collection getAvailableServers();
+  Collection getAvailableServers();
 
-  Collection getUnavailableServers();
+  Collection getUnavailableServers();
 
   Transcoder getTranscoder();
 
@@ -158,11 +157,11 @@  Future touch(final String key, final int exp,
 
    Future touch(final String key, final int exp);
 
-  Map getVersions();
+  Map getVersions();
 
-  Map> getStats();
+  Map> getStats();
 
-  Map> getStats(String prefix);
+  Map> getStats(String prefix);
 
   long incr(String key, long by);
 
diff --git a/src/main/java/net/spy/memcached/MemcachedConnection.java b/src/main/java/net/spy/memcached/MemcachedConnection.java
index 09f7c49ce..781fdd8fc 100644
--- a/src/main/java/net/spy/memcached/MemcachedConnection.java
+++ b/src/main/java/net/spy/memcached/MemcachedConnection.java
@@ -46,7 +46,6 @@
 
 import java.io.IOException;
 import java.net.ConnectException;
-import java.net.InetSocketAddress;
 import java.net.SocketAddress;
 import java.net.SocketException;
 import java.nio.ByteBuffer;
@@ -251,9 +250,13 @@ public class MemcachedConnection extends SpyThread {
    * @param opfactory the operation factory.
    * @throws IOException if a connection attempt fails early
    */
-  public MemcachedConnection(final int bufSize, final ConnectionFactory f,
-      final List a, final Collection obs,
-      final FailureMode fm, final OperationFactory opfactory) throws IOException {
+  public MemcachedConnection(
+      final int bufSize,
+      final ConnectionFactory f,
+      final List a,
+      final Collection obs,
+      final FailureMode fm,
+      final OperationFactory opfactory) throws IOException {
     connObservers.addAll(obs);
     reconnectQueue = new TreeMap();
     addedQueue = new ConcurrentLinkedQueue();
@@ -325,14 +328,15 @@ protected void registerMetrics() {
    * @return addrs list of {@link MemcachedNode}s.
    * @throws IOException if connecting was not successful.
    */
-  protected List createConnections(
-    final Collection addrs) throws IOException {
+  protected List createConnections(final Collection addrs)
+      throws IOException {
     List connections = new ArrayList(addrs.size());
 
-    for (SocketAddress sa : addrs) {
+    for (HostPort hp : addrs) {
+      SocketAddress sa = hp.resolveAddress();
       SocketChannel ch = SocketChannel.open();
       ch.configureBlocking(false);
-      MemcachedNode qa = connectionFactory.createMemcachedNode(sa, ch, bufSize);
+      MemcachedNode qa = connectionFactory.createMemcachedNode(hp, ch, bufSize);
       qa.setConnection(this);
       int ops = 0;
       ch.socket().setTcpNoDelay(!connectionFactory.useNagleAlgorithm());
@@ -621,7 +625,7 @@ private void connected(final MemcachedNode node) {
     node.connected();
 
     for (ConnectionObserver observer : connObservers) {
-      observer.connectionEstablished(node.getSocketAddress(), rt);
+      observer.connectionEstablished(node.getHostPort(), rt);
     }
   }
 
@@ -633,7 +637,7 @@ private void connected(final MemcachedNode node) {
   private void lostConnection(final MemcachedNode node) {
     queueReconnect(node);
     for (ConnectionObserver observer : connObservers) {
-      observer.connectionLost(node.getSocketAddress());
+      observer.connectionLost(node.getHostPort());
     }
   }
 
@@ -645,7 +649,7 @@ private void lostConnection(final MemcachedNode node) {
    */
   boolean belongsToCluster(final MemcachedNode node) {
     for (MemcachedNode n : locator.getAll()) {
-      if (n.getSocketAddress().equals(node.getSocketAddress())) {
+      if (n.getHostPort().equals(node.getHostPort())) {
         return true;
       }
     }
@@ -1117,7 +1121,7 @@ private void attemptReconnects() {
           ch.configureBlocking(false);
           ch.socket().setTcpNoDelay(!connectionFactory.useNagleAlgorithm());
           int ops = 0;
-          if (ch.connect(node.getSocketAddress())) {
+          if (ch.connect(node.getHostPort().resolveAddress())) {
             connected(node);
             addedQueue.offer(node);
             getLogger().info("Immediately reconnected to %s", node);
@@ -1351,7 +1355,7 @@ public String toString() {
     StringBuilder sb = new StringBuilder();
     sb.append("{MemcachedConnection to");
     for (MemcachedNode qa : locator.getAll()) {
-      sb.append(" ").append(qa.getSocketAddress());
+      sb.append(" ").append(qa.getHostPort());
     }
     sb.append("}");
     return sb.toString();
@@ -1368,7 +1372,7 @@ public String connectionsStatus() {
     for (MemcachedNode node : locator.getAll()) {
       connStatus
         .append(" ")
-        .append(node.getSocketAddress())
+        .append(node.getHostPort())
         .append(" active: ")
         .append(node.isActive())
         .append(", authed: ")
diff --git a/src/main/java/net/spy/memcached/MemcachedNode.java b/src/main/java/net/spy/memcached/MemcachedNode.java
index c86f96e70..6f2727bbe 100644
--- a/src/main/java/net/spy/memcached/MemcachedNode.java
+++ b/src/main/java/net/spy/memcached/MemcachedNode.java
@@ -24,7 +24,6 @@
 package net.spy.memcached;
 
 import java.io.IOException;
-import java.net.SocketAddress;
 import java.nio.ByteBuffer;
 import java.nio.channels.SelectionKey;
 import java.nio.channels.SocketChannel;
@@ -131,9 +130,10 @@ public interface MemcachedNode {
   ByteBuffer getWbuf();
 
   /**
-   * Get the SocketAddress of the server to which this node is connected.
+   * Get the HostPort of the server to which this node is connected.
+   *
    */
-  SocketAddress getSocketAddress();
+  HostPort getHostPort();
 
   /**
    * True if this node is active. i.e. is is currently connected and
diff --git a/src/main/java/net/spy/memcached/MemcachedNodeROImpl.java b/src/main/java/net/spy/memcached/MemcachedNodeROImpl.java
index ea07b9b11..d182c6030 100644
--- a/src/main/java/net/spy/memcached/MemcachedNodeROImpl.java
+++ b/src/main/java/net/spy/memcached/MemcachedNodeROImpl.java
@@ -24,7 +24,6 @@
 package net.spy.memcached;
 
 import java.io.IOException;
-import java.net.SocketAddress;
 import java.nio.ByteBuffer;
 import java.nio.channels.SelectionKey;
 import java.nio.channels.SocketChannel;
@@ -102,8 +101,8 @@ public SelectionKey getSk() {
     throw new UnsupportedOperationException();
   }
 
-  public SocketAddress getSocketAddress() {
-    return root.getSocketAddress();
+  public HostPort getHostPort() {
+    return root.getHostPort();
   }
 
   public ByteBuffer getWbuf() {
diff --git a/src/main/java/net/spy/memcached/TapClient.java b/src/main/java/net/spy/memcached/TapClient.java
index 5f4993ee4..b8022293f 100644
--- a/src/main/java/net/spy/memcached/TapClient.java
+++ b/src/main/java/net/spy/memcached/TapClient.java
@@ -23,7 +23,6 @@
 package net.spy.memcached;
 
 import java.io.IOException;
-import java.net.InetSocketAddress;
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Iterator;
@@ -53,7 +52,7 @@ public class TapClient {
   protected BlockingQueue rqueue;
   protected final HashMap omap;
   protected long messagesRead;
-  private List addrs;
+  private List addrs;
 
   /**
    * Creates a tap client against the specified servers.
@@ -64,7 +63,7 @@ public class TapClient {
    *
    * @param ia the addresses of each node in the cluster.
    */
-  public TapClient(InetSocketAddress... ia) {
+  public TapClient(HostPort... ia) {
     this(Arrays.asList(ia));
   }
 
@@ -77,7 +76,7 @@ public TapClient(InetSocketAddress... ia) {
    *
    * @param addrs a list of addresses containing each node in the cluster.
    */
-  public TapClient(List addrs) {
+  public TapClient(List addrs) {
     this.rqueue = new LinkedBlockingQueue();
     this.omap = new HashMap();
     this.addrs = addrs;
diff --git a/src/main/java/net/spy/memcached/TapConnectionProvider.java b/src/main/java/net/spy/memcached/TapConnectionProvider.java
index 9444aa2ff..bfa749033 100644
--- a/src/main/java/net/spy/memcached/TapConnectionProvider.java
+++ b/src/main/java/net/spy/memcached/TapConnectionProvider.java
@@ -23,8 +23,6 @@
 package net.spy.memcached;
 
 import java.io.IOException;
-import java.net.InetSocketAddress;
-import java.net.SocketAddress;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.List;
@@ -63,17 +61,17 @@ public class TapConnectionProvider extends SpyObject implements
    * @param ia the memcached locations
    * @throws IOException if connections cannot be established
    */
-  public TapConnectionProvider(InetSocketAddress... ia) throws IOException {
+  public TapConnectionProvider(HostPort... ia) throws IOException {
     this(new BinaryConnectionFactory(), Arrays.asList(ia));
   }
 
   /**
    * Get a tap client operating on the specified memcached locations.
    *
-   * @param addrs the socket addrs
+   * @param addrs the address
    * @throws IOException if connections cannot be established
    */
-  public TapConnectionProvider(List addrs)
+  public TapConnectionProvider(List addrs)
     throws IOException {
     this(new BinaryConnectionFactory(), addrs);
   }
@@ -82,11 +80,11 @@ public TapConnectionProvider(List addrs)
    * Get a tap client operating on the specified memcached locations.
    *
    * @param cf the connection factory to configure connections for this client
-   * @param addrs the socket addresses
+   * @param addrs the addresses
    * @throws IOException if connections cannot be established
    */
   public TapConnectionProvider(ConnectionFactory cf,
-      List addrs) throws IOException {
+      List addrs) throws IOException {
     if (cf == null) {
       throw new NullPointerException("Connection factory required");
     }
@@ -137,7 +135,7 @@ public boolean addObserver(ConnectionObserver obs) {
     if (rv) {
       for (MemcachedNode node : conn.getLocator().getAll()) {
         if (node.isActive()) {
-          obs.connectionEstablished(node.getSocketAddress(), -1);
+          obs.connectionEstablished(node.getHostPort(), -1);
         }
       }
     }
@@ -154,28 +152,28 @@ public boolean removeObserver(ConnectionObserver obs) {
     return conn.removeObserver(obs);
   }
 
-  public void connectionEstablished(SocketAddress sa, int reconnectCount) {
+  public void connectionEstablished(HostPort hp, int reconnectCount) {
     if (authDescriptor != null) {
       if (authDescriptor.authThresholdReached()) {
         this.shutdown();
       } else {
-        authMonitor.authConnection(conn, opFact, authDescriptor, findNode(sa));
+        authMonitor.authConnection(conn, opFact, authDescriptor, findNode(hp));
       }
     }
   }
 
-  private MemcachedNode findNode(SocketAddress sa) {
+  private MemcachedNode findNode(HostPort hp) {
     MemcachedNode node = null;
     for (MemcachedNode n : conn.getLocator().getAll()) {
-      if (n.getSocketAddress().equals(sa)) {
+      if (n.getHostPort().equals(hp)) {
         node = n;
       }
     }
-    assert node != null : "Couldn't find node connected to " + sa;
+    assert node != null : "Couldn't find node connected to " + hp;
     return node;
   }
 
-  public void connectionLost(SocketAddress sa) {
+  public void connectionLost(HostPort hp) {
     // Don't care.
   }
 
diff --git a/src/main/java/net/spy/memcached/auth/AuthThread.java b/src/main/java/net/spy/memcached/auth/AuthThread.java
index 2a635b64a..1c8549e95 100644
--- a/src/main/java/net/spy/memcached/auth/AuthThread.java
+++ b/src/main/java/net/spy/memcached/auth/AuthThread.java
@@ -144,7 +144,7 @@ public void run() {
     getLogger().log(level, msg);
 
     if (supportedMechs == null || supportedMechs.length == 0) {
-      getLogger().warn("Authentication failed to " + node.getSocketAddress()
+      getLogger().warn("Authentication failed to " + node.getHostPort()
         + ", got empty SASL auth mech list.");
       throw new IllegalStateException("Got empty SASL auth mech list.");
     }
@@ -164,7 +164,7 @@ public void receivedStatus(OperationStatus val) {
           if (val.getMessage().length() == 0) {
             done.set(true);
             node.authComplete();
-            getLogger().info("Authenticated to " + node.getSocketAddress());
+            getLogger().info("Authenticated to " + node.getHostPort());
           } else {
             foundStatus.set(val);
           }
@@ -210,7 +210,7 @@ public void complete() {
       priorStatus = foundStatus.get();
       if (priorStatus != null) {
         if (!priorStatus.isSuccess()) {
-          getLogger().warn("Authentication failed to " + node.getSocketAddress()
+          getLogger().warn("Authentication failed to " + node.getHostPort()
             + ", Status: " + priorStatus);
         }
       }
@@ -228,11 +228,11 @@ private Operation buildOperation(OperationStatus st, OperationCallback cb,
     final String [] supportedMechs) {
     if (st == null) {
       return opFact.saslAuth(supportedMechs,
-          node.getSocketAddress().toString(), null,
+          node.getHostPort().toString(), null,
           authDescriptor.getCallback(), cb);
     } else {
       return opFact.saslStep(supportedMechs, KeyUtil.getKeyBytes(
-          st.getMessage()), node.getSocketAddress().toString(), null,
+          st.getMessage()), node.getHostPort().toString(), null,
           authDescriptor.getCallback(), cb);
     }
   }
diff --git a/src/main/java/net/spy/memcached/internal/CheckedOperationTimeoutException.java b/src/main/java/net/spy/memcached/internal/CheckedOperationTimeoutException.java
index 98168f2f2..b9a2eefa5 100644
--- a/src/main/java/net/spy/memcached/internal/CheckedOperationTimeoutException.java
+++ b/src/main/java/net/spy/memcached/internal/CheckedOperationTimeoutException.java
@@ -68,7 +68,7 @@ public CheckedOperationTimeoutException(String message,
         rv.append(", ");
       }
       MemcachedNode node = op == null ? null : op.getHandlingNode();
-      rv.append(node == null ? "" : node.getSocketAddress());
+      rv.append(node == null ? "" : node.getHostPort());
     }
     return rv.toString();
   }
diff --git a/src/main/java/net/spy/memcached/protocol/TCPMemcachedNodeImpl.java b/src/main/java/net/spy/memcached/protocol/TCPMemcachedNodeImpl.java
index 52deeb04b..6425fb5c9 100644
--- a/src/main/java/net/spy/memcached/protocol/TCPMemcachedNodeImpl.java
+++ b/src/main/java/net/spy/memcached/protocol/TCPMemcachedNodeImpl.java
@@ -24,7 +24,6 @@
 package net.spy.memcached.protocol;
 
 import java.io.IOException;
-import java.net.SocketAddress;
 import java.nio.ByteBuffer;
 import java.nio.channels.SelectionKey;
 import java.nio.channels.SocketChannel;
@@ -37,6 +36,7 @@
 
 import net.spy.memcached.ConnectionFactory;
 import net.spy.memcached.FailureMode;
+import net.spy.memcached.HostPort;
 import net.spy.memcached.MemcachedConnection;
 import net.spy.memcached.MemcachedNode;
 import net.spy.memcached.compat.SpyObject;
@@ -51,7 +51,7 @@
 public abstract class TCPMemcachedNodeImpl extends SpyObject implements
     MemcachedNode {
 
-  private final SocketAddress socketAddress;
+  private final HostPort hostPort;
   private final ByteBuffer rbuf;
   private final ByteBuffer wbuf;
   protected final BlockingQueue writeQ;
@@ -75,18 +75,26 @@ public abstract class TCPMemcachedNodeImpl extends SpyObject implements
   // operation Future.get timeout counter
   private final AtomicInteger continuousTimeout = new AtomicInteger(0);
 
-  public TCPMemcachedNodeImpl(SocketAddress sa, SocketChannel c, int bufSize,
-      BlockingQueue rq, BlockingQueue wq,
-      BlockingQueue iq, long opQueueMaxBlockTime,
-      boolean waitForAuth, long dt, long authWaitTime, ConnectionFactory fact) {
+  public TCPMemcachedNodeImpl(
+      HostPort hp,
+      SocketChannel c,
+      int bufSize,
+      BlockingQueue rq,
+      BlockingQueue wq,
+      BlockingQueue iq,
+      long opQueueMaxBlockTime,
+      boolean waitForAuth,
+      long dt,
+      long authWaitTime,
+      ConnectionFactory fact) {
     super();
-    assert sa != null : "No SocketAddress";
+    assert hp != null : "No HostPort";
     assert c != null : "No SocketChannel";
     assert bufSize > 0 : "Invalid buffer size: " + bufSize;
     assert rq != null : "No operation read queue";
     assert wq != null : "No operation write queue";
     assert iq != null : "No input queue";
-    socketAddress = sa;
+    hostPort = hp;
     connectionFactory = fact;
     this.authWaitTime = authWaitTime;
     setChannel(c);
@@ -346,7 +354,7 @@ public final void addOp(Operation op) {
         if (mode == FailureMode.Redistribute || mode == FailureMode.Retry) {
           getLogger().debug("Redistributing Operation " + op + " because auth "
             + "latch taken longer than " + authWaitTime + " milliseconds to "
-            + "complete on node " + getSocketAddress());
+            + "complete on node " + getHostPort());
           connection.retryOperation(op);
         } else {
           op.cancel();
@@ -423,10 +431,10 @@ public final ByteBuffer getWbuf() {
   /*
    * (non-Javadoc)
    *
-   * @see net.spy.memcached.MemcachedNode#getSocketAddress()
+   * @see net.spy.memcached.MemcachedNode#getHostPort()
    */
-  public final SocketAddress getSocketAddress() {
-    return socketAddress;
+  public final HostPort getHostPort() {
+    return hostPort;
   }
 
   /*
@@ -491,7 +499,8 @@ public final String toString() {
     int rsize = readQ.size() + (optimizedOp == null ? 0 : 1);
     int wsize = writeQ.size();
     int isize = inputQueue.size();
-    return "{QA sa=" + getSocketAddress() + ", #Rops=" + rsize
+    return "{QA hp=" + getHostPort()
+        + ", #Rops=" + rsize
         + ", #Wops=" + wsize
         + ", #iq=" + isize
         + ", topRop=" + getCurrentReadOp()
diff --git a/src/main/java/net/spy/memcached/protocol/ascii/AsciiMemcachedNodeImpl.java b/src/main/java/net/spy/memcached/protocol/ascii/AsciiMemcachedNodeImpl.java
index fb5a3f7bf..5578bf9fb 100644
--- a/src/main/java/net/spy/memcached/protocol/ascii/AsciiMemcachedNodeImpl.java
+++ b/src/main/java/net/spy/memcached/protocol/ascii/AsciiMemcachedNodeImpl.java
@@ -23,11 +23,11 @@
 
 package net.spy.memcached.protocol.ascii;
 
-import java.net.SocketAddress;
 import java.nio.channels.SocketChannel;
 import java.util.concurrent.BlockingQueue;
 
 import net.spy.memcached.ConnectionFactory;
+import net.spy.memcached.HostPort;
 import net.spy.memcached.ops.GetOperation;
 import net.spy.memcached.ops.Operation;
 import net.spy.memcached.ops.OperationState;
@@ -39,12 +39,19 @@
  */
 public final class AsciiMemcachedNodeImpl extends TCPMemcachedNodeImpl {
 
-  public AsciiMemcachedNodeImpl(SocketAddress sa, SocketChannel c, int bufSize,
-      BlockingQueue rq, BlockingQueue wq,
-      BlockingQueue iq, Long opQueueMaxBlockTimeNs, long dt,
-      long at, ConnectionFactory fa) {
+  public AsciiMemcachedNodeImpl(
+      HostPort hp,
+      SocketChannel c,
+      int bufSize,
+      BlockingQueue rq,
+      BlockingQueue wq,
+      BlockingQueue iq,
+      Long opQueueMaxBlockTimeNs,
+      long dt,
+      long at,
+      ConnectionFactory fa) {
     // ASCII never does auth
-    super(sa, c, bufSize, rq, wq, iq, opQueueMaxBlockTimeNs, false, dt, at, fa);
+    super(hp, c, bufSize, rq, wq, iq, opQueueMaxBlockTimeNs, false, dt, at, fa);
   }
 
   @Override
diff --git a/src/main/java/net/spy/memcached/protocol/binary/BinaryMemcachedNodeImpl.java b/src/main/java/net/spy/memcached/protocol/binary/BinaryMemcachedNodeImpl.java
index 494197d2c..4eea85b6a 100644
--- a/src/main/java/net/spy/memcached/protocol/binary/BinaryMemcachedNodeImpl.java
+++ b/src/main/java/net/spy/memcached/protocol/binary/BinaryMemcachedNodeImpl.java
@@ -23,11 +23,11 @@
 
 package net.spy.memcached.protocol.binary;
 
-import java.net.SocketAddress;
 import java.nio.channels.SocketChannel;
 import java.util.concurrent.BlockingQueue;
 
 import net.spy.memcached.ConnectionFactory;
+import net.spy.memcached.HostPort;
 import net.spy.memcached.ops.CASOperation;
 import net.spy.memcached.ops.GetOperation;
 import net.spy.memcached.ops.Operation;
@@ -45,12 +45,19 @@ public class BinaryMemcachedNodeImpl extends TCPMemcachedNodeImpl {
   private static final int MAX_SET_OPTIMIZATION_COUNT = 65535;
   private static final int MAX_SET_OPTIMIZATION_BYTES = 2 * 1024 * 1024;
 
-  public BinaryMemcachedNodeImpl(SocketAddress sa, SocketChannel c,
-      int bufSize, BlockingQueue rq, BlockingQueue wq,
-      BlockingQueue iq, Long opQueueMaxBlockTimeNs,
-      boolean waitForAuth, long dt, long at, ConnectionFactory fa) {
-    super(sa, c, bufSize, rq, wq, iq, opQueueMaxBlockTimeNs, waitForAuth, dt,
-      at, fa);
+  public BinaryMemcachedNodeImpl(
+      HostPort hp,
+      SocketChannel c,
+      int bufSize,
+      BlockingQueue rq,
+      BlockingQueue wq,
+      BlockingQueue iq,
+      Long opQueueMaxBlockTimeNs,
+      boolean waitForAuth,
+      long dt,
+      long at,
+      ConnectionFactory fa) {
+    super(hp, c, bufSize, rq, wq, iq, opQueueMaxBlockTimeNs, waitForAuth, dt, at, fa);
   }
 
   @Override
diff --git a/src/main/java/net/spy/memcached/spring/MemcachedClientFactoryBean.java b/src/main/java/net/spy/memcached/spring/MemcachedClientFactoryBean.java
index 63d5c6463..9adf88da0 100644
--- a/src/main/java/net/spy/memcached/spring/MemcachedClientFactoryBean.java
+++ b/src/main/java/net/spy/memcached/spring/MemcachedClientFactoryBean.java
@@ -69,7 +69,8 @@ public class MemcachedClientFactoryBean implements FactoryBean {
 
   @Override
   public Object getObject() throws Exception {
-    return new MemcachedClient(connectionFactoryBuilder.build(),
+    return new MemcachedClient(
+        connectionFactoryBuilder.build(),
         AddrUtil.getAddresses(servers));
   }
 
diff --git a/src/main/java/net/spy/memcached/util/DefaultKetamaNodeLocatorConfiguration.java b/src/main/java/net/spy/memcached/util/DefaultKetamaNodeLocatorConfiguration.java
index 521fa7e97..2805966d0 100644
--- a/src/main/java/net/spy/memcached/util/DefaultKetamaNodeLocatorConfiguration.java
+++ b/src/main/java/net/spy/memcached/util/DefaultKetamaNodeLocatorConfiguration.java
@@ -39,31 +39,30 @@ public class DefaultKetamaNodeLocatorConfiguration implements
 
   // Internal lookup map to try to carry forward the optimisation that was
   // previously in KetamaNodeLocator
-  protected Map socketAddresses =
+  protected Map address =
       new HashMap();
 
   /**
-   * Returns the socket address of a given MemcachedNode.
+   * Returns the address of a given MemcachedNode.
    *
    * @param node The node which we're interested in
-   * @return String the socket address of that node.
+   * @return String the address of that node.
    */
-  protected String getSocketAddressForNode(MemcachedNode node) {
-    // Using the internal map retrieve the socket addresses
-    // for given nodes.
+  protected String getAddressForNode(MemcachedNode node) {
+    // Using the internal map retrieve the addresses for given nodes.
     // I'm aware that this code is inherently thread-unsafe as
     // I'm using a HashMap implementation of the map, but the worst
     // case ( I believe) is we're slightly in-efficient when
     // a node has never been seen before concurrently on two different
-    // threads, so it the socketaddress will be requested multiple times!
+    // threads, so it the address will be requested multiple times!
     // all other cases should be as fast as possible.
-    String result = socketAddresses.get(node);
+    String result = address.get(node);
     if (result == null) {
-      result = String.valueOf(node.getSocketAddress());
+      result = String.valueOf(node.getHostPort());
       if (result.startsWith("/")) {
         result = result.substring(1);
       }
-      socketAddresses.put(node, result);
+      address.put(node, result);
     }
     return result;
   }
@@ -83,9 +82,9 @@ public int getNodeRepetitions() {
    * KetamaNodeLocator algorithm.
    *
    * 

- * This default implementation uses the socket-address of the MemcachedNode - * and concatenates it with a hyphen directly against the repetition number - * for example a key for a particular server's first repetition may look like: + * This default implementation uses the address of the MemcachedNode and + * concatenates it with a hyphen directly against the repetition number for + * example a key for a particular server's first repetition may look like: *

* *

@@ -115,6 +114,6 @@ public int getNodeRepetitions() { * @return The key that represents the specific repetition of the node */ public String getKeyForNode(MemcachedNode node, int repetition) { - return getSocketAddressForNode(node) + "-" + repetition; + return getAddressForNode(node) + "-" + repetition; } } diff --git a/src/test/java/net/spy/memcached/AddrUtilTest.java b/src/test/java/net/spy/memcached/AddrUtilTest.java index 55d9b0c00..1256c28b4 100644 --- a/src/test/java/net/spy/memcached/AddrUtilTest.java +++ b/src/test/java/net/spy/memcached/AddrUtilTest.java @@ -22,7 +22,6 @@ package net.spy.memcached; -import java.net.InetSocketAddress; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -35,14 +34,14 @@ public class AddrUtilTest extends TestCase { public void testSingle() throws Exception { - List addrs = AddrUtil.getAddresses("www.google.com:80"); + List addrs = AddrUtil.getAddresses("www.google.com:80"); assertEquals(1, addrs.size()); assertEquals("www.google.com", addrs.get(0).getHostName()); assertEquals(80, addrs.get(0).getPort()); } public void testTwo() throws Exception { - List addrs = + List addrs = AddrUtil.getAddresses("www.google.com:80 www.yahoo.com:81"); assertEquals(2, addrs.size()); assertEquals("www.google.com", addrs.get(0).getHostName()); @@ -52,7 +51,7 @@ public void testTwo() throws Exception { } public void testThree() throws Exception { - List addrs = AddrUtil + List addrs = AddrUtil .getAddresses(" , www.google.com:80 ,, ,, www.yahoo.com:81 , ,,"); assertEquals(2, addrs.size()); assertEquals("www.google.com", addrs.get(0).getHostName()); @@ -64,7 +63,7 @@ public void testThree() throws Exception { public void testBrokenHost() throws Exception { String s = "www.google.com:80 www.yahoo.com:81:more"; try { - List addrs = AddrUtil.getAddresses(s); + List addrs = AddrUtil.getAddresses(s); fail("Expected failure, got " + addrs); } catch (NumberFormatException e) { e.printStackTrace(); @@ -75,7 +74,7 @@ public void testBrokenHost() throws Exception { public void testBrokenHost2() throws Exception { String s = "www.google.com:80 www.yahoo.com"; try { - List addrs = AddrUtil.getAddresses(s); + List addrs = AddrUtil.getAddresses(s); fail("Expected failure, got " + addrs); } catch (IllegalArgumentException e) { assertEquals("Invalid server ``www.yahoo.com'' in list: " + s, @@ -86,7 +85,7 @@ public void testBrokenHost2() throws Exception { public void testBrokenList() throws Exception { String s = ""; try { - List addrs = AddrUtil.getAddresses(s); + List addrs = AddrUtil.getAddresses(s); fail("Expected failure, got " + addrs); } catch (IllegalArgumentException e) { assertEquals("No hosts in list: ``''", e.getMessage()); @@ -96,7 +95,7 @@ public void testBrokenList() throws Exception { public void testBrokenList2() throws Exception { String s = " "; try { - List addrs = AddrUtil.getAddresses(s); + List addrs = AddrUtil.getAddresses(s); fail("Expected failure, got " + addrs); } catch (IllegalArgumentException e) { assertEquals("No hosts in list: `` ''", e.getMessage()); @@ -106,7 +105,7 @@ public void testBrokenList2() throws Exception { public void testNullList() throws Exception { String s = null; try { - List addrs = AddrUtil.getAddresses(s); + List addrs = AddrUtil.getAddresses(s); fail("Expected failure, got " + addrs); } catch (NullPointerException e) { assertEquals("Null host list", e.getMessage()); @@ -114,7 +113,7 @@ public void testNullList() throws Exception { } public void testIPv6Host() throws Exception { - List addrs = AddrUtil.getAddresses("::1:80"); + List addrs = AddrUtil.getAddresses("::1:80"); assertEquals(1, addrs.size()); Set validLocalhostNames = new HashSet(); @@ -122,7 +121,7 @@ public void testIPv6Host() throws Exception { validLocalhostNames.add("ip6-localhost"); validLocalhostNames.add("0:0:0:0:0:0:0:1"); validLocalhostNames.add("localhost6.localdomain6"); - assert (validLocalhostNames.contains(addrs.get(0).getHostName())); + assert (validLocalhostNames.contains(addrs.get(0).getAddress().getHostName())); assertEquals(80, addrs.get(0).getPort()); } } diff --git a/src/test/java/net/spy/memcached/ClientBaseCase.java b/src/test/java/net/spy/memcached/ClientBaseCase.java index 50dc26320..8f2f8367b 100644 --- a/src/test/java/net/spy/memcached/ClientBaseCase.java +++ b/src/test/java/net/spy/memcached/ClientBaseCase.java @@ -23,7 +23,6 @@ package net.spy.memcached; -import java.net.SocketAddress; import java.util.ArrayList; import java.util.Collection; import java.util.Map; @@ -96,7 +95,7 @@ protected boolean isMoxi() { } // some tests are invalid if using moxi - Map> stats = client.getStats("proxy"); + Map> stats = client.getStats("proxy"); for (Map node : stats.values()) { if (node.get("basic:version") != null) { moxi = true; diff --git a/src/test/java/net/spy/memcached/ConnectionFactoryBuilderTest.java b/src/test/java/net/spy/memcached/ConnectionFactoryBuilderTest.java index 9c64142b0..e4729a6d7 100644 --- a/src/test/java/net/spy/memcached/ConnectionFactoryBuilderTest.java +++ b/src/test/java/net/spy/memcached/ConnectionFactoryBuilderTest.java @@ -24,7 +24,6 @@ package net.spy.memcached; import java.net.InetSocketAddress; -import java.net.SocketAddress; import java.nio.channels.SocketChannel; import java.util.Collections; import java.util.concurrent.ArrayBlockingQueue; @@ -91,8 +90,7 @@ public void testDefaults() throws Exception { SocketChannel sc = SocketChannel.open(); try { assertTrue(f.createMemcachedNode( - InetSocketAddress.createUnresolved("localhost", - TestConfig.PORT_NUMBER), sc, 1) + new HostPort("localhost", TestConfig.PORT_NUMBER), sc, 1) instanceof AsciiMemcachedNodeImpl); } finally { sc.close(); @@ -109,11 +107,11 @@ public void testDefaults() throws Exception { public void testModifications() throws Exception { ConnectionObserver testObserver = new ConnectionObserver() { - public void connectionLost(SocketAddress sa) { + public void connectionLost(HostPort hp) { // none } - public void connectionEstablished(SocketAddress sa, int reconnectCount) { + public void connectionEstablished(HostPort hp, int reconnectCount) { // none } }; @@ -161,16 +159,14 @@ public void connectionEstablished(SocketAddress sa, int reconnectCount) { assertEquals(f.getAuthWaitTime(), 3000); MemcachedNode n = new MockMemcachedNode( - InetSocketAddress.createUnresolved("localhost", - TestConfig.PORT_NUMBER)); + new HostPort("localhost", TestConfig.PORT_NUMBER)); assertTrue(f.createLocator(Collections.singletonList(n)) instanceof KetamaNodeLocator); SocketChannel sc = SocketChannel.open(); try { assertTrue(f.createMemcachedNode( - InetSocketAddress.createUnresolved("localhost", - TestConfig.PORT_NUMBER), sc, 1) + new HostPort("lcoalhost", TestConfig.PORT_NUMBER), sc, 1) instanceof BinaryMemcachedNodeImpl); } finally { sc.close(); diff --git a/src/test/java/net/spy/memcached/ConsistentHashingTest.java b/src/test/java/net/spy/memcached/ConsistentHashingTest.java index f4c1fbd3a..d524acc03 100644 --- a/src/test/java/net/spy/memcached/ConsistentHashingTest.java +++ b/src/test/java/net/spy/memcached/ConsistentHashingTest.java @@ -23,7 +23,6 @@ package net.spy.memcached; -import java.net.InetSocketAddress; import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -80,8 +79,8 @@ private void runThisManyNodes(final int totalNodes) { failed = true; System.out.println("---------------"); System.out.println("Key: " + key); - System.out.println("Small: " + smallNode.getSocketAddress()); - System.out.println("Large: " + largeNode.getSocketAddress()); + System.out.println("Small: " + smallNode.getHostPort()); + System.out.println("Large: " + largeNode.getHostPort()); } } assertFalse(failed); @@ -93,7 +92,7 @@ private void runThisManyNodes(final int totalNodes) { final MemcachedNode newNode = smLocator.getNodeForKey(key); if (!smaller.contains(newNode)) { System.out.println("Error - " + key + " -> " - + newNode.getSocketAddress()); + + newNode.getHostPort()); failed = true; } } @@ -138,10 +137,10 @@ private String[] generateAddresses(final int maxSize) { return results; } - private List createNodes(List addresses) { + private List createNodes(List addresses) { List results = new ArrayList(); - for (InetSocketAddress addr : addresses) { + for (HostPort addr : addresses) { results.add(new MockMemcachedNode(addr)); } diff --git a/src/test/java/net/spy/memcached/KetamaNodeLocatorTest.java b/src/test/java/net/spy/memcached/KetamaNodeLocatorTest.java index 5d5d41785..7a8009850 100644 --- a/src/test/java/net/spy/memcached/KetamaNodeLocatorTest.java +++ b/src/test/java/net/spy/memcached/KetamaNodeLocatorTest.java @@ -23,7 +23,6 @@ package net.spy.memcached; -import java.net.InetSocketAddress; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -38,9 +37,8 @@ public class KetamaNodeLocatorTest extends AbstractNodeLocationCase { protected void setupNodes(HashAlgorithm alg, int n) { super.setupNodes(n); for (int i = 0; i < nodeMocks.length; i++) { - nodeMocks[i].expects(atLeastOnce()).method("getSocketAddress") - .will(returnValue(InetSocketAddress.createUnresolved("127.0.0.1", - 10000 + i))); + nodeMocks[i].expects(atLeastOnce()).method("getHostPort") + .will(returnValue(new HostPort("127.0.0.1", 10000 + i))); } locator = new KetamaNodeLocator(Arrays.asList(nodes), alg); @@ -72,9 +70,8 @@ public void testAllAfterUpdate() throws Exception { ArrayList toUpdate = new ArrayList(); Mock mock = mock(MemcachedNode.class); - mock.expects(atLeastOnce()).method("getSocketAddress") - .will(returnValue(InetSocketAddress.createUnresolved("127.0.0.1", - 10000))); + mock.expects(atLeastOnce()).method("getHostPort") + .will(returnValue(new HostPort("127.0.0.1", 10000))); toUpdate.add((MemcachedNode) mock.proxy()); locator.updateLocator(toUpdate); @@ -175,9 +172,9 @@ private MemcachedNode[] mockNodes(String[] servers) { setupNodes(servers.length); for (int i = 0; i < nodeMocks.length; i++) { - List a = AddrUtil.getAddresses(servers[i]); + List a = AddrUtil.getAddresses(servers[i]); - nodeMocks[i].expects(atLeastOnce()).method("getSocketAddress") + nodeMocks[i].expects(atLeastOnce()).method("getHostPort") .will(returnValue(a.iterator().next())); } @@ -1817,7 +1814,7 @@ public void testLibKetamaCompatTwo() { String k = s[0]; String server = s[1]; MemcachedNode n = locator.getPrimary(k); - assertEquals("/" + server, n.getSocketAddress().toString()); + assertEquals(server, n.getHostPort().toString()); } } diff --git a/src/test/java/net/spy/memcached/LongClientTest.java b/src/test/java/net/spy/memcached/LongClientTest.java index 332993e7e..e046cc3e3 100644 --- a/src/test/java/net/spy/memcached/LongClientTest.java +++ b/src/test/java/net/spy/memcached/LongClientTest.java @@ -46,8 +46,7 @@ public void testParallelGet() throws Throwable { client.shutdown(); initClient(new DefaultConnectionFactory() { @Override - public MemcachedConnection - createConnection(List addrs) throws IOException { + public MemcachedConnection createConnection(List addrs) throws IOException { MemcachedConnection rv = super.createConnection(addrs); return rv; } diff --git a/src/test/java/net/spy/memcached/MemcachedClientConstructorTest.java b/src/test/java/net/spy/memcached/MemcachedClientConstructorTest.java index 0d23e44e9..a1f0ab345 100644 --- a/src/test/java/net/spy/memcached/MemcachedClientConstructorTest.java +++ b/src/test/java/net/spy/memcached/MemcachedClientConstructorTest.java @@ -24,9 +24,6 @@ package net.spy.memcached; import java.io.IOException; -import java.net.InetAddress; -import java.net.InetSocketAddress; -import java.net.SocketAddress; import java.util.Collections; import java.util.List; import java.util.Map; @@ -59,7 +56,7 @@ protected void tearDown() throws Exception { } private void assertWorking() throws Exception { - Map versions = client.getVersions(); + Map versions = client.getVersions(); assertEquals("/" + TestConfig.IPV4_ADDR + ":" + TestConfig.PORT_NUMBER, versions.keySet().iterator().next().toString()); } @@ -71,8 +68,9 @@ private void assertArgRequired(IllegalArgumentException e) { public void testVarargConstructor() throws Exception { client = - new MemcachedClient(new InetSocketAddress( - InetAddress.getByName(TestConfig.IPV4_ADDR), + new MemcachedClient( + new HostPort( + TestConfig.IPV4_ADDR, TestConfig.PORT_NUMBER)); assertWorking(); } @@ -88,7 +86,7 @@ public void testEmptyVarargConstructor() throws Exception { public void testNulListConstructor() throws Exception { try { - List l = null; + List l = null; client = new MemcachedClient(l); fail("Expected null pointer exception, got " + client); } catch (NullPointerException e) { @@ -98,7 +96,7 @@ public void testNulListConstructor() throws Exception { public void testEmptyListConstructor() throws Exception { try { - client = new MemcachedClient(Collections.emptyList()); + client = new MemcachedClient(Collections.emptyList()); fail("Expected illegal arg exception, got " + client); } catch (IllegalArgumentException e) { assertArgRequired(e); @@ -165,8 +163,7 @@ public void testConnFactoryWithoutConns() throws Exception { try { client = new MemcachedClient(new DefaultConnectionFactory() { @Override - public MemcachedConnection createConnection( - List addrs) throws IOException { + public MemcachedConnection createConnection(List addrs) throws IOException { return null; } }, AddrUtil.getAddresses(TestConfig.IPV4_ADDR + ":" diff --git a/src/test/java/net/spy/memcached/MemcachedConnectionTest.java b/src/test/java/net/spy/memcached/MemcachedConnectionTest.java index c656ad1de..0dfbf36a8 100644 --- a/src/test/java/net/spy/memcached/MemcachedConnectionTest.java +++ b/src/test/java/net/spy/memcached/MemcachedConnectionTest.java @@ -45,8 +45,7 @@ public void testDebugBuffer() throws Exception { public void testConnectionsStatus() throws Exception { ConnectionFactory factory = new DefaultConnectionFactory(); - List addresses = - AddrUtil.getAddresses(TestConfig.IPV4_ADDR + ":11211"); + List addresses = AddrUtil.getAddresses(TestConfig.IPV4_ADDR + ":11211"); Collection observers = new ArrayList(); MemcachedConnection mcc = new MemcachedConnection(10240, factory, addresses, @@ -61,12 +60,12 @@ public void testBelongsToCluster() throws Exception { OperationFactory opfactory = new BinaryOperationFactory(); MemcachedNode node = new MockMemcachedNode( - new InetSocketAddress(TestConfig.IPV4_ADDR, TestConfig.PORT_NUMBER)); + new HostPort(TestConfig.IPV4_ADDR, TestConfig.PORT_NUMBER)); MemcachedNode node2 = new MockMemcachedNode( - new InetSocketAddress("invalidIpAddr", TestConfig.PORT_NUMBER)); + new HostPort("invalidIpAddr", TestConfig.PORT_NUMBER)); - List nodes = new ArrayList(); - nodes.add((InetSocketAddress)node.getSocketAddress()); + List nodes = new ArrayList(); + nodes.add(node.getHostPort()); MemcachedConnection conn = new MemcachedConnection( 100, factory, nodes, observers, FailureMode.Retry, opfactory); diff --git a/src/test/java/net/spy/memcached/MemcachedNodeROImplTest.java b/src/test/java/net/spy/memcached/MemcachedNodeROImplTest.java index 5013d69a1..04c826c6c 100644 --- a/src/test/java/net/spy/memcached/MemcachedNodeROImplTest.java +++ b/src/test/java/net/spy/memcached/MemcachedNodeROImplTest.java @@ -25,8 +25,6 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import java.net.InetSocketAddress; -import java.net.SocketAddress; import java.util.Arrays; import java.util.HashSet; import java.util.Set; @@ -40,17 +38,17 @@ public class MemcachedNodeROImplTest extends MockObjectTestCase { public void testReadOnliness() throws Exception { - SocketAddress sa = new InetSocketAddress(TestConfig.PORT_NUMBER); + HostPort hp = new HostPort("", TestConfig.PORT_NUMBER); Mock m = mock(MemcachedNode.class, "node"); MemcachedNodeROImpl node = new MemcachedNodeROImpl((MemcachedNode) m.proxy()); - m.expects(once()).method("getSocketAddress").will(returnValue(sa)); + m.expects(once()).method("getHostPort").will(returnValue(hp)); - assertSame(sa, node.getSocketAddress()); + assertSame(hp, node.getHostPort()); assertEquals(m.proxy().toString(), node.toString()); Set acceptable = new HashSet(Arrays.asList("toString", - "getSocketAddress", "getBytesRemainingToWrite", "getReconnectCount", + "getHostPort", "getBytesRemainingToWrite", "getReconnectCount", "getSelectionOps", "hasReadOp", "hasWriteOp", "isActive")); for (Method meth : MemcachedNode.class.getMethods()) { diff --git a/src/test/java/net/spy/memcached/MockMemcachedNode.java b/src/test/java/net/spy/memcached/MockMemcachedNode.java index b35c32949..edf1513c3 100644 --- a/src/test/java/net/spy/memcached/MockMemcachedNode.java +++ b/src/test/java/net/spy/memcached/MockMemcachedNode.java @@ -24,8 +24,6 @@ package net.spy.memcached; import java.io.IOException; -import java.net.InetSocketAddress; -import java.net.SocketAddress; import java.nio.ByteBuffer; import java.nio.channels.SelectionKey; import java.nio.channels.SocketChannel; @@ -37,14 +35,14 @@ * A MockMemcachedNode. */ public class MockMemcachedNode implements MemcachedNode { - private final InetSocketAddress socketAddress; + private final HostPort hostPort; - public SocketAddress getSocketAddress() { - return socketAddress; + public HostPort getHostPort() { + return hostPort; } - public MockMemcachedNode(InetSocketAddress socketAddress) { - this.socketAddress = socketAddress; + public MockMemcachedNode(HostPort hostPort) { + this.hostPort = hostPort; } @Override @@ -58,8 +56,8 @@ public boolean equals(Object o) { MockMemcachedNode that = (MockMemcachedNode) o; - if (socketAddress != null ? !socketAddress.equals(that.socketAddress) - : that.socketAddress != null) { + if (hostPort != null ? !hostPort.equals(that.hostPort) + : that.hostPort != null) { return false; } @@ -68,7 +66,7 @@ public boolean equals(Object o) { @Override public int hashCode() { - return (socketAddress != null ? socketAddress.hashCode() : 0); + return (hostPort != null ? hostPort.hashCode() : 0); } public void copyInputQueue() { diff --git a/src/test/java/net/spy/memcached/ObserverTest.java b/src/test/java/net/spy/memcached/ObserverTest.java index d8f71fe10..7fb5af17d 100644 --- a/src/test/java/net/spy/memcached/ObserverTest.java +++ b/src/test/java/net/spy/memcached/ObserverTest.java @@ -22,7 +22,6 @@ package net.spy.memcached; -import java.net.SocketAddress; import java.util.Collection; import java.util.Collections; import java.util.concurrent.CountDownLatch; @@ -49,11 +48,11 @@ public void testInitialObservers() throws Exception { final CountDownLatch latch = new CountDownLatch(1); final ConnectionObserver obs = new ConnectionObserver() { - public void connectionEstablished(SocketAddress sa, int reconnectCount) { + public void connectionEstablished(HostPort hp, int reconnectCount) { latch.countDown(); } - public void connectionLost(SocketAddress sa) { + public void connectionLost(HostPort hp) { assert false : "Should not see this."; } @@ -75,12 +74,12 @@ public Collection getInitialObservers() { } static class LoggingObserver extends SpyObject implements ConnectionObserver { - public void connectionEstablished(SocketAddress sa, int reconnectCount) { - getLogger().info("Connection established to %s (%s)", sa, reconnectCount); + public void connectionEstablished(HostPort hp, int reconnectCount) { + getLogger().info("Connection established to %s (%s)", hp, reconnectCount); } - public void connectionLost(SocketAddress sa) { - getLogger().info("Connection lost from %s", sa); + public void connectionLost(HostPort hp) { + getLogger().info("Connection lost from %s", hp); } } } diff --git a/src/test/java/net/spy/memcached/ProtocolBaseCase.java b/src/test/java/net/spy/memcached/ProtocolBaseCase.java index 3790f3825..4cd2c6bd4 100644 --- a/src/test/java/net/spy/memcached/ProtocolBaseCase.java +++ b/src/test/java/net/spy/memcached/ProtocolBaseCase.java @@ -23,7 +23,6 @@ package net.spy.memcached; -import java.net.SocketAddress; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.Arrays; @@ -38,9 +37,6 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; -import static junit.framework.Assert.assertEquals; -import static junit.framework.Assert.assertSame; -import static junit.framework.Assert.assertTrue; import net.spy.memcached.compat.SyncThread; import net.spy.memcached.internal.BulkFuture; @@ -73,7 +69,7 @@ public void testAssertions() { } public void testGetStats() throws Exception { - Map> stats = client.getStats(); + Map> stats = client.getStats(); System.out.println("Stats: " + stats); assertEquals(1, stats.size()); Map oneStat = stats.values().iterator().next(); @@ -87,7 +83,7 @@ public void testGetStatsSlabs() throws Exception { // There needs to at least have been one value set or there may be // no slabs to check. client.set("slabinitializer", 0, "hi"); - Map> stats = client.getStats("slabs"); + Map> stats = client.getStats("slabs"); System.out.println("Stats: " + stats); assertEquals(1, stats.size()); Map oneStat = stats.values().iterator().next(); @@ -104,7 +100,7 @@ public void testGetStatsSizes() throws Exception { // use flush when testing, so we check that there's at least // one. client.set("sizeinitializer", 0, "hi"); - Map> stats = client.getStats("sizes"); + Map> stats = client.getStats("sizes"); System.out.println("Stats sizes: " + stats); assertEquals(1, stats.size()); Map oneStat = stats.values().iterator().next(); @@ -516,9 +512,9 @@ public void testUnavailableServers() { protected abstract String getExpectedVersionSource(); public void testGetVersions() throws Exception { - Map vs = client.getVersions(); + Map vs = client.getVersions(); assertEquals(1, vs.size()); - Map.Entry me = vs.entrySet().iterator().next(); + Map.Entry me = vs.entrySet().iterator().next(); assertEquals(getExpectedVersionSource(), me.getKey().toString()); assertNotNull(me.getValue()); } @@ -672,7 +668,7 @@ public void testSyncGetTimeouts() throws Exception { client.shutdown(5, TimeUnit.SECONDS)); syncGetTimeoutsInitClient(); - Thread.sleep(100); // allow connections to be established + Thread.sleep(1000); // allow connections to be established int i = 0; GetFuture g = null; @@ -686,7 +682,7 @@ public void testSyncGetTimeouts() throws Exception { assert !g.getStatus().isSuccess(); System.err.println("Got a timeout at iteration " + i + "."); } - Thread.sleep(100); // let whatever caused the timeout to pass + Thread.sleep(1000); // let whatever caused the timeout to pass try { if (value.equals(client.asyncGet(key).get(30, TimeUnit.SECONDS))) { System.err.println("Got the right value."); @@ -727,7 +723,7 @@ public void xtestGracefulShutdownTooSlow() throws Exception { client.shutdown(1, TimeUnit.MILLISECONDS)); try { - Map m = client.getVersions(); + Map m = client.getVersions(); fail("Expected failure, got " + m); } catch (IllegalStateException e) { assertEquals("Shutting down", e.getMessage()); diff --git a/src/test/java/net/spy/memcached/QueueOverflowTest.java b/src/test/java/net/spy/memcached/QueueOverflowTest.java index ef18f1332..beb470ec1 100644 --- a/src/test/java/net/spy/memcached/QueueOverflowTest.java +++ b/src/test/java/net/spy/memcached/QueueOverflowTest.java @@ -24,7 +24,6 @@ package net.spy.memcached; import java.io.IOException; -import java.net.InetSocketAddress; import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -36,6 +35,7 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; +import net.spy.memcached.internal.OperationFuture; import net.spy.memcached.ops.Operation; /** @@ -52,7 +52,7 @@ protected void initClient() throws Exception { initClient(new DefaultConnectionFactory(5, 1024) { @Override public MemcachedConnection - createConnection(List addrs) throws IOException { + createConnection(List addrs) throws IOException { MemcachedConnection rv = super.createConnection(addrs); return rv; } @@ -146,7 +146,7 @@ public void testOverflowingReadQueue() throws Exception { } catch (IllegalStateException e) { // expected } - Thread.sleep(50); + Thread.sleep(100); for (Future f : c) { try { f.get(1, TimeUnit.SECONDS); @@ -156,7 +156,7 @@ public void testOverflowingReadQueue() throws Exception { // OK, at least we got one back. } } - Thread.sleep(500); + Thread.sleep(1000); assertTrue(client.set("kx", 0, "woo").get(5, TimeUnit.SECONDS)); } } diff --git a/src/test/java/net/spy/memcached/WokenUpOnIdleTest.java b/src/test/java/net/spy/memcached/WokenUpOnIdleTest.java index bc7efece4..5cce07075 100644 --- a/src/test/java/net/spy/memcached/WokenUpOnIdleTest.java +++ b/src/test/java/net/spy/memcached/WokenUpOnIdleTest.java @@ -26,7 +26,6 @@ import org.junit.Test; import java.io.IOException; -import java.net.InetSocketAddress; import java.util.Arrays; import java.util.Collection; import java.util.Collections; @@ -49,7 +48,7 @@ public void shouldWakeUpOnIdle() throws Exception { latch, 1024, new BinaryConnectionFactory(), - Arrays.asList(new InetSocketAddress(11211)), + Arrays.asList(new HostPort("", 11211)), Collections.emptyList(), FailureMode.Redistribute, new BinaryOperationFactory() @@ -61,7 +60,7 @@ public void shouldWakeUpOnIdle() throws Exception { static class InstrumentedConnection extends MemcachedConnection { final CountDownLatch latch; InstrumentedConnection(CountDownLatch latch, int bufSize, ConnectionFactory f, - List a, Collection obs, + List a, Collection obs, FailureMode fm, OperationFactory opfactory) throws IOException { super(bufSize, f, a, obs, fm, opfactory); this.latch = latch; diff --git a/src/test/java/net/spy/memcached/internal/CheckedOperationTimeoutExceptionTest.java b/src/test/java/net/spy/memcached/internal/CheckedOperationTimeoutExceptionTest.java index 6d988e679..812803e46 100644 --- a/src/test/java/net/spy/memcached/internal/CheckedOperationTimeoutExceptionTest.java +++ b/src/test/java/net/spy/memcached/internal/CheckedOperationTimeoutExceptionTest.java @@ -30,6 +30,7 @@ import java.util.Collection; import junit.framework.TestCase; +import net.spy.memcached.HostPort; import net.spy.memcached.MockMemcachedNode; import net.spy.memcached.TestConfig; import net.spy.memcached.ops.Operation; @@ -76,8 +77,7 @@ public void testMultipleOperation() { private TestOperation buildOp(int portNum) { TestOperation op = new TestOperation(); MockMemcachedNode node = - new MockMemcachedNode(InetSocketAddress.createUnresolved( - TestConfig.IPV4_ADDR, portNum)); + new MockMemcachedNode(new HostPort(TestConfig.IPV4_ADDR, portNum)); op.setHandlingNode(node); return op; }