Search in sources :

Example 31 with SSTableReader

use of org.apache.cassandra.io.sstable.format.SSTableReader in project cassandra by apache.

the class RealTransactionsTest method replaceSSTable.

private SSTableReader replaceSSTable(ColumnFamilyStore cfs, LifecycleTransaction txn, boolean fail) {
    List<SSTableReader> newsstables = null;
    int nowInSec = FBUtilities.nowInSeconds();
    try (CompactionController controller = new CompactionController(cfs, txn.originals(), cfs.gcBefore(FBUtilities.nowInSeconds()))) {
        try (SSTableRewriter rewriter = SSTableRewriter.constructKeepingOriginals(txn, false, 1000);
            AbstractCompactionStrategy.ScannerList scanners = cfs.getCompactionStrategyManager().getScanners(txn.originals());
            CompactionIterator ci = new CompactionIterator(txn.opType(), scanners.scanners, controller, nowInSec, txn.opId())) {
            long lastCheckObsoletion = System.nanoTime();
            File directory = txn.originals().iterator().next().descriptor.directory;
            Descriptor desc = cfs.newSSTableDescriptor(directory);
            TableMetadataRef metadata = Schema.instance.getTableMetadataRef(desc);
            rewriter.switchWriter(SSTableWriter.create(metadata, desc, 0, 0, null, 0, SerializationHeader.make(cfs.metadata(), txn.originals()), cfs.indexManager.listIndexes(), txn));
            while (ci.hasNext()) {
                rewriter.append(ci.next());
                if (System.nanoTime() - lastCheckObsoletion > TimeUnit.MINUTES.toNanos(1L)) {
                    controller.maybeRefreshOverlaps();
                    lastCheckObsoletion = System.nanoTime();
                }
            }
            if (!fail)
                newsstables = rewriter.finish();
            else
                rewriter.abort();
        }
    }
    assertTrue(fail || newsstables != null);
    if (newsstables != null) {
        Assert.assertEquals(1, newsstables.size());
        return newsstables.iterator().next();
    }
    return null;
}
Also used : CompactionController(org.apache.cassandra.db.compaction.CompactionController) SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) AbstractCompactionStrategy(org.apache.cassandra.db.compaction.AbstractCompactionStrategy) CompactionIterator(org.apache.cassandra.db.compaction.CompactionIterator) TableMetadataRef(org.apache.cassandra.schema.TableMetadataRef) Descriptor(org.apache.cassandra.io.sstable.Descriptor) SSTableRewriter(org.apache.cassandra.io.sstable.SSTableRewriter) File(java.io.File)

Example 32 with SSTableReader

use of org.apache.cassandra.io.sstable.format.SSTableReader in project cassandra by apache.

the class RealTransactionsTest method testRewriteFinished.

@Test
public void testRewriteFinished() throws IOException {
    Keyspace keyspace = Keyspace.open(KEYSPACE);
    ColumnFamilyStore cfs = keyspace.getColumnFamilyStore(REWRITE_FINISHED_CF);
    SSTableReader oldSSTable = getSSTable(cfs, 1);
    LifecycleTransaction txn = cfs.getTracker().tryModify(oldSSTable, OperationType.COMPACTION);
    SSTableReader newSSTable = replaceSSTable(cfs, txn, false);
    LogTransaction.waitForDeletions();
    // both sstables are in the same folder
    assertFiles(oldSSTable.descriptor.directory.getPath(), new HashSet<>(newSSTable.getAllFilePaths()));
    assertFiles(newSSTable.descriptor.directory.getPath(), new HashSet<>(newSSTable.getAllFilePaths()));
}
Also used : SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) Keyspace(org.apache.cassandra.db.Keyspace) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) Test(org.junit.Test)

Example 33 with SSTableReader

use of org.apache.cassandra.io.sstable.format.SSTableReader in project cassandra by apache.

the class TrackerTest method testTryModify.

@Test
public void testTryModify() {
    ColumnFamilyStore cfs = MockSchema.newCFS();
    Tracker tracker = new Tracker(null, false);
    List<SSTableReader> readers = ImmutableList.of(MockSchema.sstable(0, true, cfs), MockSchema.sstable(1, cfs), MockSchema.sstable(2, cfs));
    tracker.addInitialSSTables(copyOf(readers));
    Assert.assertNull(tracker.tryModify(ImmutableList.of(MockSchema.sstable(0, cfs)), OperationType.COMPACTION));
    try (LifecycleTransaction txn = tracker.tryModify(readers.get(0), OperationType.COMPACTION)) {
        Assert.assertNotNull(txn);
        Assert.assertNull(tracker.tryModify(readers.get(0), OperationType.COMPACTION));
        Assert.assertEquals(1, txn.originals().size());
        Assert.assertTrue(txn.originals().contains(readers.get(0)));
    }
    try (LifecycleTransaction txn = tracker.tryModify(Collections.<SSTableReader>emptyList(), OperationType.COMPACTION)) {
        Assert.assertNotNull(txn);
        Assert.assertEquals(0, txn.originals().size());
    }
    readers.get(0).selfRef().release();
}
Also used : SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) Test(org.junit.Test)

Example 34 with SSTableReader

use of org.apache.cassandra.io.sstable.format.SSTableReader in project cassandra by apache.

the class TrackerTest method testMemtableReplacement.

@Test
public void testMemtableReplacement() {
    boolean backups = DatabaseDescriptor.isIncrementalBackupsEnabled();
    DatabaseDescriptor.setIncrementalBackupsEnabled(false);
    ColumnFamilyStore cfs = MockSchema.newCFS();
    MockListener listener = new MockListener(false);
    Tracker tracker = cfs.getTracker();
    tracker.subscribe(listener);
    Memtable prev1 = tracker.switchMemtable(true, new Memtable(new AtomicReference<>(CommitLog.instance.getCurrentPosition()), cfs));
    OpOrder.Group write1 = cfs.keyspace.writeOrder.getCurrent();
    OpOrder.Barrier barrier1 = cfs.keyspace.writeOrder.newBarrier();
    prev1.setDiscarding(barrier1, new AtomicReference<>(CommitLog.instance.getCurrentPosition()));
    barrier1.issue();
    Memtable prev2 = tracker.switchMemtable(false, new Memtable(new AtomicReference<>(CommitLog.instance.getCurrentPosition()), cfs));
    OpOrder.Group write2 = cfs.keyspace.writeOrder.getCurrent();
    OpOrder.Barrier barrier2 = cfs.keyspace.writeOrder.newBarrier();
    prev2.setDiscarding(barrier2, new AtomicReference<>(CommitLog.instance.getCurrentPosition()));
    barrier2.issue();
    Memtable cur = tracker.getView().getCurrentMemtable();
    OpOrder.Group writecur = cfs.keyspace.writeOrder.getCurrent();
    Assert.assertEquals(prev1, tracker.getMemtableFor(write1, CommitLogPosition.NONE));
    Assert.assertEquals(prev2, tracker.getMemtableFor(write2, CommitLogPosition.NONE));
    Assert.assertEquals(cur, tracker.getMemtableFor(writecur, CommitLogPosition.NONE));
    Assert.assertEquals(2, listener.received.size());
    Assert.assertTrue(listener.received.get(0) instanceof MemtableRenewedNotification);
    Assert.assertTrue(listener.received.get(1) instanceof MemtableSwitchedNotification);
    listener.received.clear();
    tracker.markFlushing(prev2);
    Assert.assertEquals(1, tracker.getView().flushingMemtables.size());
    Assert.assertTrue(tracker.getView().flushingMemtables.contains(prev2));
    tracker.markFlushing(prev1);
    Assert.assertTrue(tracker.getView().flushingMemtables.contains(prev1));
    Assert.assertEquals(2, tracker.getView().flushingMemtables.size());
    tracker.replaceFlushed(prev1, Collections.emptyList());
    Assert.assertEquals(1, tracker.getView().flushingMemtables.size());
    Assert.assertTrue(tracker.getView().flushingMemtables.contains(prev2));
    SSTableReader reader = MockSchema.sstable(0, 10, false, cfs);
    tracker.replaceFlushed(prev2, singleton(reader));
    Assert.assertEquals(1, tracker.getView().sstables.size());
    Assert.assertEquals(2, listener.received.size());
    Assert.assertEquals(prev2, ((MemtableDiscardedNotification) listener.received.get(0)).memtable);
    Assert.assertEquals(singleton(reader), ((SSTableAddedNotification) listener.received.get(1)).added);
    listener.received.clear();
    Assert.assertTrue(reader.isKeyCacheSetup());
    Assert.assertEquals(10, cfs.metric.liveDiskSpaceUsed.getCount());
    // test invalidated CFS
    cfs = MockSchema.newCFS();
    tracker = cfs.getTracker();
    listener = new MockListener(false);
    tracker.subscribe(listener);
    prev1 = tracker.switchMemtable(false, new Memtable(new AtomicReference<>(CommitLog.instance.getCurrentPosition()), cfs));
    tracker.markFlushing(prev1);
    reader = MockSchema.sstable(0, 10, true, cfs);
    cfs.invalidate(false);
    tracker.replaceFlushed(prev1, singleton(reader));
    Assert.assertEquals(0, tracker.getView().sstables.size());
    Assert.assertEquals(0, tracker.getView().flushingMemtables.size());
    Assert.assertEquals(0, cfs.metric.liveDiskSpaceUsed.getCount());
    Assert.assertEquals(5, listener.received.size());
    Assert.assertEquals(prev1, ((MemtableSwitchedNotification) listener.received.get(0)).memtable);
    Assert.assertEquals(prev1, ((MemtableDiscardedNotification) listener.received.get(1)).memtable);
    Assert.assertEquals(singleton(reader), ((SSTableAddedNotification) listener.received.get(2)).added);
    Assert.assertTrue(listener.received.get(3) instanceof SSTableDeletingNotification);
    Assert.assertEquals(1, ((SSTableListChangedNotification) listener.received.get(4)).removed.size());
    DatabaseDescriptor.setIncrementalBackupsEnabled(backups);
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) OpOrder(org.apache.cassandra.utils.concurrent.OpOrder) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) Memtable(org.apache.cassandra.db.Memtable) Test(org.junit.Test)

Example 35 with SSTableReader

use of org.apache.cassandra.io.sstable.format.SSTableReader in project cassandra by apache.

the class ViewTest method testSSTablesInBounds.

@Test
public void testSSTablesInBounds() {
    ColumnFamilyStore cfs = MockSchema.newCFS();
    View initialView = fakeView(0, 5, cfs);
    for (int i = 0; i < 5; i++) {
        for (int j = i; j < 5; j++) {
            PartitionPosition min = MockSchema.readerBounds(i);
            PartitionPosition max = MockSchema.readerBounds(j);
            for (//, false} )
            boolean minInc : //, false} )
            new boolean[] { true }) {
                for (//, false} )
                boolean maxInc : //, false} )
                new boolean[] { true }) {
                    if (i == j && !(minInc && maxInc))
                        continue;
                    AbstractBounds<PartitionPosition> bounds = AbstractBounds.bounds(min, minInc, max, maxInc);
                    List<SSTableReader> r = ImmutableList.copyOf(initialView.liveSSTablesInBounds(bounds.left, bounds.right));
                    Assert.assertEquals(String.format("%d(%s) %d(%s)", i, minInc, j, maxInc), j - i + (minInc ? 0 : -1) + (maxInc ? 1 : 0), r.size());
                }
            }
        }
    }
}
Also used : SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) PartitionPosition(org.apache.cassandra.db.PartitionPosition) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) Test(org.junit.Test)

Aggregations

SSTableReader (org.apache.cassandra.io.sstable.format.SSTableReader)289 Test (org.junit.Test)159 ColumnFamilyStore (org.apache.cassandra.db.ColumnFamilyStore)91 LifecycleTransaction (org.apache.cassandra.db.lifecycle.LifecycleTransaction)55 Keyspace (org.apache.cassandra.db.Keyspace)49 File (java.io.File)45 UUID (java.util.UUID)28 Range (org.apache.cassandra.dht.Range)28 Directories (org.apache.cassandra.db.Directories)27 Token (org.apache.cassandra.dht.Token)24 RandomAccessFile (java.io.RandomAccessFile)22 AbstractTransactionalTest (org.apache.cassandra.utils.concurrent.AbstractTransactionalTest)20 ArrayList (java.util.ArrayList)18 ByteBuffer (java.nio.ByteBuffer)17 HashSet (java.util.HashSet)16 SchemaLoader.createKeyspace (org.apache.cassandra.SchemaLoader.createKeyspace)16 DecoratedKey (org.apache.cassandra.db.DecoratedKey)16 RowUpdateBuilder (org.apache.cassandra.db.RowUpdateBuilder)16 CompactionController (org.apache.cassandra.db.compaction.CompactionController)14 CompactionIterator (org.apache.cassandra.db.compaction.CompactionIterator)13