Search in sources :

Example 6 with DelegatingFileSystemAbstraction

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

the class FileUserRepositoryTest method shouldRecoverIfCrashedDuringMove.

@Test
void shouldRecoverIfCrashedDuringMove() throws Throwable {
    // Given
    final IOException exception = new IOException("simulated IO Exception on create");
    FileSystemAbstraction crashingFileSystem = new DelegatingFileSystemAbstraction(fs) {

        @Override
        public void renameFile(Path oldLocation, Path newLocation, CopyOption... copyOptions) throws IOException {
            if (authFile.getFileName().equals(newLocation.getFileName())) {
                throw exception;
            }
            super.renameFile(oldLocation, newLocation, copyOptions);
        }
    };
    FileUserRepository users = new FileUserRepository(crashingFileSystem, authFile, logProvider);
    users.start();
    User user = new User.Builder("jake", LegacyCredential.INACCESSIBLE).withRequiredPasswordChange(true).build();
    // When
    var e = assertThrows(IOException.class, () -> users.create(user));
    assertSame(exception, e);
    // Then
    assertFalse(crashingFileSystem.fileExists(authFile));
    assertThat(crashingFileSystem.listFiles(authFile.getParent()).length).isEqualTo(0);
}
Also used : Path(java.nio.file.Path) DelegatingFileSystemAbstraction(org.neo4j.io.fs.DelegatingFileSystemAbstraction) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) CopyOption(java.nio.file.CopyOption) User(org.neo4j.kernel.impl.security.User) IOException(java.io.IOException) DelegatingFileSystemAbstraction(org.neo4j.io.fs.DelegatingFileSystemAbstraction) Test(org.junit.jupiter.api.Test)

Example 7 with DelegatingFileSystemAbstraction

use of org.neo4j.io.fs.DelegatingFileSystemAbstraction 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 8 with DelegatingFileSystemAbstraction

use of org.neo4j.io.fs.DelegatingFileSystemAbstraction 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 9 with DelegatingFileSystemAbstraction

use of org.neo4j.io.fs.DelegatingFileSystemAbstraction 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 10 with DelegatingFileSystemAbstraction

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

Aggregations

Path (java.nio.file.Path)14 DelegatingFileSystemAbstraction (org.neo4j.io.fs.DelegatingFileSystemAbstraction)14 FileSystemAbstraction (org.neo4j.io.fs.FileSystemAbstraction)13 IOException (java.io.IOException)12 DelegatingStoreChannel (org.neo4j.io.fs.DelegatingStoreChannel)10 Test (org.junit.jupiter.api.Test)9 ByteBuffer (java.nio.ByteBuffer)8 Set (java.util.Set)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 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 MethodSource (org.junit.jupiter.params.provider.MethodSource)4 StoreChannel (org.neo4j.io.fs.StoreChannel)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 LinkedList (java.util.LinkedList)2