Search in sources :

Example 6 with TokenRange

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

the class TokenITBase method rangeQuery.

private List<Row> rangeQuery(PreparedStatement rangeStatement, TokenRange range) {
    List<Row> rows = Lists.newArrayList();
    for (TokenRange subRange : range.unwrap()) {
        Statement<?> statement = rangeStatement.bind(subRange.getStart(), subRange.getEnd());
        session().execute(statement).forEach(rows::add);
    }
    return rows;
}
Also used : TokenRange(com.datastax.oss.driver.api.core.metadata.token.TokenRange) Row(com.datastax.oss.driver.api.core.cql.Row)

Example 7 with TokenRange

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

the class TokenITBase method should_be_consistent_with_range_queries.

/**
 * Validates that the token metadata is consistent with server-side range queries. That is,
 * querying the data in a range does return a PK that the driver thinks is in that range.
 *
 * @test_category metadata:token
 * @expected_result token ranges are exposed and usable.
 * @jira_ticket JAVA-312
 * @since 2.0.10, 2.1.5
 */
@Test
public void should_be_consistent_with_range_queries() {
    TokenMap tokenMap = getTokenMap();
    // Find the replica for a given partition key of ks1.foo.
    int key = 1;
    ProtocolVersion protocolVersion = session().getContext().getProtocolVersion();
    ByteBuffer serializedKey = TypeCodecs.INT.encodePrimitive(key, protocolVersion);
    assertThat(serializedKey).isNotNull();
    Set<Node> replicas = tokenMap.getReplicas(KS1, serializedKey);
    assertThat(replicas).hasSize(1);
    Node replica = replicas.iterator().next();
    // Iterate the cluster's token ranges. For each one, use a range query to get all the keys of
    // ks1.foo that are in this range.
    PreparedStatement rangeStatement = session().prepare("SELECT i FROM foo WHERE token(i) > ? and token(i) <= ?");
    TokenRange foundRange = null;
    for (TokenRange range : tokenMap.getTokenRanges()) {
        List<Row> rows = rangeQuery(rangeStatement, range);
        for (Row row : rows) {
            if (row.getInt("i") == key) {
                // We should find our initial key exactly once
                assertThat(foundRange).describedAs("found the same key in two ranges: " + foundRange + " and " + range).isNull();
                foundRange = range;
                // That range should be managed by the replica
                assertThat(tokenMap.getReplicas(KS1, range)).contains(replica);
            }
        }
    }
    assertThat(foundRange).isNotNull();
}
Also used : Node(com.datastax.oss.driver.api.core.metadata.Node) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) TokenRange(com.datastax.oss.driver.api.core.metadata.token.TokenRange) Row(com.datastax.oss.driver.api.core.cql.Row) TokenMap(com.datastax.oss.driver.api.core.metadata.TokenMap) ProtocolVersion(com.datastax.oss.driver.api.core.ProtocolVersion) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 8 with TokenRange

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

the class TokenITBase method should_have_only_one_wrapped_range.

/**
 * Ensures that for there is at most one wrapped range in the ring, and check that unwrapping it
 * produces two ranges.
 *
 * @test_category metadata:token
 * @expected_result there is at most one wrapped range.
 * @jira_ticket JAVA-312
 * @since 2.0.10, 2.1.5
 */
@Test
public void should_have_only_one_wrapped_range() {
    TokenMap tokenMap = getTokenMap();
    TokenRange wrappedRange = null;
    for (TokenRange range : tokenMap.getTokenRanges()) {
        if (range.isWrappedAround()) {
            assertThat(wrappedRange).as("Found a wrapped around TokenRange (%s) when one already exists (%s).", range, wrappedRange).isNull();
            wrappedRange = range;
            assertThat(wrappedRange.unwrap()).hasSize(2);
        }
    }
}
Also used : TokenRange(com.datastax.oss.driver.api.core.metadata.token.TokenRange) TokenMap(com.datastax.oss.driver.api.core.metadata.TokenMap) Test(org.junit.Test)

Example 9 with TokenRange

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

the class RemovedNodeIT method should_signal_and_destroy_pool_when_node_gets_removed.

@Test
public void should_signal_and_destroy_pool_when_node_gets_removed() {
    RemovalListener removalListener = new RemovalListener();
    try (CqlSession session = SessionUtils.newSession(CCM_RULE, null, removalListener, null, null)) {
        assertThat(session.getMetadata().getTokenMap()).isPresent();
        Set<TokenRange> tokenRanges = session.getMetadata().getTokenMap().get().getTokenRanges();
        assertThat(tokenRanges).hasSize(4);
        CCM_RULE.getCcmBridge().decommission(2);
        await().pollInterval(500, TimeUnit.MILLISECONDS).atMost(60, TimeUnit.SECONDS).until(() -> removalListener.removedNode != null);
        Map<Node, ChannelPool> pools = ((DefaultSession) session).getPools();
        await().pollInterval(500, TimeUnit.MILLISECONDS).atMost(60, TimeUnit.SECONDS).until(() -> !pools.containsKey(removalListener.removedNode));
        await().pollInterval(500, TimeUnit.MILLISECONDS).atMost(60, TimeUnit.SECONDS).until(() -> session.getMetadata().getTokenMap().get().getTokenRanges().size() == 3);
    }
}
Also used : ChannelPool(com.datastax.oss.driver.internal.core.pool.ChannelPool) Node(com.datastax.oss.driver.api.core.metadata.Node) TokenRange(com.datastax.oss.driver.api.core.metadata.token.TokenRange) CqlSession(com.datastax.oss.driver.api.core.CqlSession) DefaultSession(com.datastax.oss.driver.internal.core.session.DefaultSession) Test(org.junit.Test)

Example 10 with TokenRange

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

the class TokenRangeBase method intersectWith.

@NonNull
@Override
public List<TokenRange> intersectWith(@NonNull TokenRange that) {
    if (!this.intersects(that)) {
        throw new IllegalArgumentException("The two ranges do not intersect, use intersects() before calling this method");
    }
    List<TokenRange> intersected = Lists.newArrayList();
    // Compare the unwrapped ranges to one another.
    List<TokenRange> unwrappedForThis = this.unwrap();
    List<TokenRange> unwrappedForThat = that.unwrap();
    for (TokenRange t1 : unwrappedForThis) {
        for (TokenRange t2 : unwrappedForThat) {
            if (t1.intersects(t2)) {
                intersected.add(newTokenRange(contains(t1, t2.getStart(), true) ? t2.getStart() : t1.getStart(), contains(t1, t2.getEnd(), false) ? t2.getEnd() : t1.getEnd()));
            }
        }
    }
    // This could happen in the case that two wrapped ranges intersected.
    if (intersected.size() == 2) {
        TokenRange t1 = intersected.get(0);
        TokenRange t2 = intersected.get(1);
        if (t1.getEnd().equals(t2.getStart()) || t2.getEnd().equals(t1.getStart())) {
            return ImmutableList.of(t1.mergeWith(t2));
        }
    }
    return intersected;
}
Also used : TokenRange(com.datastax.oss.driver.api.core.metadata.token.TokenRange) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

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