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);
}
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);
}
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();
}
}
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));
}
}
}
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);
}
Aggregations