Search in sources :

Example 76 with TokenMetadata

use of org.apache.cassandra.locator.TokenMetadata 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"), InetAddressAndPort.getByName("127.0.0.1"));
    metadata.updateNormalToken(new StringToken("C"), InetAddressAndPort.getByName("127.0.0.2"));
    // DC2
    metadata.updateNormalToken(new StringToken("B"), InetAddressAndPort.getByName("127.0.0.4"));
    metadata.updateNormalToken(new StringToken("D"), InetAddressAndPort.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");
    Schema.instance.maybeRemoveKeyspaceInstance("Keyspace1");
    KeyspaceMetadata meta = KeyspaceMetadata.create("Keyspace1", KeyspaceParams.create(false, replicationMap));
    Schema.instance.load(meta);
    Optional<Replica> naturalEndpoint = ViewUtils.getViewNaturalEndpoint(Keyspace.open("Keyspace1").getReplicationStrategy(), 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) Replica(org.apache.cassandra.locator.Replica) StringToken(org.apache.cassandra.dht.OrderPreservingPartitioner.StringToken) Test(org.junit.Test)

Example 77 with TokenMetadata

use of org.apache.cassandra.locator.TokenMetadata in project cassandra by apache.

the class TokenAllocationTest method testAllocateTokensNetworkStrategy.

public void testAllocateTokensNetworkStrategy(int rackCount, int replicas) throws UnknownHostException {
    IEndpointSnitch oldSnitch = DatabaseDescriptor.getEndpointSnitch();
    try {
        DatabaseDescriptor.setEndpointSnitch(new RackInferringSnitch());
        int vn = 16;
        String ks = "TokenAllocationTestNTSKeyspace" + rackCount + replicas;
        String dc = "1";
        // Register peers with expected DC for NetworkTopologyStrategy.
        TokenMetadata metadata = StorageService.instance.getTokenMetadata();
        metadata.clearUnsafe();
        metadata.updateHostId(UUID.randomUUID(), InetAddressAndPort.getByName("127.1.0.99"));
        metadata.updateHostId(UUID.randomUUID(), InetAddressAndPort.getByName("127.15.0.99"));
        SchemaLoader.createKeyspace(ks, KeyspaceParams.nts(dc, replicas, "15", 15), SchemaLoader.standardCFMD(ks, "Standard1"));
        TokenMetadata tm = StorageService.instance.getTokenMetadata();
        tm.clearUnsafe();
        for (int i = 0; i < rackCount; ++i) generateFakeEndpoints(tm, 10, vn, dc, Integer.toString(i));
        InetAddressAndPort addr = InetAddressAndPort.getByName("127." + dc + ".0.99");
        allocateTokensForKeyspace(vn, ks, tm, addr);
    // Note: Not matching replication factor in second datacentre, but this should not affect us.
    } finally {
        DatabaseDescriptor.setEndpointSnitch(oldSnitch);
    }
}
Also used : InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) TokenMetadata(org.apache.cassandra.locator.TokenMetadata) IEndpointSnitch(org.apache.cassandra.locator.IEndpointSnitch) RackInferringSnitch(org.apache.cassandra.locator.RackInferringSnitch)

Example 78 with TokenMetadata

use of org.apache.cassandra.locator.TokenMetadata in project cassandra by apache.

the class TokenAllocationTest method testAllocateTokensForLocalRF.

@Test
public void testAllocateTokensForLocalRF() throws UnknownHostException {
    int vn = 16;
    int allocateTokensForLocalRf = 3;
    TokenMetadata tm = new TokenMetadata();
    generateFakeEndpoints(tm, 10, vn);
    InetAddressAndPort addr = FBUtilities.getBroadcastAddressAndPort();
    allocateTokensForLocalReplicationFactor(vn, allocateTokensForLocalRf, tm, addr);
}
Also used : InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) TokenMetadata(org.apache.cassandra.locator.TokenMetadata) Test(org.junit.Test)

Example 79 with TokenMetadata

use of org.apache.cassandra.locator.TokenMetadata in project cassandra by apache.

the class TokenAllocationTest method testAllocateTokensMultipleKeyspaces.

/**
 * TODO: This scenario isn't supported very well. Investigate a multi-keyspace version of the algorithm.
 */
@Test
public void testAllocateTokensMultipleKeyspaces() throws UnknownHostException {
    final int TOKENS = 16;
    TokenMetadata tokenMetadata = new TokenMetadata();
    generateFakeEndpoints(tokenMetadata, 10, TOKENS);
    // Do not clone token metadata so tokens allocated by different allocators are reflected on the parent TokenMetadata
    TokenAllocation rf2Allocator = createForTest(tokenMetadata, 2, TOKENS);
    TokenAllocation rf3Allocator = createForTest(tokenMetadata, 3, TOKENS);
    SummaryStatistics rf2StatsBefore = rf2Allocator.getAllocationRingOwnership(FBUtilities.getBroadcastAddressAndPort());
    SummaryStatistics rf3StatsBefore = rf3Allocator.getAllocationRingOwnership(FBUtilities.getBroadcastAddressAndPort());
    TokenAllocation current = rf3Allocator;
    TokenAllocation next = rf2Allocator;
    for (int i = 11; i <= 20; ++i) {
        InetAddressAndPort endpoint = InetAddressAndPort.getByName("127.0.0." + (i + 1));
        Collection<Token> tokens = current.allocate(endpoint);
        // Update tokens on next to verify ownership calculation below
        next.tokenMetadata.updateNormalTokens(tokens, endpoint);
        TokenAllocation tmp = current;
        current = next;
        next = tmp;
    }
    SummaryStatistics rf2StatsAfter = rf2Allocator.getAllocationRingOwnership(FBUtilities.getBroadcastAddressAndPort());
    SummaryStatistics rf3StatsAfter = rf3Allocator.getAllocationRingOwnership(FBUtilities.getBroadcastAddressAndPort());
    verifyImprovement(rf2StatsBefore, rf2StatsAfter);
    verifyImprovement(rf3StatsBefore, rf3StatsAfter);
}
Also used : InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) SummaryStatistics(org.apache.commons.math3.stat.descriptive.SummaryStatistics) Token(org.apache.cassandra.dht.Token) TokenMetadata(org.apache.cassandra.locator.TokenMetadata) Test(org.junit.Test)

Example 80 with TokenMetadata

use of org.apache.cassandra.locator.TokenMetadata in project cassandra by apache.

the class TokenAllocationTest method testAllocateTokensForKeyspace.

@Test
public void testAllocateTokensForKeyspace() throws UnknownHostException {
    int vn = 16;
    String ks = "TokenAllocationTestKeyspace3";
    TokenMetadata tm = new TokenMetadata();
    generateFakeEndpoints(tm, 10, vn);
    InetAddressAndPort addr = FBUtilities.getBroadcastAddressAndPort();
    allocateTokensForKeyspace(vn, ks, tm, addr);
}
Also used : InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) TokenMetadata(org.apache.cassandra.locator.TokenMetadata) Test(org.junit.Test)

Aggregations

TokenMetadata (org.apache.cassandra.locator.TokenMetadata)109 Test (org.junit.Test)66 InetAddressAndPort (org.apache.cassandra.locator.InetAddressAndPort)43 Token (org.apache.cassandra.dht.Token)37 VersionedValue (org.apache.cassandra.gms.VersionedValue)22 InetAddress (java.net.InetAddress)21 Range (org.apache.cassandra.dht.Range)19 AbstractReplicationStrategy (org.apache.cassandra.locator.AbstractReplicationStrategy)19 StringToken (org.apache.cassandra.dht.OrderPreservingPartitioner.StringToken)13 BigIntegerToken (org.apache.cassandra.dht.RandomPartitioner.BigIntegerToken)13 Replica (org.apache.cassandra.locator.Replica)13 RangesAtEndpoint (org.apache.cassandra.locator.RangesAtEndpoint)11 IPartitioner (org.apache.cassandra.dht.IPartitioner)10 LongToken (org.apache.cassandra.dht.Murmur3Partitioner.LongToken)10 SSTableReader (org.apache.cassandra.io.sstable.format.SSTableReader)8 IEndpointSnitch (org.apache.cassandra.locator.IEndpointSnitch)8 RangesByEndpoint (org.apache.cassandra.locator.RangesByEndpoint)8 BeforeClass (org.junit.BeforeClass)8 Multimap (com.google.common.collect.Multimap)7 EndpointsForToken (org.apache.cassandra.locator.EndpointsForToken)7