Search in sources :

Example 1 with ByteBufferPage

use of org.neo4j.io.pagecache.impl.ByteBufferPage in project neo4j by neo4j.

the class PageSwapperTest method positionedVectoredReadWhereSecondLastPageExtendBeyondEndOfFileMustHaveRestZeroFilled.

@Test
public void positionedVectoredReadWhereSecondLastPageExtendBeyondEndOfFileMustHaveRestZeroFilled() throws Exception {
    File file = file("file");
    PageSwapperFactory factory = createSwapperFactory();
    PageSwapper swapper = createSwapperAndFile(factory, file, 4);
    ByteBufferPage output = createPage(4);
    output.putInt(1, 0);
    swapper.write(0, output);
    output.putInt(2, 0);
    swapper.write(1, output);
    output.putInt(3, 0);
    swapper.write(2, output);
    swapper.close();
    swapper = createSwapper(factory, file, 8, NO_CALLBACK, false);
    ByteBufferPage pageA = createPage(8);
    ByteBufferPage pageB = createPage(8);
    ByteBufferPage pageC = createPage(8);
    pageA.putInt(-1, 0);
    pageB.putInt(-1, 0);
    pageC.putInt(-1, 0);
    assertThat(swapper.read(0, new Page[] { pageA, pageB, pageC }, 0, 3), isOneOf(12L, 16L));
    assertThat(pageA.getInt(0), is(1));
    assertThat(pageA.getInt(4), is(2));
    assertThat(pageB.getInt(0), is(3));
    assertThat(pageB.getInt(4), is(0));
    assertThat(pageC.getLong(0), is(0L));
}
Also used : ByteBufferPage(org.neo4j.io.pagecache.impl.ByteBufferPage) ByteBufferPage(org.neo4j.io.pagecache.impl.ByteBufferPage) File(java.io.File) Test(org.junit.Test)

Example 2 with ByteBufferPage

use of org.neo4j.io.pagecache.impl.ByteBufferPage in project neo4j by neo4j.

the class PageSwapperTest method mustReopenChannelWhenVectoredWriteFailsWithAsynchronousCloseException.

@Test
public void mustReopenChannelWhenVectoredWriteFailsWithAsynchronousCloseException() throws Exception {
    ByteBufferPage page = createPage();
    page.putLong(X, 0);
    page.putLong(Y, 8);
    page.putInt(Z, 16);
    File file = file("a");
    PageSwapperFactory swapperFactory = createSwapperFactory();
    PageSwapper swapper = createSwapperAndFile(swapperFactory, file);
    Thread.currentThread().interrupt();
    swapper.write(0, new Page[] { page }, 0, 1);
    // Clear the interrupted flag and assert that it was still raised
    assertTrue(Thread.interrupted());
    // This must not throw because we should still have a usable channel
    swapper.force();
    clear(page);
    swapper.read(0, page);
    assertThat(page.getLong(0), is(X));
    assertThat(page.getLong(8), is(Y));
    assertThat(page.getInt(16), is(Z));
}
Also used : ByteBufferPage(org.neo4j.io.pagecache.impl.ByteBufferPage) File(java.io.File) Test(org.junit.Test)

Example 3 with ByteBufferPage

use of org.neo4j.io.pagecache.impl.ByteBufferPage in project neo4j by neo4j.

the class PageSwapperTest method concurrentPositionedVectoredReadsAndWritesMustNotInterfere.

@Test
public void concurrentPositionedVectoredReadsAndWritesMustNotInterfere() throws Exception {
    File file = file("file");
    PageSwapperFactory factory = createSwapperFactory();
    final PageSwapper swapper = createSwapperAndFile(factory, file, 4);
    final int pageCount = 100;
    final int iterations = 20000;
    final CountDownLatch startLatch = new CountDownLatch(1);
    ByteBufferPage output = createPage(4);
    for (int i = 0; i < pageCount; i++) {
        output.putInt(i + 1, 0);
        swapper.write(i, output);
    }
    Callable<Void> work = () -> {
        ThreadLocalRandom rng = ThreadLocalRandom.current();
        ByteBufferPage[] pages = new ByteBufferPage[10];
        for (int i = 0; i < pages.length; i++) {
            pages[i] = createPage(4);
        }
        startLatch.await();
        for (int i = 0; i < iterations; i++) {
            long startFilePageId = rng.nextLong(0, pageCount - pages.length);
            if (rng.nextBoolean()) {
                // Do read
                long bytesRead = swapper.read(startFilePageId, pages, 0, pages.length);
                assertThat(bytesRead, is(pages.length * 4L));
                for (int j = 0; j < pages.length; j++) {
                    int expectedValue = (int) (1 + j + startFilePageId);
                    int actualValue = pages[j].getInt(0);
                    assertThat(actualValue, is(expectedValue));
                }
            } else {
                // Do write
                for (int j = 0; j < pages.length; j++) {
                    int value = (int) (1 + j + startFilePageId);
                    pages[j].putInt(value, 0);
                }
                assertThat(swapper.write(startFilePageId, pages, 0, pages.length), is(pages.length * 4L));
            }
        }
        return null;
    };
    int threads = 8;
    ExecutorService executor = Executors.newFixedThreadPool(threads, r -> {
        Thread thread = Executors.defaultThreadFactory().newThread(r);
        thread.setDaemon(true);
        return thread;
    });
    List<Future<Void>> futures = new ArrayList<>(threads);
    for (int i = 0; i < threads; i++) {
        futures.add(executor.submit(work));
    }
    startLatch.countDown();
    for (Future<Void> future : futures) {
        future.get();
    }
}
Also used : ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBufferPage(org.neo4j.io.pagecache.impl.ByteBufferPage) ExecutorService(java.util.concurrent.ExecutorService) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Future(java.util.concurrent.Future) File(java.io.File) Test(org.junit.Test)

Example 4 with ByteBufferPage

use of org.neo4j.io.pagecache.impl.ByteBufferPage in project neo4j by neo4j.

the class PageSwapperTest method vectoredReadMustReadNothingWhenLengthIsZero.

@Test
public void vectoredReadMustReadNothingWhenLengthIsZero() throws Exception {
    File file = file("file");
    PageSwapperFactory factory = createSwapperFactory();
    PageSwapper swapper = createSwapperAndFile(factory, file, 4);
    ByteBufferPage pageA = createPage(4);
    ByteBufferPage pageB = createPage(4);
    pageA.putInt(1, 0);
    pageB.putInt(2, 0);
    Page[] pages = { pageA, pageB };
    swapper.write(0, pages, 0, 2);
    pageA.putInt(3, 0);
    pageB.putInt(4, 0);
    swapper.read(0, pages, 0, 0);
    int[] expectedValues = { 3, 4 };
    int[] actualValues = { pageA.getInt(0), pageB.getInt(0) };
    assertThat(actualValues, is(expectedValues));
}
Also used : ByteBufferPage(org.neo4j.io.pagecache.impl.ByteBufferPage) ByteBufferPage(org.neo4j.io.pagecache.impl.ByteBufferPage) File(java.io.File) Test(org.junit.Test)

Example 5 with ByteBufferPage

use of org.neo4j.io.pagecache.impl.ByteBufferPage in project neo4j by neo4j.

the class PageSwapperTest method positionedVectoredReadBeyondEndOfFileMustFillPagesWithZeros.

@Test
public void positionedVectoredReadBeyondEndOfFileMustFillPagesWithZeros() throws Exception {
    File file = file("file");
    PageSwapperFactory factory = createSwapperFactory();
    PageSwapper swapper = createSwapperAndFile(factory, file, 4);
    ByteBufferPage output = createPage(4);
    output.putInt(0xFFFF_FFFF, 0);
    swapper.write(0, new Page[] { output, output, output }, 0, 3);
    ByteBufferPage pageA = createPage(4);
    ByteBufferPage pageB = createPage(4);
    pageA.putInt(-1, 0);
    pageB.putInt(-1, 0);
    assertThat(swapper.read(3, new Page[] { pageA, pageB }, 0, 2), is(0L));
    assertThat(pageA.getInt(0), is(0));
    assertThat(pageB.getInt(0), is(0));
}
Also used : ByteBufferPage(org.neo4j.io.pagecache.impl.ByteBufferPage) ByteBufferPage(org.neo4j.io.pagecache.impl.ByteBufferPage) File(java.io.File) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)31 ByteBufferPage (org.neo4j.io.pagecache.impl.ByteBufferPage)31 File (java.io.File)30 ClosedChannelException (java.nio.channels.ClosedChannelException)4 ArrayList (java.util.ArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1