Search in sources :

Example 11 with Directories

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

the class LogTransactionTest method testAbortOnlyNew.

@Test
public void testAbortOnlyNew() throws Throwable {
    ColumnFamilyStore cfs = MockSchema.newCFS(KEYSPACE);
    File dataFolder = new Directories(cfs.metadata()).getDirectoryForNewSSTables();
    SSTableReader sstable = sstable(dataFolder, cfs, 0, 128);
    LogTransaction log = new LogTransaction(OperationType.COMPACTION);
    assertNotNull(log);
    log.trackNew(sstable);
    log.abort();
    sstable.selfRef().release();
    assertFiles(dataFolder.getPath(), new HashSet<>());
}
Also used : Directories(org.apache.cassandra.db.Directories) SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) Test(org.junit.Test) AbstractTransactionalTest(org.apache.cassandra.utils.concurrent.AbstractTransactionalTest)

Example 12 with Directories

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

the class LogTransactionTest method testGetTemporaryFiles.

@Test
public void testGetTemporaryFiles() throws IOException {
    ColumnFamilyStore cfs = MockSchema.newCFS(KEYSPACE);
    File dataFolder = new Directories(cfs.metadata()).getDirectoryForNewSSTables();
    SSTableReader sstable1 = sstable(dataFolder, cfs, 0, 128);
    Set<File> tmpFiles = getTemporaryFiles(dataFolder);
    assertNotNull(tmpFiles);
    assertEquals(0, tmpFiles.size());
    try (LogTransaction log = new LogTransaction(OperationType.WRITE)) {
        Directories directories = new Directories(cfs.metadata());
        File[] beforeSecondSSTable = dataFolder.listFiles(pathname -> !pathname.isDirectory());
        SSTableReader sstable2 = sstable(dataFolder, cfs, 1, 128);
        log.trackNew(sstable2);
        Map<Descriptor, Set<Component>> sstables = directories.sstableLister(Directories.OnTxnErr.THROW).list();
        assertEquals(2, sstables.size());
        // this should contain sstable1, sstable2 and the transaction log file
        File[] afterSecondSSTable = dataFolder.listFiles(pathname -> !pathname.isDirectory());
        int numNewFiles = afterSecondSSTable.length - beforeSecondSSTable.length;
        // new files except for transaction log file
        assertEquals(numNewFiles - 1, sstable2.getAllFilePaths().size());
        tmpFiles = getTemporaryFiles(dataFolder);
        assertNotNull(tmpFiles);
        assertEquals(numNewFiles - 1, tmpFiles.size());
        File ssTable2DataFile = new File(sstable2.descriptor.filenameFor(Component.DATA));
        File ssTable2IndexFile = new File(sstable2.descriptor.filenameFor(Component.PRIMARY_INDEX));
        assertTrue(tmpFiles.contains(ssTable2DataFile));
        assertTrue(tmpFiles.contains(ssTable2IndexFile));
        List<File> files = directories.sstableLister(Directories.OnTxnErr.THROW).listFiles();
        List<File> filesNoTmp = directories.sstableLister(Directories.OnTxnErr.THROW).skipTemporary(true).listFiles();
        assertNotNull(files);
        assertNotNull(filesNoTmp);
        assertTrue(files.contains(ssTable2DataFile));
        assertTrue(files.contains(ssTable2IndexFile));
        assertFalse(filesNoTmp.contains(ssTable2DataFile));
        assertFalse(filesNoTmp.contains(ssTable2IndexFile));
        log.finish();
        //Now it should be empty since the transaction has finished
        tmpFiles = getTemporaryFiles(dataFolder);
        assertNotNull(tmpFiles);
        assertEquals(0, tmpFiles.size());
        filesNoTmp = directories.sstableLister(Directories.OnTxnErr.THROW).skipTemporary(true).listFiles();
        assertNotNull(filesNoTmp);
        assertTrue(filesNoTmp.contains(ssTable2DataFile));
        assertTrue(filesNoTmp.contains(ssTable2IndexFile));
        sstable1.selfRef().release();
        sstable2.selfRef().release();
    }
}
Also used : Directories(org.apache.cassandra.db.Directories) SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) ImmutableSet(com.google.common.collect.ImmutableSet) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) Test(org.junit.Test) AbstractTransactionalTest(org.apache.cassandra.utils.concurrent.AbstractTransactionalTest)

Example 13 with Directories

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

the class LogTransactionTest method testObsoletedFilesChanged.

private static void testObsoletedFilesChanged(Consumer<SSTableReader> modifier) throws IOException {
    ColumnFamilyStore cfs = MockSchema.newCFS(KEYSPACE);
    File dataFolder = new Directories(cfs.metadata()).getDirectoryForNewSSTables();
    SSTableReader sstableOld = sstable(dataFolder, cfs, 0, 128);
    SSTableReader sstableNew = sstable(dataFolder, cfs, 1, 128);
    // simulate tracking sstables with a committed transaction except the checksum will be wrong
    LogTransaction log = new LogTransaction(OperationType.COMPACTION);
    assertNotNull(log);
    log.trackNew(sstableNew);
    LogTransaction.SSTableTidier tidier = log.obsoleted(sstableOld);
    //modify the old sstable files
    modifier.accept(sstableOld);
    //Fake a commit
    log.txnFile().commit();
    //This should not remove the old files
    LogTransaction.removeUnfinishedLeftovers(cfs.metadata());
    assertFiles(dataFolder.getPath(), Sets.newHashSet(Iterables.concat(sstableNew.getAllFilePaths(), sstableOld.getAllFilePaths(), log.logFilePaths())));
    sstableOld.selfRef().release();
    sstableNew.selfRef().release();
    // complete the transaction to avoid LEAK errors
    assertNull(log.complete(null));
    assertFiles(dataFolder.getPath(), Sets.newHashSet(Iterables.concat(sstableNew.getAllFilePaths(), sstableOld.getAllFilePaths(), log.logFilePaths())));
    // make sure to run the tidier to avoid any leaks in the logs
    tidier.run();
}
Also used : Directories(org.apache.cassandra.db.Directories) SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 14 with Directories

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

the class LogTransactionTest method testCommitOnlyNew.

@Test
public void testCommitOnlyNew() throws Throwable {
    ColumnFamilyStore cfs = MockSchema.newCFS(KEYSPACE);
    File dataFolder = new Directories(cfs.metadata()).getDirectoryForNewSSTables();
    SSTableReader sstable = sstable(dataFolder, cfs, 0, 128);
    LogTransaction log = new LogTransaction(OperationType.COMPACTION);
    assertNotNull(log);
    log.trackNew(sstable);
    log.finish();
    assertFiles(dataFolder.getPath(), new HashSet<>(sstable.getAllFilePaths()));
    sstable.selfRef().release();
}
Also used : Directories(org.apache.cassandra.db.Directories) SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) Test(org.junit.Test) AbstractTransactionalTest(org.apache.cassandra.utils.concurrent.AbstractTransactionalTest)

Example 15 with Directories

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

the class LogTransactionTest method testAbortOnlyOld.

@Test
public void testAbortOnlyOld() throws Throwable {
    ColumnFamilyStore cfs = MockSchema.newCFS(KEYSPACE);
    File dataFolder = new Directories(cfs.metadata()).getDirectoryForNewSSTables();
    SSTableReader sstable = sstable(dataFolder, cfs, 0, 128);
    LogTransaction log = new LogTransaction(OperationType.COMPACTION);
    assertNotNull(log);
    LogTransaction.SSTableTidier tidier = log.obsoleted(sstable);
    assertNotNull(tidier);
    tidier.abort();
    log.abort();
    sstable.selfRef().release();
    assertFiles(dataFolder.getPath(), new HashSet<>(sstable.getAllFilePaths()));
}
Also used : Directories(org.apache.cassandra.db.Directories) SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) Test(org.junit.Test) AbstractTransactionalTest(org.apache.cassandra.utils.concurrent.AbstractTransactionalTest)

Aggregations

File (java.io.File)19 RandomAccessFile (java.io.RandomAccessFile)19 ColumnFamilyStore (org.apache.cassandra.db.ColumnFamilyStore)19 Directories (org.apache.cassandra.db.Directories)19 SSTableReader (org.apache.cassandra.io.sstable.format.SSTableReader)19 AbstractTransactionalTest (org.apache.cassandra.utils.concurrent.AbstractTransactionalTest)17 Test (org.junit.Test)17 ImmutableSet (com.google.common.collect.ImmutableSet)4 Iterables (com.google.common.collect.Iterables)1 Sets (com.google.common.collect.Sets)1 IOError (java.io.IOError)1 IOException (java.io.IOException)1 Files (java.nio.file.Files)1 java.util (java.util)1 BiConsumer (java.util.function.BiConsumer)1 Consumer (java.util.function.Consumer)1 Collectors (java.util.stream.Collectors)1 Assert (junit.framework.Assert)1 Assert.assertNotNull (junit.framework.Assert.assertNotNull)1 Assert.assertNull (junit.framework.Assert.assertNull)1