Search in sources :

Example 61 with TokenMetadata

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();
}
Also used : InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) Arrays(java.util.Arrays) LoggerFactory(org.slf4j.LoggerFactory) Range(org.apache.cassandra.dht.Range) Multimap(com.google.common.collect.Multimap) Future(java.util.concurrent.Future) Token(org.apache.cassandra.dht.Token) TokenMetadata(org.apache.cassandra.locator.TokenMetadata) Pair(org.apache.cassandra.utils.Pair) RangeStreamer(org.apache.cassandra.dht.RangeStreamer) DatabaseDescriptor(org.apache.cassandra.config.DatabaseDescriptor) StreamOperation(org.apache.cassandra.streaming.StreamOperation) Keyspace(org.apache.cassandra.db.Keyspace) EndpointsForRange(org.apache.cassandra.locator.EndpointsForRange) Logger(org.slf4j.Logger) FBUtilities(org.apache.cassandra.utils.FBUtilities) Collection(java.util.Collection) Set(java.util.Set) RangesByEndpoint(org.apache.cassandra.locator.RangesByEndpoint) RangesAtEndpoint(org.apache.cassandra.locator.RangesAtEndpoint) Replica(org.apache.cassandra.locator.Replica) StreamState(org.apache.cassandra.streaming.StreamState) List(java.util.List) AbstractReplicationStrategy(org.apache.cassandra.locator.AbstractReplicationStrategy) StreamPlan(org.apache.cassandra.streaming.StreamPlan) EndpointsByReplica(org.apache.cassandra.locator.EndpointsByReplica) VisibleForTesting(com.google.common.annotations.VisibleForTesting) FailureDetector(org.apache.cassandra.gms.FailureDetector) Collections(java.util.Collections) EndpointsForRange(org.apache.cassandra.locator.EndpointsForRange) Token(org.apache.cassandra.dht.Token) Range(org.apache.cassandra.dht.Range) EndpointsForRange(org.apache.cassandra.locator.EndpointsForRange) Replica(org.apache.cassandra.locator.Replica) EndpointsByReplica(org.apache.cassandra.locator.EndpointsByReplica) RangesByEndpoint(org.apache.cassandra.locator.RangesByEndpoint)

Example 62 with TokenMetadata

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";
        }
    });
}
Also used : InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) ArrayList(java.util.ArrayList) Token(org.apache.cassandra.dht.Token) TokenMetadata(org.apache.cassandra.locator.TokenMetadata) RangesAtEndpoint(org.apache.cassandra.locator.RangesAtEndpoint) StorageService(org.apache.cassandra.service.StorageService) AbstractNetworkTopologySnitch(org.apache.cassandra.locator.AbstractNetworkTopologySnitch) UUID(java.util.UUID) BeforeClass(org.junit.BeforeClass)

Example 63 with TokenMetadata

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")));
}
Also used : OrderPreservingPartitioner(org.apache.cassandra.dht.OrderPreservingPartitioner) InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) TokenMetadata(org.apache.cassandra.locator.TokenMetadata) Test(org.junit.Test)

Example 64 with TokenMetadata

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());
}
Also used : ColumnMetadata(org.apache.cassandra.schema.ColumnMetadata) RowFilter(org.apache.cassandra.db.filter.RowFilter) BytesToken(org.apache.cassandra.dht.ByteOrderedPartitioner.BytesToken) TokenMetadata(org.apache.cassandra.locator.TokenMetadata) Test(org.junit.Test)

Example 65 with TokenMetadata

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));
    }
}
Also used : SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) TokenMetadata(org.apache.cassandra.locator.TokenMetadata) ConfigurationException(org.apache.cassandra.exceptions.ConfigurationException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

TokenMetadata (org.apache.cassandra.locator.TokenMetadata)109 Test (org.junit.Test)66 InetAddressAndPort (org.apache.cassandra.locator.InetAddressAndPort)43 Token (org.apache.cassandra.dht.Token)37 VersionedValue (org.apache.cassandra.gms.VersionedValue)22 InetAddress (java.net.InetAddress)21 Range (org.apache.cassandra.dht.Range)19 AbstractReplicationStrategy (org.apache.cassandra.locator.AbstractReplicationStrategy)19 StringToken (org.apache.cassandra.dht.OrderPreservingPartitioner.StringToken)13 BigIntegerToken (org.apache.cassandra.dht.RandomPartitioner.BigIntegerToken)13 Replica (org.apache.cassandra.locator.Replica)13 RangesAtEndpoint (org.apache.cassandra.locator.RangesAtEndpoint)11 IPartitioner (org.apache.cassandra.dht.IPartitioner)10 LongToken (org.apache.cassandra.dht.Murmur3Partitioner.LongToken)10 SSTableReader (org.apache.cassandra.io.sstable.format.SSTableReader)8 IEndpointSnitch (org.apache.cassandra.locator.IEndpointSnitch)8 RangesByEndpoint (org.apache.cassandra.locator.RangesByEndpoint)8 BeforeClass (org.junit.BeforeClass)8 Multimap (com.google.common.collect.Multimap)7 EndpointsForToken (org.apache.cassandra.locator.EndpointsForToken)7