Search in sources :

Example 1 with TokenMetadata

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

the class BootStrapperTest method testAllocateTokensMultipleKeyspaces.

@Test
public void testAllocateTokensMultipleKeyspaces() throws UnknownHostException {
    // TODO: This scenario isn't supported very well. Investigate a multi-keyspace version of the algorithm.
    int vn = 16;
    // RF = 3
    String ks3 = "BootStrapperTestKeyspace4";
    // RF = 2
    String ks2 = "BootStrapperTestKeyspace5";
    TokenMetadata tm = new TokenMetadata();
    generateFakeEndpoints(tm, 10, vn);
    InetAddress dcaddr = FBUtilities.getBroadcastAddress();
    SummaryStatistics os3 = TokenAllocation.replicatedOwnershipStats(tm, Keyspace.open(ks3).getReplicationStrategy(), dcaddr);
    SummaryStatistics os2 = TokenAllocation.replicatedOwnershipStats(tm, Keyspace.open(ks2).getReplicationStrategy(), dcaddr);
    String cks = ks3;
    String nks = ks2;
    for (int i = 11; i <= 20; ++i) {
        allocateTokensForNode(vn, cks, tm, InetAddress.getByName("127.0.0." + (i + 1)));
        String t = cks;
        cks = nks;
        nks = t;
    }
    SummaryStatistics ns3 = TokenAllocation.replicatedOwnershipStats(tm, Keyspace.open(ks3).getReplicationStrategy(), dcaddr);
    SummaryStatistics ns2 = TokenAllocation.replicatedOwnershipStats(tm, Keyspace.open(ks2).getReplicationStrategy(), dcaddr);
    verifyImprovement(os3, ns3);
    verifyImprovement(os2, ns2);
}
Also used : SummaryStatistics(org.apache.commons.math3.stat.descriptive.SummaryStatistics) TokenMetadata(org.apache.cassandra.locator.TokenMetadata) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 2 with TokenMetadata

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

the class StorageProxyTest method beforeClass.

@BeforeClass
public static void beforeClass() throws Throwable {
    DatabaseDescriptor.daemonInitialization();
    DatabaseDescriptor.getHintsDirectory().mkdir();
    TokenMetadata tmd = StorageService.instance.getTokenMetadata();
    tmd.updateNormalToken(token("1"), InetAddress.getByName("127.0.0.1"));
    tmd.updateNormalToken(token("6"), InetAddress.getByName("127.0.0.6"));
}
Also used : TokenMetadata(org.apache.cassandra.locator.TokenMetadata) BeforeClass(org.junit.BeforeClass)

Example 3 with TokenMetadata

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

the class TokenAllocation method allocateTokens.

public static Collection<Token> allocateTokens(final TokenMetadata tokenMetadata, final AbstractReplicationStrategy rs, final InetAddress endpoint, int numTokens) {
    TokenMetadata tokenMetadataCopy = tokenMetadata.cloneOnlyTokenMap();
    StrategyAdapter strategy = getStrategy(tokenMetadataCopy, rs, endpoint);
    Collection<Token> tokens = create(tokenMetadata, strategy).addUnit(endpoint, numTokens);
    tokens = adjustForCrossDatacenterClashes(tokenMetadata, strategy, tokens);
    if (logger.isWarnEnabled()) {
        logger.warn("Selected tokens {}", tokens);
        SummaryStatistics os = replicatedOwnershipStats(tokenMetadataCopy, rs, endpoint);
        tokenMetadataCopy.updateNormalTokens(tokens, endpoint);
        SummaryStatistics ns = replicatedOwnershipStats(tokenMetadataCopy, rs, endpoint);
        logger.warn("Replicated node load in datacentre before allocation {}", statToString(os));
        logger.warn("Replicated node load in datacentre after allocation {}", statToString(ns));
        // TODO: Is it worth doing the replicated ownership calculation always to be able to raise this alarm?
        if (ns.getStandardDeviation() > os.getStandardDeviation())
            logger.warn("Unexpected growth in standard deviation after allocation.");
    }
    return tokens;
}
Also used : SummaryStatistics(org.apache.commons.math3.stat.descriptive.SummaryStatistics) Token(org.apache.cassandra.dht.Token) TokenMetadata(org.apache.cassandra.locator.TokenMetadata)

Example 4 with TokenMetadata

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

the class ReplicaPlanIterator method getRestrictedRanges.

/**
 * Compute all ranges we're going to query, in sorted order. Nodes can be replica destinations for many ranges,
 * so we need to restrict each scan to the specific range we want, or else we'd get duplicate results.
 */
private static List<AbstractBounds<PartitionPosition>> getRestrictedRanges(final AbstractBounds<PartitionPosition> queryRange) {
    // special case for bounds containing exactly 1 (non-minimum) token
    if (queryRange instanceof Bounds && queryRange.left.equals(queryRange.right) && !queryRange.left.isMinimum()) {
        return Collections.singletonList(queryRange);
    }
    TokenMetadata tokenMetadata = StorageService.instance.getTokenMetadata();
    List<AbstractBounds<PartitionPosition>> ranges = new ArrayList<>();
    // divide the queryRange into pieces delimited by the ring and minimum tokens
    Iterator<Token> ringIter = TokenMetadata.ringIterator(tokenMetadata.sortedTokens(), queryRange.left.getToken(), true);
    AbstractBounds<PartitionPosition> remainder = queryRange;
    while (ringIter.hasNext()) {
        /*
             * remainder is a range/bounds of partition positions and we want to split it with a token. We want to split
             * using the key returned by token.maxKeyBound. For instance, if remainder is [DK(10, 'foo'), DK(20, 'bar')],
             * and we have 3 nodes with tokens 0, 15, 30, we want to split remainder to A=[DK(10, 'foo'), 15] and
             * B=(15, DK(20, 'bar')]. But since we can't mix tokens and keys at the same time in a range, we use
             * 15.maxKeyBound() to have A include all keys having 15 as token and B include none of those (since that is
             * what our node owns).
             */
        Token upperBoundToken = ringIter.next();
        PartitionPosition upperBound = upperBoundToken.maxKeyBound();
        if (!remainder.left.equals(upperBound) && !remainder.contains(upperBound))
            // no more splits
            break;
        Pair<AbstractBounds<PartitionPosition>, AbstractBounds<PartitionPosition>> splits = remainder.split(upperBound);
        if (splits == null)
            continue;
        ranges.add(splits.left);
        remainder = splits.right;
    }
    ranges.add(remainder);
    return ranges;
}
Also used : AbstractBounds(org.apache.cassandra.dht.AbstractBounds) PartitionPosition(org.apache.cassandra.db.PartitionPosition) Bounds(org.apache.cassandra.dht.Bounds) AbstractBounds(org.apache.cassandra.dht.AbstractBounds) ArrayList(java.util.ArrayList) Token(org.apache.cassandra.dht.Token) TokenMetadata(org.apache.cassandra.locator.TokenMetadata)

Example 5 with TokenMetadata

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

the class VerifyTest method testOutOfRangeTokens.

@Test(expected = RuntimeException.class)
public void testOutOfRangeTokens() throws IOException {
    Keyspace keyspace = Keyspace.open(KEYSPACE);
    ColumnFamilyStore cfs = keyspace.getColumnFamilyStore(CF);
    fillCF(cfs, 100);
    TokenMetadata tmd = StorageService.instance.getTokenMetadata();
    byte[] tk1 = new byte[1], tk2 = new byte[1];
    tk1[0] = 2;
    tk2[0] = 1;
    tmd.updateNormalToken(new ByteOrderedPartitioner.BytesToken(tk1), InetAddressAndPort.getByName("127.0.0.1"));
    tmd.updateNormalToken(new ByteOrderedPartitioner.BytesToken(tk2), InetAddressAndPort.getByName("127.0.0.2"));
    SSTableReader sstable = cfs.getLiveSSTables().iterator().next();
    try (Verifier verifier = new Verifier(cfs, sstable, false, Verifier.options().checkOwnsTokens(true).extendedVerification(true).build())) {
        verifier.verify();
    } finally {
        StorageService.instance.getTokenMetadata().clearUnsafe();
    }
}
Also used : SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) SchemaLoader.createKeyspace(org.apache.cassandra.SchemaLoader.createKeyspace) TokenMetadata(org.apache.cassandra.locator.TokenMetadata) Verifier(org.apache.cassandra.db.compaction.Verifier) ByteOrderedPartitioner(org.apache.cassandra.dht.ByteOrderedPartitioner) 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