Search in sources :

Example 56 with Keyspace

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

the class SSTableRewriterTest method testNumberOfFiles_finish_empty_new_writer.

@Test
public void testNumberOfFiles_finish_empty_new_writer() throws Exception {
    Keyspace keyspace = Keyspace.open(KEYSPACE);
    ColumnFamilyStore cfs = keyspace.getColumnFamilyStore(CF);
    truncate(cfs);
    SSTableReader s = writeFile(cfs, 1000);
    cfs.addSSTable(s);
    Set<SSTableReader> compacting = Sets.newHashSet(s);
    int files = 1;
    try (ISSTableScanner scanner = s.getScanner();
        CompactionController controller = new CompactionController(cfs, compacting, 0);
        LifecycleTransaction txn = cfs.getTracker().tryModify(compacting, OperationType.UNKNOWN);
        SSTableRewriter rewriter = new SSTableRewriter(txn, 1000, 10000000, false);
        CompactionIterator ci = new CompactionIterator(OperationType.COMPACTION, Collections.singletonList(scanner), controller, FBUtilities.nowInSeconds(), UUIDGen.getTimeUUID())) {
        rewriter.switchWriter(getWriter(cfs, s.descriptor.directory, txn));
        while (ci.hasNext()) {
            rewriter.append(ci.next());
            if (rewriter.currentWriter().getFilePointer() > 2500000) {
                rewriter.switchWriter(getWriter(cfs, s.descriptor.directory, txn));
                files++;
                // we have one original file plus the ones we have switched out.
                assertEquals(cfs.getLiveSSTables().size(), files);
            }
            if (files == 3) {
                //testing to finish when we have nothing written in the new file
                rewriter.finish();
                break;
            }
        }
    }
    LifecycleTransaction.waitForDeletions();
    // we never wrote anything to the last file
    assertEquals(files - 1, cfs.getLiveSSTables().size());
    assertFileCounts(s.descriptor.directory.list());
    validateCFS(cfs);
}
Also used : CompactionController(org.apache.cassandra.db.compaction.CompactionController) SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) CompactionIterator(org.apache.cassandra.db.compaction.CompactionIterator) Keyspace(org.apache.cassandra.db.Keyspace) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) LifecycleTransaction(org.apache.cassandra.db.lifecycle.LifecycleTransaction) Test(org.junit.Test)

Example 57 with Keyspace

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

the class SSTableRewriterTest method testCanonicalSSTables.

@Test
public void testCanonicalSSTables() throws ExecutionException, InterruptedException {
    Keyspace keyspace = Keyspace.open(KEYSPACE);
    final ColumnFamilyStore cfs = keyspace.getColumnFamilyStore(CF);
    truncate(cfs);
    cfs.addSSTable(writeFile(cfs, 100));
    Collection<SSTableReader> allSSTables = cfs.getLiveSSTables();
    assertEquals(1, allSSTables.size());
    final AtomicBoolean done = new AtomicBoolean(false);
    final AtomicBoolean failed = new AtomicBoolean(false);
    Runnable r = () -> {
        while (!done.get()) {
            Iterable<SSTableReader> sstables = cfs.getSSTables(SSTableSet.CANONICAL);
            if (Iterables.size(sstables) != 1) {
                failed.set(true);
                return;
            }
        }
    };
    Thread t = NamedThreadFactory.createThread(r);
    try {
        t.start();
        cfs.forceMajorCompaction();
    } finally {
        done.set(true);
        t.join(20);
    }
    assertFalse(failed.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) 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 58 with Keyspace

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

the class SSTableRewriterTest method basicTest2.

@Test
public void basicTest2() throws InterruptedException {
    Keyspace keyspace = Keyspace.open(KEYSPACE);
    ColumnFamilyStore cfs = keyspace.getColumnFamilyStore(CF);
    truncate(cfs);
    SSTableReader s = writeFile(cfs, 1000);
    cfs.addSSTable(s);
    Set<SSTableReader> sstables = new HashSet<>(cfs.getLiveSSTables());
    assertEquals(1, sstables.size());
    int nowInSec = FBUtilities.nowInSeconds();
    try (AbstractCompactionStrategy.ScannerList scanners = cfs.getCompactionStrategyManager().getScanners(sstables);
        LifecycleTransaction txn = cfs.getTracker().tryModify(sstables, OperationType.UNKNOWN);
        SSTableRewriter writer = new SSTableRewriter(txn, 1000, 10000000, false);
        CompactionController controller = new CompactionController(cfs, sstables, cfs.gcBefore(nowInSec));
        CompactionIterator ci = new CompactionIterator(OperationType.COMPACTION, scanners.scanners, controller, nowInSec, UUIDGen.getTimeUUID())) {
        writer.switchWriter(getWriter(cfs, sstables.iterator().next().descriptor.directory, txn));
        while (ci.hasNext()) {
            writer.append(ci.next());
        }
        writer.finish();
    }
    LifecycleTransaction.waitForDeletions();
    assertEquals(1, assertFileCounts(sstables.iterator().next().descriptor.directory.list()));
    validateCFS(cfs);
}
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) Keyspace(org.apache.cassandra.db.Keyspace) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) LifecycleTransaction(org.apache.cassandra.db.lifecycle.LifecycleTransaction) Test(org.junit.Test)

Example 59 with Keyspace

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

the class SSTableRewriterTest method testSSTableSectionsForRanges.

@Test
public void testSSTableSectionsForRanges() throws IOException, InterruptedException, ExecutionException {
    Keyspace keyspace = Keyspace.open(KEYSPACE);
    final ColumnFamilyStore cfs = keyspace.getColumnFamilyStore(CF);
    truncate(cfs);
    cfs.addSSTable(writeFile(cfs, 1000));
    Collection<SSTableReader> allSSTables = cfs.getLiveSSTables();
    assertEquals(1, allSSTables.size());
    final Token firstToken = allSSTables.iterator().next().first.getToken();
    DatabaseDescriptor.setSSTablePreempiveOpenIntervalInMB(1);
    List<StreamSession.SSTableStreamingSections> sectionsBeforeRewrite = StreamSession.getSSTableSectionsForRanges(Collections.singleton(new Range<Token>(firstToken, firstToken)), Collections.singleton(cfs), 0L, null);
    assertEquals(1, sectionsBeforeRewrite.size());
    for (StreamSession.SSTableStreamingSections section : sectionsBeforeRewrite) section.ref.release();
    final AtomicInteger checkCount = new AtomicInteger();
    // needed since we get notified when compaction is done as well - we can't get sections for ranges for obsoleted sstables
    final AtomicBoolean done = new AtomicBoolean(false);
    final AtomicBoolean failed = new AtomicBoolean(false);
    Runnable r = new Runnable() {

        public void run() {
            while (!done.get()) {
                Set<Range<Token>> range = Collections.singleton(new Range<Token>(firstToken, firstToken));
                List<StreamSession.SSTableStreamingSections> sections = StreamSession.getSSTableSectionsForRanges(range, Collections.singleton(cfs), 0L, null);
                if (sections.size() != 1)
                    failed.set(true);
                for (StreamSession.SSTableStreamingSections section : sections) section.ref.release();
                checkCount.incrementAndGet();
                Uninterruptibles.sleepUninterruptibly(5, TimeUnit.MILLISECONDS);
            }
        }
    };
    Thread t = NamedThreadFactory.createThread(r);
    try {
        t.start();
        cfs.forceMajorCompaction();
    // reset
    } finally {
        DatabaseDescriptor.setSSTablePreempiveOpenIntervalInMB(50);
        done.set(true);
        t.join(20);
    }
    assertFalse(failed.get());
    assertTrue(checkCount.get() >= 2);
    truncate(cfs);
}
Also used : Token(org.apache.cassandra.dht.Token) Range(org.apache.cassandra.dht.Range) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) StreamSession(org.apache.cassandra.streaming.StreamSession) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Keyspace(org.apache.cassandra.db.Keyspace) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) Test(org.junit.Test)

Example 60 with Keyspace

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

the class SSTableScannerTest method testMultipleRanges.

@Test
public void testMultipleRanges() throws IOException {
    Keyspace keyspace = Keyspace.open(KEYSPACE);
    ColumnFamilyStore store = keyspace.getColumnFamilyStore(TABLE);
    store.clearUnsafe();
    // disable compaction while flushing
    store.disableAutoCompaction();
    for (int i = 0; i < 3; i++) for (int j = 2; j < 10; j++) insertRowWithKey(store.metadata(), i * 100 + j);
    store.forceBlockingFlush();
    assertEquals(1, store.getLiveSSTables().size());
    SSTableReader sstable = store.getLiveSSTables().iterator().next();
    // full range scan
    ISSTableScanner fullScanner = sstable.getScanner();
    assertScanContainsRanges(fullScanner, 2, 9, 102, 109, 202, 209);
    // scan all three ranges separately
    ISSTableScanner scanner = sstable.getScanner(makeRanges(1, 9, 101, 109, 201, 209));
    assertScanContainsRanges(scanner, 2, 9, 102, 109, 202, 209);
    // skip the first range
    scanner = sstable.getScanner(makeRanges(101, 109, 201, 209));
    assertScanContainsRanges(scanner, 102, 109, 202, 209);
    // skip the second range
    scanner = sstable.getScanner(makeRanges(1, 9, 201, 209));
    assertScanContainsRanges(scanner, 2, 9, 202, 209);
    // skip the last range
    scanner = sstable.getScanner(makeRanges(1, 9, 101, 109));
    assertScanContainsRanges(scanner, 2, 9, 102, 109);
    // the first scanned range stops short of the actual data in the first range
    scanner = sstable.getScanner(makeRanges(1, 5, 101, 109, 201, 209));
    assertScanContainsRanges(scanner, 2, 5, 102, 109, 202, 209);
    // the first scanned range requests data beyond actual data in the first range
    scanner = sstable.getScanner(makeRanges(1, 20, 101, 109, 201, 209));
    assertScanContainsRanges(scanner, 2, 9, 102, 109, 202, 209);
    // the middle scan range splits the outside two data ranges
    scanner = sstable.getScanner(makeRanges(1, 5, 6, 205, 206, 209));
    assertScanContainsRanges(scanner, 2, 5, 7, 9, 102, 109, 202, 205, 207, 209);
    // empty ranges
    scanner = sstable.getScanner(makeRanges(0, 1, 2, 20, 101, 109, 150, 159, 201, 209, 1000, 1001));
    assertScanContainsRanges(scanner, 3, 9, 102, 109, 202, 209);
    // out of order ranges
    scanner = sstable.getScanner(makeRanges(201, 209, 1, 20, 201, 209, 101, 109, 1000, 1001, 150, 159));
    assertScanContainsRanges(scanner, 2, 9, 102, 109, 202, 209);
    // only empty ranges
    scanner = sstable.getScanner(makeRanges(0, 1, 150, 159, 250, 259));
    assertFalse(scanner.hasNext());
    // no ranges is equivalent to a full scan
    scanner = sstable.getScanner(new ArrayList<Range<Token>>());
    assertFalse(scanner.hasNext());
}
Also used : SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) Keyspace(org.apache.cassandra.db.Keyspace) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) ArrayList(java.util.ArrayList) Token(org.apache.cassandra.dht.Token) Test(org.junit.Test)

Aggregations

Keyspace (org.apache.cassandra.db.Keyspace)69 ColumnFamilyStore (org.apache.cassandra.db.ColumnFamilyStore)63 SSTableReader (org.apache.cassandra.io.sstable.format.SSTableReader)48 Test (org.junit.Test)42 LifecycleTransaction (org.apache.cassandra.db.lifecycle.LifecycleTransaction)23 RowUpdateBuilder (org.apache.cassandra.db.RowUpdateBuilder)17 CompactionController (org.apache.cassandra.db.compaction.CompactionController)13 CompactionIterator (org.apache.cassandra.db.compaction.CompactionIterator)12 DecoratedKey (org.apache.cassandra.db.DecoratedKey)11 ByteBuffer (java.nio.ByteBuffer)10 File (java.io.File)5 Directories (org.apache.cassandra.db.Directories)5 SystemKeyspace (org.apache.cassandra.db.SystemKeyspace)5 RestorableMeter (org.apache.cassandra.metrics.RestorableMeter)5 ArrayList (java.util.ArrayList)4 AbstractCompactionStrategy (org.apache.cassandra.db.compaction.AbstractCompactionStrategy)4 Range (org.apache.cassandra.dht.Range)4 Token (org.apache.cassandra.dht.Token)4 Descriptor (org.apache.cassandra.io.sstable.Descriptor)4 PrintStream (java.io.PrintStream)3