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