use of org.apache.cassandra.dht.Token in project cassandra by apache.
the class StorageServiceServerTest method testPrimaryRangesWithNetworkTopologyStrategy.
@Test
public void testPrimaryRangesWithNetworkTopologyStrategy() throws Exception {
TokenMetadata metadata = StorageService.instance.getTokenMetadata();
metadata.clearUnsafe();
// DC1
metadata.updateNormalToken(new StringToken("A"), InetAddress.getByName("127.0.0.1"));
metadata.updateNormalToken(new StringToken("C"), InetAddress.getByName("127.0.0.2"));
// DC2
metadata.updateNormalToken(new StringToken("B"), InetAddress.getByName("127.0.0.4"));
metadata.updateNormalToken(new StringToken("D"), InetAddress.getByName("127.0.0.5"));
Map<String, String> configOptions = new HashMap<>();
configOptions.put("DC1", "1");
configOptions.put("DC2", "1");
configOptions.put(ReplicationParams.CLASS, "NetworkTopologyStrategy");
Keyspace.clear("Keyspace1");
KeyspaceMetadata meta = KeyspaceMetadata.create("Keyspace1", KeyspaceParams.create(false, configOptions));
Schema.instance.load(meta);
Collection<Range<Token>> primaryRanges = StorageService.instance.getPrimaryRangesForEndpoint(meta.name, InetAddress.getByName("127.0.0.1"));
assert primaryRanges.size() == 1;
assert primaryRanges.contains(new Range<Token>(new StringToken("D"), new StringToken("A")));
primaryRanges = StorageService.instance.getPrimaryRangesForEndpoint(meta.name, InetAddress.getByName("127.0.0.2"));
assert primaryRanges.size() == 1;
assert primaryRanges.contains(new Range<Token>(new StringToken("B"), new StringToken("C")));
primaryRanges = StorageService.instance.getPrimaryRangesForEndpoint(meta.name, InetAddress.getByName("127.0.0.4"));
assert primaryRanges.size() == 1;
assert primaryRanges.contains(new Range<Token>(new StringToken("A"), new StringToken("B")));
primaryRanges = StorageService.instance.getPrimaryRangesForEndpoint(meta.name, InetAddress.getByName("127.0.0.5"));
assert primaryRanges.size() == 1;
assert primaryRanges.contains(new Range<Token>(new StringToken("C"), new StringToken("D")));
}
use of org.apache.cassandra.dht.Token in project cassandra by apache.
the class StorageServiceServerTest method testPrimaryRangesWithSimpleStrategy.
@Test
public void testPrimaryRangesWithSimpleStrategy() throws Exception {
TokenMetadata metadata = StorageService.instance.getTokenMetadata();
metadata.clearUnsafe();
metadata.updateNormalToken(new StringToken("A"), InetAddress.getByName("127.0.0.1"));
metadata.updateNormalToken(new StringToken("B"), InetAddress.getByName("127.0.0.2"));
metadata.updateNormalToken(new StringToken("C"), InetAddress.getByName("127.0.0.3"));
Keyspace.clear("Keyspace1");
KeyspaceMetadata meta = KeyspaceMetadata.create("Keyspace1", KeyspaceParams.simpleTransient(2));
Schema.instance.load(meta);
Collection<Range<Token>> primaryRanges = StorageService.instance.getPrimaryRangesForEndpoint(meta.name, InetAddress.getByName("127.0.0.1"));
assert primaryRanges.size() == 1;
assert primaryRanges.contains(new Range<Token>(new StringToken("C"), new StringToken("A")));
primaryRanges = StorageService.instance.getPrimaryRangesForEndpoint(meta.name, InetAddress.getByName("127.0.0.2"));
assert primaryRanges.size() == 1;
assert primaryRanges.contains(new Range<Token>(new StringToken("A"), new StringToken("B")));
primaryRanges = StorageService.instance.getPrimaryRangesForEndpoint(meta.name, InetAddress.getByName("127.0.0.3"));
assert primaryRanges.size() == 1;
assert primaryRanges.contains(new Range<Token>(new StringToken("B"), new StringToken("C")));
}
use of org.apache.cassandra.dht.Token in project cassandra by apache.
the class CompactionStress method generateTokens.
/**
* Populate tokenMetadata consistently across runs.
*
* We need consistency to write and compact the same data offline
* in the case of a range aware sstable writer.
*/
private void generateTokens(String seed, TokenMetadata tokenMetadata, Integer numTokens) {
Random random = new Random(seed.hashCode());
IPartitioner p = tokenMetadata.partitioner;
tokenMetadata.clearUnsafe();
for (int i = 1; i <= numTokens; i++) {
InetAddress addr = FBUtilities.getBroadcastAddress();
List<Token> tokens = Lists.newArrayListWithCapacity(numTokens);
for (int j = 0; j < numTokens; ++j) tokens.add(p.getRandomToken(random));
tokenMetadata.updateNormalTokens(tokens, addr);
}
}
use of org.apache.cassandra.dht.Token in project cassandra by apache.
the class LegacyJMXProgressSupportTest method testSessionSuccess.
@Test
public void testSessionSuccess() {
int cmd = 321;
String message = String.format("Repair session %s for range %s finished", UUID.randomUUID(), new Range<Token>(new Murmur3Partitioner.LongToken(3), new Murmur3Partitioner.LongToken(4)));
Optional<int[]> result = LegacyJMXProgressSupport.getLegacyUserdata(String.format("repair:%d", cmd), new ProgressEvent(ProgressEventType.PROGRESS, 2, 10, message));
assertTrue(result.isPresent());
assertArrayEquals(new int[] { cmd, ActiveRepairService.Status.SESSION_SUCCESS.ordinal() }, result.get());
}
use of org.apache.cassandra.dht.Token in project cassandra by apache.
the class TokenFilter method toRangeSet.
/**
* Converts the specified slice into a range set.
*
* @param slice the slice to convert
* @param options the query option
* @return the range set corresponding to the specified slice
* @throws InvalidRequestException if the request is invalid
*/
private RangeSet<Token> toRangeSet(TokenRestriction slice, QueryOptions options) throws InvalidRequestException {
if (slice.hasBound(START)) {
Token start = deserializeToken(slice.bounds(START, options).get(0));
BoundType startBoundType = toBoundType(slice.isInclusive(START));
if (slice.hasBound(END)) {
BoundType endBoundType = toBoundType(slice.isInclusive(END));
Token end = deserializeToken(slice.bounds(END, options).get(0));
if (start.equals(end) && (BoundType.OPEN == startBoundType || BoundType.OPEN == endBoundType))
return ImmutableRangeSet.of();
if (start.compareTo(end) <= 0)
return ImmutableRangeSet.of(Range.range(start, startBoundType, end, endBoundType));
return ImmutableRangeSet.<Token>builder().add(Range.upTo(end, endBoundType)).add(Range.downTo(start, startBoundType)).build();
}
return ImmutableRangeSet.of(Range.downTo(start, startBoundType));
}
Token end = deserializeToken(slice.bounds(END, options).get(0));
return ImmutableRangeSet.of(Range.upTo(end, toBoundType(slice.isInclusive(END))));
}
Aggregations