use of org.apache.cassandra.locator.TokenMetadata in project cassandra by apache.
the class BootStrapperTest method testAllocateTokensMultipleKeyspaces.
@Test
public void testAllocateTokensMultipleKeyspaces() throws UnknownHostException {
// TODO: This scenario isn't supported very well. Investigate a multi-keyspace version of the algorithm.
int vn = 16;
// RF = 3
String ks3 = "BootStrapperTestKeyspace4";
// RF = 2
String ks2 = "BootStrapperTestKeyspace5";
TokenMetadata tm = new TokenMetadata();
generateFakeEndpoints(tm, 10, vn);
InetAddress dcaddr = FBUtilities.getBroadcastAddress();
SummaryStatistics os3 = TokenAllocation.replicatedOwnershipStats(tm, Keyspace.open(ks3).getReplicationStrategy(), dcaddr);
SummaryStatistics os2 = TokenAllocation.replicatedOwnershipStats(tm, Keyspace.open(ks2).getReplicationStrategy(), dcaddr);
String cks = ks3;
String nks = ks2;
for (int i = 11; i <= 20; ++i) {
allocateTokensForNode(vn, cks, tm, InetAddress.getByName("127.0.0." + (i + 1)));
String t = cks;
cks = nks;
nks = t;
}
SummaryStatistics ns3 = TokenAllocation.replicatedOwnershipStats(tm, Keyspace.open(ks3).getReplicationStrategy(), dcaddr);
SummaryStatistics ns2 = TokenAllocation.replicatedOwnershipStats(tm, Keyspace.open(ks2).getReplicationStrategy(), dcaddr);
verifyImprovement(os3, ns3);
verifyImprovement(os2, ns2);
}
use of org.apache.cassandra.locator.TokenMetadata in project cassandra by apache.
the class StorageProxyTest method beforeClass.
@BeforeClass
public static void beforeClass() throws Throwable {
DatabaseDescriptor.daemonInitialization();
DatabaseDescriptor.getHintsDirectory().mkdir();
TokenMetadata tmd = StorageService.instance.getTokenMetadata();
tmd.updateNormalToken(token("1"), InetAddress.getByName("127.0.0.1"));
tmd.updateNormalToken(token("6"), InetAddress.getByName("127.0.0.6"));
}
use of org.apache.cassandra.locator.TokenMetadata in project cassandra by apache.
the class TokenAllocation method allocateTokens.
public static Collection<Token> allocateTokens(final TokenMetadata tokenMetadata, final AbstractReplicationStrategy rs, final InetAddress endpoint, int numTokens) {
TokenMetadata tokenMetadataCopy = tokenMetadata.cloneOnlyTokenMap();
StrategyAdapter strategy = getStrategy(tokenMetadataCopy, rs, endpoint);
Collection<Token> tokens = create(tokenMetadata, strategy).addUnit(endpoint, numTokens);
tokens = adjustForCrossDatacenterClashes(tokenMetadata, strategy, tokens);
if (logger.isWarnEnabled()) {
logger.warn("Selected tokens {}", tokens);
SummaryStatistics os = replicatedOwnershipStats(tokenMetadataCopy, rs, endpoint);
tokenMetadataCopy.updateNormalTokens(tokens, endpoint);
SummaryStatistics ns = replicatedOwnershipStats(tokenMetadataCopy, rs, endpoint);
logger.warn("Replicated node load in datacentre before allocation {}", statToString(os));
logger.warn("Replicated node load in datacentre after allocation {}", statToString(ns));
// TODO: Is it worth doing the replicated ownership calculation always to be able to raise this alarm?
if (ns.getStandardDeviation() > os.getStandardDeviation())
logger.warn("Unexpected growth in standard deviation after allocation.");
}
return tokens;
}
use of org.apache.cassandra.locator.TokenMetadata in project cassandra by apache.
the class ReplicaPlanIterator method getRestrictedRanges.
/**
* Compute all ranges we're going to query, in sorted order. Nodes can be replica destinations for many ranges,
* so we need to restrict each scan to the specific range we want, or else we'd get duplicate results.
*/
private static List<AbstractBounds<PartitionPosition>> getRestrictedRanges(final AbstractBounds<PartitionPosition> queryRange) {
// special case for bounds containing exactly 1 (non-minimum) token
if (queryRange instanceof Bounds && queryRange.left.equals(queryRange.right) && !queryRange.left.isMinimum()) {
return Collections.singletonList(queryRange);
}
TokenMetadata tokenMetadata = StorageService.instance.getTokenMetadata();
List<AbstractBounds<PartitionPosition>> ranges = new ArrayList<>();
// divide the queryRange into pieces delimited by the ring and minimum tokens
Iterator<Token> ringIter = TokenMetadata.ringIterator(tokenMetadata.sortedTokens(), queryRange.left.getToken(), true);
AbstractBounds<PartitionPosition> remainder = queryRange;
while (ringIter.hasNext()) {
/*
* remainder is a range/bounds of partition positions and we want to split it with a token. We want to split
* using the key returned by token.maxKeyBound. For instance, if remainder is [DK(10, 'foo'), DK(20, 'bar')],
* and we have 3 nodes with tokens 0, 15, 30, we want to split remainder to A=[DK(10, 'foo'), 15] and
* B=(15, DK(20, 'bar')]. But since we can't mix tokens and keys at the same time in a range, we use
* 15.maxKeyBound() to have A include all keys having 15 as token and B include none of those (since that is
* what our node owns).
*/
Token upperBoundToken = ringIter.next();
PartitionPosition upperBound = upperBoundToken.maxKeyBound();
if (!remainder.left.equals(upperBound) && !remainder.contains(upperBound))
// no more splits
break;
Pair<AbstractBounds<PartitionPosition>, AbstractBounds<PartitionPosition>> splits = remainder.split(upperBound);
if (splits == null)
continue;
ranges.add(splits.left);
remainder = splits.right;
}
ranges.add(remainder);
return ranges;
}
use of org.apache.cassandra.locator.TokenMetadata in project cassandra by apache.
the class VerifyTest method testOutOfRangeTokens.
@Test(expected = RuntimeException.class)
public void testOutOfRangeTokens() throws IOException {
Keyspace keyspace = Keyspace.open(KEYSPACE);
ColumnFamilyStore cfs = keyspace.getColumnFamilyStore(CF);
fillCF(cfs, 100);
TokenMetadata tmd = StorageService.instance.getTokenMetadata();
byte[] tk1 = new byte[1], tk2 = new byte[1];
tk1[0] = 2;
tk2[0] = 1;
tmd.updateNormalToken(new ByteOrderedPartitioner.BytesToken(tk1), InetAddressAndPort.getByName("127.0.0.1"));
tmd.updateNormalToken(new ByteOrderedPartitioner.BytesToken(tk2), InetAddressAndPort.getByName("127.0.0.2"));
SSTableReader sstable = cfs.getLiveSSTables().iterator().next();
try (Verifier verifier = new Verifier(cfs, sstable, false, Verifier.options().checkOwnsTokens(true).extendedVerification(true).build())) {
verifier.verify();
} finally {
StorageService.instance.getTokenMetadata().clearUnsafe();
}
}
Aggregations