Search in sources :

Example 1 with DataDirectory

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

the class Util method markDirectoriesUnwriteable.

public static Closeable markDirectoriesUnwriteable(ColumnFamilyStore cfs) {
    try {
        for (; ; ) {
            DataDirectory dir = cfs.getDirectories().getWriteableLocation(1);
            BlacklistedDirectories.maybeMarkUnwritable(cfs.getDirectories().getLocationForDisk(dir));
        }
    } catch (IOError e) {
    // Expected -- marked all directories as unwritable
    }
    return () -> BlacklistedDirectories.clearUnwritableUnsafe();
}
Also used : IOError(java.io.IOError) DataDirectory(org.apache.cassandra.db.Directories.DataDirectory)

Example 2 with DataDirectory

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

the class DirectoriesTest method testDiskFailurePolicy_best_effort.

@Test
public void testDiskFailurePolicy_best_effort() {
    DiskFailurePolicy origPolicy = DatabaseDescriptor.getDiskFailurePolicy();
    try {
        DatabaseDescriptor.setDiskFailurePolicy(DiskFailurePolicy.best_effort);
        // Fake a Directory creation failure
        if (Directories.dataDirectories.length > 0) {
            String[] path = new String[] { KS, "bad" };
            File dir = new File(Directories.dataDirectories[0].location, StringUtils.join(path, File.separator));
            FileUtils.handleFSError(new FSWriteError(new IOException("Unable to create directory " + dir), dir));
        }
        for (DataDirectory dd : Directories.dataDirectories) {
            File file = new File(dd.location, new File(KS, "bad").getPath());
            assertTrue(BlacklistedDirectories.isUnwritable(file));
        }
    } finally {
        DatabaseDescriptor.setDiskFailurePolicy(origPolicy);
    }
}
Also used : DiskFailurePolicy(org.apache.cassandra.config.Config.DiskFailurePolicy) FSWriteError(org.apache.cassandra.io.FSWriteError) DataDirectory(org.apache.cassandra.db.Directories.DataDirectory) IOException(java.io.IOException) File(java.io.File) Test(org.junit.Test)

Example 3 with DataDirectory

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

the class DirectoriesTest method testDiskFreeSpace.

@Test
public void testDiskFreeSpace() {
    DataDirectory[] dataDirectories = new DataDirectory[] { new DataDirectory(new File("/nearlyFullDir1")) {

        public long getAvailableSpace() {
            return 11L;
        }
    }, new DataDirectory(new File("/nearlyFullDir2")) {

        public long getAvailableSpace() {
            return 10L;
        }
    }, new DataDirectory(new File("/uniformDir1")) {

        public long getAvailableSpace() {
            return 1000L;
        }
    }, new DataDirectory(new File("/uniformDir2")) {

        public long getAvailableSpace() {
            return 999L;
        }
    }, new DataDirectory(new File("/veryFullDir")) {

        public long getAvailableSpace() {
            return 4L;
        }
    } };
    // directories should be sorted
    // 1. by their free space ratio
    // before weighted random is applied
    List<Directories.DataDirectoryCandidate> candidates = getWriteableDirectories(dataDirectories, 0L);
    // available: 1000
    assertSame(dataDirectories[2], candidates.get(0).dataDirectory);
    // available: 999
    assertSame(dataDirectories[3], candidates.get(1).dataDirectory);
    // available: 11
    assertSame(dataDirectories[0], candidates.get(2).dataDirectory);
    // available: 10
    assertSame(dataDirectories[1], candidates.get(3).dataDirectory);
    // check for writeSize == 5
    Map<DataDirectory, DataDirectory> testMap = new IdentityHashMap<>();
    for (int i = 0; ; i++) {
        candidates = getWriteableDirectories(dataDirectories, 5L);
        assertEquals(4, candidates.size());
        DataDirectory dir = Directories.pickWriteableDirectory(candidates);
        testMap.put(dir, dir);
        assertFalse(testMap.size() > 4);
        if (testMap.size() == 4) {
            // at least (rule of thumb) 100 iterations to see whether there are more (wrong) directories returned
            if (i >= 100)
                break;
        }
        // many tries
        if (i >= 10000000)
            fail();
    }
    // check for writeSize == 11
    testMap.clear();
    for (int i = 0; ; i++) {
        candidates = getWriteableDirectories(dataDirectories, 11L);
        assertEquals(3, candidates.size());
        for (Directories.DataDirectoryCandidate candidate : candidates) assertTrue(candidate.dataDirectory.getAvailableSpace() >= 11L);
        DataDirectory dir = Directories.pickWriteableDirectory(candidates);
        testMap.put(dir, dir);
        assertFalse(testMap.size() > 3);
        if (testMap.size() == 3) {
            // at least (rule of thumb) 100 iterations
            if (i >= 100)
                break;
        }
        // many tries
        if (i >= 10000000)
            fail();
    }
}
Also used : DataDirectory(org.apache.cassandra.db.Directories.DataDirectory) File(java.io.File) Test(org.junit.Test)

Example 4 with DataDirectory

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

the class DirectoriesTest method getWriteableDirectories.

private List<Directories.DataDirectoryCandidate> getWriteableDirectories(DataDirectory[] dataDirectories, long writeSize) {
    // copied from Directories.getWriteableLocation(long)
    List<Directories.DataDirectoryCandidate> candidates = new ArrayList<>();
    long totalAvailable = 0L;
    for (DataDirectory dataDir : dataDirectories) {
        Directories.DataDirectoryCandidate candidate = new Directories.DataDirectoryCandidate(dataDir);
        // exclude directory if its total writeSize does not fit to data directory
        if (candidate.availableSpace < writeSize)
            continue;
        candidates.add(candidate);
        totalAvailable += candidate.availableSpace;
    }
    Directories.sortWriteableCandidates(candidates, totalAvailable);
    return candidates;
}
Also used : DataDirectory(org.apache.cassandra.db.Directories.DataDirectory)

Aggregations

DataDirectory (org.apache.cassandra.db.Directories.DataDirectory)4 File (java.io.File)2 Test (org.junit.Test)2 IOError (java.io.IOError)1 IOException (java.io.IOException)1 DiskFailurePolicy (org.apache.cassandra.config.Config.DiskFailurePolicy)1 FSWriteError (org.apache.cassandra.io.FSWriteError)1