Search in sources :

Example 6 with PageSwapper

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

the class SingleFilePageSwapperTest method mustHandleMischiefInPositionedVectoredRead.

@Test
public void mustHandleMischiefInPositionedVectoredRead() throws Exception {
    int bytesTotal = 512;
    int bytesPerPage = 32;
    int pageCount = bytesTotal / bytesPerPage;
    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, bytesPerPage, NO_CALLBACK, false);
    ByteBufferPage[] pages = new ByteBufferPage[pageCount];
    for (int i = 0; i < pageCount; i++) {
        pages[i] = createPage(bytesPerPage);
    }
    byte[] temp = new byte[bytesPerPage];
    try {
        for (int i = 0; i < 10_000; i++) {
            for (ByteBufferPage page : pages) {
                clear(page);
            }
            assertThat(swapper.read(0, pages, 0, pages.length), is((long) bytesTotal));
            for (int j = 0; j < pageCount; j++) {
                System.arraycopy(data, j * bytesPerPage, temp, 0, bytesPerPage);
                assertThat(array(pages[j].buffer), is(temp));
            }
        }
    } 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 7 with PageSwapper

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

the class MuninnPage method evict.

/**
     * NOTE: This method MUST be called while holding the page write lock.
     */
public void evict(EvictionEvent evictionEvent) throws IOException {
    long filePageId = this.filePageId;
    evictionEvent.setCachePageId(getCachePageId());
    evictionEvent.setFilePageId(filePageId);
    PageSwapper swapper = this.swapper;
    evictionEvent.setSwapper(swapper);
    flush(evictionEvent.flushEventOpportunity());
    this.filePageId = PageCursor.UNBOUND_PAGE_ID;
    this.swapper = null;
    if (swapper != null) {
        // The swapper can be null if the last page fault
        // that page threw an exception.
        swapper.evicted(filePageId, this);
    }
}
Also used : PageSwapper(org.neo4j.io.pagecache.PageSwapper)

Example 8 with PageSwapper

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

the class SingleFilePageSwapperTest method swappingOutMustNotOverwriteDataBeyondPage.

@Test
public void swappingOutMustNotOverwriteDataBeyondPage() throws Exception {
    byte[] initialData = new byte[] { // --- page 0:
    1, 2, 3, 4, // --- page 1:
    5, 6, 7, 8, // --- page 2:
    9, 10 };
    byte[] finalData = new byte[] { // --- page 0:
    1, 2, 3, 4, // --- page 1:
    8, 7, 6, 5, // --- page 2:
    9, 10 };
    StoreChannel channel = getFs().create(getFile());
    channel.writeAll(wrap(initialData));
    channel.close();
    byte[] change = new byte[] { 8, 7, 6, 5 };
    ByteBufferPage page = new ByteBufferPage(wrap(change));
    PageSwapperFactory factory = createSwapperFactory();
    PageSwapper swapper = createSwapper(factory, getFile(), 4, null, false);
    swapper.write(1, page);
    InputStream stream = getFs().openAsInputStream(getFile());
    byte[] actual = new byte[(int) getFs().getFileSize(getFile())];
    assertThat(stream.read(actual), is(actual.length));
    assertThat(actual, byteArray(finalData));
}
Also used : PageSwapperFactory(org.neo4j.io.pagecache.PageSwapperFactory) InputStream(java.io.InputStream) StoreChannel(org.neo4j.io.fs.StoreChannel) DelegatingStoreChannel(org.neo4j.graphdb.mockfs.DelegatingStoreChannel) PageSwapper(org.neo4j.io.pagecache.PageSwapper) PageSwapperTest(org.neo4j.io.pagecache.PageSwapperTest) Test(org.junit.Test)

Example 9 with PageSwapper

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

the class SingleFilePageSwapperTest method mustZeroFillPageBeyondEndOfFile.

@Test
public void mustZeroFillPageBeyondEndOfFile() throws Exception {
    byte[] bytes = new byte[] { // --- page 0:
    1, 2, 3, 4, // --- page 1:
    5, 6 };
    StoreChannel channel = getFs().create(getFile());
    channel.writeAll(wrap(bytes));
    channel.close();
    PageSwapperFactory factory = createSwapperFactory();
    PageSwapper swapper = createSwapper(factory, getFile(), 4, null, false);
    ByteBuffer target = ByteBuffer.allocateDirect(4);
    ByteBufferPage page = new ByteBufferPage(target);
    swapper.read(1, page);
    assertThat(array(target), byteArray(new byte[] { 5, 6, 0, 0 }));
}
Also used : PageSwapperFactory(org.neo4j.io.pagecache.PageSwapperFactory) StoreChannel(org.neo4j.io.fs.StoreChannel) DelegatingStoreChannel(org.neo4j.graphdb.mockfs.DelegatingStoreChannel) PageSwapper(org.neo4j.io.pagecache.PageSwapper) ByteBuffer(java.nio.ByteBuffer) PageSwapperTest(org.neo4j.io.pagecache.PageSwapperTest) Test(org.junit.Test)

Example 10 with PageSwapper

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

the class SingleFilePageSwapperTest method mustHandleMischiefInPositionedVectoredWrite.

@Test
public void mustHandleMischiefInPositionedVectoredWrite() throws Exception {
    int bytesTotal = 512;
    int bytesPerPage = 32;
    int pageCount = bytesTotal / bytesPerPage;
    byte[] data = new byte[bytesTotal];
    ThreadLocalRandom.current().nextBytes(data);
    ByteBufferPage zeroPage = createPage(bytesPerPage);
    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, bytesPerPage, NO_CALLBACK, true);
    ByteBufferPage[] writePages = new ByteBufferPage[pageCount];
    ByteBufferPage[] readPages = new ByteBufferPage[pageCount];
    ByteBufferPage[] zeroPages = new ByteBufferPage[pageCount];
    for (int i = 0; i < pageCount; i++) {
        writePages[i] = createPage(bytesPerPage);
        writePages[i].putBytes(data, 0, i * bytesPerPage, bytesPerPage);
        readPages[i] = createPage(bytesPerPage);
        zeroPages[i] = zeroPage;
    }
    try {
        for (int i = 0; i < 10_000; i++) {
            adversary.setProbabilityFactor(0);
            swapper.write(0, zeroPages, 0, pageCount);
            adversary.setProbabilityFactor(1);
            swapper.write(0, writePages, 0, pageCount);
            for (ByteBufferPage readPage : readPages) {
                clear(readPage);
            }
            adversary.setProbabilityFactor(0);
            assertThat(swapper.read(0, readPages, 0, pageCount), is((long) bytesTotal));
            for (int j = 0; j < pageCount; j++) {
                assertThat(array(readPages[j].buffer), is(array(writePages[j].buffer)));
            }
        }
    } 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