Search in sources :

Example 1 with PageSwapper

use of org.neo4j.io.pagecache.PageSwapper in project neo4j by neo4j.

the class SingleFilePageSwapperTest method mustHandleMischiefInPositionedRead.

@Test
public void mustHandleMischiefInPositionedRead() throws Exception {
    int bytesTotal = 512;
    byte[] data = new byte[bytesTotal];
    ThreadLocalRandom.current().nextBytes(data);
    PageSwapperFactory factory = createSwapperFactory();
    factory.setFileSystemAbstraction(getFs());
    File file = getFile();
    PageSwapper swapper = createSwapper(factory, file, bytesTotal, NO_CALLBACK, true);
    try {
        swapper.write(0, new ByteBufferPage(wrap(data)));
    } finally {
        swapper.close();
    }
    RandomAdversary adversary = new RandomAdversary(0.5, 0.0, 0.0);
    factory.setFileSystemAbstraction(new AdversarialFileSystemAbstraction(adversary, getFs()));
    swapper = createSwapper(factory, file, bytesTotal, NO_CALLBACK, false);
    ByteBufferPage page = createPage(bytesTotal);
    try {
        for (int i = 0; i < 10_000; i++) {
            clear(page);
            assertThat(swapper.read(0, page), is((long) bytesTotal));
            assertThat(array(page.buffer), is(data));
        }
    } finally {
        swapper.close();
    }
}
Also used : PageSwapperFactory(org.neo4j.io.pagecache.PageSwapperFactory) PageSwapper(org.neo4j.io.pagecache.PageSwapper) File(java.io.File) AdversarialFileSystemAbstraction(org.neo4j.adversaries.fs.AdversarialFileSystemAbstraction) RandomAdversary(org.neo4j.adversaries.RandomAdversary) PageSwapperTest(org.neo4j.io.pagecache.PageSwapperTest) Test(org.junit.Test)

Example 2 with PageSwapper

use of org.neo4j.io.pagecache.PageSwapper in project neo4j by neo4j.

the class SingleFilePageSwapperTest method fileMustRemainLockedEvenIfChannelIsClosedByStrayInterrupt.

@Test
public void fileMustRemainLockedEvenIfChannelIsClosedByStrayInterrupt() throws Exception {
    // no file locking on Windows.
    assumeFalse("No file locking on Windows", SystemUtils.IS_OS_WINDOWS);
    PageSwapperFactory factory = createSwapperFactory();
    factory.setFileSystemAbstraction(fileSystem);
    File file = testDir.file("file");
    fileSystem.create(file).close();
    PageSwapper pageSwapper = createSwapper(factory, file, 4, NO_CALLBACK, false);
    try {
        StoreChannel channel = fileSystem.open(file, "rw");
        Thread.currentThread().interrupt();
        pageSwapper.force();
        expectedException.expect(OverlappingFileLockException.class);
        channel.tryLock();
    } finally {
        pageSwapper.close();
    }
}
Also used : PageSwapperFactory(org.neo4j.io.pagecache.PageSwapperFactory) PageSwapper(org.neo4j.io.pagecache.PageSwapper) StoreChannel(org.neo4j.io.fs.StoreChannel) DelegatingStoreChannel(org.neo4j.graphdb.mockfs.DelegatingStoreChannel) File(java.io.File) PageSwapperTest(org.neo4j.io.pagecache.PageSwapperTest) Test(org.junit.Test)

Example 3 with PageSwapper

use of org.neo4j.io.pagecache.PageSwapper in project neo4j by neo4j.

the class SingleFilePageSwapperTest method swappingOutMustWritePageToFile.

@Test
public void swappingOutMustWritePageToFile() throws Exception {
    getFs().create(getFile()).close();
    byte[] expected = new byte[] { 1, 2, 3, 4 };
    ByteBufferPage page = new ByteBufferPage(wrap(expected));
    PageSwapperFactory factory = createSwapperFactory();
    PageSwapper swapper = createSwapper(factory, getFile(), 4, null, false);
    swapper.write(0, page);
    InputStream stream = getFs().openAsInputStream(getFile());
    byte[] actual = new byte[expected.length];
    assertThat(stream.read(actual), is(actual.length));
    assertThat(actual, byteArray(expected));
}
Also used : PageSwapperFactory(org.neo4j.io.pagecache.PageSwapperFactory) InputStream(java.io.InputStream) PageSwapper(org.neo4j.io.pagecache.PageSwapper) PageSwapperTest(org.neo4j.io.pagecache.PageSwapperTest) Test(org.junit.Test)

Example 4 with PageSwapper

use of org.neo4j.io.pagecache.PageSwapper in project neo4j by neo4j.

the class SingleFilePageSwapperTest method creatingSwapperForFileMustTakeLockOnFile.

/**
     * The OverlappingFileLockException is thrown when tryLock is called on the same file *in the same JVM*.
     */
@Test
public void creatingSwapperForFileMustTakeLockOnFile() throws Exception {
    assumeFalse("No file locking on Windows", SystemUtils.IS_OS_WINDOWS);
    PageSwapperFactory factory = createSwapperFactory();
    factory.setFileSystemAbstraction(fileSystem);
    File file = testDir.file("file");
    fileSystem.create(file).close();
    PageSwapper pageSwapper = createSwapper(factory, file, 4, NO_CALLBACK, false);
    try {
        StoreChannel channel = fileSystem.open(file, "rw");
        expectedException.expect(OverlappingFileLockException.class);
        channel.tryLock();
    } finally {
        pageSwapper.close();
    }
}
Also used : PageSwapperFactory(org.neo4j.io.pagecache.PageSwapperFactory) PageSwapper(org.neo4j.io.pagecache.PageSwapper) StoreChannel(org.neo4j.io.fs.StoreChannel) DelegatingStoreChannel(org.neo4j.graphdb.mockfs.DelegatingStoreChannel) File(java.io.File) PageSwapperTest(org.neo4j.io.pagecache.PageSwapperTest) Test(org.junit.Test)

Example 5 with PageSwapper

use of org.neo4j.io.pagecache.PageSwapper in project neo4j by neo4j.

the class SingleFilePageSwapperTest method mustHandleMischiefInPositionedWrite.

@Test
public void mustHandleMischiefInPositionedWrite() throws Exception {
    int bytesTotal = 512;
    byte[] data = new byte[bytesTotal];
    ThreadLocalRandom.current().nextBytes(data);
    ByteBufferPage zeroPage = createPage(bytesTotal);
    clear(zeroPage);
    File file = getFile();
    PageSwapperFactory factory = createSwapperFactory();
    RandomAdversary adversary = new RandomAdversary(0.5, 0.0, 0.0);
    factory.setFileSystemAbstraction(new AdversarialFileSystemAbstraction(adversary, getFs()));
    PageSwapper swapper = createSwapper(factory, file, bytesTotal, NO_CALLBACK, true);
    ByteBufferPage page = createPage(bytesTotal);
    try {
        for (int i = 0; i < 10_000; i++) {
            adversary.setProbabilityFactor(0);
            swapper.write(0, zeroPage);
            page.putBytes(data, 0, 0, data.length);
            adversary.setProbabilityFactor(1);
            assertThat(swapper.write(0, page), is((long) bytesTotal));
            clear(page);
            adversary.setProbabilityFactor(0);
            swapper.read(0, page);
            assertThat(array(page.buffer), is(data));
        }
    } finally {
        swapper.close();
    }
}
Also used : PageSwapperFactory(org.neo4j.io.pagecache.PageSwapperFactory) PageSwapper(org.neo4j.io.pagecache.PageSwapper) File(java.io.File) AdversarialFileSystemAbstraction(org.neo4j.adversaries.fs.AdversarialFileSystemAbstraction) RandomAdversary(org.neo4j.adversaries.RandomAdversary) PageSwapperTest(org.neo4j.io.pagecache.PageSwapperTest) Test(org.junit.Test)

Aggregations

PageSwapper (org.neo4j.io.pagecache.PageSwapper)11 Test (org.junit.Test)10 PageSwapperFactory (org.neo4j.io.pagecache.PageSwapperFactory)10 PageSwapperTest (org.neo4j.io.pagecache.PageSwapperTest)10 File (java.io.File)6 DelegatingStoreChannel (org.neo4j.graphdb.mockfs.DelegatingStoreChannel)5 StoreChannel (org.neo4j.io.fs.StoreChannel)5 RandomAdversary (org.neo4j.adversaries.RandomAdversary)4 AdversarialFileSystemAbstraction (org.neo4j.adversaries.fs.AdversarialFileSystemAbstraction)4 InputStream (java.io.InputStream)2 ByteBuffer (java.nio.ByteBuffer)2