Search in sources :

Example 1 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 2 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 3 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 4 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 5 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)

Aggregations

TokenRange (com.datastax.oss.driver.api.core.metadata.token.TokenRange)15 Node (com.datastax.oss.driver.api.core.metadata.Node)6 Test (org.junit.Test)6 TokenMap (com.datastax.oss.driver.api.core.metadata.TokenMap)5 Token (com.datastax.oss.driver.api.core.metadata.token.Token)5 CqlSession (com.datastax.oss.driver.api.core.CqlSession)2 Row (com.datastax.oss.driver.api.core.cql.Row)2 ChannelPool (com.datastax.oss.driver.internal.core.pool.ChannelPool)2 DefaultSession (com.datastax.oss.driver.internal.core.session.DefaultSession)2 ImmutableSetMultimap (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableSetMultimap)2 NonNull (edu.umd.cs.findbugs.annotations.NonNull)2 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)1 ProtocolVersion (com.datastax.oss.driver.api.core.ProtocolVersion)1 PreparedStatement (com.datastax.oss.driver.api.core.cql.PreparedStatement)1 DefaultNode (com.datastax.oss.driver.internal.core.metadata.DefaultNode)1 ImmutableMap (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap)1 ImmutableSet (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableSet)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1