Search in sources :

Example 21 with PageSwapperFactory

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

the class SingleFilePageSwapperTest method reportExternalIoOnSwapInWithLength.

@Test
void reportExternalIoOnSwapInWithLength() throws IOException {
    byte[] bytes = new byte[] { 1, 2, 3, 4 };
    try (StoreChannel channel = getFs().write(getPath())) {
        channel.writeAll(wrap(bytes));
    }
    PageSwapperFactory factory = createSwapperFactory(getFs());
    CountingIOController controller = new CountingIOController();
    try (var swapper = createSwapper(factory, getPath(), 4, null, false, false, true, controller)) {
        long target = createPage(4);
        int numberOfReads = 12;
        for (int i = 0; i < numberOfReads; i++) {
            assertEquals(4, swapper.read(0, target, bytes.length));
        }
        assertEquals(numberOfReads, controller.getExternalIOCounter());
    }
}
Also used : PageSwapperFactory(org.neo4j.io.pagecache.PageSwapperFactory) StoreChannel(org.neo4j.io.fs.StoreChannel) DelegatingStoreChannel(org.neo4j.io.fs.DelegatingStoreChannel) PageSwapperTest(org.neo4j.io.pagecache.PageSwapperTest) Test(org.junit.jupiter.api.Test)

Example 22 with PageSwapperFactory

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

the class SingleFilePageSwapperTest method mustCloseFilesIfTakingFileLockThrows.

@Test
@DisabledOnOs(OS.WINDOWS)
void mustCloseFilesIfTakingFileLockThrows() throws Exception {
    final AtomicInteger openFilesCounter = new AtomicInteger();
    PageSwapperFactory factory = createSwapperFactory(new DelegatingFileSystemAbstraction(fileSystem) {

        @Override
        public StoreChannel open(Path fileName, Set<OpenOption> options) throws IOException {
            openFilesCounter.getAndIncrement();
            return new DelegatingStoreChannel(super.open(fileName, options)) {

                @Override
                public void close() throws IOException {
                    openFilesCounter.getAndDecrement();
                    super.close();
                }
            };
        }
    });
    Path file = testDir.file("file");
    try (StoreChannel ch = fileSystem.write(file);
        FileLock ignore = ch.tryLock()) {
        createSwapper(factory, file, 4, NO_CALLBACK, false).close();
        fail("Creating a page swapper for a locked channel should have thrown");
    } catch (FileLockException e) {
    // As expected.
    }
    assertThat(openFilesCounter.get()).isEqualTo(0);
}
Also used : Path(java.nio.file.Path) PageSwapperFactory(org.neo4j.io.pagecache.PageSwapperFactory) OpenOption(java.nio.file.OpenOption) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StoreChannel(org.neo4j.io.fs.StoreChannel) DelegatingStoreChannel(org.neo4j.io.fs.DelegatingStoreChannel) FileLock(java.nio.channels.FileLock) DelegatingStoreChannel(org.neo4j.io.fs.DelegatingStoreChannel) IOException(java.io.IOException) DelegatingFileSystemAbstraction(org.neo4j.io.fs.DelegatingFileSystemAbstraction) DisabledOnOs(org.junit.jupiter.api.condition.DisabledOnOs) PageSwapperTest(org.neo4j.io.pagecache.PageSwapperTest) Test(org.junit.jupiter.api.Test)

Example 23 with PageSwapperFactory

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

the class SingleFilePageSwapperTest method swappingInMustFillPageWithData.

@Test
void swappingInMustFillPageWithData() throws Exception {
    byte[] bytes = new byte[] { 1, 2, 3, 4 };
    StoreChannel channel = getFs().write(getPath());
    channel.writeAll(wrap(bytes));
    channel.close();
    PageSwapperFactory factory = createSwapperFactory(getFs());
    PageSwapper swapper = createSwapper(factory, getPath(), 4, null, false);
    long target = createPage(4);
    assertEquals(4, swapper.read(0, target));
    assertThat(array(target)).containsExactly(bytes);
}
Also used : PageSwapperFactory(org.neo4j.io.pagecache.PageSwapperFactory) StoreChannel(org.neo4j.io.fs.StoreChannel) DelegatingStoreChannel(org.neo4j.io.fs.DelegatingStoreChannel) PageSwapper(org.neo4j.io.pagecache.PageSwapper) PageSwapperTest(org.neo4j.io.pagecache.PageSwapperTest) Test(org.junit.jupiter.api.Test)

Example 24 with PageSwapperFactory

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

the class SingleFilePageSwapperTest method swappingOutMustNotOverwriteDataBeyondPage.

@Test
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().write(getPath());
    channel.writeAll(wrap(initialData));
    channel.close();
    byte[] change = new byte[] { 8, 7, 6, 5 };
    long page = createPage(change);
    PageSwapperFactory factory = createSwapperFactory(getFs());
    PageSwapper swapper = createSwapper(factory, getPath(), 4, null, false);
    swapper.write(1, page);
    try (InputStream stream = getFs().openAsInputStream(getPath())) {
        byte[] actual = new byte[(int) getFs().getFileSize(getPath())];
        assertThat(stream.read(actual)).isEqualTo(actual.length);
        assertThat(actual).containsExactly(finalData);
    }
}
Also used : PageSwapperFactory(org.neo4j.io.pagecache.PageSwapperFactory) InputStream(java.io.InputStream) StoreChannel(org.neo4j.io.fs.StoreChannel) DelegatingStoreChannel(org.neo4j.io.fs.DelegatingStoreChannel) PageSwapper(org.neo4j.io.pagecache.PageSwapper) PageSwapperTest(org.neo4j.io.pagecache.PageSwapperTest) Test(org.junit.jupiter.api.Test)

Example 25 with PageSwapperFactory

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

the class SingleFilePageSwapperTest method mustHandleMischiefInPositionedVectoredWrite.

@Test
void mustHandleMischiefInPositionedVectoredWrite() throws Exception {
    int bytesTotal = 512;
    int bytesPerPage = 32;
    int pageCount = bytesTotal / bytesPerPage;
    byte[] data = new byte[bytesTotal];
    ThreadLocalRandom.current().nextBytes(data);
    long zeroPage = createPage(bytesPerPage);
    clear(zeroPage);
    Path file = getPath();
    RandomAdversary adversary = new RandomAdversary(0.5, 0.0, 0.0);
    PageSwapperFactory factory = createSwapperFactory(new AdversarialFileSystemAbstraction(adversary, getFs()));
    PageSwapper swapper = createSwapper(factory, file, bytesPerPage, NO_CALLBACK, true);
    long[] writePages = new long[pageCount];
    long[] readPages = new long[pageCount];
    long[] zeroPages = new long[pageCount];
    int[] pageLengths = new int[pageCount];
    for (int i = 0; i < pageCount; i++) {
        writePages[i] = createPage(bytesPerPage);
        putBytes(writePages[i], data, 0, i * bytesPerPage, bytesPerPage);
        readPages[i] = createPage(bytesPerPage);
        zeroPages[i] = zeroPage;
        pageLengths[i] = bytesPerPage;
    }
    try {
        for (int i = 0; i < 10_000; i++) {
            adversary.setProbabilityFactor(0);
            swapper.write(0, zeroPages, pageLengths, pageCount, pageCount);
            adversary.setProbabilityFactor(1);
            swapper.write(0, writePages, pageLengths, pageCount, pageCount);
            for (long readPage : readPages) {
                clear(readPage);
            }
            adversary.setProbabilityFactor(0);
            assertThat(swapper.read(0, readPages, pageLengths, pageCount)).isEqualTo(bytesTotal);
            for (int j = 0; j < pageCount; j++) {
                assertThat(array(readPages[j])).containsExactly(array(writePages[j]));
            }
        }
    } finally {
        swapper.close();
    }
}
Also used : Path(java.nio.file.Path) PageSwapperFactory(org.neo4j.io.pagecache.PageSwapperFactory) PageSwapper(org.neo4j.io.pagecache.PageSwapper) AdversarialFileSystemAbstraction(org.neo4j.adversaries.fs.AdversarialFileSystemAbstraction) RandomAdversary(org.neo4j.adversaries.RandomAdversary) PageSwapperTest(org.neo4j.io.pagecache.PageSwapperTest) Test(org.junit.jupiter.api.Test)

Aggregations

PageSwapperFactory (org.neo4j.io.pagecache.PageSwapperFactory)27 Test (org.junit.jupiter.api.Test)23 PageSwapperTest (org.neo4j.io.pagecache.PageSwapperTest)22 DelegatingStoreChannel (org.neo4j.io.fs.DelegatingStoreChannel)13 StoreChannel (org.neo4j.io.fs.StoreChannel)13 PageSwapper (org.neo4j.io.pagecache.PageSwapper)12 Path (java.nio.file.Path)11 DisabledOnOs (org.junit.jupiter.api.condition.DisabledOnOs)6 RandomAdversary (org.neo4j.adversaries.RandomAdversary)5 AdversarialFileSystemAbstraction (org.neo4j.adversaries.fs.AdversarialFileSystemAbstraction)5 SingleFilePageSwapperFactory (org.neo4j.io.pagecache.impl.SingleFilePageSwapperFactory)4 InputStream (java.io.InputStream)3 FileLock (java.nio.channels.FileLock)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 IOException (java.io.IOException)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 FileSystemAbstraction (org.neo4j.io.fs.FileSystemAbstraction)2 StoreFileChannel (org.neo4j.io.fs.StoreFileChannel)2 PageCache (org.neo4j.io.pagecache.PageCache)2 PagedFile (org.neo4j.io.pagecache.PagedFile)2