use of org.apache.cassandra.dht.OrderPreservingPartitioner.StringToken in project cassandra by apache.
the class ViewUtilsTest method testBaseTokenDoesNotBelongToLocalReplicaShouldReturnEmpty.
@Test
public void testBaseTokenDoesNotBelongToLocalReplicaShouldReturnEmpty() throws Exception {
TokenMetadata metadata = StorageService.instance.getTokenMetadata();
metadata.clearUnsafe();
// DC1
metadata.updateNormalToken(new StringToken("A"), InetAddress.getByName("127.0.0.1"));
metadata.updateNormalToken(new StringToken("C"), InetAddress.getByName("127.0.0.2"));
// DC2
metadata.updateNormalToken(new StringToken("B"), InetAddress.getByName("127.0.0.4"));
metadata.updateNormalToken(new StringToken("D"), InetAddress.getByName("127.0.0.5"));
Map<String, String> replicationMap = new HashMap<>();
replicationMap.put(ReplicationParams.CLASS, NetworkTopologyStrategy.class.getName());
replicationMap.put("DC1", "1");
replicationMap.put("DC2", "1");
Keyspace.clear("Keyspace1");
KeyspaceMetadata meta = KeyspaceMetadata.create("Keyspace1", KeyspaceParams.create(false, replicationMap));
Schema.instance.load(meta);
Optional<InetAddress> naturalEndpoint = ViewUtils.getViewNaturalEndpoint("Keyspace1", new StringToken("AB"), new StringToken("BB"));
Assert.assertFalse(naturalEndpoint.isPresent());
}
use of org.apache.cassandra.dht.OrderPreservingPartitioner.StringToken in project cassandra by apache.
the class ViewUtilsTest method testGetIndexNaturalEndpoint.
@Test
public void testGetIndexNaturalEndpoint() throws Exception {
TokenMetadata metadata = StorageService.instance.getTokenMetadata();
metadata.clearUnsafe();
// DC1
metadata.updateNormalToken(new StringToken("A"), InetAddress.getByName("127.0.0.1"));
metadata.updateNormalToken(new StringToken("C"), InetAddress.getByName("127.0.0.2"));
// DC2
metadata.updateNormalToken(new StringToken("B"), InetAddress.getByName("127.0.0.4"));
metadata.updateNormalToken(new StringToken("D"), InetAddress.getByName("127.0.0.5"));
Map<String, String> replicationMap = new HashMap<>();
replicationMap.put(ReplicationParams.CLASS, NetworkTopologyStrategy.class.getName());
replicationMap.put("DC1", "1");
replicationMap.put("DC2", "1");
Keyspace.clear("Keyspace1");
KeyspaceMetadata meta = KeyspaceMetadata.create("Keyspace1", KeyspaceParams.create(false, replicationMap));
Schema.instance.load(meta);
Optional<InetAddress> naturalEndpoint = ViewUtils.getViewNaturalEndpoint("Keyspace1", new StringToken("CA"), new StringToken("BB"));
Assert.assertTrue(naturalEndpoint.isPresent());
Assert.assertEquals(InetAddress.getByName("127.0.0.2"), naturalEndpoint.get());
}
use of org.apache.cassandra.dht.OrderPreservingPartitioner.StringToken in project cassandra by apache.
the class ViewUtilsTest method testLocalHostPreference.
@Test
public void testLocalHostPreference() throws Exception {
TokenMetadata metadata = StorageService.instance.getTokenMetadata();
metadata.clearUnsafe();
// DC1
metadata.updateNormalToken(new StringToken("A"), InetAddress.getByName("127.0.0.1"));
metadata.updateNormalToken(new StringToken("C"), InetAddress.getByName("127.0.0.2"));
// DC2
metadata.updateNormalToken(new StringToken("B"), InetAddress.getByName("127.0.0.4"));
metadata.updateNormalToken(new StringToken("D"), InetAddress.getByName("127.0.0.5"));
Map<String, String> replicationMap = new HashMap<>();
replicationMap.put(ReplicationParams.CLASS, NetworkTopologyStrategy.class.getName());
replicationMap.put("DC1", "2");
replicationMap.put("DC2", "2");
Keyspace.clear("Keyspace1");
KeyspaceMetadata meta = KeyspaceMetadata.create("Keyspace1", KeyspaceParams.create(false, replicationMap));
Schema.instance.load(meta);
Optional<InetAddress> naturalEndpoint = ViewUtils.getViewNaturalEndpoint("Keyspace1", new StringToken("CA"), new StringToken("BB"));
Assert.assertTrue(naturalEndpoint.isPresent());
Assert.assertEquals(InetAddress.getByName("127.0.0.1"), naturalEndpoint.get());
}
use of org.apache.cassandra.dht.OrderPreservingPartitioner.StringToken in project cassandra by apache.
the class SimpleStrategyTest method testStringEndpoints.
@Test
public void testStringEndpoints() throws UnknownHostException {
IPartitioner partitioner = OrderPreservingPartitioner.instance;
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.getToken(ByteBufferUtil.bytes(String.valueOf((char) ('a' + i * 2 + 1)))));
}
verifyGetNaturalEndpoints(endpointTokens.toArray(new Token[0]), keyTokens.toArray(new Token[0]));
}
use of org.apache.cassandra.dht.OrderPreservingPartitioner.StringToken in project cassandra by apache.
the class NetworkTopologyStrategyTest method testLargeCluster.
@Test
public void testLargeCluster() throws UnknownHostException, ConfigurationException {
int[] dcRacks = new int[] { 2, 4, 8 };
int[] dcEndpoints = new int[] { 128, 256, 512 };
int[] dcReplication = new int[] { 2, 6, 6 };
IEndpointSnitch snitch = new RackInferringSnitch();
DatabaseDescriptor.setEndpointSnitch(snitch);
TokenMetadata metadata = new TokenMetadata();
Map<String, String> configOptions = new HashMap<String, String>();
Multimap<InetAddress, Token> tokens = HashMultimap.create();
int totalRF = 0;
for (int dc = 0; dc < dcRacks.length; ++dc) {
totalRF += dcReplication[dc];
configOptions.put(Integer.toString(dc), Integer.toString(dcReplication[dc]));
for (int rack = 0; rack < dcRacks[dc]; ++rack) {
for (int ep = 1; ep <= dcEndpoints[dc] / dcRacks[dc]; ++ep) {
byte[] ipBytes = new byte[] { 10, (byte) dc, (byte) rack, (byte) ep };
InetAddress address = InetAddress.getByAddress(ipBytes);
StringToken token = new StringToken(String.format("%02x%02x%02x", ep, rack, dc));
logger.debug("adding node {} at {}", address, token);
tokens.put(address, token);
}
}
}
metadata.updateNormalTokens(tokens);
NetworkTopologyStrategy strategy = new NetworkTopologyStrategy(keyspaceName, metadata, snitch, configOptions);
for (String testToken : new String[] { "123456", "200000", "000402", "ffffff", "400200" }) {
List<InetAddress> endpoints = strategy.calculateNaturalEndpoints(new StringToken(testToken), metadata);
Set<InetAddress> epSet = new HashSet<InetAddress>(endpoints);
Assert.assertEquals(totalRF, endpoints.size());
Assert.assertEquals(totalRF, epSet.size());
logger.debug("{}: {}", testToken, endpoints);
}
}
Aggregations