Search in sources :

Example 1 with DataDirectories

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

the class DirectoriesTest method testDataDirectoriesIterator.

@Test
public void testDataDirectoriesIterator() throws IOException {
    Path tmpDir = Files.createTempDirectory(this.getClass().getSimpleName());
    Path subDir_1 = Files.createDirectory(tmpDir.resolve("a"));
    Path subDir_2 = Files.createDirectory(tmpDir.resolve("b"));
    Path subDir_3 = Files.createDirectory(tmpDir.resolve("c"));
    DataDirectories directories = new DataDirectories(new String[] { subDir_1.toString(), subDir_2.toString() }, new String[] { subDir_3.toString() });
    Iterator<DataDirectory> iter = directories.iterator();
    assertTrue(iter.hasNext());
    assertEquals(new DataDirectory(new File(subDir_1)), iter.next());
    assertTrue(iter.hasNext());
    assertEquals(new DataDirectory(new File(subDir_2)), iter.next());
    assertTrue(iter.hasNext());
    assertEquals(new DataDirectory(new File(subDir_3)), iter.next());
    assertFalse(iter.hasNext());
    directories = new DataDirectories(new String[] { subDir_1.toString(), subDir_2.toString() }, new String[] { subDir_1.toString() });
    iter = directories.iterator();
    assertTrue(iter.hasNext());
    assertEquals(new DataDirectory(new File(subDir_1)), iter.next());
    assertTrue(iter.hasNext());
    assertEquals(new DataDirectory(new File(subDir_2)), iter.next());
    assertFalse(iter.hasNext());
}
Also used : Path(java.nio.file.Path) DataDirectory(org.apache.cassandra.db.Directories.DataDirectory) DataDirectories(org.apache.cassandra.db.Directories.DataDirectories) File(org.apache.cassandra.io.util.File) Test(org.junit.Test)

Example 2 with DataDirectories

use of org.apache.cassandra.db.Directories.DataDirectories 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 : DataDirectories(org.apache.cassandra.db.Directories.DataDirectories) DataDirectory(org.apache.cassandra.db.Directories.DataDirectory) File(org.apache.cassandra.io.util.File) Test(org.junit.Test)

Example 3 with DataDirectories

use of org.apache.cassandra.db.Directories.DataDirectories 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 : DataDirectories(org.apache.cassandra.db.Directories.DataDirectories) DataDirectory(org.apache.cassandra.db.Directories.DataDirectory)

Aggregations

DataDirectories (org.apache.cassandra.db.Directories.DataDirectories)3 DataDirectory (org.apache.cassandra.db.Directories.DataDirectory)3 File (org.apache.cassandra.io.util.File)2 Test (org.junit.Test)2 Path (java.nio.file.Path)1