Search in sources :

Example 6 with Directories

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

the class LogTransactionTest method testCommitMultipleFolders.

@Test
public void testCommitMultipleFolders() throws Throwable {
    ColumnFamilyStore cfs = MockSchema.newCFS(KEYSPACE);
    File origiFolder = new Directories(cfs.metadata()).getDirectoryForNewSSTables();
    File dataFolder1 = new File(origiFolder, "1");
    File dataFolder2 = new File(origiFolder, "2");
    Files.createDirectories(dataFolder1.toPath());
    Files.createDirectories(dataFolder2.toPath());
    SSTableReader[] sstables = { sstable(dataFolder1, cfs, 0, 128), sstable(dataFolder1, cfs, 1, 128), sstable(dataFolder2, cfs, 2, 128), sstable(dataFolder2, cfs, 3, 128) };
    LogTransaction log = new LogTransaction(OperationType.COMPACTION);
    assertNotNull(log);
    LogTransaction.SSTableTidier[] tidiers = { log.obsoleted(sstables[0]), log.obsoleted(sstables[2]) };
    log.trackNew(sstables[1]);
    log.trackNew(sstables[3]);
    log.finish();
    sstables[0].markObsolete(tidiers[0]);
    sstables[2].markObsolete(tidiers[1]);
    Arrays.stream(sstables).forEach(s -> s.selfRef().release());
    LogTransaction.waitForDeletions();
    assertFiles(dataFolder1.getPath(), new HashSet<>(sstables[1].getAllFilePaths()));
    assertFiles(dataFolder2.getPath(), new HashSet<>(sstables[3].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)

Example 7 with Directories

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

the class LogTransactionTest method testRemoveUnfinishedLeftovers_abort.

@Test
public void testRemoveUnfinishedLeftovers_abort() throws Throwable {
    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 failed transaction (new log file NOT deleted)
    LogTransaction log = new LogTransaction(OperationType.COMPACTION);
    assertNotNull(log);
    log.trackNew(sstableNew);
    LogTransaction.SSTableTidier tidier = log.obsoleted(sstableOld);
    Set<File> tmpFiles = sstableNew.getAllFilePaths().stream().map(File::new).collect(Collectors.toSet());
    sstableNew.selfRef().release();
    sstableOld.selfRef().release();
    Assert.assertEquals(tmpFiles, getTemporaryFiles(sstableNew.descriptor.directory));
    // normally called at startup
    LogTransaction.removeUnfinishedLeftovers(cfs.metadata());
    // sstableOld should be only table left
    Directories directories = new Directories(cfs.metadata());
    Map<Descriptor, Set<Component>> sstables = directories.sstableLister(Directories.OnTxnErr.THROW).list();
    assertEquals(1, sstables.size());
    assertFiles(dataFolder.getPath(), new HashSet<>(sstableOld.getAllFilePaths()));
    // complete the transaction before releasing files
    tidier.run();
    log.close();
}
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 8 with Directories

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

the class LogTransactionTest method testCommitOnlyOld.

@Test
public void testCommitOnlyOld() 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);
    log.finish();
    sstable.markObsolete(tidier);
    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 9 with Directories

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

the class LogTransactionTest method testRemoveUnfinishedLeftovers_commit.

@Test
public void testRemoveUnfinishedLeftovers_commit() throws Throwable {
    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 (new log file deleted)
    LogTransaction log = new LogTransaction(OperationType.COMPACTION);
    assertNotNull(log);
    log.trackNew(sstableNew);
    LogTransaction.SSTableTidier tidier = log.obsoleted(sstableOld);
    //Fake a commit
    log.txnFile().commit();
    Set<File> tmpFiles = sstableOld.getAllFilePaths().stream().map(File::new).collect(Collectors.toSet());
    sstableNew.selfRef().release();
    sstableOld.selfRef().release();
    Assert.assertEquals(tmpFiles, getTemporaryFiles(sstableOld.descriptor.directory));
    // normally called at startup
    LogTransaction.removeUnfinishedLeftovers(cfs.metadata());
    // sstableNew should be only table left
    Directories directories = new Directories(cfs.metadata());
    Map<Descriptor, Set<Component>> sstables = directories.sstableLister(Directories.OnTxnErr.THROW).list();
    assertEquals(1, sstables.size());
    assertFiles(dataFolder.getPath(), new HashSet<>(sstableNew.getAllFilePaths()));
    // complete the transaction to avoid LEAK errors
    tidier.run();
    assertNull(log.complete(null));
}
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 10 with Directories

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

the class LogTransactionTest method testCorruptRecord.

private static void testCorruptRecord(BiConsumer<LogTransaction, SSTableReader> modifier, boolean isRecoverable) 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 transaction log or disk state for sstableOld
    modifier.accept(log, sstableOld);
    // Sync the folder to make sure that later on removeUnfinishedLeftovers picks up
    // any changes to the txn files done by the modifier
    assertNull(log.txnFile().syncDirectory(null));
    assertNull(log.complete(null));
    sstableOld.selfRef().release();
    sstableNew.selfRef().release();
    // The files on disk, for old files make sure to exclude the files that were deleted by the modifier
    Set<String> newFiles = sstableNew.getAllFilePaths().stream().collect(Collectors.toSet());
    Set<String> oldFiles = sstableOld.getAllFilePaths().stream().filter(p -> new File(p).exists()).collect(Collectors.toSet());
    //This should filter as in progress since the last record is corrupt
    assertFiles(newFiles, getTemporaryFiles(dataFolder));
    assertFiles(oldFiles, getFinalFiles(dataFolder));
    if (isRecoverable) {
        // the corruption is recoverable but the commit record is unreadable so the transaction is still in progress
        //This should remove new files
        LogTransaction.removeUnfinishedLeftovers(cfs.metadata());
        // make sure to exclude the old files that were deleted by the modifier
        assertFiles(dataFolder.getPath(), oldFiles);
    } else {
        // if an intermediate line was also modified, it should ignore the tx log file
        //This should not remove any files
        LogTransaction.removeUnfinishedLeftovers(cfs.metadata());
        assertFiles(dataFolder.getPath(), Sets.newHashSet(Iterables.concat(newFiles, oldFiles, 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) Assert.fail(junit.framework.Assert.fail) RandomAccessFile(java.io.RandomAccessFile) FileHandle(org.apache.cassandra.io.util.FileHandle) java.util(java.util) Iterables(com.google.common.collect.Iterables) BeforeClass(org.junit.BeforeClass) SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) Assert.assertNotNull(junit.framework.Assert.assertNotNull) BiConsumer(java.util.function.BiConsumer) SSTableFormat(org.apache.cassandra.io.sstable.format.SSTableFormat) MetadataCollector(org.apache.cassandra.io.sstable.metadata.MetadataCollector) SerializationHeader(org.apache.cassandra.db.SerializationHeader) AlwaysPresentFilter(org.apache.cassandra.utils.AlwaysPresentFilter) Assert.assertNull(junit.framework.Assert.assertNull) Transactional(org.apache.cassandra.utils.concurrent.Transactional) ImmutableSet(com.google.common.collect.ImmutableSet) MockSchema(org.apache.cassandra.schema.MockSchema) Files(java.nio.file.Files) org.apache.cassandra.io.sstable(org.apache.cassandra.io.sstable) MetadataType(org.apache.cassandra.io.sstable.metadata.MetadataType) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) Collectors(java.util.stream.Collectors) File(java.io.File) Sets(com.google.common.collect.Sets) IOError(java.io.IOError) org.apache.cassandra.db.compaction(org.apache.cassandra.db.compaction) Consumer(java.util.function.Consumer) Assert(junit.framework.Assert) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) FileUtils(org.apache.cassandra.io.util.FileUtils) Assert.assertFalse(org.junit.Assert.assertFalse) AbstractTransactionalTest(org.apache.cassandra.utils.concurrent.AbstractTransactionalTest) StatsMetadata(org.apache.cassandra.io.sstable.metadata.StatsMetadata) Directories(org.apache.cassandra.db.Directories) Assert.assertEquals(org.junit.Assert.assertEquals) SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

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