Search in sources :

Example 6 with DelegatingStoreChannel

use of org.neo4j.graphdb.mockfs.DelegatingStoreChannel in project neo4j by neo4j.

the class NodeStoreTest method shouldCloseStoreFileOnFailureToOpen.

@Test
public void shouldCloseStoreFileOnFailureToOpen() throws Exception {
    // GIVEN
    final AtomicBoolean fired = new AtomicBoolean();
    FileSystemAbstraction fs = new DelegatingFileSystemAbstraction(efs.get()) {

        @Override
        public StoreChannel open(File fileName, String mode) throws IOException {
            return new DelegatingStoreChannel(super.open(fileName, mode)) {

                @Override
                public int read(ByteBuffer dst) throws IOException {
                    Exception stack = new Exception();
                    if (containsStackTraceElement(stack, item -> item.getMethodName().equals("initGenerator")) && !containsStackTraceElement(stack, item -> item.getMethodName().equals("createNodeStore"))) {
                        fired.set(true);
                        throw new IOException("Proving a point here");
                    }
                    return super.read(dst);
                }
            };
        }
    };
    // WHEN
    try (PageCache pageCache = pageCacheRule.getPageCache(fs)) {
        newNodeStore(fs);
        fail("Should fail");
    }// Close the page cache here so that we can see failure to close (due to still mapped files)
     catch (Exception e) {
        // THEN
        assertTrue(contains(e, IOException.class));
        assertTrue(fired.get());
    }
}
Also used : IdGeneratorFactory(org.neo4j.kernel.impl.store.id.IdGeneratorFactory) EphemeralFileSystemRule(org.neo4j.test.rule.fs.EphemeralFileSystemRule) Matchers.not(org.hamcrest.Matchers.not) NORMAL(org.neo4j.kernel.impl.store.record.RecordLoad.NORMAL) NO_NEXT_RELATIONSHIP(org.neo4j.kernel.impl.store.record.Record.NO_NEXT_RELATIONSHIP) NullLogProvider(org.neo4j.logging.NullLogProvider) ByteBuffer(java.nio.ByteBuffer) Assert.assertThat(org.junit.Assert.assertThat) StoreChannel(org.neo4j.io.fs.StoreChannel) Arrays.asList(java.util.Arrays.asList) After(org.junit.After) Assert.fail(org.junit.Assert.fail) ClassRule(org.junit.ClassRule) PageCache(org.neo4j.io.pagecache.PageCache) DefaultIdGeneratorFactory(org.neo4j.kernel.impl.store.id.DefaultIdGeneratorFactory) Predicate(java.util.function.Predicate) Collection(java.util.Collection) DelegatingStoreChannel(org.neo4j.graphdb.mockfs.DelegatingStoreChannel) DynamicRecord(org.neo4j.kernel.impl.store.record.DynamicRecord) IdType(org.neo4j.kernel.impl.store.id.IdType) Assert.assertFalse(org.junit.Assert.assertFalse) PrimitiveLongSet(org.neo4j.collection.primitive.PrimitiveLongSet) Mockito.mock(org.mockito.Mockito.mock) Primitive(org.neo4j.collection.primitive.Primitive) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Mockito.spy(org.mockito.Mockito.spy) ArrayList(java.util.ArrayList) PageCacheRule(org.neo4j.test.rule.PageCacheRule) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) NO_NEXT_PROPERTY(org.neo4j.kernel.impl.store.record.Record.NO_NEXT_PROPERTY) DynamicArrayStore.allocateFromNumbers(org.neo4j.kernel.impl.store.DynamicArrayStore.allocateFromNumbers) NodeStore.readOwnerFromDynamicLabelsRecord(org.neo4j.kernel.impl.store.NodeStore.readOwnerFromDynamicLabelsRecord) DelegatingFileSystemAbstraction(org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction) Config(org.neo4j.kernel.configuration.Config) Matchers.empty(org.hamcrest.Matchers.empty) LongStream(java.util.stream.LongStream) ReusableRecordsAllocator(org.neo4j.kernel.impl.store.allocator.ReusableRecordsAllocator) IdGenerator(org.neo4j.kernel.impl.store.id.IdGenerator) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) File(java.io.File) Mockito.verify(org.mockito.Mockito.verify) EphemeralFileSystemAbstraction(org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction) Exceptions.contains(org.neo4j.helpers.Exceptions.contains) Rule(org.junit.Rule) Iterables(org.neo4j.helpers.collection.Iterables) NodeRecord(org.neo4j.kernel.impl.store.record.NodeRecord) Visitor(org.neo4j.helpers.collection.Visitor) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DelegatingFileSystemAbstraction(org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction) EphemeralFileSystemAbstraction(org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) DelegatingStoreChannel(org.neo4j.graphdb.mockfs.DelegatingStoreChannel) IOException(java.io.IOException) DelegatingFileSystemAbstraction(org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction) File(java.io.File) ByteBuffer(java.nio.ByteBuffer) IOException(java.io.IOException) PageCache(org.neo4j.io.pagecache.PageCache) Test(org.junit.Test)

Example 7 with DelegatingStoreChannel

use of org.neo4j.graphdb.mockfs.DelegatingStoreChannel in project neo4j by neo4j.

the class PageCacheTest method flushingDuringPagedFileCloseMustRetryUntilItSucceeds.

@RepeatRule.Repeat(times = 100)
@Test(timeout = SHORT_TIMEOUT_MILLIS)
public void flushingDuringPagedFileCloseMustRetryUntilItSucceeds() throws IOException {
    FileSystemAbstraction fs = new DelegatingFileSystemAbstraction(this.fs) {

        @Override
        public StoreChannel open(File fileName, String mode) throws IOException {
            return new DelegatingStoreChannel(super.open(fileName, mode)) {

                private int writeCount = 0;

                @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, pageCachePageSize, PageCacheTracer.NULL, PageCursorTracerSupplier.NULL);
    PrintStream oldSystemErr = System.err;
    try (PagedFile pf = pageCache.map(file("a"), filePageSize);
        PageCursor cursor = pf.io(0, PF_SHARED_WRITE_LOCK)) {
        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 : PrintStream(java.io.PrintStream) DelegatingFileSystemAbstraction(org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction) EphemeralFileSystemAbstraction(org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) AdversarialPagedFile(org.neo4j.adversaries.pagecache.AdversarialPagedFile) DelegatingStoreChannel(org.neo4j.graphdb.mockfs.DelegatingStoreChannel) Long.toHexString(java.lang.Long.toHexString) Matchers.containsString(org.hamcrest.Matchers.containsString) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DelegatingFileSystemAbstraction(org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction) AdversarialPagedFile(org.neo4j.adversaries.pagecache.AdversarialPagedFile) File(java.io.File) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 8 with DelegatingStoreChannel

use of org.neo4j.graphdb.mockfs.DelegatingStoreChannel in project neo4j by neo4j.

the class SingleFilePageSwapperTest method mustCloseFilesIfTakingFileLockThrows.

@Test
public void mustCloseFilesIfTakingFileLockThrows() throws Exception {
    // no file locking on Windows.
    assumeFalse("No file locking on Windows", SystemUtils.IS_OS_WINDOWS);
    final AtomicInteger openFilesCounter = new AtomicInteger();
    PageSwapperFactory factory = createSwapperFactory();
    factory.setFileSystemAbstraction(new DelegatingFileSystemAbstraction(fileSystem) {

        @Override
        public StoreChannel open(File fileName, String mode) throws IOException {
            openFilesCounter.getAndIncrement();
            return new DelegatingStoreChannel(super.open(fileName, mode)) {

                @Override
                public void close() throws IOException {
                    openFilesCounter.getAndDecrement();
                    super.close();
                }
            };
        }
    });
    File file = testDir.file("file");
    try (StoreChannel ch = fileSystem.create(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(), is(0));
}
Also used : PageSwapperFactory(org.neo4j.io.pagecache.PageSwapperFactory) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StoreChannel(org.neo4j.io.fs.StoreChannel) DelegatingStoreChannel(org.neo4j.graphdb.mockfs.DelegatingStoreChannel) FileLock(java.nio.channels.FileLock) DelegatingStoreChannel(org.neo4j.graphdb.mockfs.DelegatingStoreChannel) IOException(java.io.IOException) DelegatingFileSystemAbstraction(org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction) File(java.io.File) PageSwapperTest(org.neo4j.io.pagecache.PageSwapperTest) Test(org.junit.Test)

Example 9 with DelegatingStoreChannel

use of org.neo4j.graphdb.mockfs.DelegatingStoreChannel in project neo4j by neo4j.

the class StoreLockerTest method shouldNotObtainLockWhenStoreAlreadyInUse.

@Test
public void shouldNotObtainLockWhenStoreAlreadyInUse() throws Exception {
    FileSystemAbstraction fileSystemAbstraction = new DelegatingFileSystemAbstraction(fileSystemRule.get()) {

        @Override
        public boolean fileExists(File fileName) {
            return false;
        }

        @Override
        public StoreChannel open(File fileName, String mode) throws IOException {
            return new DelegatingStoreChannel(super.open(fileName, mode)) {

                @Override
                public FileLock tryLock() throws IOException {
                    // 'null' implies that the file has been externally locked
                    return null;
                }
            };
        }
    };
    try (StoreLocker storeLocker = new StoreLocker(fileSystemAbstraction)) {
        storeLocker.checkLock(target.directory("unused"));
        fail();
    } catch (StoreLockException e) {
        assertThat(e.getMessage(), containsString("Store and its lock file has been locked by another process"));
    }
}
Also used : DelegatingFileSystemAbstraction(org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) StoreLockException(org.neo4j.kernel.StoreLockException) DelegatingStoreChannel(org.neo4j.graphdb.mockfs.DelegatingStoreChannel) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) DelegatingFileSystemAbstraction(org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction) File(java.io.File) Test(org.junit.Test)

Aggregations

File (java.io.File)9 Test (org.junit.Test)9 DelegatingFileSystemAbstraction (org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction)9 DelegatingStoreChannel (org.neo4j.graphdb.mockfs.DelegatingStoreChannel)9 IOException (java.io.IOException)8 FileSystemAbstraction (org.neo4j.io.fs.FileSystemAbstraction)8 ByteBuffer (java.nio.ByteBuffer)7 EphemeralFileSystemAbstraction (org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction)6 Long.toHexString (java.lang.Long.toHexString)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 AdversarialPagedFile (org.neo4j.adversaries.pagecache.AdversarialPagedFile)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 StoreChannel (org.neo4j.io.fs.StoreChannel)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 PrintStream (java.io.PrintStream)1 FileLock (java.nio.channels.FileLock)1 OverlappingFileLockException (java.nio.channels.OverlappingFileLockException)1 ArrayList (java.util.ArrayList)1 Arrays.asList (java.util.Arrays.asList)1