Search in sources :

Example 6 with DelegatingStoreChannel

use of org.neo4j.io.fs.DelegatingStoreChannel in project neo4j by neo4j.

the class MuninnPageCacheTest method mustUnblockPageFaultersWhenEvictionGetsException.

@Test
void mustUnblockPageFaultersWhenEvictionGetsException() {
    assertTimeoutPreemptively(ofMillis(SEMI_LONG_TIMEOUT_MILLIS), () -> {
        writeInitialDataTo(file("a"));
        MutableBoolean throwException = new MutableBoolean(true);
        FileSystemAbstraction fs = new DelegatingFileSystemAbstraction(this.fs) {

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

                    @Override
                    public void writeAll(ByteBuffer src, long position) throws IOException {
                        if (throwException.booleanValue()) {
                            throw new IOException("uh-oh...");
                        } else {
                            super.writeAll(src, position);
                        }
                    }
                };
            }
        };
        try (MuninnPageCache pageCache = createPageCache(fs, 2, PageCacheTracer.NULL);
            PagedFile pagedFile = map(pageCache, file("a"), 8)) {
            // exceptions on all writes.
            try (PageCursor cursor = pagedFile.io(0, PF_SHARED_WRITE_LOCK, NULL)) {
                for (int i = 0; i < 1000; i++) {
                    assertTrue(cursor.next());
                }
                fail("Expected an exception at this point");
            } catch (IOException ignore) {
            // Good.
            }
            throwException.setFalse();
        }
    });
}
Also used : Path(java.nio.file.Path) DelegatingFileSystemAbstraction(org.neo4j.io.fs.DelegatingFileSystemAbstraction) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) Set(java.util.Set) PagedFile(org.neo4j.io.pagecache.PagedFile) MutableBoolean(org.apache.commons.lang3.mutable.MutableBoolean) DelegatingStoreChannel(org.neo4j.io.fs.DelegatingStoreChannel) IOException(java.io.IOException) DelegatingFileSystemAbstraction(org.neo4j.io.fs.DelegatingFileSystemAbstraction) ByteBuffer(java.nio.ByteBuffer) PageCursor(org.neo4j.io.pagecache.PageCursor) PageCacheTest(org.neo4j.io.pagecache.PageCacheTest) Test(org.junit.jupiter.api.Test)

Example 7 with DelegatingStoreChannel

use of org.neo4j.io.fs.DelegatingStoreChannel in project neo4j by neo4j.

the class PageCacheTest method flushingDuringPagedFileCloseMustRetryUntilItSucceeds.

@RepeatedTest(100)
void flushingDuringPagedFileCloseMustRetryUntilItSucceeds() {
    assertTimeoutPreemptively(ofMillis(SHORT_TIMEOUT_MILLIS), () -> {
        FileSystemAbstraction fs = new DelegatingFileSystemAbstraction(this.fs) {

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

                    private int writeCount;

                    @Override
                    public void writeAll(ByteBuffer src, long position) throws IOException {
                        if (writeCount++ < 10) {
                            throw new IOException("This is a benign exception that we expect to be thrown " + "during a flush of a PagedFile.");
                        }
                        super.writeAll(src, position);
                    }
                };
            }
        };
        getPageCache(fs, maxPages, PageCacheTracer.NULL);
        PrintStream oldSystemErr = System.err;
        try (PagedFile pf = map(file("a"), filePageSize);
            PageCursor cursor = pf.io(0, PF_SHARED_WRITE_LOCK, NULL)) {
            assertTrue(cursor.next());
            writeRecords(cursor);
            // Silence any stack traces the failed flushes might print.
            System.setErr(new PrintStream(new ByteArrayOutputStream()));
        } finally {
            System.setErr(oldSystemErr);
        }
        verifyRecordsInFile(file("a"), recordsPerFilePage);
    });
}
Also used : Path(java.nio.file.Path) PrintStream(java.io.PrintStream) AdversarialFileSystemAbstraction(org.neo4j.adversaries.fs.AdversarialFileSystemAbstraction) DelegatingFileSystemAbstraction(org.neo4j.io.fs.DelegatingFileSystemAbstraction) EphemeralFileSystemAbstraction(org.neo4j.io.fs.EphemeralFileSystemAbstraction) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) SwapperSet(org.neo4j.io.pagecache.impl.muninn.SwapperSet) Set(java.util.Set) ImmutableSet(org.eclipse.collections.api.set.ImmutableSet) DelegatingStoreChannel(org.neo4j.io.fs.DelegatingStoreChannel) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DelegatingFileSystemAbstraction(org.neo4j.io.fs.DelegatingFileSystemAbstraction) ByteBuffer(java.nio.ByteBuffer) RepeatedTest(org.junit.jupiter.api.RepeatedTest)

Example 8 with DelegatingStoreChannel

use of org.neo4j.io.fs.DelegatingStoreChannel in project neo4j by neo4j.

the class MuninnPageCacheTest method pageCacheFlushAndForceMustClearBackgroundEvictionException.

@Test
void pageCacheFlushAndForceMustClearBackgroundEvictionException() {
    assertTimeoutPreemptively(ofMillis(SEMI_LONG_TIMEOUT_MILLIS), () -> {
        MutableBoolean throwException = new MutableBoolean(true);
        FileSystemAbstraction fs = new DelegatingFileSystemAbstraction(this.fs) {

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

                    @Override
                    public void writeAll(ByteBuffer src, long position) throws IOException {
                        if (throwException.booleanValue()) {
                            throw new IOException("uh-oh...");
                        } else {
                            super.writeAll(src, position);
                        }
                    }
                };
            }
        };
        try (MuninnPageCache pageCache = createPageCache(fs, 2, PageCacheTracer.NULL);
            PagedFile pagedFile = map(pageCache, file("a"), 8)) {
            try (PageCursor cursor = pagedFile.io(0, PF_SHARED_WRITE_LOCK, NULL)) {
                // Page 0 is now dirty, but flushing it will throw an exception.
                assertTrue(cursor.next());
            }
            // This will run into that exception, in background eviction:
            pageCache.evictPages(1, 0, EvictionRunEvent.NULL);
            // We now have a background eviction exception. A successful flushAndForce should clear it, though.
            throwException.setFalse();
            pageCache.flushAndForce();
            // And with a cleared exception, we should be able to work with the page cache without worry.
            try (PageCursor cursor = pagedFile.io(0, PF_SHARED_WRITE_LOCK, NULL)) {
                for (int i = 0; i < maxPages * 20; i++) {
                    assertTrue(cursor.next());
                }
            }
        }
    });
}
Also used : Path(java.nio.file.Path) DelegatingFileSystemAbstraction(org.neo4j.io.fs.DelegatingFileSystemAbstraction) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) Set(java.util.Set) PagedFile(org.neo4j.io.pagecache.PagedFile) MutableBoolean(org.apache.commons.lang3.mutable.MutableBoolean) DelegatingStoreChannel(org.neo4j.io.fs.DelegatingStoreChannel) IOException(java.io.IOException) DelegatingFileSystemAbstraction(org.neo4j.io.fs.DelegatingFileSystemAbstraction) ByteBuffer(java.nio.ByteBuffer) PageCursor(org.neo4j.io.pagecache.PageCursor) PageCacheTest(org.neo4j.io.pagecache.PageCacheTest) Test(org.junit.jupiter.api.Test)

Example 9 with DelegatingStoreChannel

use of org.neo4j.io.fs.DelegatingStoreChannel in project neo4j by neo4j.

the class FileLockerTest method shouldNotObtainLockWhenAlreadyInUse.

@ParameterizedTest
@MethodSource("lockerFactories")
void shouldNotObtainLockWhenAlreadyInUse(LockerFactory lockerFactory) {
    FileSystemAbstraction fileSystemAbstraction = new DelegatingFileSystemAbstraction(fileSystem) {

        @Override
        public boolean fileExists(Path file) {
            return false;
        }

        @Override
        public StoreChannel write(Path fileName) throws IOException {
            return new DelegatingStoreChannel(super.write(fileName)) {

                @Override
                public FileLock tryLock() {
                    // 'null' implies that the file has been externally locked
                    return null;
                }
            };
        }
    };
    FileLockException fileLockException = assertThrows(FileLockException.class, () -> {
        try (Locker storeLocker = lockerFactory.createLocker(fileSystemAbstraction, testDirectory)) {
            storeLocker.checkLock();
        }
    });
    assertThat(fileLockException.getMessage()).contains("Lock file has been locked by another process");
}
Also used : Path(java.nio.file.Path) DelegatingFileSystemAbstraction(org.neo4j.io.fs.DelegatingFileSystemAbstraction) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) DelegatingStoreChannel(org.neo4j.io.fs.DelegatingStoreChannel) DelegatingFileSystemAbstraction(org.neo4j.io.fs.DelegatingFileSystemAbstraction) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 10 with DelegatingStoreChannel

use of org.neo4j.io.fs.DelegatingStoreChannel 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)

Aggregations

Path (java.nio.file.Path)10 DelegatingFileSystemAbstraction (org.neo4j.io.fs.DelegatingFileSystemAbstraction)10 DelegatingStoreChannel (org.neo4j.io.fs.DelegatingStoreChannel)10 IOException (java.io.IOException)9 FileSystemAbstraction (org.neo4j.io.fs.FileSystemAbstraction)9 ByteBuffer (java.nio.ByteBuffer)8 Set (java.util.Set)8 Test (org.junit.jupiter.api.Test)8 UncheckedIOException (java.io.UncheckedIOException)6 ImmutableSet (org.eclipse.collections.api.set.ImmutableSet)6 RepeatedTest (org.junit.jupiter.api.RepeatedTest)6 AdversarialFileSystemAbstraction (org.neo4j.adversaries.fs.AdversarialFileSystemAbstraction)6 EphemeralFileSystemAbstraction (org.neo4j.io.fs.EphemeralFileSystemAbstraction)6 SwapperSet (org.neo4j.io.pagecache.impl.muninn.SwapperSet)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 StoreChannel (org.neo4j.io.fs.StoreChannel)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 LinkedList (java.util.LinkedList)2 List (java.util.List)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2