Search in sources :

Example 1 with PagedFile

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

the class GBPTreeTest method setFormatVersion.

private void setFormatVersion(int pageSize, int formatVersion) throws IOException {
    try (PagedFile pagedFile = pageCache.map(indexFile, pageSize);
        PageCursor cursor = pagedFile.io(IdSpace.META_PAGE_ID, PagedFile.PF_SHARED_WRITE_LOCK)) {
        assertTrue(cursor.next());
        cursor.putInt(formatVersion);
    }
}
Also used : PagedFile(org.neo4j.io.pagecache.PagedFile) PageCursor(org.neo4j.io.pagecache.PageCursor)

Example 2 with PagedFile

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

the class MuninnPageCacheTest method mustFlushDirtyPagesOnEvictingAllPages.

@Test
public void mustFlushDirtyPagesOnEvictingAllPages() throws Exception {
    writeInitialDataTo(file("a"));
    RecordingPageCacheTracer tracer = new RecordingPageCacheTracer();
    RecordingPageCursorTracer cursorTracer = new RecordingPageCursorTracer(Fault.class);
    ConfigurablePageCursorTracerSupplier cursorTracerSupplier = new ConfigurablePageCursorTracerSupplier(cursorTracer);
    MuninnPageCache pageCache = createPageCache(fs, 4, 8, blockCacheFlush(tracer), cursorTracerSupplier);
    PagedFile pagedFile = pageCache.map(file("a"), 8);
    try (PageCursor cursor = pagedFile.io(0, PF_SHARED_WRITE_LOCK | PF_NO_GROW)) {
        assertTrue(cursor.next());
        cursor.putLong(0L);
        assertTrue(cursor.next());
        cursor.putLong(0L);
        assertFalse(cursor.next());
    }
    cursorTracer.reportEvents();
    assertNotNull(cursorTracer.observe(Fault.class));
    assertNotNull(cursorTracer.observe(Fault.class));
    assertEquals(2, cursorTracer.faults());
    assertEquals(2, tracer.faults());
    int clockArm = pageCache.evictPages(2, 0, tracer.beginPageEvictions(2));
    assertThat(clockArm, is(2));
    assertNotNull(tracer.observe(Evict.class));
    assertNotNull(tracer.observe(Evict.class));
    ByteBuffer buf = ByteBuffer.allocate(16);
    StoreChannel channel = fs.open(file("a"), "r");
    channel.read(buf);
    buf.flip();
    assertThat(buf.getLong(), is(0L));
    assertThat(buf.getLong(), is(0L));
}
Also used : PagedFile(org.neo4j.io.pagecache.PagedFile) StoreChannel(org.neo4j.io.fs.StoreChannel) DelegatingStoreChannel(org.neo4j.graphdb.mockfs.DelegatingStoreChannel) RecordingPageCacheTracer(org.neo4j.io.pagecache.tracing.recording.RecordingPageCacheTracer) Fault(org.neo4j.io.pagecache.tracing.recording.RecordingPageCursorTracer.Fault) Evict(org.neo4j.io.pagecache.tracing.recording.RecordingPageCacheTracer.Evict) RecordingPageCursorTracer(org.neo4j.io.pagecache.tracing.recording.RecordingPageCursorTracer) ByteBuffer(java.nio.ByteBuffer) ConfigurablePageCursorTracerSupplier(org.neo4j.io.pagecache.tracing.ConfigurablePageCursorTracerSupplier) PageCursor(org.neo4j.io.pagecache.PageCursor) PageCacheTest(org.neo4j.io.pagecache.PageCacheTest) Test(org.junit.Test)

Example 3 with PagedFile

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

the class MuninnPageCacheTest method closingTheCursorMustUnlockModifiedPage.

@Test
public void closingTheCursorMustUnlockModifiedPage() throws Exception {
    writeInitialDataTo(file("a"));
    final MuninnPageCache pageCache = createPageCache(fs, 2, 8, PageCacheTracer.NULL, DefaultPageCursorTracerSupplier.INSTANCE);
    final PagedFile pagedFile = pageCache.map(file("a"), 8);
    Future<?> task = executor.submit(() -> {
        try (PageCursor cursor = pagedFile.io(0, PF_SHARED_WRITE_LOCK)) {
            assertTrue(cursor.next());
            cursor.putLong(41);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    });
    task.get();
    try (PageCursor cursor = pagedFile.io(0, PF_SHARED_WRITE_LOCK)) {
        assertTrue(cursor.next());
        long value = cursor.getLong();
        cursor.setOffset(0);
        cursor.putLong(value + 1);
    }
    int clockArm = pageCache.evictPages(1, 0, EvictionRunEvent.NULL);
    assertThat(clockArm, is(1));
    ByteBuffer buf = ByteBuffer.allocate(16);
    StoreChannel channel = fs.open(file("a"), "r");
    channel.read(buf);
    buf.flip();
    assertThat(buf.getLong(), is(42L));
    assertThat(buf.getLong(), is(y));
}
Also used : PagedFile(org.neo4j.io.pagecache.PagedFile) StoreChannel(org.neo4j.io.fs.StoreChannel) DelegatingStoreChannel(org.neo4j.graphdb.mockfs.DelegatingStoreChannel) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) PageCursor(org.neo4j.io.pagecache.PageCursor) PageCacheTest(org.neo4j.io.pagecache.PageCacheTest) Test(org.junit.Test)

Example 4 with PagedFile

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

the class MuninnPageCacheTest method mustUnblockPageFaultersWhenEvictionGetsException.

@Test(timeout = SEMI_LONG_TIMEOUT_MILLIS)
public void mustUnblockPageFaultersWhenEvictionGetsException() throws Exception {
    writeInitialDataTo(file("a"));
    FileSystemAbstraction fs = new DelegatingFileSystemAbstraction(this.fs) {

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

                @Override
                public void writeAll(ByteBuffer src, long position) throws IOException {
                    throw new IOException("uh-oh...");
                }
            };
        }
    };
    MuninnPageCache pageCache = createPageCache(fs, 2, 8, PageCacheTracer.NULL, DefaultPageCursorTracerSupplier.INSTANCE);
    final PagedFile pagedFile = pageCache.map(file("a"), 8);
    // all writes.
    try (PageCursor cursor = pagedFile.io(0, PF_SHARED_WRITE_LOCK)) {
        for (int i = 0; i < 1000; i++) {
            assertTrue(cursor.next());
        }
        fail("Expected an exception at this point");
    } catch (IOException ignore) {
    // Good.
    }
}
Also used : DelegatingFileSystemAbstraction(org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) PagedFile(org.neo4j.io.pagecache.PagedFile) DelegatingStoreChannel(org.neo4j.graphdb.mockfs.DelegatingStoreChannel) IOException(java.io.IOException) DelegatingFileSystemAbstraction(org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction) PagedFile(org.neo4j.io.pagecache.PagedFile) File(java.io.File) ByteBuffer(java.nio.ByteBuffer) PageCursor(org.neo4j.io.pagecache.PageCursor) PageCacheTest(org.neo4j.io.pagecache.PageCacheTest) Test(org.junit.Test)

Example 5 with PagedFile

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

the class CommonAbstractStore method checkAndLoadStorage.

/**
     * This method is called by constructors. Checks the header record and loads the store.
     * <p>
     * Note: This method will map the file with the page cache. The store file must not
     * be accessed directly until it has been unmapped - the store file must only be
     * accessed through the page cache.
     * @param createIfNotExists If true, creates and initialises the store file if it does not exist already. If false,
     * this method will instead throw an exception in that situation.
     */
protected void checkAndLoadStorage(boolean createIfNotExists) {
    int pageSize = pageCache.pageSize();
    int filePageSize;
    try (PagedFile pagedFile = pageCache.map(storageFileName, pageSize, ANY_PAGE_SIZE)) {
        extractHeaderRecord(pagedFile);
        filePageSize = pageCache.pageSize() - pageCache.pageSize() % getRecordSize();
    } catch (NoSuchFileException | StoreNotFoundException e) {
        if (createIfNotExists) {
            try {
                createStore(pageSize);
                return;
            } catch (IOException e1) {
                e.addSuppressed(e1);
            }
        }
        if (e instanceof StoreNotFoundException) {
            throw (StoreNotFoundException) e;
        }
        throw new StoreNotFoundException("Store file not found: " + storageFileName, e);
    } catch (IOException e) {
        throw new UnderlyingStorageException("Unable to open store file: " + storageFileName, e);
    }
    loadStorage(filePageSize);
}
Also used : PagedFile(org.neo4j.io.pagecache.PagedFile) NoSuchFileException(java.nio.file.NoSuchFileException) IOException(java.io.IOException)

Aggregations

PagedFile (org.neo4j.io.pagecache.PagedFile)39 File (java.io.File)23 PageCursor (org.neo4j.io.pagecache.PageCursor)20 Test (org.junit.Test)16 IOException (java.io.IOException)13 PageCache (org.neo4j.io.pagecache.PageCache)10 ByteBuffer (java.nio.ByteBuffer)8 PageCacheTest (org.neo4j.io.pagecache.PageCacheTest)7 DelegatingStoreChannel (org.neo4j.graphdb.mockfs.DelegatingStoreChannel)5 FileSystemAbstraction (org.neo4j.io.fs.FileSystemAbstraction)5 StoreChannel (org.neo4j.io.fs.StoreChannel)5 NoSuchFileException (java.nio.file.NoSuchFileException)4 DelegatingPagedFile (org.neo4j.io.pagecache.DelegatingPagedFile)3 DelegatingPageCursor (org.neo4j.io.pagecache.impl.DelegatingPageCursor)3 ConfigurablePageCursorTracerSupplier (org.neo4j.io.pagecache.tracing.ConfigurablePageCursorTracerSupplier)3 RecordingPageCacheTracer (org.neo4j.io.pagecache.tracing.recording.RecordingPageCacheTracer)3 Evict (org.neo4j.io.pagecache.tracing.recording.RecordingPageCacheTracer.Evict)3 RecordingPageCursorTracer (org.neo4j.io.pagecache.tracing.recording.RecordingPageCursorTracer)3 Fault (org.neo4j.io.pagecache.tracing.recording.RecordingPageCursorTracer.Fault)3 TransactionId (org.neo4j.kernel.impl.store.TransactionId)3