Search in sources :

Example 6 with PageSwapperFactory

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

the class SingleFilePageSwapperTest method creatingSwapperForExternallyLockedFileMustThrow.

@Test
@DisabledOnOs(OS.WINDOWS)
void creatingSwapperForExternallyLockedFileMustThrow() throws Exception {
    PageSwapperFactory factory = createSwapperFactory(fileSystem);
    Path file = testDir.file("file");
    ((StoreChannel) fileSystem.write(file)).close();
    var process = start(pb -> pb.directory(Path.of("target/test-classes").toAbsolutePath().toFile()), LockThisFileProgram.class.getCanonicalName(), file.toAbsolutePath().toString());
    BufferedReader stdout = new BufferedReader(new InputStreamReader(process.getInputStream()));
    InputStream stderr = process.getErrorStream();
    try {
        assumeTrue(LockThisFileProgram.LOCKED_OUTPUT.equals(stdout.readLine()));
    } catch (Throwable e) {
        int b = stderr.read();
        while (b != -1) {
            System.err.write(b);
            b = stderr.read();
        }
        System.err.flush();
        int exitCode = process.waitFor();
        System.out.println("exitCode = " + exitCode);
        throw e;
    }
    try {
        assertThrows(FileLockException.class, () -> createSwapper(factory, file, 4, NO_CALLBACK, true));
    } finally {
        process.getOutputStream().write(0);
        process.getOutputStream().flush();
        process.waitFor();
    }
}
Also used : Path(java.nio.file.Path) PageSwapperFactory(org.neo4j.io.pagecache.PageSwapperFactory) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) StoreChannel(org.neo4j.io.fs.StoreChannel) DelegatingStoreChannel(org.neo4j.io.fs.DelegatingStoreChannel) BufferedReader(java.io.BufferedReader) DisabledOnOs(org.junit.jupiter.api.condition.DisabledOnOs) PageSwapperTest(org.neo4j.io.pagecache.PageSwapperTest) Test(org.junit.jupiter.api.Test)

Example 7 with PageSwapperFactory

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

the class SingleFilePageSwapperTest method swappingOutMustWritePageToFile.

@Test
void swappingOutMustWritePageToFile() throws Exception {
    getFs().write(getPath()).close();
    byte[] expected = new byte[] { 1, 2, 3, 4 };
    long page = createPage(expected);
    PageSwapperFactory factory = createSwapperFactory(getFs());
    PageSwapper swapper = createSwapper(factory, getPath(), 4, null, false);
    assertEquals(4, swapper.write(0, page));
    try (InputStream stream = getFs().openAsInputStream(getPath())) {
        byte[] actual = new byte[expected.length];
        assertThat(stream.read(actual)).isEqualTo(actual.length);
        assertThat(actual).containsExactly(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.jupiter.api.Test)

Example 8 with PageSwapperFactory

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

the class SingleFilePageSwapperTest method creatingSwapperForInternallyLockedFileMustThrow.

@Test
@DisabledOnOs(OS.WINDOWS)
void creatingSwapperForInternallyLockedFileMustThrow() throws Exception {
    PageSwapperFactory factory = createSwapperFactory(fileSystem);
    Path file = testDir.file("file");
    StoreFileChannel channel = fileSystem.write(file);
    try (FileLock fileLock = channel.tryLock()) {
        assertThat(fileLock).isNotNull();
        assertThrows(FileLockException.class, () -> createSwapper(factory, file, 4, NO_CALLBACK, true));
    }
}
Also used : Path(java.nio.file.Path) PageSwapperFactory(org.neo4j.io.pagecache.PageSwapperFactory) StoreFileChannel(org.neo4j.io.fs.StoreFileChannel) FileLock(java.nio.channels.FileLock) DisabledOnOs(org.junit.jupiter.api.condition.DisabledOnOs) PageSwapperTest(org.neo4j.io.pagecache.PageSwapperTest) Test(org.junit.jupiter.api.Test)

Example 9 with PageSwapperFactory

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

the class SingleFilePageSwapperTest method uninterruptibleRead.

@Test
void uninterruptibleRead() throws Exception {
    byte[] pageContent = new byte[] { 1, 2, 3, 4 };
    StoreChannel channel = getFs().write(getPath());
    channel.writeAll(wrap(pageContent));
    channel.close();
    PageSwapperFactory factory = createSwapperFactory(getFs());
    PageSwapper swapper = createSwapper(factory, getPath(), 4, null, false);
    long target = createPage(4);
    CountDownLatch startInterruptsLatch = new CountDownLatch(1);
    AtomicBoolean readFlag = new AtomicBoolean(true);
    var uninterruptibleFuture = operationExecutor.submit(() -> {
        startInterruptsLatch.countDown();
        while (readFlag.get()) {
            try {
                swapper.read(0, target);
            } catch (Throwable t) {
                throw new RuntimeException(t);
            }
        }
    });
    startInterruptsLatch.await();
    assertFalse(threadRegistryFactory.getThreads().isEmpty());
    for (int i = 0; i < INTERRUPT_ATTEMPTS; i++) {
        threadRegistryFactory.getThreads().forEach(Thread::interrupt);
        parkNanos(MILLISECONDS.toNanos(10));
    }
    readFlag.set(false);
    assertDoesNotThrow((ThrowingSupplier<?>) uninterruptibleFuture::get);
}
Also used : PageSwapperFactory(org.neo4j.io.pagecache.PageSwapperFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) StoreChannel(org.neo4j.io.fs.StoreChannel) DelegatingStoreChannel(org.neo4j.io.fs.DelegatingStoreChannel) PageSwapper(org.neo4j.io.pagecache.PageSwapper) CountDownLatch(java.util.concurrent.CountDownLatch) PageSwapperTest(org.neo4j.io.pagecache.PageSwapperTest) Test(org.junit.jupiter.api.Test)

Example 10 with PageSwapperFactory

use of org.neo4j.io.pagecache.PageSwapperFactory 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
@DisabledOnOs(OS.WINDOWS)
void creatingSwapperForFileMustTakeLockOnFile() throws Exception {
    PageSwapperFactory factory = createSwapperFactory(fileSystem);
    Path file = testDir.file("file");
    ((StoreChannel) fileSystem.write(file)).close();
    PageSwapper pageSwapper = createSwapper(factory, file, 4, NO_CALLBACK, false);
    try {
        StoreChannel channel = fileSystem.write(file);
        assertThrows(OverlappingFileLockException.class, channel::tryLock);
    } finally {
        pageSwapper.close();
    }
}
Also used : Path(java.nio.file.Path) PageSwapperFactory(org.neo4j.io.pagecache.PageSwapperFactory) StoreChannel(org.neo4j.io.fs.StoreChannel) DelegatingStoreChannel(org.neo4j.io.fs.DelegatingStoreChannel) PageSwapper(org.neo4j.io.pagecache.PageSwapper) DisabledOnOs(org.junit.jupiter.api.condition.DisabledOnOs) 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