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;
}
use of com.datastax.oss.driver.api.core.metadata.token.TokenRange in project java-driver by datastax.
the class TokenRangeTest method should_create_empty_token_ranges_if_too_many_splits.
@Test
public void should_create_empty_token_ranges_if_too_many_splits() {
TokenRange range = range(0, 10);
List<TokenRange> ranges = range.splitEvenly(255);
assertThat(ranges).hasSize(255);
for (int i = 0; i < ranges.size(); i++) {
TokenRange tr = ranges.get(i);
if (i < 10) {
assertThat(tr).isEqualTo(range(i, i + 1));
} else {
assertThat(tr.isEmpty());
}
}
}
use of com.datastax.oss.driver.api.core.metadata.token.TokenRange in project java-driver by datastax.
the class TokenITBase method checkRanges.
// Ensures that no ranges intersect and that they cover the entire ring.
private void checkRanges(Collection<TokenRange> ranges) {
// Ensure no ranges intersect.
TokenRange[] rangesArray = ranges.toArray(new TokenRange[0]);
for (int i = 0; i < rangesArray.length; i++) {
TokenRange rangeI = rangesArray[i];
for (int j = i + 1; j < rangesArray.length; j++) {
TokenRange rangeJ = rangesArray[j];
assertThat(rangeI.intersects(rangeJ)).as("Range " + rangeI + " intersects with " + rangeJ).isFalse();
}
}
// Ensure the defined ranges cover the entire ring.
Iterator<TokenRange> it = ranges.iterator();
TokenRange mergedRange = it.next();
while (it.hasNext()) {
TokenRange next = it.next();
mergedRange = mergedRange.mergeWith(next);
}
boolean isFullRing = mergedRange.getStart().equals(mergedRange.getEnd()) && !mergedRange.isEmpty();
assertThat(isFullRing).as("Ring is not fully defined for cluster.").isTrue();
}
use of com.datastax.oss.driver.api.core.metadata.token.TokenRange 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);
}
use of com.datastax.oss.driver.api.core.metadata.token.TokenRange in project java-driver by datastax.
the class TokenITBase method should_create_tokens_and_ranges.
@Test
public void should_create_tokens_and_ranges() {
TokenMap tokenMap = getTokenMap();
// Pick a random range
TokenRange range = tokenMap.getTokenRanges().iterator().next();
Token start = tokenMap.parse(tokenMap.format(range.getStart()));
Token end = tokenMap.parse(tokenMap.format(range.getEnd()));
assertThat(tokenMap.newTokenRange(start, end)).isEqualTo(range);
}
Aggregations