cassandra-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbel...@apache.org
Subject svn commit: r832273 - in /incubator/cassandra/trunk/src/java/org/apache/cassandra: dht/BootStrapper.java locator/DatacenterEndPointSnitch.java locator/EndPointSnitch.java locator/IEndPointSnitch.java
Date Tue, 03 Nov 2009 02:27:50 GMT
Author: jbellis
Date: Tue Nov  3 02:27:50 2009
New Revision: 832273

URL: http://svn.apache.org/viewvc?rev=832273&view=rev
Log:
add sortByProximity and use to prefer bootstrap from closer nodes.
patch by jbellis and Vijay Parthasarathy for CASSANDRA-513

Modified:
    incubator/cassandra/trunk/src/java/org/apache/cassandra/dht/BootStrapper.java
    incubator/cassandra/trunk/src/java/org/apache/cassandra/locator/DatacenterEndPointSnitch.java
    incubator/cassandra/trunk/src/java/org/apache/cassandra/locator/EndPointSnitch.java
    incubator/cassandra/trunk/src/java/org/apache/cassandra/locator/IEndPointSnitch.java

Modified: incubator/cassandra/trunk/src/java/org/apache/cassandra/dht/BootStrapper.java
URL: http://svn.apache.org/viewvc/incubator/cassandra/trunk/src/java/org/apache/cassandra/dht/BootStrapper.java?rev=832273&r1=832272&r2=832273&view=diff
==============================================================================
--- incubator/cassandra/trunk/src/java/org/apache/cassandra/dht/BootStrapper.java (original)
+++ incubator/cassandra/trunk/src/java/org/apache/cassandra/dht/BootStrapper.java Tue Nov
 3 02:27:50 2009
@@ -148,6 +148,7 @@
         }
     }
 
+    /** get potential sources for each range, ordered by proximity (as determined by EndPointSnitch)
*/
     Multimap<Range, InetAddress> getRangesWithSources()
     {
         TokenMetadata temp = tokenMetadata.cloneMe();
@@ -155,7 +156,7 @@
         temp.update(token, address);
         Collection<Range> myRanges = replicationStrategy.getAddressRanges(temp).get(address);
 
-        Multimap<Range, InetAddress> myRangeAddresses = HashMultimap.create();
+        Multimap<Range, InetAddress> myRangeAddresses = ArrayListMultimap.create();
         Multimap<Range, InetAddress> rangeAddresses = replicationStrategy.getRangeAddresses(tokenMetadata);
         for (Range range : rangeAddresses.keySet())
         {
@@ -163,7 +164,8 @@
             {
                 if (range.contains(myRange.right()))
                 {
-                    myRangeAddresses.putAll(myRange, rangeAddresses.get(range));
+                    List<InetAddress> preferred = DatabaseDescriptor.getEndPointSnitch().sortByProximity(address,
rangeAddresses.get(range));
+                    myRangeAddresses.putAll(myRange, preferred);
                     break;
                 }
             }

Modified: incubator/cassandra/trunk/src/java/org/apache/cassandra/locator/DatacenterEndPointSnitch.java
URL: http://svn.apache.org/viewvc/incubator/cassandra/trunk/src/java/org/apache/cassandra/locator/DatacenterEndPointSnitch.java?rev=832273&r1=832272&r2=832273&view=diff
==============================================================================
--- incubator/cassandra/trunk/src/java/org/apache/cassandra/locator/DatacenterEndPointSnitch.java
(original)
+++ incubator/cassandra/trunk/src/java/org/apache/cassandra/locator/DatacenterEndPointSnitch.java
Tue Nov  3 02:27:50 2009
@@ -3,8 +3,7 @@
 import java.io.IOException;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
-import java.util.HashMap;
-import java.util.Map;
+import java.util.*;
 import javax.xml.parsers.ParserConfigurationException;
 
 import org.apache.log4j.Logger;
@@ -40,14 +39,12 @@
     /**
      * Reference to the logger.
      */
-    private static Logger logger_ = Logger
-            .getLogger(DatacenterEndPointSnitch.class);
+    private static Logger logger_ = Logger.getLogger(DatacenterEndPointSnitch.class);
 
     /**
      * Constructor, intialize XML config and read the config in...
      */
-    public DatacenterEndPointSnitch() throws IOException,
-                                             ParserConfigurationException, SAXException
+    public DatacenterEndPointSnitch() throws IOException, ParserConfigurationException, SAXException
     {
         xmlUtils = new XMLUtils(DEFAULT_RACK_CONFIG_FILE);
         reloadConfiguration();
@@ -185,4 +182,34 @@
         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 (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/trunk/src/java/org/apache/cassandra/locator/EndPointSnitch.java
URL: http://svn.apache.org/viewvc/incubator/cassandra/trunk/src/java/org/apache/cassandra/locator/EndPointSnitch.java?rev=832273&r1=832272&r2=832273&view=diff
==============================================================================
--- incubator/cassandra/trunk/src/java/org/apache/cassandra/locator/EndPointSnitch.java (original)
+++ incubator/cassandra/trunk/src/java/org/apache/cassandra/locator/EndPointSnitch.java Tue
Nov  3 02:27:50 2009
@@ -20,6 +20,7 @@
 
 import java.net.InetAddress;
 import java.net.UnknownHostException;
+import java.util.*;
 
 public class EndPointSnitch implements IEndPointSnitch
 {
@@ -53,4 +54,33 @@
     {
         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 (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/trunk/src/java/org/apache/cassandra/locator/IEndPointSnitch.java
URL: http://svn.apache.org/viewvc/incubator/cassandra/trunk/src/java/org/apache/cassandra/locator/IEndPointSnitch.java?rev=832273&r1=832272&r2=832273&view=diff
==============================================================================
--- incubator/cassandra/trunk/src/java/org/apache/cassandra/locator/IEndPointSnitch.java (original)
+++ incubator/cassandra/trunk/src/java/org/apache/cassandra/locator/IEndPointSnitch.java Tue
Nov  3 02:27:50 2009
@@ -21,6 +21,9 @@
 import java.net.UnknownHostException;
 
 import java.net.InetAddress;
+import java.util.Set;
+import java.util.List;
+import java.util.Collection;
 
 
 /**
@@ -55,4 +58,9 @@
      * Given endpoints this method will help us know the datacenter name where the node is
located at.
      */
     public String getLocation(InetAddress endpoint) throws UnknownHostException;
+
+    /**
+     * This method will sort the Set<InetAddress> according to the proximity of the
given address.
+     */
+    public List<InetAddress> sortByProximity(InetAddress address, Collection<InetAddress>
unsortedAddress);
 }



Mime
View raw message