cassandra-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbel...@apache.org
Subject svn commit: r945911 - in /cassandra/trunk: src/java/org/apache/cassandra/locator/DatacenterShardStrategy.java test/unit/org/apache/cassandra/locator/DatacenterShardStrategyTest.java
Date Tue, 18 May 2010 21:50:53 GMT
Author: jbellis
Date: Tue May 18 21:50:52 2010
New Revision: 945911

URL: http://svn.apache.org/viewvc?rev=945911&view=rev
Log:
clean up DSS: inline getNaturalEndpointsInternal, remove reload-on-every-call, r/m redundant
dcEndpints variable
patch by jbellis; reviewed by Jeremy Hanna for CASSANDRA-1103

Modified:
    cassandra/trunk/src/java/org/apache/cassandra/locator/DatacenterShardStrategy.java
    cassandra/trunk/test/unit/org/apache/cassandra/locator/DatacenterShardStrategyTest.java

Modified: cassandra/trunk/src/java/org/apache/cassandra/locator/DatacenterShardStrategy.java
URL: http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/locator/DatacenterShardStrategy.java?rev=945911&r1=945910&r2=945911&view=diff
==============================================================================
--- cassandra/trunk/src/java/org/apache/cassandra/locator/DatacenterShardStrategy.java (original)
+++ cassandra/trunk/src/java/org/apache/cassandra/locator/DatacenterShardStrategy.java Tue
May 18 21:50:52 2010
@@ -26,7 +26,6 @@ import java.io.IOException;
 import java.io.IOError;
 import java.net.InetAddress;
 import java.net.URL;
-import java.net.UnknownHostException;
 import java.util.*;
 import java.util.Map.Entry;
 
@@ -55,39 +54,9 @@ public class DatacenterShardStrategy ext
 {
     private static final String DATACENTER_PROPERTY_FILENAME = "datacenters.properties";
     private Map<String, List<Token>> dcTokens;
-    private int tokensize = 0;
     private AbstractRackAwareSnitch snitch;
     private Map<String, Map<String, Integer>> datacenters = new HashMap<String,
Map<String, Integer>>();
 
-    private synchronized void loadEndpoints(TokenMetadata metadata) throws UnknownHostException
-    {
-        String localDC = snitch.getDatacenter(DatabaseDescriptor.getListenAddress());
-        assert (localDC != null) : "Invalid configuration, Coldn't find the host: " + FBUtilities.getLocalAddress();
-        // re -init the map
-        dcTokens = new HashMap<String, List<Token>>();
-        List<Token> tokens = metadata.sortedTokens();
-        for (Token token : tokens)
-        {
-            InetAddress endPoint = metadata.getEndpoint(token);
-            String dataCenter = snitch.getDatacenter(endPoint);
-            // add tokens to dcmap.
-            List<Token> lst = dcTokens.get(dataCenter);
-            if (lst == null)
-            {
-                lst = new ArrayList<Token>();
-            }
-            lst.add(token);
-            dcTokens.put(dataCenter, lst);
-        }
-        for (Entry<String, List<Token>> entry : dcTokens.entrySet())
-        {
-            List<Token> valueList = entry.getValue();
-            Collections.sort(valueList);
-            dcTokens.put(entry.getKey(), valueList);
-        }
-        tokensize = tokens.size();
-    }
-
     public DatacenterShardStrategy(TokenMetadata tokenMetadata, IEndpointSnitch snitch) throws
ConfigurationException
     {
         super(tokenMetadata, snitch);
@@ -122,34 +91,48 @@ public class DatacenterShardStrategy ext
         {
             throw new IOError(ioe);
         }
+
+        loadEndpoints(tokenMetadata);
     }
 
-    public ArrayList<InetAddress> getNaturalEndpoints(Token token, TokenMetadata metadata,
String table)
+    private synchronized void loadEndpoints(TokenMetadata metadata) throws ConfigurationException
     {
-        try
+        String localDC = snitch.getDatacenter(DatabaseDescriptor.getListenAddress());
+        if (localDC == null)
+            throw new ConfigurationException("Invalid datacenter configuration; couldn't
find local host " + FBUtilities.getLocalAddress());
+
+        dcTokens = new HashMap<String, List<Token>>();
+        for (Token token : metadata.sortedTokens())
         {
-            return getNaturalEndpointsInternal(token, metadata, table);
+            InetAddress endPoint = metadata.getEndpoint(token);
+            String dataCenter = snitch.getDatacenter(endPoint);
+            // add tokens to dcmap.
+            List<Token> lst = dcTokens.get(dataCenter);
+            if (lst == null)
+            {
+                lst = new ArrayList<Token>();
+            }
+            lst.add(token);
+            dcTokens.put(dataCenter, lst);
         }
-        catch (IOException e)
+        for (Entry<String, List<Token>> entry : dcTokens.entrySet())
         {
-            throw new IOError(e);
+            List<Token> valueList = entry.getValue();
+            Collections.sort(valueList);
+            dcTokens.put(entry.getKey(), valueList);
         }
     }
 
-    private ArrayList<InetAddress> getNaturalEndpointsInternal(Token searchToken, TokenMetadata
metadata, String table) throws UnknownHostException
+    public ArrayList<InetAddress> getNaturalEndpoints(Token searchToken, TokenMetadata
metadata, String table)
     {
         ArrayList<InetAddress> endpoints = new ArrayList<InetAddress>();
 
         if (metadata.sortedTokens().isEmpty())
             return endpoints;
 
-        if (tokensize != metadata.sortedTokens().size())
-            loadEndpoints(metadata);
-
         for (String dc : dcTokens.keySet())
         {
             int replicas = getReplicationFactor(dc, table);
-        	int dcEpCount = 0;
             List<Token> tokens = dcTokens.get(dc);
             boolean bOtherRack = false;
             boolean doneDataCenterItr;
@@ -157,16 +140,14 @@ public class DatacenterShardStrategy ext
             Iterator<Token> iter = TokenMetadata.ringIterator(tokens, searchToken);
             InetAddress primaryHost = metadata.getEndpoint(iter.next());
             endpoints.add(primaryHost);
-            dcEpCount++;
-            
-            while (dcEpCount < replicas && iter.hasNext())
+
+            while (endpoints.size() < replicas && iter.hasNext())
             {
                 Token t = iter.next();
                 InetAddress endPointOfInterest = metadata.getEndpoint(t);
-                if (dcEpCount < replicas - 1)
+                if (endpoints.size() < replicas - 1)
                 {
                     endpoints.add(endPointOfInterest);
-                    dcEpCount++;
                     continue;
                 }
                 else
@@ -180,15 +161,12 @@ public class DatacenterShardStrategy ext
                     if (!snitch.getRack(primaryHost).equals(snitch.getRack(endPointOfInterest)))
                     {
                         endpoints.add(metadata.getEndpoint(t));
-                        dcEpCount++;
                         bOtherRack = true;
                     }
                 }
                 // If both already found exit loop.
                 if (doneDataCenterItr && bOtherRack)
-                {
                     break;
-                }
             }
 
             /*
@@ -196,17 +174,14 @@ public class DatacenterShardStrategy ext
             * exit. Otherwise just loop through the list and add until we
             * have N nodes.
             */
-            if (dcEpCount < replicas)
+            if (endpoints.size() < replicas)
             {
                 iter = TokenMetadata.ringIterator(tokens, searchToken);
-                while (dcEpCount < replicas && iter.hasNext())
+                while (endpoints.size() < replicas && iter.hasNext())
                 {
                     Token t = iter.next();
                     if (!endpoints.contains(metadata.getEndpoint(t)))
-                    {
                     	endpoints.add(metadata.getEndpoint(t));
-                    	dcEpCount++;
-                    }
                 }
             }
         }

Modified: cassandra/trunk/test/unit/org/apache/cassandra/locator/DatacenterShardStrategyTest.java
URL: http://svn.apache.org/viewvc/cassandra/trunk/test/unit/org/apache/cassandra/locator/DatacenterShardStrategyTest.java?rev=945911&r1=945910&r2=945911&view=diff
==============================================================================
--- cassandra/trunk/test/unit/org/apache/cassandra/locator/DatacenterShardStrategyTest.java
(original)
+++ cassandra/trunk/test/unit/org/apache/cassandra/locator/DatacenterShardStrategyTest.java
Tue May 18 21:50:52 2010
@@ -4,6 +4,7 @@ import java.io.IOException;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
 import java.util.ArrayList;
+import java.util.HashSet;
 
 import javax.xml.parsers.ParserConfigurationException;
 
@@ -33,6 +34,7 @@ public class DatacenterShardStrategyTest
         // Query for the natural hosts
         ArrayList<InetAddress> endpoints = strategy.getNaturalEndpoints(new StringToken("123"),
table);
         assert 6 == endpoints.size();
+        assert 6 == new HashSet<InetAddress>(endpoints).size(); // ensure uniqueness
     }
 
     public void createDummyTokens(TokenMetadata metadata) throws UnknownHostException



Mime
View raw message