use of com.datastax.oss.driver.api.core.metadata.TokenMap in project java-driver by datastax.
the class AllLoadBalancingPoliciesSimulacronIT method replicasInDc.
private List<Node> replicasInDc(CqlSession session, String dcName) {
assertThat(session.getMetadata().getTokenMap()).isPresent();
TokenMap tokenMap = session.getMetadata().getTokenMap().get();
return tokenMap.getReplicas("test", ROUTING_KEY).stream().filter(n -> Objects.equals(n.getDatacenter(), dcName)).collect(Collectors.toList());
}
use of com.datastax.oss.driver.api.core.metadata.TokenMap in project java-driver by datastax.
the class DefaultLoadBalancingPolicyIT method should_use_round_robin_on_local_dc_when_not_enough_routing_information.
@Test
public void should_use_round_robin_on_local_dc_when_not_enough_routing_information() {
ByteBuffer routingKey = TypeCodecs.INT.encodePrimitive(1, ProtocolVersion.DEFAULT);
TokenMap tokenMap = SESSION_RULE.session().getMetadata().getTokenMap().get();
// TODO add statements with setKeyspace when that is supported
List<Statement> statements = ImmutableList.of(// No information at all
SimpleStatement.newInstance("SELECT * FROM test.foo WHERE k = 1"), // Keyspace present, missing routing key
SimpleStatement.newInstance("SELECT * FROM test.foo WHERE k = 1").setRoutingKeyspace(CqlIdentifier.fromCql("test")), // Routing key present, missing keyspace
SimpleStatement.newInstance("SELECT * FROM test.foo WHERE k = 1").setRoutingKey(routingKey), // Routing token present, missing keyspace
SimpleStatement.newInstance("SELECT * FROM test.foo WHERE k = 1").setRoutingToken(tokenMap.newToken(routingKey)));
for (Statement statement : statements) {
List<Node> coordinators = new ArrayList<>();
for (int i = 0; i < 12; i++) {
ResultSet rs = SESSION_RULE.session().execute(statement);
Node coordinator = rs.getExecutionInfo().getCoordinator();
assertThat(coordinator.getDatacenter()).isEqualTo(LOCAL_DC);
coordinators.add(coordinator);
}
for (int i = 0; i < 4; i++) {
assertThat(coordinators.get(i)).isEqualTo(coordinators.get(4 + i)).isEqualTo(coordinators.get(8 + i));
}
}
}
use of com.datastax.oss.driver.api.core.metadata.TokenMap in project java-driver by datastax.
the class DefaultLoadBalancingPolicyIT method should_hit_non_replicas_when_routing_information_present_but_all_replicas_down.
@Test
public void should_hit_non_replicas_when_routing_information_present_but_all_replicas_down() {
CqlIdentifier keyspace = CqlIdentifier.fromCql("test");
ByteBuffer routingKey = TypeCodecs.INT.encodePrimitive(1, ProtocolVersion.DEFAULT);
TokenMap tokenMap = SESSION_RULE.session().getMetadata().getTokenMap().get();
InternalDriverContext context = (InternalDriverContext) SESSION_RULE.session().getContext();
Set<Node> localReplicas = new HashSet<>();
for (Node replica : tokenMap.getReplicas(keyspace, routingKey)) {
if (replica.getDatacenter().equals(LOCAL_DC)) {
localReplicas.add(replica);
context.getEventBus().fire(TopologyEvent.forceDown(replica.getBroadcastRpcAddress().get()));
await().pollInterval(500, TimeUnit.MILLISECONDS).atMost(60, TimeUnit.SECONDS).untilAsserted(() -> assertThat(replica.getOpenConnections()).isZero());
}
}
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) {
List<Node> coordinators = new ArrayList<>();
for (int i = 0; i < 6; i++) {
ResultSet rs = SESSION_RULE.session().execute(statement);
Node coordinator = rs.getExecutionInfo().getCoordinator();
coordinators.add(coordinator);
assertThat(coordinator.getDatacenter()).isEqualTo(LOCAL_DC);
assertThat(localReplicas).doesNotContain(coordinator);
}
// Should round-robin on the two non-replicas
for (int i = 0; i < 2; i++) {
assertThat(coordinators.get(i)).isEqualTo(coordinators.get(2 + i)).isEqualTo(coordinators.get(4 + i));
}
}
for (Node replica : localReplicas) {
context.getEventBus().fire(TopologyEvent.forceUp(replica.getBroadcastRpcAddress().get()));
await().pollInterval(500, TimeUnit.MILLISECONDS).atMost(60, TimeUnit.SECONDS).untilAsserted(() -> assertThat(replica.getOpenConnections()).isPositive());
}
}
use of com.datastax.oss.driver.api.core.metadata.TokenMap 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();
}
use of com.datastax.oss.driver.api.core.metadata.TokenMap 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);
}
}
}
Aggregations