use of org.apache.cassandra.locator.TokenMetadata in project cassandra by apache.
the class RangeRelocator method calculateRangesToStreamWithEndpoints.
/**
* calculating endpoints to stream current ranges to if needed
* in some situations node will handle current ranges as part of the new ranges
*/
public static RangesByEndpoint calculateRangesToStreamWithEndpoints(RangesAtEndpoint streamRanges, AbstractReplicationStrategy strat, TokenMetadata tmdBefore, TokenMetadata tmdAfter) {
RangesByEndpoint.Builder endpointRanges = new RangesByEndpoint.Builder();
for (Replica toStream : streamRanges) {
// If the range we are sending is full only send it to the new full replica
// There will also be a new transient replica we need to send the data to, but not
// the repaired data
EndpointsForRange oldEndpoints = strat.calculateNaturalReplicas(toStream.range().right, tmdBefore);
EndpointsForRange newEndpoints = strat.calculateNaturalReplicas(toStream.range().right, tmdAfter);
logger.debug("Need to stream {}, current endpoints {}, new endpoints {}", toStream, oldEndpoints, newEndpoints);
for (Replica newEndpoint : newEndpoints) {
Replica oldEndpoint = oldEndpoints.byEndpoint().get(newEndpoint.endpoint());
// Nothing to do
if (newEndpoint.equals(oldEndpoint))
continue;
// Completely new range for this endpoint
if (oldEndpoint == null) {
if (toStream.isTransient() && newEndpoint.isFull())
throw new AssertionError(String.format("Need to stream %s, but only have %s which is transient and not full", newEndpoint, toStream));
for (Range<Token> intersection : newEndpoint.range().intersectionWith(toStream.range())) {
endpointRanges.put(newEndpoint.endpoint(), newEndpoint.decorateSubrange(intersection));
}
} else {
Set<Range<Token>> subsToStream = Collections.singleton(toStream.range());
// First subtract what we already have
if (oldEndpoint.isFull() == newEndpoint.isFull() || oldEndpoint.isFull())
subsToStream = toStream.range().subtract(oldEndpoint.range());
// Now we only stream what is still replicated
subsToStream.stream().flatMap(range -> range.intersectionWith(newEndpoint.range()).stream()).forEach(tokenRange -> endpointRanges.put(newEndpoint.endpoint(), newEndpoint.decorateSubrange(tokenRange)));
}
}
}
return endpointRanges.build();
}
use of org.apache.cassandra.locator.TokenMetadata in project cassandra by apache.
the class CleanupTransientTest method setup.
@BeforeClass
public static void setup() throws Exception {
DatabaseDescriptor.daemonInitialization();
DatabaseDescriptor.setTransientReplicationEnabledUnsafe(true);
oldPartitioner = StorageService.instance.setPartitionerUnsafe(partitioner);
SchemaLoader.prepareServer();
SchemaLoader.createKeyspace(KEYSPACE1, KeyspaceParams.simple("2/1"), SchemaLoader.standardCFMD(KEYSPACE1, CF_STANDARD1), SchemaLoader.compositeIndexCFMD(KEYSPACE1, CF_INDEXED1, true));
StorageService ss = StorageService.instance;
final int RING_SIZE = 2;
TokenMetadata tmd = ss.getTokenMetadata();
tmd.clearUnsafe();
ArrayList<Token> endpointTokens = new ArrayList<>();
ArrayList<Token> keyTokens = new ArrayList<>();
List<InetAddressAndPort> hosts = new ArrayList<>();
List<UUID> hostIds = new ArrayList<>();
endpointTokens.add(RandomPartitioner.MINIMUM);
endpointTokens.add(RandomPartitioner.instance.midpoint(RandomPartitioner.MINIMUM, new RandomPartitioner.BigIntegerToken(RandomPartitioner.MAXIMUM)));
Util.createInitialRing(ss, partitioner, endpointTokens, keyTokens, hosts, hostIds, RING_SIZE);
PendingRangeCalculatorService.instance.blockUntilFinished();
DatabaseDescriptor.setEndpointSnitch(new AbstractNetworkTopologySnitch() {
@Override
public String getRack(InetAddressAndPort endpoint) {
return "RC1";
}
@Override
public String getDatacenter(InetAddressAndPort endpoint) {
return "DC1";
}
});
}
use of org.apache.cassandra.locator.TokenMetadata in project cassandra by apache.
the class AlterTest method testCreateSimpleAlterNTSDefaults.
@Test
public void testCreateSimpleAlterNTSDefaults() throws Throwable {
TokenMetadata metadata = StorageService.instance.getTokenMetadata();
metadata.clearUnsafe();
InetAddressAndPort local = FBUtilities.getBroadcastAddressAndPort();
InetAddressAndPort remote = InetAddressAndPort.getByName("127.0.0.4");
metadata.updateHostId(UUID.randomUUID(), local);
metadata.updateNormalToken(new OrderPreservingPartitioner.StringToken("A"), local);
metadata.updateHostId(UUID.randomUUID(), remote);
metadata.updateNormalToken(new OrderPreservingPartitioner.StringToken("B"), remote);
// Let's create a keyspace first with SimpleStrategy
String ks1 = createKeyspace("CREATE KEYSPACE %s WITH replication={ 'class' : 'SimpleStrategy', 'replication_factor' : 2}");
assertRowsIgnoringOrderAndExtra(execute("SELECT keyspace_name, durable_writes, replication FROM system_schema.keyspaces"), row(KEYSPACE, true, map("class", "org.apache.cassandra.locator.SimpleStrategy", "replication_factor", "1")), row(KEYSPACE_PER_TEST, true, map("class", "org.apache.cassandra.locator.SimpleStrategy", "replication_factor", "1")), row(ks1, true, map("class", "org.apache.cassandra.locator.SimpleStrategy", "replication_factor", "2")));
// Now we should be able to ALTER to NetworkTopologyStrategy directly from SimpleStrategy without supplying replication_factor
schemaChange("ALTER KEYSPACE " + ks1 + " WITH replication = { 'class' : 'NetworkTopologyStrategy'}");
assertRowsIgnoringOrderAndExtra(execute("SELECT keyspace_name, durable_writes, replication FROM system_schema.keyspaces"), row(KEYSPACE, true, map("class", "org.apache.cassandra.locator.SimpleStrategy", "replication_factor", "1")), row(KEYSPACE_PER_TEST, true, map("class", "org.apache.cassandra.locator.SimpleStrategy", "replication_factor", "1")), row(ks1, true, map("class", "org.apache.cassandra.locator.NetworkTopologyStrategy", DATA_CENTER, "2", DATA_CENTER_REMOTE, "2")));
schemaChange("ALTER KEYSPACE " + ks1 + " WITH replication = { 'class' : 'SimpleStrategy', 'replication_factor' : 3}");
schemaChange("ALTER KEYSPACE " + ks1 + " WITH replication = { 'class' : 'NetworkTopologyStrategy', 'replication_factor': 2}");
assertRowsIgnoringOrderAndExtra(execute("SELECT keyspace_name, durable_writes, replication FROM system_schema.keyspaces"), row(KEYSPACE, true, map("class", "org.apache.cassandra.locator.SimpleStrategy", "replication_factor", "1")), row(KEYSPACE_PER_TEST, true, map("class", "org.apache.cassandra.locator.SimpleStrategy", "replication_factor", "1")), row(ks1, true, map("class", "org.apache.cassandra.locator.NetworkTopologyStrategy", DATA_CENTER, "2", DATA_CENTER_REMOTE, "2")));
}
use of org.apache.cassandra.locator.TokenMetadata in project cassandra by apache.
the class CleanupTest method testCleanupWithIndexes.
@Test
public void testCleanupWithIndexes() throws IOException, ExecutionException, InterruptedException {
Keyspace keyspace = Keyspace.open(KEYSPACE1);
ColumnFamilyStore cfs = keyspace.getColumnFamilyStore(CF_INDEXED1);
// insert data and verify we get it back w/ range query
fillCF(cfs, "birthdate", LOOPS);
assertEquals(LOOPS, Util.getAll(Util.cmd(cfs).build()).size());
ColumnMetadata cdef = cfs.metadata().getColumn(COLUMN);
String indexName = "birthdate_key_index";
long start = nanoTime();
while (!cfs.getBuiltIndexes().contains(indexName) && nanoTime() - start < TimeUnit.SECONDS.toNanos(10)) Thread.sleep(10);
RowFilter cf = RowFilter.create();
cf.add(cdef, Operator.EQ, VALUE);
assertEquals(LOOPS, Util.getAll(Util.cmd(cfs).filterOn("birthdate", Operator.EQ, VALUE).build()).size());
// we don't allow cleanup when the local host has no range to avoid wipping up all data when a node has not join the ring.
// So to make sure cleanup erase everything here, we give the localhost the tiniest possible range.
TokenMetadata tmd = StorageService.instance.getTokenMetadata();
byte[] tk1 = new byte[1], tk2 = new byte[1];
tk1[0] = 2;
tk2[0] = 1;
tmd.updateNormalToken(new BytesToken(tk1), InetAddressAndPort.getByName("127.0.0.1"));
tmd.updateNormalToken(new BytesToken(tk2), InetAddressAndPort.getByName("127.0.0.2"));
CompactionManager.instance.performCleanup(cfs, 2);
// row data should be gone
assertEquals(0, Util.getAll(Util.cmd(cfs).build()).size());
// not only should it be gone but there should be no data on disk, not even tombstones
assert cfs.getLiveSSTables().isEmpty();
// 2ary indexes should result in no results, too (although tombstones won't be gone until compacted)
assertEquals(0, Util.getAll(Util.cmd(cfs).filterOn("birthdate", Operator.EQ, VALUE).build()).size());
}
use of org.apache.cassandra.locator.TokenMetadata in project cassandra by apache.
the class CleanupTest method testCleanupSkippingSSTablesHelper.
public void testCleanupSkippingSSTablesHelper(boolean repaired) throws UnknownHostException, ExecutionException, InterruptedException {
Keyspace keyspace = Keyspace.open(KEYSPACE3);
ColumnFamilyStore cfs = keyspace.getColumnFamilyStore(CF_STANDARD3);
cfs.disableAutoCompaction();
TokenMetadata tmd = StorageService.instance.getTokenMetadata();
tmd.clearUnsafe();
tmd.updateNormalToken(token(new byte[] { 50 }), InetAddressAndPort.getByName("127.0.0.1"));
for (byte i = 0; i < 100; i++) {
new RowUpdateBuilder(cfs.metadata(), System.currentTimeMillis(), ByteBuffer.wrap(new byte[] { i })).clustering(COLUMN).add("val", VALUE).build().applyUnsafe();
cfs.forceBlockingFlush();
}
Set<SSTableReader> beforeFirstCleanup = Sets.newHashSet(cfs.getLiveSSTables());
if (repaired) {
beforeFirstCleanup.forEach((sstable) -> {
try {
sstable.descriptor.getMetadataSerializer().mutateRepairMetadata(sstable.descriptor, System.currentTimeMillis(), null, false);
sstable.reloadSSTableMetadata();
} catch (Exception e) {
throw new RuntimeException(e);
}
});
}
// single token - 127.0.0.1 owns everything, cleanup should be noop
cfs.forceCleanup(2);
assertEquals(beforeFirstCleanup, cfs.getLiveSSTables());
tmd.updateNormalToken(token(new byte[] { 120 }), InetAddressAndPort.getByName("127.0.0.2"));
cfs.forceCleanup(2);
for (SSTableReader sstable : cfs.getLiveSSTables()) {
// single-token sstables
assertEquals(sstable.first, sstable.last);
assertTrue(sstable.first.getToken().compareTo(token(new byte[] { 50 })) <= 0);
// with single-token sstables they should all either be skipped or dropped:
assertTrue(beforeFirstCleanup.contains(sstable));
}
}
Aggregations