use of org.apache.cassandra.locator.TokenMetadata in project cassandra by apache.
the class StorageServiceServerTest method testPrimaryRangesWithVnodes.
@Test
public void testPrimaryRangesWithVnodes() 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("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(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.getPrimaryRangesForEndpoint(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 testCreateRepairRangeFrom.
@Test
public void testCreateRepairRangeFrom() throws Exception {
StorageService.instance.setPartitionerUnsafe(Murmur3Partitioner.instance);
TokenMetadata metadata = StorageService.instance.getTokenMetadata();
metadata.clearUnsafe();
metadata.updateNormalToken(new LongToken(1000L), InetAddressAndPort.getByName("127.0.0.1"));
metadata.updateNormalToken(new LongToken(2000L), InetAddressAndPort.getByName("127.0.0.2"));
metadata.updateNormalToken(new LongToken(3000L), InetAddressAndPort.getByName("127.0.0.3"));
metadata.updateNormalToken(new LongToken(4000L), InetAddressAndPort.getByName("127.0.0.4"));
Collection<Range<Token>> repairRangeFrom = StorageService.instance.createRepairRangeFrom("1500", "3700");
Assertions.assertThat(repairRangeFrom.size()).as(repairRangeFrom.toString()).isEqualTo(3);
Assertions.assertThat(repairRangeFrom).contains(new Range<Token>(new LongToken(1500L), new LongToken(2000L)));
Assertions.assertThat(repairRangeFrom).contains(new Range<Token>(new LongToken(2000L), new LongToken(3000L)));
Assertions.assertThat(repairRangeFrom).contains(new Range<Token>(new LongToken(3000L), new LongToken(3700L)));
repairRangeFrom = StorageService.instance.createRepairRangeFrom("500", "700");
Assertions.assertThat(repairRangeFrom.size()).as(repairRangeFrom.toString()).isEqualTo(1);
Assertions.assertThat(repairRangeFrom).contains(new Range<Token>(new LongToken(500L), new LongToken(700L)));
repairRangeFrom = StorageService.instance.createRepairRangeFrom("500", "1700");
Assertions.assertThat(repairRangeFrom.size()).as(repairRangeFrom.toString()).isEqualTo(2);
Assertions.assertThat(repairRangeFrom).contains(new Range<Token>(new LongToken(500L), new LongToken(1000L)));
Assertions.assertThat(repairRangeFrom).contains(new Range<Token>(new LongToken(1000L), new LongToken(1700L)));
repairRangeFrom = StorageService.instance.createRepairRangeFrom("2500", "2300");
Assertions.assertThat(repairRangeFrom.size()).as(repairRangeFrom.toString()).isEqualTo(5);
Assertions.assertThat(repairRangeFrom).contains(new Range<Token>(new LongToken(2500L), new LongToken(3000L)));
Assertions.assertThat(repairRangeFrom).contains(new Range<Token>(new LongToken(3000L), new LongToken(4000L)));
Assertions.assertThat(repairRangeFrom).contains(new Range<Token>(new LongToken(4000L), new LongToken(1000L)));
Assertions.assertThat(repairRangeFrom).contains(new Range<Token>(new LongToken(1000L), new LongToken(2000L)));
Assertions.assertThat(repairRangeFrom).contains(new Range<Token>(new LongToken(2000L), new LongToken(2300L)));
repairRangeFrom = StorageService.instance.createRepairRangeFrom("2000", "3000");
Assertions.assertThat(repairRangeFrom.size()).as(repairRangeFrom.toString()).isEqualTo(1);
Assertions.assertThat(repairRangeFrom).contains(new Range<Token>(new LongToken(2000L), new LongToken(3000L)));
repairRangeFrom = StorageService.instance.createRepairRangeFrom("2000", "2000");
Assertions.assertThat(repairRangeFrom).isEmpty();
}
use of org.apache.cassandra.locator.TokenMetadata in project cassandra by apache.
the class StorageServiceServerTest method testPrimaryRangeForEndpointWithinDCWithNetworkTopologyStrategy.
@Test
public void testPrimaryRangeForEndpointWithinDCWithNetworkTopologyStrategy() 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("DC1", "1");
configOptions.put("DC2", "1");
configOptions.put(ReplicationParams.CLASS, "NetworkTopologyStrategy");
Schema.instance.maybeRemoveKeyspaceInstance("Keyspace1");
KeyspaceMetadata meta = KeyspaceMetadata.create("Keyspace1", KeyspaceParams.create(false, configOptions));
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(2);
Assertions.assertThat(primaryRanges).contains(new Range<Token>(new StringToken("D"), new StringToken("A")));
Assertions.assertThat(primaryRanges).contains(new Range<Token>(new StringToken("C"), new StringToken("D")));
primaryRanges = StorageService.instance.getPrimaryRangeForEndpointWithinDC(meta.name, InetAddressAndPort.getByName("127.0.0.2"));
Assertions.assertThat(primaryRanges.size()).as(primaryRanges.toString()).isEqualTo(2);
Assertions.assertThat(primaryRanges).contains(new Range<Token>(new StringToken("A"), new StringToken("B")));
Assertions.assertThat(primaryRanges).contains(new Range<Token>(new StringToken("B"), new StringToken("C")));
primaryRanges = StorageService.instance.getPrimaryRangeForEndpointWithinDC(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.getPrimaryRangeForEndpointWithinDC(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("B"), new StringToken("C")));
Assertions.assertThat(primaryRanges).contains(new Range<Token>(new StringToken("C"), new StringToken("D")));
}
use of org.apache.cassandra.locator.TokenMetadata in project cassandra by apache.
the class WriteResponseHandlerTest method setUpClass.
@BeforeClass
public static void setUpClass() throws Throwable {
SchemaLoader.loadSchema();
DatabaseDescriptor.setPartitionerUnsafe(Murmur3Partitioner.instance);
// Register peers with expected DC for NetworkTopologyStrategy.
TokenMetadata metadata = StorageService.instance.getTokenMetadata();
metadata.clearUnsafe();
metadata.updateHostId(UUID.randomUUID(), InetAddressAndPort.getByName("127.1.0.255"));
metadata.updateHostId(UUID.randomUUID(), InetAddressAndPort.getByName("127.2.0.255"));
DatabaseDescriptor.setEndpointSnitch(new IEndpointSnitch() {
public String getRack(InetAddressAndPort endpoint) {
return null;
}
public String getDatacenter(InetAddressAndPort endpoint) {
byte[] address = endpoint.getAddress().getAddress();
if (address[1] == 1)
return "datacenter1";
else
return "datacenter2";
}
public <C extends ReplicaCollection<? extends C>> C sortedByProximity(InetAddressAndPort address, C replicas) {
return replicas;
}
public int compareEndpoints(InetAddressAndPort target, Replica a1, Replica a2) {
return 0;
}
public void gossiperStarting() {
}
public boolean isWorthMergingForRangeQuery(ReplicaCollection<?> merged, ReplicaCollection<?> l1, ReplicaCollection<?> l2) {
return false;
}
});
DatabaseDescriptor.setBroadcastAddress(InetAddress.getByName("127.1.0.1"));
SchemaLoader.createKeyspace("Foo", KeyspaceParams.nts("datacenter1", 3, "datacenter2", 3), SchemaLoader.standardCFMD("Foo", "Bar"));
ks = Keyspace.open("Foo");
cfs = ks.getColumnFamilyStore("Bar");
targets = EndpointsForToken.of(DatabaseDescriptor.getPartitioner().getToken(ByteBufferUtil.bytes(0)), full("127.1.0.255"), full("127.1.0.254"), full("127.1.0.253"), full("127.2.0.255"), full("127.2.0.254"), full("127.2.0.253"));
pending = EndpointsForToken.empty(DatabaseDescriptor.getPartitioner().getToken(ByteBufferUtil.bytes(0)));
}
use of org.apache.cassandra.locator.TokenMetadata in project cassandra by apache.
the class ActiveRepairServiceTest method addTokens.
Set<InetAddressAndPort> addTokens(int max) throws Throwable {
TokenMetadata tmd = StorageService.instance.getTokenMetadata();
Set<InetAddressAndPort> endpoints = new HashSet<>();
for (int i = 1; i <= max; i++) {
InetAddressAndPort endpoint = InetAddressAndPort.getByName("127.0.0." + i);
tmd.updateNormalToken(tmd.partitioner.getRandomToken(), endpoint);
endpoints.add(endpoint);
}
return endpoints;
}
Aggregations