Search in sources :

Example 86 with PageCursor

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

the class FreeListIdProvider method acquireNewIdFromFreelistOrEnd.

private long acquireNewIdFromFreelistOrEnd(long stableGeneration, long unstableGeneration, boolean allowTakeLastFromPage) throws IOException {
    if ((readPageId != writePageId || readPos < writePos) && (allowTakeLastFromPage || readPos < freelistNode.maxEntries() - 1)) {
        // or the read pos is < write pos so check if we can grab the next id (generation could still mismatch).
        try (PageCursor cursor = pagedFile.io(readPageId, PagedFile.PF_SHARED_READ_LOCK)) {
            if (!cursor.next()) {
                throw new IOException("Couldn't go to free-list read page " + readPageId);
            }
            long resultPageId;
            do {
                resultPageId = freelistNode.read(cursor, stableGeneration, readPos);
            } while (cursor.shouldRetry());
            if (resultPageId != FreelistNode.NO_PAGE_ID) {
                // FreelistNode compares generation and so this means that we have an available
                // id in the free list which we can acquire from a stable generation. Increment readPos
                readPos++;
                if (readPos >= freelistNode.maxEntries()) {
                    // The current reader page is exhausted, go to the next free-list page.
                    readPos = 0;
                    do {
                        readPageId = FreelistNode.next(cursor);
                    } while (cursor.shouldRetry());
                    // Put the exhausted free-list page id itself on the free-list
                    long exhaustedFreelistPageId = cursor.getCurrentPageId();
                    releaseId(stableGeneration, unstableGeneration, exhaustedFreelistPageId);
                    monitor.releasedFreelistPageId(exhaustedFreelistPageId);
                }
                return resultPageId;
            }
        }
    }
    // Fall-back to acquiring at the end of the file
    return nextLastId();
}
Also used : IOException(java.io.IOException) PageCursor(org.neo4j.io.pagecache.PageCursor)

Example 87 with PageCursor

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

the class FreeListIdProvider method acquireNewId.

private long acquireNewId(long stableGeneration, long unstableGeneration, boolean allowTakeLastFromPage) throws IOException {
    // Acquire id from free-list or end of store file
    long acquiredId = acquireNewIdFromFreelistOrEnd(stableGeneration, unstableGeneration, allowTakeLastFromPage);
    // Zap the page, i.e. set all bytes to zero
    try (PageCursor cursor = pagedFile.io(acquiredId, PagedFile.PF_SHARED_WRITE_LOCK)) {
        PageCursorUtil.goTo(cursor, "newly allocated free-list page", acquiredId);
        cursor.zapPage();
    // don't initialize node here since this acquisition can be used both for tree nodes
    // as well as free-list nodes.
    }
    return acquiredId;
}
Also used : PageCursor(org.neo4j.io.pagecache.PageCursor)

Example 88 with PageCursor

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

the class GBPTree method writerHeader.

private static void writerHeader(PagedFile pagedFile, Header.Writer headerWriter, TreeState otherState, PageCursor cursor) throws IOException {
    // Write/carry over header
    int headerOffset = cursor.getOffset();
    // will contain length of written header data (below)
    int headerDataOffset = headerOffset + Integer.BYTES;
    if (otherState.isValid()) {
        PageCursor previousCursor = pagedFile.io(otherState.pageId(), PagedFile.PF_SHARED_READ_LOCK);
        PageCursorUtil.goTo(previousCursor, "previous state page", otherState.pageId());
        do {
            // Place the previous state cursor after state data
            TreeState.read(previousCursor);
            // Read length of previous header
            int previousLength = previousCursor.getInt();
            // Reserve space to store length
            cursor.setOffset(headerDataOffset);
            // Write
            headerWriter.write(previousCursor, previousLength, cursor);
        } while (previousCursor.shouldRetry());
        checkOutOfBounds(previousCursor);
        checkOutOfBounds(cursor);
        int length = cursor.getOffset() - headerDataOffset;
        cursor.putInt(headerOffset, length);
    }
}
Also used : PageCursor(org.neo4j.io.pagecache.PageCursor)

Example 89 with PageCursor

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

the class GBPTree method readMeta.

private void readMeta(File indexFile, Layout<KEY, VALUE> layout, PagedFile pagedFile) throws IOException {
    // Read meta
    int formatVersion;
    long layoutIdentifier;
    int majorVersion;
    int minorVersion;
    try (PageCursor metaCursor = openMetaPageCursor(pagedFile, PagedFile.PF_SHARED_READ_LOCK)) {
        do {
            formatVersion = metaCursor.getInt();
            pageSize = metaCursor.getInt();
            layoutIdentifier = metaCursor.getLong();
            majorVersion = metaCursor.getInt();
            minorVersion = metaCursor.getInt();
            layout.readMetaData(metaCursor);
        } while (metaCursor.shouldRetry());
        checkOutOfBounds(metaCursor);
        metaCursor.checkAndClearCursorException();
    } catch (CursorException e) {
        throw new MetadataMismatchException(format("Tried to open %s, but caught an error while reading meta data. " + "File is expected to be corrupt, try to rebuild.", indexFile), e);
    }
    if (formatVersion != FORMAT_VERSION) {
        throw new MetadataMismatchException("Tried to open %s with a different format version than " + "what it was created with. Created with:%d, opened with %d", indexFile, formatVersion, FORMAT_VERSION);
    }
    if (layoutIdentifier != layout.identifier()) {
        throw new MetadataMismatchException("Tried to open " + indexFile + " using different layout identifier " + "than what it was created with. Created with:" + layoutIdentifier + ", opened with " + layout.identifier());
    }
    if (majorVersion != layout.majorVersion() || minorVersion != layout.minorVersion()) {
        throw new MetadataMismatchException("Tried to open " + indexFile + " using different layout version " + "than what it was created with. Created with:" + majorVersion + "." + minorVersion + ", opened with " + layout.majorVersion() + "." + layout.minorVersion());
    }
}
Also used : CursorException(org.neo4j.io.pagecache.CursorException) PageCursor(org.neo4j.io.pagecache.PageCursor)

Example 90 with PageCursor

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

the class MuninnPageCacheTest method mustFlushDirtyPagesOnEvictingFirstPage.

@Test
public void mustFlushDirtyPagesOnEvictingFirstPage() throws Exception {
    writeInitialDataTo(file("a"));
    RecordingPageCacheTracer tracer = new RecordingPageCacheTracer();
    RecordingPageCursorTracer cursorTracer = new RecordingPageCursorTracer();
    ConfigurablePageCursorTracerSupplier cursorTracerSupplier = new ConfigurablePageCursorTracerSupplier(cursorTracer);
    MuninnPageCache pageCache = createPageCache(fs, 2, 8, blockCacheFlush(tracer), cursorTracerSupplier);
    PagedFile pagedFile = pageCache.map(file("a"), 8);
    try (PageCursor cursor = pagedFile.io(0, PF_SHARED_WRITE_LOCK)) {
        assertTrue(cursor.next());
        cursor.putLong(0L);
    }
    cursorTracer.reportEvents();
    assertNotNull(cursorTracer.observe(Fault.class));
    assertEquals(1, cursorTracer.faults());
    assertEquals(1, tracer.faults());
    int clockArm = pageCache.evictPages(1, 0, tracer.beginPageEvictions(1));
    assertThat(clockArm, is(1));
    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(y));
}
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)

Aggregations

PageCursor (org.neo4j.io.pagecache.PageCursor)184 Test (org.junit.Test)124 StubPageCursor (org.neo4j.io.pagecache.StubPageCursor)106 PagedFile (org.neo4j.io.pagecache.PagedFile)19 IOException (java.io.IOException)12 File (java.io.File)8 CursorException (org.neo4j.io.pagecache.CursorException)6 PageCacheTest (org.neo4j.io.pagecache.PageCacheTest)6 CompositePageCursor (org.neo4j.io.pagecache.impl.CompositePageCursor)6 DelegatingPageCursor (org.neo4j.io.pagecache.impl.DelegatingPageCursor)6 ByteBuffer (java.nio.ByteBuffer)5 DelegatingStoreChannel (org.neo4j.graphdb.mockfs.DelegatingStoreChannel)5 StoreChannel (org.neo4j.io.fs.StoreChannel)4 DelegatingPagedFile (org.neo4j.io.pagecache.DelegatingPagedFile)4 ConfigurablePageCursorTracerSupplier (org.neo4j.io.pagecache.tracing.ConfigurablePageCursorTracerSupplier)4 RecordingPageCacheTracer (org.neo4j.io.pagecache.tracing.recording.RecordingPageCacheTracer)4 Evict (org.neo4j.io.pagecache.tracing.recording.RecordingPageCacheTracer.Evict)4 RecordingPageCursorTracer (org.neo4j.io.pagecache.tracing.recording.RecordingPageCursorTracer)4 Fault (org.neo4j.io.pagecache.tracing.recording.RecordingPageCursorTracer.Fault)4 AtomicLong (java.util.concurrent.atomic.AtomicLong)3