Author: jbellis
Date: Mon Dec 21 19:11:18 2009
New Revision: 892931

URL: http://svn.apache.org/viewvc?rev=892931&view=rev
Log:
move isInSameRack and isInSameDC into AbstractEPS, making the basic IEPS 
interface sorting by proximity and the rest an implementation detail that only 
the corresponding kind of ReplicationStrategy should care about.  split 
sortByProximity into in-place and copying versions, fixing bug in 
findSuitableEndPoint.  patch by jbellis and goffinet for CASSANDRA-648

Added:
    
incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/locator/AbstractEndpointSnitch.java
   (with props)
Modified:
    incubator/cassandra/branches/cassandra-0.5/CHANGES.txt
    
incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/dht/BootStrapper.java
    
incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/locator/DatacenterEndPointSnitch.java
    
incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/locator/EndPointSnitch.java
    
incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/locator/IEndPointSnitch.java
    
incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/locator/RackAwareStrategy.java
    
incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/service/DatacenterSyncWriteResponseHandler.java
    
incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/service/DatacenterWriteResponseHandler.java
    
incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/service/StorageService.java

Modified: incubator/cassandra/branches/cassandra-0.5/CHANGES.txt
URL: 
http://svn.apache.org/viewvc/incubator/cassandra/branches/cassandra-0.5/CHANGES.txt?rev=892931&r1=892930&r2=892931&view=diff
==============================================================================
--- incubator/cassandra/branches/cassandra-0.5/CHANGES.txt (original)
+++ incubator/cassandra/branches/cassandra-0.5/CHANGES.txt Mon Dec 21 19:11:18 
2009
@@ -13,6 +13,8 @@
    (CASSANDRA-634)
  * return an InvalidRequestException for mal-formed SlicePredicates
    (CASSANDRA-643)
+ * fix bug determining closest neighbor for use in multiple datacenters
+   (CASSANDRA-648)
 
 
 0.5.0 beta 2

Modified: 
incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/dht/BootStrapper.java
URL: 
http://svn.apache.org/viewvc/incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/dht/BootStrapper.java?rev=892931&r1=892930&r2=892931&view=diff
==============================================================================
--- 
incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/dht/BootStrapper.java
 (original)
+++ 
incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/dht/BootStrapper.java
 Mon Dec 21 19:11:18 2009
@@ -22,7 +22,6 @@
  import java.util.concurrent.locks.Condition;
  import java.io.IOException;
  import java.io.UnsupportedEncodingException;
- import java.io.File;
  import java.net.InetAddress;
 
  import org.apache.log4j.Logger;
@@ -169,7 +168,7 @@
             {
                 if (range.contains(myRange))
                 {
-                    List<InetAddress> preferred = 
DatabaseDescriptor.getEndPointSnitch().sortByProximity(address, 
rangeAddresses.get(range));
+                    List<InetAddress> preferred = 
DatabaseDescriptor.getEndPointSnitch().getSortedListByProximity(address, 
rangeAddresses.get(range));
                     myRangeAddresses.putAll(myRange, preferred);
                     break;
                 }

Added: 
incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/locator/AbstractEndpointSnitch.java
URL: 
http://svn.apache.org/viewvc/incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/locator/AbstractEndpointSnitch.java?rev=892931&view=auto
==============================================================================
--- 
incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/locator/AbstractEndpointSnitch.java
 (added)
+++ 
incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/locator/AbstractEndpointSnitch.java
 Mon Dec 21 19:11:18 2009
@@ -0,0 +1,69 @@
+package org.apache.cassandra.locator;
+
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.util.*;
+
+public abstract class AbstractEndpointSnitch implements IEndPointSnitch
+{
+    /**
+     * Helps determine if 2 nodes are in the same rack in the data center.
+     * @param host a specified endpoint
+     * @param host2 another specified endpoint
+     * @return true if on the same rack false otherwise
+     * @throws UnknownHostException
+     */
+    abstract public boolean isOnSameRack(InetAddress host, InetAddress host2) 
throws UnknownHostException;
+
+    /**
+     * Helps determine if 2 nodes are in the same data center.
+     * @param host a specified endpoint
+     * @param host2 another specified endpoint
+     * @return true if in the same data center false otherwise
+     * @throws UnknownHostException
+     */
+    abstract public boolean isInSameDataCenter(InetAddress host, InetAddress 
host2) throws UnknownHostException;
+
+    /**
+     * Given endpoints this method will help us know the datacenter name where 
the node is located at.
+     */
+    abstract public String getLocation(InetAddress endpoint) throws 
UnknownHostException;
+
+    public List<InetAddress> getSortedListByProximity(final InetAddress 
address, Collection<InetAddress> unsortedAddress)
+    {
+        List<InetAddress> preferred = new 
ArrayList<InetAddress>(unsortedAddress);
+        sortByProximity(address, preferred);
+        return preferred;
+    }
+
+    public List<InetAddress> sortByProximity(final InetAddress address, 
List<InetAddress> addresses)
+    {
+        Collections.sort(addresses, new Comparator<InetAddress>()
+        {
+            public int compare(InetAddress a1, InetAddress a2)
+            {
+                try
+                {
+                    if (address.equals(a1) && !address.equals(a2))
+                        return -1;
+                    if (address.equals(a2) && !address.equals(a1))
+                        return 1;
+                    if (isOnSameRack(address, a1) && !isOnSameRack(address, 
a2))
+                        return -1;
+                    if (isOnSameRack(address, a2) && !isOnSameRack(address, 
a1))
+                        return 1;
+                    if (isInSameDataCenter(address, a1) && 
!isInSameDataCenter(address, a2))
+                        return -1;
+                    if (isInSameDataCenter(address, a2) && 
!isInSameDataCenter(address, a1))
+                        return 1;
+                    return 0;
+                }
+                catch (UnknownHostException e)
+                {
+                    throw new RuntimeException(e);
+                }
+            }
+        });
+        return addresses;
+    }
+}

Propchange: 
incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/locator/AbstractEndpointSnitch.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: 
incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/locator/DatacenterEndPointSnitch.java
URL: 
http://svn.apache.org/viewvc/incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/locator/DatacenterEndPointSnitch.java?rev=892931&r1=892930&r2=892931&view=diff
==============================================================================
--- 
incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/locator/DatacenterEndPointSnitch.java
 (original)
+++ 
incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/locator/DatacenterEndPointSnitch.java
 Mon Dec 21 19:11:18 2009
@@ -40,7 +40,7 @@
  * EndPoints and also get details from the same.
  */
 
-public class DatacenterEndPointSnitch implements IEndPointSnitch
+public class DatacenterEndPointSnitch extends AbstractEndpointSnitch
 {
     /**
      * This Map will contain the information of the EndPoints and its Location
@@ -203,38 +203,4 @@
         byte[] ipQuads = getIPAddress(endpoint.getHostAddress());
         return ipDC.get(ipQuads[1]).get(ipQuads[2]);
     }
-
-    // TODO add Datacenter proximity in the XML file or a trace rt to find the 
number of hops.
-    public List<InetAddress> sortByProximity(final InetAddress address, 
Collection<InetAddress> unsortedAddress)
-    {
-        List<InetAddress> preferred = new 
ArrayList<InetAddress>(unsortedAddress);
-        Collections.sort(preferred, new Comparator<InetAddress>()
-        {
-            public int compare(InetAddress a1, InetAddress a2)
-            {
-                try
-                {
-                    if (address.equals(a1) && !address.equals(a2))
-                        return -1;
-                    if (address.equals(a2) && !address.equals(a1))
-                        return 1;
-                    if (isOnSameRack(address, a1) && !isOnSameRack(address, 
a2))
-                        return -1;
-                    if (isOnSameRack(address, a2) && !isOnSameRack(address, 
a1))
-                        return 1;
-                    if (isInSameDataCenter(address, a1) && 
!isInSameDataCenter(address, a2))
-                        return -1;
-                    if (isInSameDataCenter(address, a2) && 
!isInSameDataCenter(address, a1))
-                        return 1;
-                    return 0;
-                }
-                catch (UnknownHostException e)
-                {
-                    throw new RuntimeException(e);
-                }
-            }
-        });
-
-        return preferred;
-    }
 }

Modified: 
incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/locator/EndPointSnitch.java
URL: 
http://svn.apache.org/viewvc/incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/locator/EndPointSnitch.java?rev=892931&r1=892930&r2=892931&view=diff
==============================================================================
--- 
incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/locator/EndPointSnitch.java
 (original)
+++ 
incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/locator/EndPointSnitch.java
 Mon Dec 21 19:11:18 2009
@@ -22,7 +22,7 @@
 import java.net.UnknownHostException;
 import java.util.*;
 
-public class EndPointSnitch implements IEndPointSnitch
+public class EndPointSnitch extends AbstractEndpointSnitch
 {
     public boolean isOnSameRack(InetAddress host, InetAddress host2) throws 
UnknownHostException
     {
@@ -55,36 +55,4 @@
         throw new UnknownHostException("Not Supported");
     }
 
-    public List<InetAddress> sortByProximity(final InetAddress address, 
Collection<InetAddress> unsortedAddress)
-    {
-        List<InetAddress> preferred = new 
ArrayList<InetAddress>(unsortedAddress);
-        Collections.sort(preferred, new Comparator<InetAddress>()
-        {
-            public int compare(InetAddress a1, InetAddress a2)
-            {
-                try
-                {
-                    if (address.equals(a1) && !address.equals(a2))
-                        return -1;
-                    if (address.equals(a2) && !address.equals(a1))
-                        return 1;
-                    if (isOnSameRack(address, a1) && !isOnSameRack(address, 
a2))
-                        return -1;
-                    if (isOnSameRack(address, a2) && !isOnSameRack(address, 
a1))
-                        return 1;
-                    if (isInSameDataCenter(address, a1) && 
!isInSameDataCenter(address, a2))
-                        return -1;
-                    if (isInSameDataCenter(address, a2) && 
!isInSameDataCenter(address, a1))
-                        return 1;
-                    return 0;
-                }
-                catch (UnknownHostException e)
-                {
-                    throw new RuntimeException(e);
-                }
-            }
-        });
-
-        return preferred;
-    }
 }

Modified: 
incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/locator/IEndPointSnitch.java
URL: 
http://svn.apache.org/viewvc/incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/locator/IEndPointSnitch.java?rev=892931&r1=892930&r2=892931&view=diff
==============================================================================
--- 
incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/locator/IEndPointSnitch.java
 (original)
+++ 
incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/locator/IEndPointSnitch.java
 Mon Dec 21 19:11:18 2009
@@ -37,30 +37,13 @@
 public interface IEndPointSnitch
 {
     /**
-     * Helps determine if 2 nodes are in the same rack in the data center.
-     * @param host a specified endpoint
-     * @param host2 another specified endpoint
-     * @return true if on the same rack false otherwise
-     * @throws UnknownHostException
+     * returns a new List<InetAddress> sorted by proximity to the given 
endpoint
      */
-    public boolean isOnSameRack(InetAddress host, InetAddress host2) throws 
UnknownHostException;
-    
-    /**
-     * Helps determine if 2 nodes are in the same data center.
-     * @param host a specified endpoint
-     * @param host2 another specified endpoint
-     * @return true if in the same data center false otherwise
-     * @throws UnknownHostException
-     */
-    public boolean isInSameDataCenter(InetAddress host, InetAddress host2) 
throws UnknownHostException;
-    
-    /**
-     * Given endpoints this method will help us know the datacenter name where 
the node is located at.
-     */
-    public String getLocation(InetAddress endpoint) throws 
UnknownHostException;
+    public List<InetAddress> getSortedListByProximity(InetAddress address, 
Collection<InetAddress> unsortedAddress);
 
     /**
-     * This method will sort the Set<InetAddress> according to the proximity 
of the given address.
+     * This method will sort the List<InetAddress> according to the proximity 
of the given address.
      */
-    public List<InetAddress> sortByProximity(InetAddress address, 
Collection<InetAddress> unsortedAddress);
+    public List<InetAddress> sortByProximity(InetAddress address, 
List<InetAddress> addresses);
 }
+

Modified: 
incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/locator/RackAwareStrategy.java
URL: 
http://svn.apache.org/viewvc/incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/locator/RackAwareStrategy.java?rev=892931&r1=892930&r2=892931&view=diff
==============================================================================
--- 
incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/locator/RackAwareStrategy.java
 (original)
+++ 
incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/locator/RackAwareStrategy.java
 Mon Dec 21 19:11:18 2009
@@ -39,9 +39,12 @@
  */
 public class RackAwareStrategy extends AbstractReplicationStrategy
 {
+    private final EndPointSnitch endPointSnitch;
+
     public RackAwareStrategy(TokenMetadata tokenMetadata, IPartitioner 
partitioner, int replicas)
     {
         super(tokenMetadata, partitioner, replicas);
+        endPointSnitch = (EndPointSnitch) 
StorageService.instance().getEndPointSnitch();
     }
 
     public ArrayList<InetAddress> getNaturalEndpoints(Token token, 
TokenMetadata metadata)
@@ -73,7 +76,6 @@
             return endpoints;
         }
         startIndex = (index + 1)%totalNodes;
-        IEndPointSnitch endPointSnitch = 
StorageService.instance().getEndPointSnitch();
 
         for (int i = startIndex, count = 1; count < totalNodes && foundCount < 
replicas_; ++count, i = (i + 1) % totalNodes)
         {

Modified: 
incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/service/DatacenterSyncWriteResponseHandler.java
URL: 
http://svn.apache.org/viewvc/incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/service/DatacenterSyncWriteResponseHandler.java?rev=892931&r1=892930&r2=892931&view=diff
==============================================================================
--- 
incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/service/DatacenterSyncWriteResponseHandler.java
 (original)
+++ 
incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/service/DatacenterSyncWriteResponseHandler.java
 Mon Dec 21 19:11:18 2009
@@ -30,6 +30,8 @@
 
 import org.apache.cassandra.config.DatabaseDescriptor;
 import org.apache.cassandra.net.Message;
+import org.apache.cassandra.locator.IEndPointSnitch;
+import org.apache.cassandra.locator.DatacenterEndPointSnitch;
 
 /**
  * This class will block for the replication factor which is
@@ -40,12 +42,14 @@
 {
     private final Map<String, Integer> dcResponses = new HashMap<String, 
Integer>();
     private final Map<String, Integer> responseCounts;
+    private final DatacenterEndPointSnitch endPointSnitch;
 
     public DatacenterSyncWriteResponseHandler(Map<String, Integer> 
responseCounts)
     {
         // Response is been managed by the map so make it 1 for the superclass.
         super(1);
         this.responseCounts = responseCounts;
+        endPointSnitch = (DatacenterEndPointSnitch) 
DatabaseDescriptor.getEndPointSnitch();
     }
 
     @Override
@@ -57,7 +61,7 @@
         }
         try
         {
-            String dataCenter = 
DatabaseDescriptor.getEndPointSnitch().getLocation(message.getFrom());
+            String dataCenter = endPointSnitch.getLocation(message.getFrom());
             Object blockFor = responseCounts.get(dataCenter);
             // If this DC needs to be blocked then do the below.
             if (blockFor != null)

Modified: 
incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/service/DatacenterWriteResponseHandler.java
URL: 
http://svn.apache.org/viewvc/incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/service/DatacenterWriteResponseHandler.java?rev=892931&r1=892930&r2=892931&view=diff
==============================================================================
--- 
incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/service/DatacenterWriteResponseHandler.java
 (original)
+++ 
incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/service/DatacenterWriteResponseHandler.java
 Mon Dec 21 19:11:18 2009
@@ -29,6 +29,7 @@
 
 import org.apache.cassandra.config.DatabaseDescriptor;
 import org.apache.cassandra.locator.IEndPointSnitch;
+import org.apache.cassandra.locator.DatacenterEndPointSnitch;
 import org.apache.cassandra.net.Message;
 import org.apache.cassandra.utils.FBUtilities;
 
@@ -40,7 +41,7 @@
 public class DatacenterWriteResponseHandler extends WriteResponseHandler
 {
     private int blockFor;
-    private IEndPointSnitch endpointsnitch;
+    private DatacenterEndPointSnitch endpointsnitch;
     private InetAddress localEndpoint;
 
     public DatacenterWriteResponseHandler(int blockFor)
@@ -48,7 +49,7 @@
         // Response is been managed by the map so the waitlist size really 
doesnt matter.
         super(blockFor);
         this.blockFor = blockFor;
-        endpointsnitch = DatabaseDescriptor.getEndPointSnitch();
+        endpointsnitch = (DatacenterEndPointSnitch) 
DatabaseDescriptor.getEndPointSnitch();
         localEndpoint = FBUtilities.getLocalAddress();
     }
 

Modified: 
incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/service/StorageService.java
URL: 
http://svn.apache.org/viewvc/incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/service/StorageService.java?rev=892931&r1=892930&r2=892931&view=diff
==============================================================================
--- 
incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/service/StorageService.java
 (original)
+++ 
incubator/cassandra/branches/cassandra-0.5/src/java/org/apache/cassandra/service/StorageService.java
 Mon Dec 21 19:11:18 2009
@@ -357,17 +357,7 @@
     {
         return endPointSnitch_;
     }
-    
-    /*
-     * Given an InetAddress this method will report if the
-     * endpoint is in the same data center as the local
-     * storage endpoint.
-    */
-    public boolean isInSameDataCenter(InetAddress endpoint) throws IOException
-    {
-        return 
endPointSnitch_.isInSameDataCenter(FBUtilities.getLocalAddress(), endpoint);
-    }
-    
+
     /**
      * This method performs the requisite operations to make
      * sure that the N replicas are in sync. We do this in the
@@ -696,7 +686,7 @@
             // find alive sources for our new ranges
             for (Range myNewRange : myNewRanges)
             {
-                List<InetAddress> sources = 
DatabaseDescriptor.getEndPointSnitch().sortByProximity(myAddress, 
rangeAddresses.get(myNewRange));
+                List<InetAddress> sources = 
DatabaseDescriptor.getEndPointSnitch().getSortedListByProximity(myAddress, 
rangeAddresses.get(myNewRange));
 
                 assert (!sources.contains(myAddress));
 


Reply via email to