Search in sources :

Example 1 with ReadExecutionController

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

the class ViewBuilderTask method buildKey.

@SuppressWarnings("resource")
private void buildKey(DecoratedKey key) {
    ReadQuery selectQuery = view.getReadQuery();
    if (!selectQuery.selectsKey(key)) {
        logger.trace("Skipping {}, view query filters", key);
        return;
    }
    int nowInSec = FBUtilities.nowInSeconds();
    SinglePartitionReadCommand command = view.getSelectStatement().internalReadForView(key, nowInSec);
    // We're rebuilding everything from what's on disk, so we read everything, consider that as new updates
    // and pretend that there is nothing pre-existing.
    UnfilteredRowIterator empty = UnfilteredRowIterators.noRowsIterator(baseCfs.metadata(), key, Rows.EMPTY_STATIC_ROW, DeletionTime.LIVE, false);
    try (ReadExecutionController orderGroup = command.executionController();
        UnfilteredRowIterator data = UnfilteredPartitionIterators.getOnlyElement(command.executeLocally(orderGroup), command)) {
        Iterator<Collection<Mutation>> mutations = baseCfs.keyspace.viewManager.forTable(baseCfs.metadata.id).generateViewUpdates(Collections.singleton(view), data, empty, nowInSec, true);
        AtomicLong noBase = new AtomicLong(Long.MAX_VALUE);
        mutations.forEachRemaining(m -> StorageProxy.mutateMV(key.getKey(), m, true, noBase, nanoTime()));
    }
}
Also used : UnfilteredRowIterator(org.apache.cassandra.db.rows.UnfilteredRowIterator) ReadExecutionController(org.apache.cassandra.db.ReadExecutionController) AtomicLong(java.util.concurrent.atomic.AtomicLong) SinglePartitionReadCommand(org.apache.cassandra.db.SinglePartitionReadCommand) Collection(java.util.Collection) ReadQuery(org.apache.cassandra.db.ReadQuery)

Example 2 with ReadExecutionController

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

the class ThrottledUnfilteredIteratorTest method testThrottledIteratorWithRangeDeletions.

@Test
public void testThrottledIteratorWithRangeDeletions() throws Exception {
    Keyspace keyspace = Keyspace.open(KSNAME);
    ColumnFamilyStore cfs = keyspace.getColumnFamilyStore(CFNAME);
    // Inserting data
    String key = "k1";
    UpdateBuilder builder;
    builder = UpdateBuilder.create(cfs.metadata(), key).withTimestamp(0);
    for (int i = 0; i < 40; i += 2) builder.newRow(i).add("val", i);
    builder.applyUnsafe();
    new RowUpdateBuilder(cfs.metadata(), 1, key).addRangeTombstone(10, 22).build().applyUnsafe();
    cfs.forceBlockingFlush();
    builder = UpdateBuilder.create(cfs.metadata(), key).withTimestamp(2);
    for (int i = 1; i < 40; i += 2) builder.newRow(i).add("val", i);
    builder.applyUnsafe();
    new RowUpdateBuilder(cfs.metadata(), 3, key).addRangeTombstone(19, 27).build().applyUnsafe();
    // We don't flush to test with both a range tomsbtone in memtable and in sstable
    // Queries by name
    int[] live = new int[] { 4, 9, 11, 17, 28 };
    int[] dead = new int[] { 12, 19, 21, 24, 27 };
    AbstractReadCommandBuilder.PartitionRangeBuilder cmdBuilder = Util.cmd(cfs);
    ReadCommand cmd = cmdBuilder.build();
    for (int batchSize = 2; batchSize <= 40; batchSize++) {
        List<UnfilteredRowIterator> unfilteredRowIterators = new LinkedList<>();
        try (ReadExecutionController executionController = cmd.executionController();
            UnfilteredPartitionIterator iterator = cmd.executeLocally(executionController)) {
            assertTrue(iterator.hasNext());
            Iterator<UnfilteredRowIterator> throttled = ThrottledUnfilteredIterator.throttle(iterator, batchSize);
            while (throttled.hasNext()) {
                UnfilteredRowIterator next = throttled.next();
                ImmutableBTreePartition materializedPartition = ImmutableBTreePartition.create(next);
                int unfilteredCount = Iterators.size(materializedPartition.unfilteredIterator());
                System.out.println("batchsize " + batchSize + " unfilteredCount " + unfilteredCount + " materializedPartition " + materializedPartition);
                if (throttled.hasNext()) {
                    if (unfilteredCount != batchSize) {
                        // when there is extra unfiltered, it must be close bound marker
                        assertEquals(batchSize + 1, unfilteredCount);
                        Unfiltered last = Iterators.getLast(materializedPartition.unfilteredIterator());
                        assertTrue(last.isRangeTombstoneMarker());
                        RangeTombstoneMarker marker = (RangeTombstoneMarker) last;
                        assertFalse(marker.isBoundary());
                        assertTrue(marker.isClose(false));
                    }
                } else {
                    // only last batch can be smaller than batchSize
                    assertTrue(unfilteredCount <= batchSize + 1);
                }
                unfilteredRowIterators.add(materializedPartition.unfilteredIterator());
            }
            assertFalse(iterator.hasNext());
        }
        // Verify throttled data after merge
        Partition partition = ImmutableBTreePartition.create(UnfilteredRowIterators.merge(unfilteredRowIterators));
        int nowInSec = FBUtilities.nowInSeconds();
        for (int i : live) assertTrue("Row " + i + " should be live", partition.getRow(Clustering.make(ByteBufferUtil.bytes((i)))).hasLiveData(nowInSec, cfs.metadata().enforceStrictLiveness()));
        for (int i : dead) assertFalse("Row " + i + " shouldn't be live", partition.getRow(Clustering.make(ByteBufferUtil.bytes((i)))).hasLiveData(nowInSec, cfs.metadata().enforceStrictLiveness()));
    }
}
Also used : Partition(org.apache.cassandra.db.partitions.Partition) ImmutableBTreePartition(org.apache.cassandra.db.partitions.ImmutableBTreePartition) AbstractReadCommandBuilder(org.apache.cassandra.db.AbstractReadCommandBuilder) ReadCommand(org.apache.cassandra.db.ReadCommand) UpdateBuilder(org.apache.cassandra.UpdateBuilder) RowUpdateBuilder(org.apache.cassandra.db.RowUpdateBuilder) UnfilteredPartitionIterator(org.apache.cassandra.db.partitions.UnfilteredPartitionIterator) AbstractUnfilteredPartitionIterator(org.apache.cassandra.db.partitions.AbstractUnfilteredPartitionIterator) LinkedList(java.util.LinkedList) ReadExecutionController(org.apache.cassandra.db.ReadExecutionController) RowUpdateBuilder(org.apache.cassandra.db.RowUpdateBuilder) Keyspace(org.apache.cassandra.db.Keyspace) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) ImmutableBTreePartition(org.apache.cassandra.db.partitions.ImmutableBTreePartition) Test(org.junit.Test)

Example 3 with ReadExecutionController

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

the class CompactionsTest method testRangeTombstones.

@Test
public void testRangeTombstones() {
    Keyspace keyspace = Keyspace.open(KEYSPACE1);
    ColumnFamilyStore cfs = keyspace.getColumnFamilyStore("Standard2");
    cfs.clearUnsafe();
    // disable compaction while flushing
    cfs.disableAutoCompaction();
    final TableMetadata table = cfs.metadata();
    Directories dir = cfs.getDirectories();
    ArrayList<DecoratedKey> keys = new ArrayList<DecoratedKey>();
    for (int i = 0; i < 4; i++) {
        keys.add(Util.dk(Integer.toString(i)));
    }
    int[] dks = { 0, 1, 3 };
    writeSSTableWithRangeTombstoneMaskingOneColumn(cfs, table, dks);
    int[] dkays = { 0, 1, 2, 3 };
    writeSSTableWithRangeTombstoneMaskingOneColumn(cfs, table, dkays);
    Collection<SSTableReader> toCompact = cfs.getLiveSSTables();
    assert toCompact.size() == 2;
    Util.compact(cfs, toCompact);
    assertEquals(1, cfs.getLiveSSTables().size());
    // Now assert we do have the 4 keys
    assertEquals(4, Util.getAll(Util.cmd(cfs).build()).size());
    ArrayList<DecoratedKey> k = new ArrayList<>();
    for (FilteredPartition p : Util.getAll(Util.cmd(cfs).build())) {
        k.add(p.partitionKey());
        final SinglePartitionReadCommand command = SinglePartitionReadCommand.create(cfs.metadata(), FBUtilities.nowInSeconds(), ColumnFilter.all(cfs.metadata()), RowFilter.NONE, DataLimits.NONE, p.partitionKey(), new ClusteringIndexSliceFilter(Slices.ALL, false));
        try (ReadExecutionController executionController = command.executionController();
            PartitionIterator iterator = command.executeInternal(executionController)) {
            try (RowIterator rowIterator = iterator.next()) {
                Row row = rowIterator.next();
                Cell<?> cell = row.getCell(cfs.metadata().getColumn(new ColumnIdentifier("val", false)));
                assertEquals(ByteBufferUtil.bytes("a"), cell.buffer());
                assertEquals(3, cell.timestamp());
                ValueAccessors.assertDataNotEquals(ByteBufferUtil.bytes("01"), row.clustering().getRawValues()[0]);
                ValueAccessors.assertDataEquals(ByteBufferUtil.bytes("02"), row.clustering().getRawValues()[0]);
            }
        }
    }
    for (SSTableReader sstable : cfs.getLiveSSTables()) {
        StatsMetadata stats = sstable.getSSTableMetadata();
        assertEquals(ByteBufferUtil.bytes("0"), stats.minClusteringValues.get(0));
        assertEquals(ByteBufferUtil.bytes("b"), stats.maxClusteringValues.get(0));
    }
    assertEquals(keys, k);
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) StatsMetadata(org.apache.cassandra.io.sstable.metadata.StatsMetadata) DecoratedKey(org.apache.cassandra.db.DecoratedKey) SinglePartitionReadCommand(org.apache.cassandra.db.SinglePartitionReadCommand) ArrayList(java.util.ArrayList) FilteredPartition(org.apache.cassandra.db.partitions.FilteredPartition) Directories(org.apache.cassandra.db.Directories) ClusteringIndexSliceFilter(org.apache.cassandra.db.filter.ClusteringIndexSliceFilter) ReadExecutionController(org.apache.cassandra.db.ReadExecutionController) SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) Keyspace(org.apache.cassandra.db.Keyspace) PartitionIterator(org.apache.cassandra.db.partitions.PartitionIterator) RowIterator(org.apache.cassandra.db.rows.RowIterator) UnfilteredRowIterator(org.apache.cassandra.db.rows.UnfilteredRowIterator) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) ColumnIdentifier(org.apache.cassandra.cql3.ColumnIdentifier) Row(org.apache.cassandra.db.rows.Row) Test(org.junit.Test)

Example 4 with ReadExecutionController

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

the class Ballots method latestBallotFromBaseTable.

public static long latestBallotFromBaseTable(DecoratedKey key, TableMetadata metadata) {
    SinglePartitionReadCommand cmd = SinglePartitionReadCommand.create(metadata, 0, key, Slice.ALL);
    ImmutableBTreePartition partition;
    try (ReadExecutionController controller = cmd.executionController();
        UnfilteredPartitionIterator partitions = cmd.executeLocally(controller)) {
        if (!partitions.hasNext())
            return 0L;
        try (UnfilteredRowIterator rows = partitions.next()) {
            partition = ImmutableBTreePartition.create(rows);
        }
    }
    return latestBallot(partition);
}
Also used : ReadExecutionController(org.apache.cassandra.db.ReadExecutionController) UnfilteredRowIterator(org.apache.cassandra.db.rows.UnfilteredRowIterator) SinglePartitionReadCommand(org.apache.cassandra.db.SinglePartitionReadCommand) UnfilteredPartitionIterator(org.apache.cassandra.db.partitions.UnfilteredPartitionIterator) ImmutableBTreePartition(org.apache.cassandra.db.partitions.ImmutableBTreePartition)

Aggregations

ReadExecutionController (org.apache.cassandra.db.ReadExecutionController)4 SinglePartitionReadCommand (org.apache.cassandra.db.SinglePartitionReadCommand)3 UnfilteredRowIterator (org.apache.cassandra.db.rows.UnfilteredRowIterator)3 ColumnFamilyStore (org.apache.cassandra.db.ColumnFamilyStore)2 Keyspace (org.apache.cassandra.db.Keyspace)2 ImmutableBTreePartition (org.apache.cassandra.db.partitions.ImmutableBTreePartition)2 UnfilteredPartitionIterator (org.apache.cassandra.db.partitions.UnfilteredPartitionIterator)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 LinkedList (java.util.LinkedList)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 UpdateBuilder (org.apache.cassandra.UpdateBuilder)1 ColumnIdentifier (org.apache.cassandra.cql3.ColumnIdentifier)1 AbstractReadCommandBuilder (org.apache.cassandra.db.AbstractReadCommandBuilder)1 DecoratedKey (org.apache.cassandra.db.DecoratedKey)1 Directories (org.apache.cassandra.db.Directories)1 ReadCommand (org.apache.cassandra.db.ReadCommand)1 ReadQuery (org.apache.cassandra.db.ReadQuery)1 RowUpdateBuilder (org.apache.cassandra.db.RowUpdateBuilder)1