Search in sources :

Example 6 with StringToken

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());
}
Also used : NetworkTopologyStrategy(org.apache.cassandra.locator.NetworkTopologyStrategy) HashMap(java.util.HashMap) TokenMetadata(org.apache.cassandra.locator.TokenMetadata) KeyspaceMetadata(org.apache.cassandra.schema.KeyspaceMetadata) InetAddress(java.net.InetAddress) StringToken(org.apache.cassandra.dht.OrderPreservingPartitioner.StringToken) Test(org.junit.Test)

Example 7 with StringToken

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());
}
Also used : NetworkTopologyStrategy(org.apache.cassandra.locator.NetworkTopologyStrategy) HashMap(java.util.HashMap) TokenMetadata(org.apache.cassandra.locator.TokenMetadata) KeyspaceMetadata(org.apache.cassandra.schema.KeyspaceMetadata) InetAddress(java.net.InetAddress) StringToken(org.apache.cassandra.dht.OrderPreservingPartitioner.StringToken) Test(org.junit.Test)

Example 8 with StringToken

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());
}
Also used : NetworkTopologyStrategy(org.apache.cassandra.locator.NetworkTopologyStrategy) HashMap(java.util.HashMap) TokenMetadata(org.apache.cassandra.locator.TokenMetadata) KeyspaceMetadata(org.apache.cassandra.schema.KeyspaceMetadata) InetAddress(java.net.InetAddress) StringToken(org.apache.cassandra.dht.OrderPreservingPartitioner.StringToken) Test(org.junit.Test)

Example 9 with StringToken

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]));
}
Also used : ArrayList(java.util.ArrayList) BigIntegerToken(org.apache.cassandra.dht.RandomPartitioner.BigIntegerToken) StringToken(org.apache.cassandra.dht.OrderPreservingPartitioner.StringToken) Token(org.apache.cassandra.dht.Token) StringToken(org.apache.cassandra.dht.OrderPreservingPartitioner.StringToken) IPartitioner(org.apache.cassandra.dht.IPartitioner) Test(org.junit.Test)

Example 10 with StringToken

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);
    }
}
Also used : StringToken(org.apache.cassandra.dht.OrderPreservingPartitioner.StringToken) Token(org.apache.cassandra.dht.Token) StringToken(org.apache.cassandra.dht.OrderPreservingPartitioner.StringToken) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Aggregations

StringToken (org.apache.cassandra.dht.OrderPreservingPartitioner.StringToken)16 Test (org.junit.Test)15 TokenMetadata (org.apache.cassandra.locator.TokenMetadata)11 KeyspaceMetadata (org.apache.cassandra.schema.KeyspaceMetadata)11 InetAddress (java.net.InetAddress)9 Token (org.apache.cassandra.dht.Token)9 Range (org.apache.cassandra.dht.Range)8 LongToken (org.apache.cassandra.dht.Murmur3Partitioner.LongToken)6 HashMap (java.util.HashMap)3 NetworkTopologyStrategy (org.apache.cassandra.locator.NetworkTopologyStrategy)3 ArrayList (java.util.ArrayList)1 IPartitioner (org.apache.cassandra.dht.IPartitioner)1 BigIntegerToken (org.apache.cassandra.dht.RandomPartitioner.BigIntegerToken)1