Search in sources :

Example 11 with TokenRange

use of com.datastax.oss.driver.api.core.metadata.token.TokenRange in project java-driver by datastax.

the class TokenRangeAssert method doesNotIntersect.

public TokenRangeAssert doesNotIntersect(TokenRange... that) {
    for (TokenRange thatRange : that) {
        assertThat(actual.intersects(thatRange)).as("%s should not intersect %s", actual, thatRange).isFalse();
        assertThat(thatRange.intersects(actual)).as("%s should not intersect %s", thatRange, actual).isFalse();
    }
    return this;
}
Also used : TokenRange(com.datastax.oss.driver.api.core.metadata.token.TokenRange)

Example 12 with TokenRange

use of com.datastax.oss.driver.api.core.metadata.token.TokenRange in project java-driver by datastax.

the class TokenRangeTest method should_create_empty_token_ranges_if_too_many_splits.

@Test
public void should_create_empty_token_ranges_if_too_many_splits() {
    TokenRange range = range(0, 10);
    List<TokenRange> ranges = range.splitEvenly(255);
    assertThat(ranges).hasSize(255);
    for (int i = 0; i < ranges.size(); i++) {
        TokenRange tr = ranges.get(i);
        if (i < 10) {
            assertThat(tr).isEqualTo(range(i, i + 1));
        } else {
            assertThat(tr.isEmpty());
        }
    }
}
Also used : TokenRange(com.datastax.oss.driver.api.core.metadata.token.TokenRange) Test(org.junit.Test)

Example 13 with TokenRange

use of com.datastax.oss.driver.api.core.metadata.token.TokenRange in project java-driver by datastax.

the class TokenITBase method checkRanges.

// Ensures that no ranges intersect and that they cover the entire ring.
private void checkRanges(Collection<TokenRange> ranges) {
    // Ensure no ranges intersect.
    TokenRange[] rangesArray = ranges.toArray(new TokenRange[0]);
    for (int i = 0; i < rangesArray.length; i++) {
        TokenRange rangeI = rangesArray[i];
        for (int j = i + 1; j < rangesArray.length; j++) {
            TokenRange rangeJ = rangesArray[j];
            assertThat(rangeI.intersects(rangeJ)).as("Range " + rangeI + " intersects with " + rangeJ).isFalse();
        }
    }
    // Ensure the defined ranges cover the entire ring.
    Iterator<TokenRange> it = ranges.iterator();
    TokenRange mergedRange = it.next();
    while (it.hasNext()) {
        TokenRange next = it.next();
        mergedRange = mergedRange.mergeWith(next);
    }
    boolean isFullRing = mergedRange.getStart().equals(mergedRange.getEnd()) && !mergedRange.isEmpty();
    assertThat(isFullRing).as("Ring is not fully defined for cluster.").isTrue();
}
Also used : TokenRange(com.datastax.oss.driver.api.core.metadata.token.TokenRange)

Example 14 with TokenRange

use of com.datastax.oss.driver.api.core.metadata.token.TokenRange in project java-driver by datastax.

the class TokenITBase method checkRanges.

private void checkRanges(Session session, CqlIdentifier keyspace, int replicationFactor) {
    assertThat(session.getMetadata().getTokenMap()).isPresent();
    TokenMap tokenMap = session.getMetadata().getTokenMap().get();
    List<TokenRange> allRangesWithDuplicates = Lists.newArrayList();
    // Get each host's ranges, the count should match the replication factor
    for (Node node : session.getMetadata().getNodes().values()) {
        Set<TokenRange> hostRanges = tokenMap.getTokenRanges(keyspace, node);
        // Special case: When using vnodes the tokens are not evenly assigned to each replica.
        if (!useVnodes) {
            assertThat(hostRanges).as("Node %s: expected %d ranges, got %d", node, replicationFactor * tokensPerNode, hostRanges.size()).hasSize(replicationFactor * tokensPerNode);
        }
        allRangesWithDuplicates.addAll(hostRanges);
    }
    // Special case check for vnodes to ensure that total number of replicated ranges is correct.
    assertThat(allRangesWithDuplicates).as("Expected %d total replicated ranges with duplicates, got %d", 3 * replicationFactor * tokensPerNode, allRangesWithDuplicates.size()).hasSize(3 * replicationFactor * tokensPerNode);
    // Once we ignore duplicates, the number of ranges should match the number of nodes.
    Set<TokenRange> allRanges = new TreeSet<>(allRangesWithDuplicates);
    assertThat(allRanges).as("Expected %d total replicated ranges, got %d", 3 * tokensPerNode, allRanges.size()).hasSize(3 * tokensPerNode);
    // And the ranges should cover the whole ring and no ranges intersect.
    checkRanges(allRanges);
}
Also used : TreeSet(java.util.TreeSet) Node(com.datastax.oss.driver.api.core.metadata.Node) TokenRange(com.datastax.oss.driver.api.core.metadata.token.TokenRange) TokenMap(com.datastax.oss.driver.api.core.metadata.TokenMap)

Example 15 with TokenRange

use of com.datastax.oss.driver.api.core.metadata.token.TokenRange in project java-driver by datastax.

the class TokenITBase method should_create_tokens_and_ranges.

@Test
public void should_create_tokens_and_ranges() {
    TokenMap tokenMap = getTokenMap();
    // Pick a random range
    TokenRange range = tokenMap.getTokenRanges().iterator().next();
    Token start = tokenMap.parse(tokenMap.format(range.getStart()));
    Token end = tokenMap.parse(tokenMap.format(range.getEnd()));
    assertThat(tokenMap.newTokenRange(start, end)).isEqualTo(range);
}
Also used : TokenRange(com.datastax.oss.driver.api.core.metadata.token.TokenRange) Token(com.datastax.oss.driver.api.core.metadata.token.Token) TokenMap(com.datastax.oss.driver.api.core.metadata.TokenMap) Test(org.junit.Test)

Aggregations

TokenRange (com.datastax.oss.driver.api.core.metadata.token.TokenRange)23 Node (com.datastax.oss.driver.api.core.metadata.Node)11 TokenMap (com.datastax.oss.driver.api.core.metadata.TokenMap)10 Token (com.datastax.oss.driver.api.core.metadata.token.Token)10 Test (org.junit.Test)6 ByteBuffer (java.nio.ByteBuffer)5 ArrayList (java.util.ArrayList)5 CqlSession (com.datastax.oss.driver.api.core.CqlSession)4 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)3 EndPoint (com.datastax.oss.driver.api.core.metadata.EndPoint)3 ImmutableSet (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableSet)3 NonNull (edu.umd.cs.findbugs.annotations.NonNull)3 Map (java.util.Map)3 ConsistencyLevel (com.datastax.oss.driver.api.core.ConsistencyLevel)2 V4 (com.datastax.oss.driver.api.core.DefaultProtocolVersion.V4)2 Version (com.datastax.oss.driver.api.core.Version)2 Row (com.datastax.oss.driver.api.core.cql.Row)2 Metadata (com.datastax.oss.driver.api.core.metadata.Metadata)2 TypeCodecs (com.datastax.oss.driver.api.core.type.codec.TypeCodecs)2 ChannelPool (com.datastax.oss.driver.internal.core.pool.ChannelPool)2