diff --git a/src/org/apache/cassandra/locator/RackAwareStrategy.java b/src/org/apache/cassandra/locator/RackAwareStrategy.java index 7b53c683b0..986bd16103 100644 --- a/src/org/apache/cassandra/locator/RackAwareStrategy.java +++ b/src/org/apache/cassandra/locator/RackAwareStrategy.java @@ -30,7 +30,7 @@ public class RackAwareStrategy extends AbstractStrategy public EndPoint[] getStorageEndPoints(BigInteger token) { - int startIndex = 0 ; + int startIndex; List list = new ArrayList(); boolean bDataCenter = false; boolean bOtherRack = false; @@ -52,7 +52,7 @@ public class RackAwareStrategy extends AbstractStrategy foundCount++; if( N == 1 ) { - return list.toArray(new EndPoint[0]); + return list.toArray(new EndPoint[list.size()]); } startIndex = (index + 1)%totalNodes; IEndPointSnitch endPointSnitch = StorageService.instance().getEndPointSnitch(); @@ -84,7 +84,6 @@ public class RackAwareStrategy extends AbstractStrategy bOtherRack = true; foundCount++; } - continue; } } catch (UnknownHostException e) @@ -101,98 +100,20 @@ public class RackAwareStrategy extends AbstractStrategy { list.add(tokenToEndPointMap.get(tokens.get(i))); foundCount++; - continue; } } retrofitPorts(list); - return list.toArray(new EndPoint[0]); + return list.toArray(new EndPoint[list.size()]); } public Map getStorageEndPoints(String[] keys) { Map results = new HashMap(); - List list = new ArrayList(); - int startIndex = 0 ; - int foundCount = 0; - boolean bDataCenter = false; - boolean bOtherRack = false; - - Map tokenToEndPointMap = tokenMetadata_.cloneTokenEndPointMap(); - int N = DatabaseDescriptor.getReplicationFactor(); - List tokens = new ArrayList(tokenToEndPointMap.keySet()); - Collections.sort(tokens); - + for ( String key : keys ) { BigInteger token = StorageService.hash(key); - int index = Collections.binarySearch(tokens, token); - if(index < 0) - { - index = (index + 1) * (-1); - if (index >= tokens.size()) - index = 0; - } - int totalNodes = tokens.size(); - // Add the node at the index by default - list.add(tokenToEndPointMap.get(tokens.get(index))); - foundCount++; - if( N == 1 ) - { - results.put( key, list.toArray(new EndPoint[0]) ); - return results; - } - startIndex = (index + 1)%totalNodes; - IEndPointSnitch endPointSnitch = StorageService.instance().getEndPointSnitch(); - - for (int i = startIndex, count = 1; count < totalNodes && foundCount < N; ++count, i = (i+1)%totalNodes) - { - try - { - // First try to find one in a different data center - if(!endPointSnitch.isInSameDataCenter(tokenToEndPointMap.get(tokens.get(index)), tokenToEndPointMap.get(tokens.get(i)))) - { - // If we have already found something in a diff datacenter no need to find another - if( !bDataCenter ) - { - list.add(tokenToEndPointMap.get(tokens.get(i))); - bDataCenter = true; - foundCount++; - } - continue; - } - // Now try to find one on a different rack - if(!endPointSnitch.isOnSameRack(tokenToEndPointMap.get(tokens.get(index)), tokenToEndPointMap.get(tokens.get(i))) && - endPointSnitch.isInSameDataCenter(tokenToEndPointMap.get(tokens.get(index)), tokenToEndPointMap.get(tokens.get(i)))) - { - // If we have already found something in a diff rack no need to find another - if( !bOtherRack ) - { - list.add(tokenToEndPointMap.get(tokens.get(i))); - bOtherRack = true; - foundCount++; - } - continue; - } - } - catch (UnknownHostException e) - { - logger_.debug(LogUtil.throwableToString(e)); - } - - } - // If we found N number of nodes we are good. This loop wil just exit. Otherwise just - // loop through the list and add until we have N nodes. - for (int i = startIndex, count = 1; count < totalNodes && foundCount < N; ++count, i = (i+1)%totalNodes) - { - if( ! list.contains(tokenToEndPointMap.get(tokens.get(i)))) - { - list.add(tokenToEndPointMap.get(tokens.get(i))); - foundCount++; - continue; - } - } - retrofitPorts(list); - results.put(key, list.toArray(new EndPoint[0])); + results.put(key, getStorageEndPoints(token)); } return results; diff --git a/src/org/apache/cassandra/locator/RackUnawareStrategy.java b/src/org/apache/cassandra/locator/RackUnawareStrategy.java index 8df938ac61..a88986c0a8 100644 --- a/src/org/apache/cassandra/locator/RackUnawareStrategy.java +++ b/src/org/apache/cassandra/locator/RackUnawareStrategy.java @@ -39,7 +39,7 @@ public class RackUnawareStrategy extends AbstractStrategy public EndPoint[] getStorageEndPoints(BigInteger token, Map tokenToEndPointMap) { - int startIndex = 0 ; + int startIndex; List list = new ArrayList(); int foundCount = 0; int N = DatabaseDescriptor.getReplicationFactor(); @@ -65,57 +65,22 @@ public class RackUnawareStrategy extends AbstractStrategy { list.add(tokenToEndPointMap.get(tokens.get(i))); foundCount++; - continue; } } retrofitPorts(list); - return list.toArray(new EndPoint[0]); + return list.toArray(new EndPoint[list.size()]); } public Map getStorageEndPoints(String[] keys) - { - Arrays.sort(keys); - Range[] ranges = StorageService.instance().getAllRanges(); - + { Map results = new HashMap(); - List list = new ArrayList(); - int startIndex = 0 ; - int foundCount = 0; - - Map tokenToEndPointMap = tokenMetadata_.cloneTokenEndPointMap(); - int N = DatabaseDescriptor.getReplicationFactor(); - List tokens = new ArrayList(tokenToEndPointMap.keySet()); - Collections.sort(tokens); + for ( String key : keys ) { BigInteger token = StorageService.hash(key); - int index = Collections.binarySearch(tokens, token); - if(index < 0) - { - index = (index + 1) * (-1); - if (index >= tokens.size()) - index = 0; - } - int totalNodes = tokens.size(); - // Add the node at the index by default - list.add(tokenToEndPointMap.get(tokens.get(index))); - foundCount++; - startIndex = (index + 1)%totalNodes; - // If we found N number of nodes we are good. This loop will just exit. Otherwise just - // loop through the list and add until we have N nodes. - for (int i = startIndex, count = 1; count < totalNodes && foundCount < N; ++count, i = (i+1)%totalNodes) - { - if( ! list.contains(tokenToEndPointMap.get(tokens.get(i)))) - { - list.add(tokenToEndPointMap.get(tokens.get(i))); - foundCount++; - continue; - } - } - retrofitPorts(list); - results.put(key, list.toArray(new EndPoint[0])); + results.put(key, getStorageEndPoints(token)); } - + return results; } }