use of org.apache.cassandra.locator.TokenMetadata in project cassandra by apache.
the class ActiveRepairServiceTest method testGetNeighborsTimesTwo.
@Test
public void testGetNeighborsTimesTwo() throws Throwable {
TokenMetadata tmd = StorageService.instance.getTokenMetadata();
// generate rf*2 nodes, and ensure that only neighbors specified by the ARS are returned
addTokens(2 * Keyspace.open(KEYSPACE5).getReplicationStrategy().getReplicationFactor().allReplicas);
AbstractReplicationStrategy ars = Keyspace.open(KEYSPACE5).getReplicationStrategy();
Set<InetAddressAndPort> expected = new HashSet<>();
for (Replica replica : ars.getAddressReplicas().get(FBUtilities.getBroadcastAddressAndPort())) {
expected.addAll(ars.getRangeAddresses(tmd.cloneOnlyTokenMap()).get(replica.range()).endpoints());
}
expected.remove(FBUtilities.getBroadcastAddressAndPort());
Iterable<Range<Token>> ranges = StorageService.instance.getLocalReplicas(KEYSPACE5).ranges();
Set<InetAddressAndPort> neighbors = new HashSet<>();
for (Range<Token> range : ranges) {
neighbors.addAll(ActiveRepairService.getNeighbors(KEYSPACE5, ranges, range, null, null).endpoints());
}
assertEquals(expected, neighbors);
}
use of org.apache.cassandra.locator.TokenMetadata in project cassandra by apache.
the class ActiveRepairServiceTest method prepare.
@Before
public void prepare() throws Exception {
if (!initialized) {
SchemaLoader.startGossiper();
initialized = true;
LOCAL = FBUtilities.getBroadcastAddressAndPort();
// generate a fake endpoint for which we can spoof receiving/sending trees
REMOTE = InetAddressAndPort.getByName("127.0.0.2");
}
TokenMetadata tmd = StorageService.instance.getTokenMetadata();
tmd.clearUnsafe();
StorageService.instance.setTokens(Collections.singleton(tmd.partitioner.getRandomToken()));
tmd.updateNormalToken(tmd.partitioner.getMinimumToken(), REMOTE);
assert tmd.isMember(REMOTE);
}
use of org.apache.cassandra.locator.TokenMetadata in project cassandra by apache.
the class StorageServiceServerTest method testPrimaryRangesWithNetworkTopologyStrategyOneDCOnly.
@Test
public void testPrimaryRangesWithNetworkTopologyStrategyOneDCOnly() throws Exception {
TokenMetadata metadata = StorageService.instance.getTokenMetadata();
metadata.clearUnsafe();
// DC1
metadata.updateNormalToken(new StringToken("A"), InetAddressAndPort.getByName("127.0.0.1"));
metadata.updateNormalToken(new StringToken("C"), InetAddressAndPort.getByName("127.0.0.2"));
// DC2
metadata.updateNormalToken(new StringToken("B"), InetAddressAndPort.getByName("127.0.0.4"));
metadata.updateNormalToken(new StringToken("D"), InetAddressAndPort.getByName("127.0.0.5"));
Map<String, String> configOptions = new HashMap<>();
configOptions.put("DC2", "2");
configOptions.put(ReplicationParams.CLASS, "NetworkTopologyStrategy");
Schema.instance.maybeRemoveKeyspaceInstance("Keyspace1");
KeyspaceMetadata meta = KeyspaceMetadata.create("Keyspace1", KeyspaceParams.create(false, configOptions));
Schema.instance.load(meta);
// endpoints in DC1 should not have primary range
Collection<Range<Token>> primaryRanges = StorageService.instance.getPrimaryRangesForEndpoint(meta.name, InetAddressAndPort.getByName("127.0.0.1"));
Assertions.assertThat(primaryRanges).isEmpty();
primaryRanges = StorageService.instance.getPrimaryRangesForEndpoint(meta.name, InetAddressAndPort.getByName("127.0.0.2"));
Assertions.assertThat(primaryRanges).isEmpty();
// endpoints in DC2 should have primary ranges which also cover DC1
primaryRanges = StorageService.instance.getPrimaryRangesForEndpoint(meta.name, InetAddressAndPort.getByName("127.0.0.4"));
Assertions.assertThat(primaryRanges.size()).as(primaryRanges.toString()).isEqualTo(2);
Assertions.assertThat(primaryRanges).contains(new Range<Token>(new StringToken("D"), new StringToken("A")));
Assertions.assertThat(primaryRanges).contains(new Range<Token>(new StringToken("A"), new StringToken("B")));
primaryRanges = StorageService.instance.getPrimaryRangesForEndpoint(meta.name, InetAddressAndPort.getByName("127.0.0.5"));
Assertions.assertThat(primaryRanges.size()).as(primaryRanges.toString()).isEqualTo(2);
Assertions.assertThat(primaryRanges).contains(new Range<Token>(new StringToken("C"), new StringToken("D")));
Assertions.assertThat(primaryRanges).contains(new Range<Token>(new StringToken("B"), new StringToken("C")));
}
use of org.apache.cassandra.locator.TokenMetadata in project cassandra by apache.
the class StorageServiceServerTest method testPrimaryRangeForEndpointWithinDCWithVnodes.
@Test
public void testPrimaryRangeForEndpointWithinDCWithVnodes() throws Exception {
TokenMetadata metadata = StorageService.instance.getTokenMetadata();
metadata.clearUnsafe();
// DC1
Multimap<InetAddressAndPort, Token> dc1 = HashMultimap.create();
dc1.put(InetAddressAndPort.getByName("127.0.0.1"), new StringToken("A"));
dc1.put(InetAddressAndPort.getByName("127.0.0.1"), new StringToken("E"));
dc1.put(InetAddressAndPort.getByName("127.0.0.1"), new StringToken("H"));
dc1.put(InetAddressAndPort.getByName("127.0.0.2"), new StringToken("C"));
dc1.put(InetAddressAndPort.getByName("127.0.0.2"), new StringToken("I"));
dc1.put(InetAddressAndPort.getByName("127.0.0.2"), new StringToken("J"));
metadata.updateNormalTokens(dc1);
// DC2
Multimap<InetAddressAndPort, Token> dc2 = HashMultimap.create();
dc2.put(InetAddressAndPort.getByName("127.0.0.4"), new StringToken("B"));
dc2.put(InetAddressAndPort.getByName("127.0.0.4"), new StringToken("G"));
dc2.put(InetAddressAndPort.getByName("127.0.0.4"), new StringToken("L"));
dc2.put(InetAddressAndPort.getByName("127.0.0.5"), new StringToken("D"));
dc2.put(InetAddressAndPort.getByName("127.0.0.5"), new StringToken("F"));
dc2.put(InetAddressAndPort.getByName("127.0.0.5"), new StringToken("K"));
metadata.updateNormalTokens(dc2);
Map<String, String> configOptions = new HashMap<>();
configOptions.put("DC1", "1");
configOptions.put("DC2", "2");
configOptions.put(ReplicationParams.CLASS, "NetworkTopologyStrategy");
Schema.instance.maybeRemoveKeyspaceInstance("Keyspace1");
KeyspaceMetadata meta = KeyspaceMetadata.create("Keyspace1", KeyspaceParams.create(false, configOptions));
Schema.instance.load(meta);
// endpoints in DC1 should have primary ranges which also cover DC2
Collection<Range<Token>> primaryRanges = StorageService.instance.getPrimaryRangeForEndpointWithinDC(meta.name, InetAddressAndPort.getByName("127.0.0.1"));
Assertions.assertThat(primaryRanges.size()).as(primaryRanges.toString()).isEqualTo(8);
Assertions.assertThat(primaryRanges).contains(new Range<Token>(new StringToken("J"), new StringToken("K")));
Assertions.assertThat(primaryRanges).contains(new Range<Token>(new StringToken("K"), new StringToken("L")));
Assertions.assertThat(primaryRanges).contains(new Range<Token>(new StringToken("L"), new StringToken("A")));
Assertions.assertThat(primaryRanges).contains(new Range<Token>(new StringToken("C"), new StringToken("D")));
Assertions.assertThat(primaryRanges).contains(new Range<Token>(new StringToken("D"), new StringToken("E")));
Assertions.assertThat(primaryRanges).contains(new Range<Token>(new StringToken("E"), new StringToken("F")));
Assertions.assertThat(primaryRanges).contains(new Range<Token>(new StringToken("F"), new StringToken("G")));
Assertions.assertThat(primaryRanges).contains(new Range<Token>(new StringToken("G"), new StringToken("H")));
// endpoints in DC1 should have primary ranges which also cover DC2
primaryRanges = StorageService.instance.getPrimaryRangeForEndpointWithinDC(meta.name, InetAddressAndPort.getByName("127.0.0.2"));
Assertions.assertThat(primaryRanges.size()).as(primaryRanges.toString()).isEqualTo(4);
Assertions.assertThat(primaryRanges).contains(new Range<Token>(new StringToken("B"), new StringToken("C")));
Assertions.assertThat(primaryRanges).contains(new Range<Token>(new StringToken("A"), new StringToken("B")));
Assertions.assertThat(primaryRanges).contains(new Range<Token>(new StringToken("H"), new StringToken("I")));
Assertions.assertThat(primaryRanges).contains(new Range<Token>(new StringToken("I"), new StringToken("J")));
// endpoints in DC2 should have primary ranges which also cover DC1
primaryRanges = StorageService.instance.getPrimaryRangeForEndpointWithinDC(meta.name, InetAddressAndPort.getByName("127.0.0.4"));
Assertions.assertThat(primaryRanges.size()).as(primaryRanges.toString()).isEqualTo(4);
Assertions.assertThat(primaryRanges).contains(new Range<Token>(new StringToken("A"), new StringToken("B")));
Assertions.assertThat(primaryRanges).contains(new Range<Token>(new StringToken("F"), new StringToken("G")));
Assertions.assertThat(primaryRanges).contains(new Range<Token>(new StringToken("K"), new StringToken("L")));
// because /127.0.0.4 holds token "B" which is the next to token "A" from /127.0.0.1,
// the node covers range (L, A]
Assertions.assertThat(primaryRanges).contains(new Range<Token>(new StringToken("L"), new StringToken("A")));
primaryRanges = StorageService.instance.getPrimaryRangeForEndpointWithinDC(meta.name, InetAddressAndPort.getByName("127.0.0.5"));
Assertions.assertThat(primaryRanges.size()).as(primaryRanges.toString()).isEqualTo(8);
Assertions.assertThat(primaryRanges).contains(new Range<Token>(new StringToken("C"), new StringToken("D")));
Assertions.assertThat(primaryRanges).contains(new Range<Token>(new StringToken("E"), new StringToken("F")));
Assertions.assertThat(primaryRanges).contains(new Range<Token>(new StringToken("J"), new StringToken("K")));
// ranges from /127.0.0.1
Assertions.assertThat(primaryRanges).contains(new Range<Token>(new StringToken("D"), new StringToken("E")));
// the next token to "H" in DC2 is "K" in /127.0.0.5, so (G, H] goes to /127.0.0.5
Assertions.assertThat(primaryRanges).contains(new Range<Token>(new StringToken("G"), new StringToken("H")));
// ranges from /127.0.0.2
Assertions.assertThat(primaryRanges).contains(new Range<Token>(new StringToken("B"), new StringToken("C")));
Assertions.assertThat(primaryRanges).contains(new Range<Token>(new StringToken("H"), new StringToken("I")));
Assertions.assertThat(primaryRanges).contains(new Range<Token>(new StringToken("I"), new StringToken("J")));
}
use of org.apache.cassandra.locator.TokenMetadata in project cassandra by apache.
the class StorageServiceServerTest method testPrimaryRangeForEndpointWithinDCWithSimpleStrategy.
/* Does not make much sense to use -local and -pr with simplestrategy, but just to prevent human errors */
@Test
public void testPrimaryRangeForEndpointWithinDCWithSimpleStrategy() throws Exception {
TokenMetadata metadata = StorageService.instance.getTokenMetadata();
metadata.clearUnsafe();
metadata.updateNormalToken(new StringToken("A"), InetAddressAndPort.getByName("127.0.0.1"));
metadata.updateNormalToken(new StringToken("B"), InetAddressAndPort.getByName("127.0.0.2"));
metadata.updateNormalToken(new StringToken("C"), InetAddressAndPort.getByName("127.0.0.3"));
Map<String, String> configOptions = new HashMap<>();
configOptions.put("replication_factor", "2");
Schema.instance.maybeRemoveKeyspaceInstance("Keyspace1");
KeyspaceMetadata meta = KeyspaceMetadata.create("Keyspace1", KeyspaceParams.simpleTransient(2));
Schema.instance.load(meta);
Collection<Range<Token>> primaryRanges = StorageService.instance.getPrimaryRangeForEndpointWithinDC(meta.name, InetAddressAndPort.getByName("127.0.0.1"));
Assertions.assertThat(primaryRanges.size()).as(primaryRanges.toString()).isEqualTo(1);
Assertions.assertThat(primaryRanges).contains(new Range<Token>(new StringToken("C"), new StringToken("A")));
primaryRanges = StorageService.instance.getPrimaryRangeForEndpointWithinDC(meta.name, InetAddressAndPort.getByName("127.0.0.2"));
Assertions.assertThat(primaryRanges.size()).as(primaryRanges.toString()).isEqualTo(1);
Assertions.assertThat(primaryRanges).contains(new Range<Token>(new StringToken("A"), new StringToken("B")));
primaryRanges = StorageService.instance.getPrimaryRangeForEndpointWithinDC(meta.name, InetAddressAndPort.getByName("127.0.0.3"));
Assertions.assertThat(primaryRanges.size()).as(primaryRanges.toString()).isEqualTo(1);
Assertions.assertThat(primaryRanges).contains(new Range<Token>(new StringToken("B"), new StringToken("C")));
}
Aggregations