mirror of https://github.com/apache/cassandra
add test for ReplicaPlacementStrategy covering both Random and OrderPreserving partitioners
patch by jbellis; reviewed by Jun Rao for CASSANDRA-65 git-svn-id: https://svn.apache.org/repos/asf/incubator/cassandra/trunk@769018 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
ec7f04b4df
commit
296e15b63c
|
|
@ -2,7 +2,7 @@ package org.apache.cassandra.dht;
|
|||
|
||||
public class StringToken extends Token<String>
|
||||
{
|
||||
protected StringToken(String token)
|
||||
public StringToken(String token)
|
||||
{
|
||||
super(token);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -867,6 +867,9 @@ public final class StorageService implements IEndPointStateChangeSubscriber, Sto
|
|||
|
||||
public String getToken(EndPoint ep)
|
||||
{
|
||||
// render a String representation of the Token corresponding to this endpoint
|
||||
// for a human-facing UI. If there is no such Token then we use "" since
|
||||
// it is not a valid value either for BigIntegerToken or StringToken.
|
||||
EndPoint ep2 = new EndPoint(ep.getHost(), DatabaseDescriptor.getStoragePort());
|
||||
Token token = tokenMetadata_.getToken(ep2);
|
||||
// if there is no token for an endpoint, return an empty string to denote that
|
||||
|
|
|
|||
|
|
@ -0,0 +1,72 @@
|
|||
package org.apache.cassandra.locator;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.math.BigInteger;
|
||||
|
||||
import org.testng.annotations.Test;
|
||||
import org.apache.cassandra.dht.IPartitioner;
|
||||
import org.apache.cassandra.dht.RandomPartitioner;
|
||||
import org.apache.cassandra.dht.BigIntegerToken;
|
||||
import org.apache.cassandra.dht.Token;
|
||||
import org.apache.cassandra.dht.OrderPreservingPartitioner;
|
||||
import org.apache.cassandra.dht.StringToken;
|
||||
import org.apache.cassandra.net.EndPoint;
|
||||
|
||||
public class RackUnawareStrategyTest
|
||||
{
|
||||
@Test
|
||||
public void testBigIntegerStorageEndPoints()
|
||||
{
|
||||
TokenMetadata tmd = new TokenMetadata();
|
||||
IPartitioner partitioner = new RandomPartitioner();
|
||||
IReplicaPlacementStrategy strategy = new RackUnawareStrategy(tmd, partitioner, 3, 7000);
|
||||
|
||||
List<Token> endPointTokens = new ArrayList<Token>();
|
||||
List<Token> keyTokens = new ArrayList<Token>();
|
||||
for (int i = 0; i < 5; i++) {
|
||||
endPointTokens.add(new BigIntegerToken(String.valueOf(10 * i)));
|
||||
keyTokens.add(new BigIntegerToken(String.valueOf(10 * i + 5)));
|
||||
}
|
||||
testGetStorageEndPoints(tmd, strategy, endPointTokens.toArray(new Token[0]), keyTokens.toArray(new Token[0]));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringStorageEndPoints()
|
||||
{
|
||||
TokenMetadata tmd = new TokenMetadata();
|
||||
IPartitioner partitioner = new OrderPreservingPartitioner();
|
||||
IReplicaPlacementStrategy strategy = new RackUnawareStrategy(tmd, partitioner, 3, 7000);
|
||||
|
||||
List<Token> endPointTokens = new ArrayList<Token>();
|
||||
List<Token> keyTokens = new ArrayList<Token>();
|
||||
for (int i = 0; i < 5; i++) {
|
||||
endPointTokens.add(new StringToken(String.valueOf((char)('a' + i * 2))));
|
||||
keyTokens.add(partitioner.getTokenForKey(String.valueOf((char)('a' + i * 2 + 1))));
|
||||
}
|
||||
testGetStorageEndPoints(tmd, strategy, endPointTokens.toArray(new Token[0]), keyTokens.toArray(new Token[0]));
|
||||
}
|
||||
|
||||
// given a list of endpoint tokens, and a set of key tokens falling between the endpoint tokens,
|
||||
// make sure that the Strategy picks the right endpoints for the keys.
|
||||
private void testGetStorageEndPoints(TokenMetadata tmd, IReplicaPlacementStrategy strategy, Token[] endPointTokens, Token[] keyTokens)
|
||||
{
|
||||
List<EndPoint> hosts = new ArrayList<EndPoint>();
|
||||
for (int i = 0; i < endPointTokens.length; i++)
|
||||
{
|
||||
EndPoint ep = new EndPoint(String.valueOf(i), 7001);
|
||||
tmd.update(endPointTokens[i], ep);
|
||||
hosts.add(ep);
|
||||
}
|
||||
|
||||
for (int i = 0; i < keyTokens.length; i++)
|
||||
{
|
||||
EndPoint[] endPoints = strategy.getStorageEndPoints(keyTokens[i]);
|
||||
assert endPoints.length == 3;
|
||||
for (int j = 0; j < endPoints.length; j++)
|
||||
{
|
||||
assert endPoints[j] == hosts.get((i + j + 1) % hosts.size());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue