Search in sources :

Example 56 with DecoratedKey

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

the class KeyIterator method computeNext.

protected DecoratedKey computeNext() {
    fileAccessLock.readLock().lock();
    try {
        if (in.isEOF())
            return endOfData();
        keyPosition = in.getFilePointer();
        DecoratedKey key = partitioner.decorateKey(ByteBufferUtil.readWithShortLength(in.get()));
        // skip remainder of the entry
        RowIndexEntry.Serializer.skip(in.get(), desc.version);
        return key;
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        fileAccessLock.readLock().unlock();
    }
}
Also used : DecoratedKey(org.apache.cassandra.db.DecoratedKey) IOException(java.io.IOException)

Example 57 with DecoratedKey

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

the class SSTableRewriter method append.

public RowIndexEntry append(UnfilteredRowIterator partition) {
    // we do this before appending to ensure we can resetAndTruncate() safely if the append fails
    DecoratedKey key = partition.partitionKey();
    maybeReopenEarly(key);
    RowIndexEntry index = writer.append(partition);
    if (DatabaseDescriptor.shouldMigrateKeycacheOnCompaction()) {
        if (!transaction.isOffline() && index != null) {
            for (SSTableReader reader : transaction.originals()) {
                if (reader.getCachedPosition(key, false) != null) {
                    cachedKeys.put(key, index);
                    break;
                }
            }
        }
    }
    return index;
}
Also used : SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) DecoratedKey(org.apache.cassandra.db.DecoratedKey) RowIndexEntry(org.apache.cassandra.db.RowIndexEntry)

Example 58 with DecoratedKey

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

the class SSTableExport method main.

/**
 * Given arguments specifying an SSTable, and optionally an output file, export the contents of the SSTable to JSON.
 *
 * @param args
 *            command lines arguments
 * @throws ConfigurationException
 *             on configuration failure (wrong params given)
 */
@SuppressWarnings("resource")
public static void main(String[] args) throws ConfigurationException {
    CommandLineParser parser = new PosixParser();
    try {
        cmd = parser.parse(options, args);
    } catch (ParseException e1) {
        System.err.println(e1.getMessage());
        printUsage();
        System.exit(1);
    }
    String[] keys = cmd.getOptionValues(KEY_OPTION);
    HashSet<String> excludes = new HashSet<>(Arrays.asList(cmd.getOptionValues(EXCLUDE_KEY_OPTION) == null ? new String[0] : cmd.getOptionValues(EXCLUDE_KEY_OPTION)));
    if (cmd.getArgs().length != 1) {
        String msg = "You must supply exactly one sstable";
        if (cmd.getArgs().length == 0 && (keys != null && keys.length > 0 || !excludes.isEmpty()))
            msg += ", which should be before the -k/-x options so it's not interpreted as a partition key.";
        System.err.println(msg);
        printUsage();
        System.exit(1);
    }
    String ssTableFileName = new File(cmd.getArgs()[0]).absolutePath();
    if (!new File(ssTableFileName).exists()) {
        System.err.println("Cannot find file " + ssTableFileName);
        System.exit(1);
    }
    Descriptor desc = Descriptor.fromFilename(ssTableFileName);
    try {
        TableMetadata metadata = Util.metadataFromSSTable(desc);
        if (cmd.hasOption(ENUMERATE_KEYS_OPTION)) {
            try (KeyIterator iter = new KeyIterator(desc, metadata)) {
                JsonTransformer.keysToJson(null, Util.iterToStream(iter), cmd.hasOption(RAW_TIMESTAMPS), metadata, System.out);
            }
        } else {
            SSTableReader sstable = SSTableReader.openNoValidation(desc, TableMetadataRef.forOfflineTools(metadata));
            IPartitioner partitioner = sstable.getPartitioner();
            final ISSTableScanner currentScanner;
            if ((keys != null) && (keys.length > 0)) {
                List<AbstractBounds<PartitionPosition>> bounds = Arrays.stream(keys).filter(key -> !excludes.contains(key)).map(metadata.partitionKeyType::fromString).map(partitioner::decorateKey).sorted().map(DecoratedKey::getToken).map(token -> new Bounds<>(token.minKeyBound(), token.maxKeyBound())).collect(Collectors.toList());
                currentScanner = sstable.getScanner(bounds.iterator());
            } else {
                currentScanner = sstable.getScanner();
            }
            Stream<UnfilteredRowIterator> partitions = Util.iterToStream(currentScanner).filter(i -> excludes.isEmpty() || !excludes.contains(metadata.partitionKeyType.getString(i.partitionKey().getKey())));
            if (cmd.hasOption(DEBUG_OUTPUT_OPTION)) {
                AtomicLong position = new AtomicLong();
                partitions.forEach(partition -> {
                    position.set(currentScanner.getCurrentPosition());
                    if (!partition.partitionLevelDeletion().isLive()) {
                        System.out.println("[" + metadata.partitionKeyType.getString(partition.partitionKey().getKey()) + "]@" + position.get() + " " + partition.partitionLevelDeletion());
                    }
                    if (!partition.staticRow().isEmpty()) {
                        System.out.println("[" + metadata.partitionKeyType.getString(partition.partitionKey().getKey()) + "]@" + position.get() + " " + partition.staticRow().toString(metadata, true));
                    }
                    partition.forEachRemaining(row -> {
                        System.out.println("[" + metadata.partitionKeyType.getString(partition.partitionKey().getKey()) + "]@" + position.get() + " " + row.toString(metadata, false, true));
                        position.set(currentScanner.getCurrentPosition());
                    });
                });
            } else if (cmd.hasOption(PARTITION_JSON_LINES)) {
                JsonTransformer.toJsonLines(currentScanner, partitions, cmd.hasOption(RAW_TIMESTAMPS), metadata, System.out);
            } else {
                JsonTransformer.toJson(currentScanner, partitions, cmd.hasOption(RAW_TIMESTAMPS), metadata, System.out);
            }
        }
    } catch (IOException e) {
        e.printStackTrace(System.err);
    }
    System.exit(0);
}
Also used : ISSTableScanner(org.apache.cassandra.io.sstable.ISSTableScanner) Arrays(java.util.Arrays) File(org.apache.cassandra.io.util.File) Options(org.apache.commons.cli.Options) HelpFormatter(org.apache.commons.cli.HelpFormatter) SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) HashSet(java.util.HashSet) KeyIterator(org.apache.cassandra.io.sstable.KeyIterator) DecoratedKey(org.apache.cassandra.db.DecoratedKey) ConfigurationException(org.apache.cassandra.exceptions.ConfigurationException) UnfilteredRowIterator(org.apache.cassandra.db.rows.UnfilteredRowIterator) AbstractBounds(org.apache.cassandra.dht.AbstractBounds) Descriptor(org.apache.cassandra.io.sstable.Descriptor) CommandLine(org.apache.commons.cli.CommandLine) PosixParser(org.apache.commons.cli.PosixParser) DatabaseDescriptor(org.apache.cassandra.config.DatabaseDescriptor) Option(org.apache.commons.cli.Option) FBUtilities(org.apache.cassandra.utils.FBUtilities) CommandLineParser(org.apache.commons.cli.CommandLineParser) ISSTableScanner(org.apache.cassandra.io.sstable.ISSTableScanner) IOException(java.io.IOException) Bounds(org.apache.cassandra.dht.Bounds) Collectors(java.util.stream.Collectors) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Stream(java.util.stream.Stream) PartitionPosition(org.apache.cassandra.db.PartitionPosition) IPartitioner(org.apache.cassandra.dht.IPartitioner) ParseException(org.apache.commons.cli.ParseException) TableMetadataRef(org.apache.cassandra.schema.TableMetadataRef) TableMetadata(org.apache.cassandra.schema.TableMetadata) UnfilteredRowIterator(org.apache.cassandra.db.rows.UnfilteredRowIterator) PosixParser(org.apache.commons.cli.PosixParser) SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) CommandLineParser(org.apache.commons.cli.CommandLineParser) HashSet(java.util.HashSet) IPartitioner(org.apache.cassandra.dht.IPartitioner) TableMetadata(org.apache.cassandra.schema.TableMetadata) KeyIterator(org.apache.cassandra.io.sstable.KeyIterator) DecoratedKey(org.apache.cassandra.db.DecoratedKey) AbstractBounds(org.apache.cassandra.dht.AbstractBounds) Bounds(org.apache.cassandra.dht.Bounds) IOException(java.io.IOException) AtomicLong(java.util.concurrent.atomic.AtomicLong) AbstractBounds(org.apache.cassandra.dht.AbstractBounds) Descriptor(org.apache.cassandra.io.sstable.Descriptor) DatabaseDescriptor(org.apache.cassandra.config.DatabaseDescriptor) ParseException(org.apache.commons.cli.ParseException) File(org.apache.cassandra.io.util.File)

Example 59 with DecoratedKey

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

the class CompactionControllerTest method testMaxPurgeableTimestamp.

@Test
public void testMaxPurgeableTimestamp() {
    Keyspace keyspace = Keyspace.open(KEYSPACE);
    ColumnFamilyStore cfs = keyspace.getColumnFamilyStore(CF1);
    cfs.truncateBlocking();
    DecoratedKey key = Util.dk("k1");
    // latest timestamp
    long timestamp1 = FBUtilities.timestampMicros();
    long timestamp2 = timestamp1 - 5;
    // oldest timestamp
    long timestamp3 = timestamp2 - 5;
    // add to first memtable
    applyMutation(cfs.metadata(), key, timestamp1);
    // check max purgeable timestamp without any sstables
    try (CompactionController controller = new CompactionController(cfs, null, 0)) {
        // memtable only
        assertPurgeBoundary(controller.getPurgeEvaluator(key), timestamp1);
        cfs.forceBlockingFlush();
        // no memtables and no sstables
        assertTrue(controller.getPurgeEvaluator(key).test(Long.MAX_VALUE));
    }
    // first sstable is compacting
    Set<SSTableReader> compacting = Sets.newHashSet(cfs.getLiveSSTables());
    // create another sstable
    applyMutation(cfs.metadata(), key, timestamp2);
    cfs.forceBlockingFlush();
    // check max purgeable timestamp when compacting the first sstable with and without a memtable
    try (CompactionController controller = new CompactionController(cfs, compacting, 0)) {
        assertPurgeBoundary(controller.getPurgeEvaluator(key), timestamp2);
        applyMutation(cfs.metadata(), key, timestamp3);
        // second sstable and second memtable
        assertPurgeBoundary(controller.getPurgeEvaluator(key), timestamp3);
    }
    // check max purgeable timestamp again without any sstables but with different insertion orders on the memtable
    cfs.forceBlockingFlush();
    // newest to oldest
    try (CompactionController controller = new CompactionController(cfs, null, 0)) {
        applyMutation(cfs.metadata(), key, timestamp1);
        applyMutation(cfs.metadata(), key, timestamp2);
        applyMutation(cfs.metadata(), key, timestamp3);
        // memtable only
        assertPurgeBoundary(controller.getPurgeEvaluator(key), timestamp3);
    }
    cfs.forceBlockingFlush();
    // oldest to newest
    try (CompactionController controller = new CompactionController(cfs, null, 0)) {
        applyMutation(cfs.metadata(), key, timestamp3);
        applyMutation(cfs.metadata(), key, timestamp2);
        applyMutation(cfs.metadata(), key, timestamp1);
        assertPurgeBoundary(controller.getPurgeEvaluator(key), timestamp3);
    }
}
Also used : SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) Keyspace(org.apache.cassandra.db.Keyspace) DecoratedKey(org.apache.cassandra.db.DecoratedKey) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) Test(org.junit.Test)

Example 60 with DecoratedKey

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

the class CompactionControllerTest method testGetFullyExpiredSSTables.

@Test
public void testGetFullyExpiredSSTables() {
    Keyspace keyspace = Keyspace.open(KEYSPACE);
    ColumnFamilyStore cfs = keyspace.getColumnFamilyStore(CF2);
    cfs.truncateBlocking();
    DecoratedKey key = Util.dk("k1");
    // latest timestamp
    long timestamp1 = FBUtilities.timestampMicros();
    long timestamp2 = timestamp1 - 5;
    // oldest timestamp
    long timestamp3 = timestamp2 - 5;
    // create sstable with tombstone that should be expired in no older timestamps
    applyDeleteMutation(cfs.metadata(), key, timestamp2);
    cfs.forceBlockingFlush();
    // first sstable with tombstone is compacting
    Set<SSTableReader> compacting = Sets.newHashSet(cfs.getLiveSSTables());
    // create another sstable with more recent timestamp
    applyMutation(cfs.metadata(), key, timestamp1);
    cfs.forceBlockingFlush();
    // second sstable is overlapping
    Set<SSTableReader> overlapping = Sets.difference(Sets.newHashSet(cfs.getLiveSSTables()), compacting);
    // the first sstable should be expired because the overlapping sstable is newer and the gc period is later
    int gcBefore = (int) (System.currentTimeMillis() / 1000) + 5;
    Set<SSTableReader> expired = CompactionController.getFullyExpiredSSTables(cfs, compacting, overlapping, gcBefore);
    assertNotNull(expired);
    assertEquals(1, expired.size());
    assertEquals(compacting.iterator().next(), expired.iterator().next());
    // however if we add an older mutation to the memtable then the sstable should not be expired
    applyMutation(cfs.metadata(), key, timestamp3);
    expired = CompactionController.getFullyExpiredSSTables(cfs, compacting, overlapping, gcBefore);
    assertNotNull(expired);
    assertEquals(0, expired.size());
    // Now if we explicitly ask to ignore overlaped sstables, we should get back our expired sstable
    expired = CompactionController.getFullyExpiredSSTables(cfs, compacting, overlapping, gcBefore, true);
    assertNotNull(expired);
    assertEquals(1, expired.size());
}
Also used : SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) Keyspace(org.apache.cassandra.db.Keyspace) DecoratedKey(org.apache.cassandra.db.DecoratedKey) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) Test(org.junit.Test)

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