cassandra-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbel...@apache.org
Subject svn commit: r936016 - in /cassandra/trunk/src/java/org/apache/cassandra: locator/ service/
Date Tue, 20 Apr 2010 18:01:09 GMT
Author: jbellis
Date: Tue Apr 20 18:01:09 2010
New Revision: 936016

URL: http://svn.apache.org/viewvc?rev=936016&view=rev
Log:
replace isOnSameRack and isInSameDataCenter with getRack and getDatacenter (replacing getLocation).
 patch by Erick Tryzelaar; reviewed by jbellis for CASSANDRA-994

Modified:
    cassandra/trunk/src/java/org/apache/cassandra/locator/AbstractRackAwareSnitch.java
    cassandra/trunk/src/java/org/apache/cassandra/locator/DatacenterEndpointSnitch.java
    cassandra/trunk/src/java/org/apache/cassandra/locator/DatacenterShardStategy.java
    cassandra/trunk/src/java/org/apache/cassandra/locator/PropertyFileSnitch.java
    cassandra/trunk/src/java/org/apache/cassandra/locator/RackAwareStrategy.java
    cassandra/trunk/src/java/org/apache/cassandra/locator/RackInferringSnitch.java
    cassandra/trunk/src/java/org/apache/cassandra/service/DatacenterSyncWriteResponseHandler.java
    cassandra/trunk/src/java/org/apache/cassandra/service/DatacenterWriteResponseHandler.java

Modified: cassandra/trunk/src/java/org/apache/cassandra/locator/AbstractRackAwareSnitch.java
URL: http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/locator/AbstractRackAwareSnitch.java?rev=936016&r1=936015&r2=936016&view=diff
==============================================================================
--- cassandra/trunk/src/java/org/apache/cassandra/locator/AbstractRackAwareSnitch.java (original)
+++ cassandra/trunk/src/java/org/apache/cassandra/locator/AbstractRackAwareSnitch.java Tue
Apr 20 18:01:09 2010
@@ -33,29 +33,20 @@ import java.util.*;
 public abstract class AbstractRackAwareSnitch implements IEndpointSnitch
 {
     /**
-     * Determines 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
+     * Return the rack for which an endpoint resides in
+     * @param endpoint a specified endpoint
+     * @return string of rack
      * @throws UnknownHostException
      */
-    abstract public boolean isOnSameRack(InetAddress host, InetAddress host2) throws UnknownHostException;
+    abstract public String getRack(InetAddress endpoint) throws UnknownHostException;
 
     /**
-     * Determines 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
+     * Return the data center for which an endpoint resides in
+     * @param endpoint a specified endpoint
+     * @return string of data center
      * @throws UnknownHostException
      */
-    abstract public boolean isInSameDataCenter(InetAddress host, InetAddress host2) throws
UnknownHostException;
-
-    /**
-     * Determines the name of the datacenter this endpoint lives in.
-     * @param endpoint
-     * @return the name of the datacenter the endpoint lives in
-     */
-    abstract public String getLocation(InetAddress endpoint) throws UnknownHostException;
+    abstract public String getDatacenter(InetAddress endpoint) throws UnknownHostException;
 
     public List<InetAddress> getSortedListByProximity(final InetAddress address, Collection<InetAddress>
unsortedAddress)
     {
@@ -76,14 +67,23 @@ public abstract class AbstractRackAwareS
                         return -1;
                     if (address.equals(a2) && !address.equals(a1))
                         return 1;
-                    if (isOnSameRack(address, a1) && !isOnSameRack(address, a2))
+
+                    String addressRack = getRack(address);
+                    String a1Rack = getRack(a1);
+                    String a2Rack = getRack(a2);
+                    if (addressRack.equals(a1Rack) && !addressRack.equals(a2Rack))
                         return -1;
-                    if (isOnSameRack(address, a2) && !isOnSameRack(address, a1))
+                    if (addressRack.equals(a2Rack) && !addressRack.equals(a1Rack))
                         return 1;
-                    if (isInSameDataCenter(address, a1) && !isInSameDataCenter(address,
a2))
+
+                    String addressDatacenter = getDatacenter(address);
+                    String a1Datacenter = getDatacenter(a1);
+                    String a2Datacenter = getDatacenter(a2);
+                    if (addressDatacenter.equals(a1Datacenter) && !addressDatacenter.equals(a2Datacenter))
                         return -1;
-                    if (isInSameDataCenter(address, a2) && !isInSameDataCenter(address,
a1))
+                    if (addressDatacenter.equals(a2Datacenter) && !addressDatacenter.equals(a1Datacenter))
                         return 1;
+
                     return 0;
                 }
                 catch (UnknownHostException e)

Modified: cassandra/trunk/src/java/org/apache/cassandra/locator/DatacenterEndpointSnitch.java
URL: http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/locator/DatacenterEndpointSnitch.java?rev=936016&r1=936015&r2=936016&view=diff
==============================================================================
--- cassandra/trunk/src/java/org/apache/cassandra/locator/DatacenterEndpointSnitch.java (original)
+++ cassandra/trunk/src/java/org/apache/cassandra/locator/DatacenterEndpointSnitch.java Tue
Apr 20 18:01:09 2010
@@ -75,11 +75,19 @@ public class DatacenterEndpointSnitch ex
     /**
      * Return the rack for which an endpoint resides in
      */
-    public String getRackForEndpoint(InetAddress endpoint)
-            throws UnknownHostException
+    public String getRack(InetAddress endpoint) throws UnknownHostException
     {
-        byte[] ipQuads = getIPAddress(endpoint.getHostAddress());
-        return ipRAC.get(ipQuads[1]).get(ipQuads[2]);
+        byte[] ip = getIPAddress(endpoint.getHostAddress());
+        return ipRAC.get(ip[1]).get(ip[2]);
+    }
+
+    /**
+     * Return the datacenter for which an endpoint resides in
+     */
+    public String getDatacenter(InetAddress endpoint) throws UnknownHostException
+    {
+        byte[] ip = getIPAddress(endpoint.getHostAddress());
+        return ipDC.get(ip[1]).get(ip[2]);
     }
 
     /**
@@ -135,42 +143,6 @@ public class DatacenterEndpointSnitch ex
     }
 
     /**
-     * This methood will return ture if the hosts are in the same RAC else
-     * false.
-     */
-    public boolean isOnSameRack(InetAddress host, InetAddress host2)
-            throws UnknownHostException
-    {
-        /*
-        * Look at the IP Address of the two hosts. Compare the 2nd and 3rd
-        * octet. If they are the same then the hosts are in the same rack else
-        * different racks.
-        */
-        byte[] ip = getIPAddress(host.getHostAddress());
-        byte[] ip2 = getIPAddress(host2.getHostAddress());
-
-        return ipRAC.get(ip[1]).get(ip[2])
-                .equals(ipRAC.get(ip2[1]).get(ip2[2]));
-    }
-
-    /**
-     * This methood will return ture if the hosts are in the same DC else false.
-     */
-    public boolean isInSameDataCenter(InetAddress host, InetAddress host2)
-            throws UnknownHostException
-    {
-        /*
-        * Look at the IP Address of the two hosts. Compare the 2nd and 3rd
-        * octet and get the DC Name. If they are the same then the hosts are in
-        * the same datacenter else different datacenter.
-        */
-        byte[] ip = getIPAddress(host.getHostAddress());
-        byte[] ip2 = getIPAddress(host2.getHostAddress());
-
-        return ipDC.get(ip[1]).get(ip[2]).equals(ipDC.get(ip2[1]).get(ip2[2]));
-    }
-
-    /**
      * Returns a DC replication map, the key will be the dc name and the value
      * will be the replication factor of that Datacenter.
      */
@@ -198,10 +170,4 @@ public class DatacenterEndpointSnitch ex
     {
         return (byte) (n & 0x000000ff);
     }
-
-    public String getLocation(InetAddress endpoint) throws UnknownHostException
-    {
-        byte[] ipQuads = getIPAddress(endpoint.getHostAddress());
-        return ipDC.get(ipQuads[1]).get(ipQuads[2]);
-    }
 }

Modified: cassandra/trunk/src/java/org/apache/cassandra/locator/DatacenterShardStategy.java
URL: http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/locator/DatacenterShardStategy.java?rev=936016&r1=936015&r2=936016&view=diff
==============================================================================
--- cassandra/trunk/src/java/org/apache/cassandra/locator/DatacenterShardStategy.java (original)
+++ cassandra/trunk/src/java/org/apache/cassandra/locator/DatacenterShardStategy.java Tue
Apr 20 18:01:09 2010
@@ -68,12 +68,12 @@ public class DatacenterShardStategy exte
     private synchronized void loadEndpoints(TokenMetadata metadata) throws IOException
     {
         this.tokens = new ArrayList<Token>(metadata.sortedTokens());
-        String localDC = ((DatacenterEndpointSnitch)snitch_).getLocation(InetAddress.getLocalHost());
+        String localDC = ((DatacenterEndpointSnitch)snitch_).getDatacenter(InetAddress.getLocalHost());
         dcMap = new HashMap<String, List<Token>>();
         for (Token token : this.tokens)
         {
             InetAddress endpoint = metadata.getEndpoint(token);
-            String dataCenter = ((DatacenterEndpointSnitch)snitch_).getLocation(endpoint);
+            String dataCenter = ((DatacenterEndpointSnitch)snitch_).getDatacenter(endpoint);
             if (dataCenter.equals(localDC))
             {
                 localEndpoints.add(endpoint);
@@ -168,7 +168,8 @@ public class DatacenterShardStategy exte
                 // Now try to find one on a different rack
                 if (!bOtherRack)
                 {
-                    if (!((DatacenterEndpointSnitch)snitch_).isOnSameRack(primaryHost, endpointOfInterest))
+                    AbstractRackAwareSnitch snitch = (AbstractRackAwareSnitch)snitch_;
+                    if (!snitch.getRack(primaryHost).equals(snitch.getRack(endpointOfInterest)))
                     {
                         forloopReturn.add(metadata.getEndpoint(t));
                         bOtherRack = true;

Modified: cassandra/trunk/src/java/org/apache/cassandra/locator/PropertyFileSnitch.java
URL: http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/locator/PropertyFileSnitch.java?rev=936016&r1=936015&r2=936016&view=diff
==============================================================================
--- cassandra/trunk/src/java/org/apache/cassandra/locator/PropertyFileSnitch.java (original)
+++ cassandra/trunk/src/java/org/apache/cassandra/locator/PropertyFileSnitch.java Tue Apr
20 18:01:09 2010
@@ -54,11 +54,6 @@ public class PropertyFileSnitch extends 
     private static String RACK_PROPERTY_FILENAME = "cassandra-rack.properties";
 
     /**
-     * Whether to use the parent for detection of same node
-     */
-    private boolean runInBaseMode = false;
-
-    /**
      * Reference to the logger.
      */
     private static Logger logger_ = LoggerFactory.getLogger(PropertyFileSnitch.class);
@@ -107,7 +102,7 @@ public class PropertyFileSnitch extends 
      * @param endpoint the endpoint to process
      * @return string of data center
      */
-    public String getDataCenterForEndpoint(InetAddress endpoint) {
+    public String getDatacenter(InetAddress endpoint) {
         return getEndpointInfo(endpoint)[0];
     }
 
@@ -118,34 +113,10 @@ public class PropertyFileSnitch extends 
      * 
      * @return string of rack
      */
-    public String getRackForEndpoint(InetAddress endpoint) {
+    public String getRack(InetAddress endpoint) {
         return getEndpointInfo(endpoint)[1];
     }
 
-    @Override
-    public boolean isInSameDataCenter(InetAddress host, InetAddress host2)
-            throws UnknownHostException {
-        if (runInBaseMode)
-        {
-            return super.isInSameDataCenter(host, host2);
-        }
-        return getDataCenterForEndpoint(host).equals(getDataCenterForEndpoint(host2));
-    }
-
-    @Override
-    public boolean isOnSameRack(InetAddress host, InetAddress host2)
-            throws UnknownHostException {
-        if (runInBaseMode)
-        {
-            return super.isOnSameRack(host, host2);
-        }
-        if (!isInSameDataCenter(host, host2))
-        {
-            return false;
-        }
-        return getRackForEndpoint(host).equals(getRackForEndpoint(host2));
-    }
-
     public String displayConfiguration() {
         StringBuffer configurationString = new StringBuffer("Current rack configuration\n=================\n");
         for (Object key: hostProperties.keySet()) {
@@ -170,7 +141,6 @@ public class PropertyFileSnitch extends 
             Properties localHostProperties = new Properties();
             localHostProperties.load(new FileReader(rackPropertyFilename));
             hostProperties = localHostProperties;
-            runInBaseMode = false;
         }
         catch (IOException ioe)
         {

Modified: cassandra/trunk/src/java/org/apache/cassandra/locator/RackAwareStrategy.java
URL: http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/locator/RackAwareStrategy.java?rev=936016&r1=936015&r2=936016&view=diff
==============================================================================
--- cassandra/trunk/src/java/org/apache/cassandra/locator/RackAwareStrategy.java (original)
+++ cassandra/trunk/src/java/org/apache/cassandra/locator/RackAwareStrategy.java Tue Apr 20
18:01:09 2010
@@ -62,9 +62,11 @@ public class RackAwareStrategy extends A
         {
             try
             {
+                AbstractRackAwareSnitch snitch = (AbstractRackAwareSnitch)snitch_;
+
                 // First try to find one in a different data center
                 Token t = iter.next();
-                if (!((AbstractRackAwareSnitch)snitch_).isInSameDataCenter(metadata.getEndpoint(primaryToken),
metadata.getEndpoint(t)))
+                if (!snitch.getDatacenter(metadata.getEndpoint(primaryToken)).equals(snitch.getDatacenter(metadata.getEndpoint(t))))
                 {
                     // If we have already found something in a diff datacenter no need to
find another
                     if (!bDataCenter)
@@ -75,8 +77,8 @@ public class RackAwareStrategy extends A
                     continue;
                 }
                 // Now  try to find one on a different rack
-                if (!((AbstractRackAwareSnitch)snitch_).isOnSameRack(metadata.getEndpoint(primaryToken),
metadata.getEndpoint(t)) &&
-                    ((AbstractRackAwareSnitch)snitch_).isInSameDataCenter(metadata.getEndpoint(primaryToken),
metadata.getEndpoint(t)))
+                if (!snitch.getRack(metadata.getEndpoint(primaryToken)).equals(snitch.getRack(metadata.getEndpoint(t)))
&&
+                    snitch.getDatacenter(metadata.getEndpoint(primaryToken)).equals(snitch.getDatacenter(metadata.getEndpoint(t))))
                 {
                     // If we have already found something in a diff rack no need to find
another
                     if (!bOtherRack)

Modified: cassandra/trunk/src/java/org/apache/cassandra/locator/RackInferringSnitch.java
URL: http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/locator/RackInferringSnitch.java?rev=936016&r1=936015&r2=936016&view=diff
==============================================================================
--- cassandra/trunk/src/java/org/apache/cassandra/locator/RackInferringSnitch.java (original)
+++ cassandra/trunk/src/java/org/apache/cassandra/locator/RackInferringSnitch.java Tue Apr
20 18:01:09 2010
@@ -24,39 +24,17 @@ import java.util.*;
 
 /**
  * A simple endpoint snitch implementation that assumes rack and dc information is encoded
- * in the ip address.
+ * in the 2nd and 3rd octets of the ip address, respectively.
  */
 public class RackInferringSnitch extends AbstractRackAwareSnitch
 {
-    public boolean isOnSameRack(InetAddress host, InetAddress host2) throws UnknownHostException
+    public String getRack(InetAddress endpoint) throws UnknownHostException
     {
-        /*
-         * Look at the IP Address of the two hosts. Compare
-         * the 3rd octet. If they are the same then the hosts
-         * are in the same rack else different racks.
-        */
-        byte[] ip = host.getAddress();
-        byte[] ip2 = host2.getAddress();
-
-        return ip[2] == ip2[2];
-    }
-
-    public boolean isInSameDataCenter(InetAddress host, InetAddress host2) throws UnknownHostException
-    {
-        /*
-         * Look at the IP Address of the two hosts. Compare
-         * the 2nd octet. If they are the same then the hosts
-         * are in the same datacenter else different datacenter.
-        */
-        byte[] ip = host.getAddress();
-        byte[] ip2 = host2.getAddress();
-
-        return ip[1] == ip2[1];
+        return Byte.toString(endpoint.getAddress()[2]);
     }
 
-    public String getLocation(InetAddress endpoint) throws UnknownHostException
+    public String getDatacenter(InetAddress endpoint) throws UnknownHostException
     {
-        throw new UnknownHostException("Not Supported");
+        return Byte.toString(endpoint.getAddress()[1]);
     }
-
 }

Modified: cassandra/trunk/src/java/org/apache/cassandra/service/DatacenterSyncWriteResponseHandler.java
URL: http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/service/DatacenterSyncWriteResponseHandler.java?rev=936016&r1=936015&r2=936016&view=diff
==============================================================================
--- cassandra/trunk/src/java/org/apache/cassandra/service/DatacenterSyncWriteResponseHandler.java
(original)
+++ cassandra/trunk/src/java/org/apache/cassandra/service/DatacenterSyncWriteResponseHandler.java
Tue Apr 20 18:01:09 2010
@@ -59,7 +59,7 @@ public class DatacenterSyncWriteResponse
     {
         try
         {
-            String dataCenter = endpointSnitch.getLocation(message.getFrom());
+            String dataCenter = endpointSnitch.getDatacenter(message.getFrom());
             Object blockFor = responseCounts.get(dataCenter);
             // If this DC needs to be blocked then do the below.
             if (blockFor != null)

Modified: cassandra/trunk/src/java/org/apache/cassandra/service/DatacenterWriteResponseHandler.java
URL: http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/service/DatacenterWriteResponseHandler.java?rev=936016&r1=936015&r2=936016&view=diff
==============================================================================
--- cassandra/trunk/src/java/org/apache/cassandra/service/DatacenterWriteResponseHandler.java
(original)
+++ cassandra/trunk/src/java/org/apache/cassandra/service/DatacenterWriteResponseHandler.java
Tue Apr 20 18:01:09 2010
@@ -60,7 +60,7 @@ public class DatacenterWriteResponseHand
         int b = -1;
         try
         {
-            if (endpointsnitch.isInSameDataCenter(localEndpoint, message.getFrom()))
+            if (endpointsnitch.getDatacenter(localEndpoint).equals(endpointsnitch.getDatacenter(message.getFrom())))
             {
                 b = blockFor.decrementAndGet();
             }



Mime
View raw message