use of org.apache.cassandra.locator.TokenMetadata in project cassandra by apache.
the class ViewUtilsTest method testBaseTokenDoesNotBelongToLocalReplicaShouldReturnEmpty.
@Test
public void testBaseTokenDoesNotBelongToLocalReplicaShouldReturnEmpty() 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> replicationMap = new HashMap<>();
replicationMap.put(ReplicationParams.CLASS, NetworkTopologyStrategy.class.getName());
replicationMap.put("DC1", "1");
replicationMap.put("DC2", "1");
Schema.instance.maybeRemoveKeyspaceInstance("Keyspace1");
KeyspaceMetadata meta = KeyspaceMetadata.create("Keyspace1", KeyspaceParams.create(false, replicationMap));
Schema.instance.load(meta);
Optional<Replica> naturalEndpoint = ViewUtils.getViewNaturalEndpoint(Keyspace.open("Keyspace1").getReplicationStrategy(), new StringToken("AB"), new StringToken("BB"));
Assert.assertFalse(naturalEndpoint.isPresent());
}
use of org.apache.cassandra.locator.TokenMetadata in project cassandra by apache.
the class TokenAllocationTest method testAllocateTokensNetworkStrategy.
public void testAllocateTokensNetworkStrategy(int rackCount, int replicas) throws UnknownHostException {
IEndpointSnitch oldSnitch = DatabaseDescriptor.getEndpointSnitch();
try {
DatabaseDescriptor.setEndpointSnitch(new RackInferringSnitch());
int vn = 16;
String ks = "TokenAllocationTestNTSKeyspace" + rackCount + replicas;
String dc = "1";
// Register peers with expected DC for NetworkTopologyStrategy.
TokenMetadata metadata = StorageService.instance.getTokenMetadata();
metadata.clearUnsafe();
metadata.updateHostId(UUID.randomUUID(), InetAddressAndPort.getByName("127.1.0.99"));
metadata.updateHostId(UUID.randomUUID(), InetAddressAndPort.getByName("127.15.0.99"));
SchemaLoader.createKeyspace(ks, KeyspaceParams.nts(dc, replicas, "15", 15), SchemaLoader.standardCFMD(ks, "Standard1"));
TokenMetadata tm = StorageService.instance.getTokenMetadata();
tm.clearUnsafe();
for (int i = 0; i < rackCount; ++i) generateFakeEndpoints(tm, 10, vn, dc, Integer.toString(i));
InetAddressAndPort addr = InetAddressAndPort.getByName("127." + dc + ".0.99");
allocateTokensForKeyspace(vn, ks, tm, addr);
// Note: Not matching replication factor in second datacentre, but this should not affect us.
} finally {
DatabaseDescriptor.setEndpointSnitch(oldSnitch);
}
}
use of org.apache.cassandra.locator.TokenMetadata in project cassandra by apache.
the class TokenAllocationTest method testAllocateTokensForLocalRF.
@Test
public void testAllocateTokensForLocalRF() throws UnknownHostException {
int vn = 16;
int allocateTokensForLocalRf = 3;
TokenMetadata tm = new TokenMetadata();
generateFakeEndpoints(tm, 10, vn);
InetAddressAndPort addr = FBUtilities.getBroadcastAddressAndPort();
allocateTokensForLocalReplicationFactor(vn, allocateTokensForLocalRf, tm, addr);
}
use of org.apache.cassandra.locator.TokenMetadata in project cassandra by apache.
the class TokenAllocationTest method testAllocateTokensMultipleKeyspaces.
/**
* TODO: This scenario isn't supported very well. Investigate a multi-keyspace version of the algorithm.
*/
@Test
public void testAllocateTokensMultipleKeyspaces() throws UnknownHostException {
final int TOKENS = 16;
TokenMetadata tokenMetadata = new TokenMetadata();
generateFakeEndpoints(tokenMetadata, 10, TOKENS);
// Do not clone token metadata so tokens allocated by different allocators are reflected on the parent TokenMetadata
TokenAllocation rf2Allocator = createForTest(tokenMetadata, 2, TOKENS);
TokenAllocation rf3Allocator = createForTest(tokenMetadata, 3, TOKENS);
SummaryStatistics rf2StatsBefore = rf2Allocator.getAllocationRingOwnership(FBUtilities.getBroadcastAddressAndPort());
SummaryStatistics rf3StatsBefore = rf3Allocator.getAllocationRingOwnership(FBUtilities.getBroadcastAddressAndPort());
TokenAllocation current = rf3Allocator;
TokenAllocation next = rf2Allocator;
for (int i = 11; i <= 20; ++i) {
InetAddressAndPort endpoint = InetAddressAndPort.getByName("127.0.0." + (i + 1));
Collection<Token> tokens = current.allocate(endpoint);
// Update tokens on next to verify ownership calculation below
next.tokenMetadata.updateNormalTokens(tokens, endpoint);
TokenAllocation tmp = current;
current = next;
next = tmp;
}
SummaryStatistics rf2StatsAfter = rf2Allocator.getAllocationRingOwnership(FBUtilities.getBroadcastAddressAndPort());
SummaryStatistics rf3StatsAfter = rf3Allocator.getAllocationRingOwnership(FBUtilities.getBroadcastAddressAndPort());
verifyImprovement(rf2StatsBefore, rf2StatsAfter);
verifyImprovement(rf3StatsBefore, rf3StatsAfter);
}
use of org.apache.cassandra.locator.TokenMetadata in project cassandra by apache.
the class TokenAllocationTest method testAllocateTokensForKeyspace.
@Test
public void testAllocateTokensForKeyspace() throws UnknownHostException {
int vn = 16;
String ks = "TokenAllocationTestKeyspace3";
TokenMetadata tm = new TokenMetadata();
generateFakeEndpoints(tm, 10, vn);
InetAddressAndPort addr = FBUtilities.getBroadcastAddressAndPort();
allocateTokensForKeyspace(vn, ks, tm, addr);
}
Aggregations