Search in sources :

Example 6 with TokenMap

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

the class TokenITBase method should_create_token_from_partition_key.

@Test
public void should_create_token_from_partition_key() {
    TokenMap tokenMap = getTokenMap();
    Row row = session().execute("SELECT token(i) FROM foo WHERE i = 1").one();
    assertThat(row).isNotNull();
    Token expected = row.getToken(0);
    ProtocolVersion protocolVersion = session().getContext().getProtocolVersion();
    assertThat(tokenMap.newToken(TypeCodecs.INT.encodePrimitive(1, protocolVersion))).isEqualTo(expected);
}
Also used : Token(com.datastax.oss.driver.api.core.metadata.token.Token) 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) Test(org.junit.Test)

Example 7 with TokenMap

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

the class BasicLoadBalancingPolicy method getReplicas.

@NonNull
protected Set<Node> getReplicas(@Nullable Request request, @Nullable Session session) {
    if (request == null || session == null) {
        return Collections.emptySet();
    }
    Optional<TokenMap> maybeTokenMap = context.getMetadataManager().getMetadata().getTokenMap();
    if (!maybeTokenMap.isPresent()) {
        return Collections.emptySet();
    }
    // Note: we're on the hot path and the getXxx methods are potentially more than simple getters,
    // so we only call each method when strictly necessary (which is why the code below looks a bit
    // weird).
    CqlIdentifier keyspace;
    Token token;
    ByteBuffer key;
    try {
        keyspace = request.getKeyspace();
        if (keyspace == null) {
            keyspace = request.getRoutingKeyspace();
        }
        if (keyspace == null && session.getKeyspace().isPresent()) {
            keyspace = session.getKeyspace().get();
        }
        if (keyspace == null) {
            return Collections.emptySet();
        }
        token = request.getRoutingToken();
        key = (token == null) ? request.getRoutingKey() : null;
        if (token == null && key == null) {
            return Collections.emptySet();
        }
    } catch (Exception e) {
        // Protect against poorly-implemented Request instances
        LOG.error("Unexpected error while trying to compute query plan", e);
        return Collections.emptySet();
    }
    TokenMap tokenMap = maybeTokenMap.get();
    return token != null ? tokenMap.getReplicas(keyspace, token) : tokenMap.getReplicas(keyspace, key);
}
Also used : Token(com.datastax.oss.driver.api.core.metadata.token.Token) TokenMap(com.datastax.oss.driver.api.core.metadata.TokenMap) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) ByteBuffer(java.nio.ByteBuffer) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Example 8 with TokenMap

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

the class SchemaIT method should_exclude_virtual_keyspaces_from_token_map.

@CassandraRequirement(min = "4.0", description = "virtual tables introduced in 4.0")
@Test
public void should_exclude_virtual_keyspaces_from_token_map() {
    skipIfDse60();
    DriverConfigLoader loader = SessionUtils.configLoaderBuilder().withStringList(DefaultDriverOption.METADATA_SCHEMA_REFRESHED_KEYSPACES, Arrays.asList("system_views", "system_virtual_schema", sessionRule.keyspace().asInternal())).build();
    try (CqlSession session = SessionUtils.newSession(ccmRule, loader)) {
        Metadata metadata = session.getMetadata();
        Map<CqlIdentifier, KeyspaceMetadata> keyspaces = metadata.getKeyspaces();
        assertThat(keyspaces).containsKey(CqlIdentifier.fromCql("system_views")).containsKey(CqlIdentifier.fromCql("system_virtual_schema"));
        TokenMap tokenMap = metadata.getTokenMap().orElseThrow(AssertionError::new);
        // value does not matter
        ByteBuffer partitionKey = Bytes.fromHexString("0x00");
        assertThat(tokenMap.getReplicas("system_views", partitionKey)).isEmpty();
        assertThat(tokenMap.getReplicas("system_virtual_schema", partitionKey)).isEmpty();
        // Check that a non-virtual keyspace is present
        assertThat(tokenMap.getReplicas(sessionRule.keyspace(), partitionKey)).isNotEmpty();
    }
}
Also used : Metadata(com.datastax.oss.driver.api.core.metadata.Metadata) TableMetadata(com.datastax.oss.driver.api.core.metadata.schema.TableMetadata) ColumnMetadata(com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata) KeyspaceMetadata(com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata) DriverConfigLoader(com.datastax.oss.driver.api.core.config.DriverConfigLoader) TokenMap(com.datastax.oss.driver.api.core.metadata.TokenMap) CqlSession(com.datastax.oss.driver.api.core.CqlSession) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) KeyspaceMetadata(com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) CassandraRequirement(com.datastax.oss.driver.api.testinfra.CassandraRequirement)

Example 9 with TokenMap

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

the class DefaultLoadBalancingPolicyIT method should_prioritize_replicas_when_routing_information_present.

@Test
public void should_prioritize_replicas_when_routing_information_present() {
    CqlIdentifier keyspace = CqlIdentifier.fromCql("test");
    ByteBuffer routingKey = TypeCodecs.INT.encodePrimitive(1, ProtocolVersion.DEFAULT);
    TokenMap tokenMap = SESSION_RULE.session().getMetadata().getTokenMap().get();
    Set<Node> localReplicas = new HashSet<>();
    for (Node replica : tokenMap.getReplicas(keyspace, routingKey)) {
        if (replica.getDatacenter().equals(LOCAL_DC)) {
            localReplicas.add(replica);
        }
    }
    assertThat(localReplicas).hasSize(2);
    // TODO add statements with setKeyspace when that is supported
    List<Statement> statements = ImmutableList.of(SimpleStatement.newInstance("SELECT * FROM test.foo WHERE k = 1").setRoutingKeyspace(keyspace).setRoutingKey(routingKey), SimpleStatement.newInstance("SELECT * FROM test.foo WHERE k = 1").setRoutingKeyspace(keyspace).setRoutingToken(tokenMap.newToken(routingKey)));
    for (Statement statement : statements) {
        // Since the exact order is randomized, just run a bunch of queries and check that we get a
        // reasonable distribution:
        Map<Node, Integer> hits = new HashMap<>();
        for (int i = 0; i < 2000; i++) {
            ResultSet rs = SESSION_RULE.session().execute(statement);
            Node coordinator = rs.getExecutionInfo().getCoordinator();
            assertThat(localReplicas).contains(coordinator);
            assertThat(coordinator.getDatacenter()).isEqualTo(LOCAL_DC);
            hits.merge(coordinator, 1, (a, b) -> a + b);
        }
        for (Integer count : hits.values()) {
            assertThat(count).isCloseTo(1000, withinPercentage(10));
        }
    }
}
Also used : HashMap(java.util.HashMap) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) Statement(com.datastax.oss.driver.api.core.cql.Statement) Node(com.datastax.oss.driver.api.core.metadata.Node) TokenMap(com.datastax.oss.driver.api.core.metadata.TokenMap) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) ByteBuffer(java.nio.ByteBuffer) EndPoint(com.datastax.oss.driver.api.core.metadata.EndPoint) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 10 with TokenMap

use of com.datastax.oss.driver.api.core.metadata.TokenMap 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)

Aggregations

TokenMap (com.datastax.oss.driver.api.core.metadata.TokenMap)12 Test (org.junit.Test)9 ByteBuffer (java.nio.ByteBuffer)7 Node (com.datastax.oss.driver.api.core.metadata.Node)6 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)4 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)4 TokenRange (com.datastax.oss.driver.api.core.metadata.token.TokenRange)4 ResultSet (com.datastax.oss.driver.api.core.cql.ResultSet)3 Statement (com.datastax.oss.driver.api.core.cql.Statement)3 EndPoint (com.datastax.oss.driver.api.core.metadata.EndPoint)3 Token (com.datastax.oss.driver.api.core.metadata.token.Token)3 CqlSession (com.datastax.oss.driver.api.core.CqlSession)2 ProtocolVersion (com.datastax.oss.driver.api.core.ProtocolVersion)2 DriverConfigLoader (com.datastax.oss.driver.api.core.config.DriverConfigLoader)2 Row (com.datastax.oss.driver.api.core.cql.Row)2 ArrayList (java.util.ArrayList)2 ConsistencyLevel (com.datastax.oss.driver.api.core.ConsistencyLevel)1 DefaultConsistencyLevel (com.datastax.oss.driver.api.core.DefaultConsistencyLevel)1 NoNodeAvailableException (com.datastax.oss.driver.api.core.NoNodeAvailableException)1 DefaultDriverOption (com.datastax.oss.driver.api.core.config.DefaultDriverOption)1