Search in sources :

Example 6 with DecoratedKey

use of org.apache.cassandra.db.DecoratedKey in project cassandra by apache.

the class SSTableRewriter method moveStarts.

/**
 * Replace the readers we are rewriting with cloneWithNewStart, reclaiming any page cache that is no longer
 * needed, and transferring any key cache entries over to the new reader, expiring them from the old. if reset
 * is true, we are instead restoring the starts of the readers from before the rewriting began
 *
 * note that we replace an existing sstable with a new *instance* of the same sstable, the replacement
 * sstable .equals() the old one, BUT, it is a new instance, so, for example, since we releaseReference() on the old
 * one, the old *instance* will have reference count == 0 and if we were to start a new compaction with that old
 * instance, we would get exceptions.
 *
 * @param newReader the rewritten reader that replaces them for this region
 * @param lowerbound if !reset, must be non-null, and marks the exclusive lowerbound of the start for each sstable
 */
private void moveStarts(SSTableReader newReader, DecoratedKey lowerbound) {
    if (transaction.isOffline() || preemptiveOpenInterval == Long.MAX_VALUE)
        return;
    newReader.setupOnline();
    List<DecoratedKey> invalidateKeys = null;
    if (!cachedKeys.isEmpty()) {
        invalidateKeys = new ArrayList<>(cachedKeys.size());
        for (Map.Entry<DecoratedKey, RowIndexEntry> cacheKey : cachedKeys.entrySet()) {
            invalidateKeys.add(cacheKey.getKey());
            newReader.cacheKey(cacheKey.getKey(), cacheKey.getValue());
        }
    }
    cachedKeys.clear();
    for (SSTableReader sstable : transaction.originals()) {
        // we call getCurrentReplacement() to support multiple rewriters operating over the same source readers at once.
        // note: only one such writer should be written to at any moment
        final SSTableReader latest = transaction.current(sstable);
        // skip any sstables that we know to already be shadowed
        if (latest.first.compareTo(lowerbound) > 0)
            continue;
        Runnable runOnClose = invalidateKeys != null ? new InvalidateKeys(latest, invalidateKeys) : null;
        if (lowerbound.compareTo(latest.last) >= 0) {
            if (!transaction.isObsolete(latest)) {
                if (runOnClose != null) {
                    latest.runOnClose(runOnClose);
                }
                transaction.obsolete(latest);
            }
            continue;
        }
        DecoratedKey newStart = latest.firstKeyBeyond(lowerbound);
        assert newStart != null;
        SSTableReader replacement = latest.cloneWithNewStart(newStart, runOnClose);
        transaction.update(replacement, true);
    }
}
Also used : SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) DecoratedKey(org.apache.cassandra.db.DecoratedKey) RowIndexEntry(org.apache.cassandra.db.RowIndexEntry)

Example 7 with DecoratedKey

use of org.apache.cassandra.db.DecoratedKey in project cassandra by apache.

the class BatchlogManagerTest method testDelete.

@Test
public void testDelete() {
    ColumnFamilyStore cfs = Keyspace.open(KEYSPACE1).getColumnFamilyStore(CF_STANDARD1);
    TableMetadata cfm = cfs.metadata();
    new RowUpdateBuilder(cfm, FBUtilities.timestampMicros(), ByteBufferUtil.bytes("1234")).clustering("c").add("val", "val" + 1234).build().applyUnsafe();
    DecoratedKey dk = cfs.decorateKey(ByteBufferUtil.bytes("1234"));
    ImmutableBTreePartition results = Util.getOnlyPartitionUnfiltered(Util.cmd(cfs, dk).build());
    Iterator<Row> iter = results.iterator();
    assert iter.hasNext();
    Mutation mutation = new Mutation(PartitionUpdate.fullPartitionDelete(cfm, dk, FBUtilities.timestampMicros(), FBUtilities.nowInSeconds()));
    mutation.applyUnsafe();
    Util.assertEmpty(Util.cmd(cfs, dk).build());
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) RowUpdateBuilder(org.apache.cassandra.db.RowUpdateBuilder) DecoratedKey(org.apache.cassandra.db.DecoratedKey) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) Row(org.apache.cassandra.db.rows.Row) Mutation(org.apache.cassandra.db.Mutation) ImmutableBTreePartition(org.apache.cassandra.db.partitions.ImmutableBTreePartition)

Example 8 with DecoratedKey

use of org.apache.cassandra.db.DecoratedKey in project cassandra by apache.

the class DateTieredCompactionStrategyTest method testDropExpiredSSTables.

@Test
public void testDropExpiredSSTables() throws InterruptedException {
    Keyspace keyspace = Keyspace.open(KEYSPACE1);
    ColumnFamilyStore cfs = keyspace.getColumnFamilyStore(CF_STANDARD1);
    cfs.disableAutoCompaction();
    ByteBuffer value = ByteBuffer.wrap(new byte[100]);
    // create 2 sstables
    DecoratedKey key = Util.dk(String.valueOf("expired"));
    new RowUpdateBuilder(cfs.metadata(), System.currentTimeMillis(), 1, key.getKey()).clustering("column").add("val", value).build().applyUnsafe();
    cfs.forceBlockingFlush();
    SSTableReader expiredSSTable = cfs.getLiveSSTables().iterator().next();
    Thread.sleep(10);
    key = Util.dk(String.valueOf("nonexpired"));
    new RowUpdateBuilder(cfs.metadata(), System.currentTimeMillis(), key.getKey()).clustering("column").add("val", value).build().applyUnsafe();
    cfs.forceBlockingFlush();
    assertEquals(cfs.getLiveSSTables().size(), 2);
    Map<String, String> options = new HashMap<>();
    options.put(DateTieredCompactionStrategyOptions.BASE_TIME_KEY, "30");
    options.put(DateTieredCompactionStrategyOptions.TIMESTAMP_RESOLUTION_KEY, "MILLISECONDS");
    options.put(DateTieredCompactionStrategyOptions.MAX_SSTABLE_AGE_KEY, Double.toString((1d / (24 * 60 * 60))));
    options.put(DateTieredCompactionStrategyOptions.EXPIRED_SSTABLE_CHECK_FREQUENCY_SECONDS_KEY, "0");
    DateTieredCompactionStrategy dtcs = new DateTieredCompactionStrategy(cfs, options);
    for (SSTableReader sstable : cfs.getLiveSSTables()) dtcs.addSSTable(sstable);
    dtcs.startup();
    assertNull(dtcs.getNextBackgroundTask((int) (System.currentTimeMillis() / 1000)));
    Thread.sleep(2000);
    AbstractCompactionTask t = dtcs.getNextBackgroundTask((int) (System.currentTimeMillis() / 1000));
    assertNotNull(t);
    assertEquals(1, Iterables.size(t.transaction.originals()));
    SSTableReader sstable = t.transaction.originals().iterator().next();
    assertEquals(sstable, expiredSSTable);
    t.transaction.abort();
    cfs.truncateBlocking();
}
Also used : SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) RowUpdateBuilder(org.apache.cassandra.db.RowUpdateBuilder) Keyspace(org.apache.cassandra.db.Keyspace) DecoratedKey(org.apache.cassandra.db.DecoratedKey) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 9 with DecoratedKey

use of org.apache.cassandra.db.DecoratedKey in project cassandra by apache.

the class DateTieredCompactionStrategyTest method testPrepBucket.

@Test
public void testPrepBucket() {
    Keyspace keyspace = Keyspace.open(KEYSPACE1);
    ColumnFamilyStore cfs = keyspace.getColumnFamilyStore(CF_STANDARD1);
    cfs.disableAutoCompaction();
    ByteBuffer value = ByteBuffer.wrap(new byte[100]);
    // create 3 sstables
    int numSSTables = 3;
    for (int r = 0; r < numSSTables; r++) {
        DecoratedKey key = Util.dk(String.valueOf(r));
        new RowUpdateBuilder(cfs.metadata(), r, key.getKey()).clustering("column").add("val", value).build().applyUnsafe();
        cfs.forceBlockingFlush();
    }
    cfs.forceBlockingFlush();
    List<SSTableReader> sstrs = new ArrayList<>(cfs.getLiveSSTables());
    List<SSTableReader> newBucket = newestBucket(Collections.singletonList(sstrs.subList(0, 2)), 4, 32, 9, 10, Long.MAX_VALUE, new SizeTieredCompactionStrategyOptions());
    assertTrue("incoming bucket should not be accepted when it has below the min threshold SSTables", newBucket.isEmpty());
    newBucket = newestBucket(Collections.singletonList(sstrs.subList(0, 2)), 4, 32, 10, 10, Long.MAX_VALUE, new SizeTieredCompactionStrategyOptions());
    assertFalse("non-incoming bucket should be accepted when it has at least 2 SSTables", newBucket.isEmpty());
    assertEquals("an sstable with a single value should have equal min/max timestamps", sstrs.get(0).getMinTimestamp(), sstrs.get(0).getMaxTimestamp());
    assertEquals("an sstable with a single value should have equal min/max timestamps", sstrs.get(1).getMinTimestamp(), sstrs.get(1).getMaxTimestamp());
    assertEquals("an sstable with a single value should have equal min/max timestamps", sstrs.get(2).getMinTimestamp(), sstrs.get(2).getMaxTimestamp());
    cfs.truncateBlocking();
}
Also used : SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) RowUpdateBuilder(org.apache.cassandra.db.RowUpdateBuilder) Keyspace(org.apache.cassandra.db.Keyspace) DecoratedKey(org.apache.cassandra.db.DecoratedKey) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 10 with DecoratedKey

use of org.apache.cassandra.db.DecoratedKey in project cassandra by apache.

the class SimpleDataSet method row.

public SimpleDataSet row(Object... primaryKeyValues) {
    if (Iterables.size(metadata.primaryKeyColumns()) != primaryKeyValues.length)
        throw new IllegalArgumentException();
    Object[] partitionKeyValues = new Object[metadata.partitionKeyColumns().size()];
    Object[] clusteringValues = new Object[metadata.clusteringColumns().size()];
    System.arraycopy(primaryKeyValues, 0, partitionKeyValues, 0, partitionKeyValues.length);
    System.arraycopy(primaryKeyValues, partitionKeyValues.length, clusteringValues, 0, clusteringValues.length);
    DecoratedKey partitionKey = makeDecoratedKey(partitionKeyValues);
    Clustering<?> clustering = makeClustering(clusteringValues);
    currentRow = new Row(metadata, clustering);
    SimplePartition partition = (SimplePartition) partitions.computeIfAbsent(partitionKey, pk -> new SimplePartition(metadata, pk));
    partition.add(currentRow);
    return this;
}
Also used : Rows(org.apache.cassandra.db.rows.Rows) Iterables(com.google.common.collect.Iterables) ColumnMetadata(org.apache.cassandra.schema.ColumnMetadata) Unfiltered(org.apache.cassandra.db.rows.Unfiltered) RegularAndStaticColumns(org.apache.cassandra.db.RegularAndStaticColumns) HashMap(java.util.HashMap) AbstractUnfilteredRowIterator(org.apache.cassandra.db.rows.AbstractUnfilteredRowIterator) AbstractType(org.apache.cassandra.db.marshal.AbstractType) BTreeRow(org.apache.cassandra.db.rows.BTreeRow) ByteBuffer(java.nio.ByteBuffer) DecoratedKey(org.apache.cassandra.db.DecoratedKey) BufferCell(org.apache.cassandra.db.rows.BufferCell) UnfilteredRowIterator(org.apache.cassandra.db.rows.UnfilteredRowIterator) Map(java.util.Map) ColumnFilter(org.apache.cassandra.db.filter.ColumnFilter) CompositeType(org.apache.cassandra.db.marshal.CompositeType) Clustering(org.apache.cassandra.db.Clustering) Iterator(java.util.Iterator) ByteBufferUtil(org.apache.cassandra.utils.ByteBufferUtil) NavigableMap(java.util.NavigableMap) TreeMap(java.util.TreeMap) DeletionTime(org.apache.cassandra.db.DeletionTime) EncodingStats(org.apache.cassandra.db.rows.EncodingStats) TableMetadata(org.apache.cassandra.schema.TableMetadata) ClusteringIndexFilter(org.apache.cassandra.db.filter.ClusteringIndexFilter) DecoratedKey(org.apache.cassandra.db.DecoratedKey) BTreeRow(org.apache.cassandra.db.rows.BTreeRow)

Aggregations

DecoratedKey (org.apache.cassandra.db.DecoratedKey)80 ColumnFamilyStore (org.apache.cassandra.db.ColumnFamilyStore)31 Test (org.junit.Test)28 SSTableReader (org.apache.cassandra.io.sstable.format.SSTableReader)22 ByteBuffer (java.nio.ByteBuffer)21 TableMetadata (org.apache.cassandra.schema.TableMetadata)20 Keyspace (org.apache.cassandra.db.Keyspace)18 RowUpdateBuilder (org.apache.cassandra.db.RowUpdateBuilder)16 ColumnFamily (org.apache.cassandra.db.ColumnFamily)8 QueryPath (org.apache.cassandra.db.filter.QueryPath)8 File (org.apache.cassandra.io.util.File)8 ArrayList (java.util.ArrayList)7 UUID (java.util.UUID)7 RowMutation (org.apache.cassandra.db.RowMutation)7 Table (org.apache.cassandra.db.Table)7 Row (org.apache.cassandra.db.rows.Row)7 UnfilteredRowIterator (org.apache.cassandra.db.rows.UnfilteredRowIterator)7 Token (org.apache.cassandra.dht.Token)7 BufferDecoratedKey (org.apache.cassandra.db.BufferDecoratedKey)6 RowIndexEntry (org.apache.cassandra.db.RowIndexEntry)5