Search in sources :

Example 16 with CursorContext

use of org.neo4j.io.pagecache.context.CursorContext in project neo4j by neo4j.

the class IndexStatisticsStoreTest method tracePageCacheAccessOnConsistencyCheck.

@Test
void tracePageCacheAccessOnConsistencyCheck() throws IOException {
    var cacheTracer = new DefaultPageCacheTracer();
    var store = openStore(cacheTracer, "consistencyCheck");
    try (var cursorContext = new CursorContext(cacheTracer.createPageCursorTracer("tracePageCacheAccessOnConsistencyCheck"))) {
        for (int i = 0; i < 100; i++) {
            store.replaceStats(i, new IndexSample());
        }
        store.checkpoint(CursorContext.NULL);
        store.consistencyCheck(noopReporterFactory(), cursorContext);
        PageCursorTracer cursorTracer = cursorContext.getCursorTracer();
        assertThat(cursorTracer.pins()).isEqualTo(16);
        assertThat(cursorTracer.unpins()).isEqualTo(16);
        assertThat(cursorTracer.hits()).isEqualTo(16);
    }
}
Also used : PageCursorTracer(org.neo4j.io.pagecache.tracing.cursor.PageCursorTracer) IndexSample(org.neo4j.kernel.api.index.IndexSample) CursorContext(org.neo4j.io.pagecache.context.CursorContext) DefaultPageCacheTracer(org.neo4j.io.pagecache.tracing.DefaultPageCacheTracer) Test(org.junit.jupiter.api.Test)

Example 17 with CursorContext

use of org.neo4j.io.pagecache.context.CursorContext in project neo4j by neo4j.

the class PageCacheTest method tracerMustBeNotifiedAboutPinUnpinFaultAndEvictEventsWhenReading.

@Test
void tracerMustBeNotifiedAboutPinUnpinFaultAndEvictEventsWhenReading() {
    assertTimeoutPreemptively(ofMillis(SHORT_TIMEOUT_MILLIS), () -> {
        DefaultPageCacheTracer tracer = new DefaultPageCacheTracer();
        getPageCache(fs, maxPages, tracer);
        generateFileWithRecords(file("a"), recordCount, recordSize);
        long initialPins = tracer.pins();
        long initialUnpins = tracer.unpins();
        long countedPages = 0;
        long countedFaults = 0;
        try (CursorContext cursorContext = new CursorContext(tracer.createPageCursorTracer("tracerMustBeNotifiedAboutPinUnpinFaultAndEvictEventsWhenReading"));
            PagedFile pagedFile = map(file("a"), filePageSize);
            PageCursor cursor = pagedFile.io(0, PF_SHARED_READ_LOCK, cursorContext)) {
            while (cursor.next()) {
                countedPages++;
                countedFaults++;
            }
            // Using next( pageId ) to the already-pinned page id does not count,
            // so we only increment once for this section
            countedPages++;
            for (int i = 0; i < 20; i++) {
                assertTrue(cursor.next(1));
            }
            // then it counts
            for (int i = 0; i < 20; i++) {
                assertTrue(cursor.next(i));
                countedPages++;
            }
        }
        assertThat(tracer.pins()).as("wrong count of pins").isEqualTo(countedPages + initialPins);
        assertThat(tracer.unpins()).as("wrong count of unpins").isEqualTo(countedPages + initialUnpins);
        // We might be unlucky and fault in the second next call, on the page
        // we brought up in the first next call. That's why we assert that we
        // have observed *at least* the countedPages number of faults.
        long faults = tracer.faults();
        long bytesRead = tracer.bytesRead();
        assertThat(faults).as("wrong count of faults").isGreaterThanOrEqualTo(countedFaults);
        assertThat(bytesRead).as("wrong number of bytes read").isGreaterThanOrEqualTo(countedFaults * filePageSize);
        // Every page we move forward can put the freelist behind so the cache
        // wants to evict more pages. Plus, every page fault we do could also
        // block and get a page directly transferred to it, and these kinds of
        // evictions can count in addition to the evictions we do when the
        // cache is behind on keeping the freelist full.
        assertThat(tracer.evictions()).as("wrong count of evictions").isGreaterThanOrEqualTo(countedFaults - maxPages).isLessThanOrEqualTo(countedPages + faults);
    });
}
Also used : CursorContext(org.neo4j.io.pagecache.context.CursorContext) DefaultPageCacheTracer(org.neo4j.io.pagecache.tracing.DefaultPageCacheTracer) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Example 18 with CursorContext

use of org.neo4j.io.pagecache.context.CursorContext in project neo4j by neo4j.

the class PageCacheTest method noFaultReadOfPagesNotInMemory.

@Test
void noFaultReadOfPagesNotInMemory() throws Exception {
    DefaultPageCacheTracer cacheTracer = new DefaultPageCacheTracer();
    getPageCache(fs, maxPages, cacheTracer);
    Path file = file("a");
    generateFileWithRecords(file, recordsPerFilePage * 2, recordSize);
    long initialFaults = cacheTracer.faults();
    try (PagedFile pf = map(file, filePageSize);
        CursorContext cursorContext = new CursorContext(cacheTracer.createPageCursorTracer("noFaultReadOfPagesNotInMemory"));
        PageCursor nofault = pf.io(0, PF_SHARED_READ_LOCK | PF_NO_FAULT, cursorContext)) {
        verifyNoFaultAccessToPagesNotInMemory(cacheTracer, cursorContext, nofault, initialFaults);
    }
}
Also used : Path(java.nio.file.Path) CursorContext(org.neo4j.io.pagecache.context.CursorContext) DefaultPageCacheTracer(org.neo4j.io.pagecache.tracing.DefaultPageCacheTracer) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Example 19 with CursorContext

use of org.neo4j.io.pagecache.context.CursorContext in project neo4j by neo4j.

the class PageCacheTest method noFaultLinkedReadOfPagesNotInMemory.

@Test
void noFaultLinkedReadOfPagesNotInMemory() throws Exception {
    DefaultPageCacheTracer cacheTracer = new DefaultPageCacheTracer();
    getPageCache(fs, maxPages, cacheTracer);
    Path file = file("a");
    generateFileWithRecords(file, recordsPerFilePage * 2, recordSize);
    long initialFaults = cacheTracer.faults();
    try (PagedFile pf = map(file, filePageSize);
        CursorContext cursorContext = new CursorContext(cacheTracer.createPageCursorTracer("noFaultLinkedReadOfPagesNotInMemory"));
        PageCursor nofault = pf.io(0, PF_SHARED_READ_LOCK | PF_NO_FAULT, cursorContext);
        PageCursor linkedNoFault = nofault.openLinkedCursor(0)) {
        verifyNoFaultAccessToPagesNotInMemory(cacheTracer, cursorContext, linkedNoFault, initialFaults);
    }
}
Also used : Path(java.nio.file.Path) CursorContext(org.neo4j.io.pagecache.context.CursorContext) DefaultPageCacheTracer(org.neo4j.io.pagecache.tracing.DefaultPageCacheTracer) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Example 20 with CursorContext

use of org.neo4j.io.pagecache.context.CursorContext in project neo4j by neo4j.

the class PageCacheTest method pinEventShouldCompleteIfRepinFromShouldRetryThrows.

@Test
void pinEventShouldCompleteIfRepinFromShouldRetryThrows() throws IOException {
    DefaultPageCacheTracer tracer = new DefaultPageCacheTracer();
    RandomAdversary adversary = new RandomAdversary(0.0, 0.9, 0.0);
    adversary.setProbabilityFactor(0.0);
    AdversarialFileSystemAbstraction afs = new AdversarialFileSystemAbstraction(adversary, fs);
    getPageCache(afs, maxPages, tracer);
    generateFileWithRecords(file("a"), recordCount, recordSize);
    try (PagedFile pagedFile = map(file("a"), filePageSize);
        CursorContext cursorContext = new CursorContext(tracer.createPageCursorTracer("test"))) {
        try (PageCursor reader = pagedFile.io(0, PF_SHARED_READ_LOCK, cursorContext)) {
            assertTrue(reader.next());
            // Cause the page under the reader cursor to be evicted.
            try (PagedFile otherPagedFile = map(existingFile("b"), filePageSize)) {
                assertThrows(IOException.class, () -> {
                    // noinspection InfiniteLoopStatement
                    for (; ; ) {
                        adversary.setProbabilityFactor(1.0);
                        try {
                            reader.shouldRetry();
                        } finally {
                            adversary.setProbabilityFactor(0.0);
                        }
                        try (PageCursor writer = otherPagedFile.io(0, PF_SHARED_WRITE_LOCK, cursorContext)) {
                            for (int i = 0; i < maxPages * 10; i++) {
                                assertTrue(writer.next(i));
                            }
                        }
                        otherPagedFile.flushAndForce();
                    }
                });
            }
        }
    }
    // Then we should see pins and unpins pair up exactly.
    assertThat(tracer.unpins()).isEqualTo(tracer.pins());
}
Also used : CursorContext(org.neo4j.io.pagecache.context.CursorContext) AdversarialFileSystemAbstraction(org.neo4j.adversaries.fs.AdversarialFileSystemAbstraction) RandomAdversary(org.neo4j.adversaries.RandomAdversary) DefaultPageCacheTracer(org.neo4j.io.pagecache.tracing.DefaultPageCacheTracer) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Aggregations

CursorContext (org.neo4j.io.pagecache.context.CursorContext)161 Test (org.junit.jupiter.api.Test)74 DefaultPageCacheTracer (org.neo4j.io.pagecache.tracing.DefaultPageCacheTracer)52 PageCursor (org.neo4j.io.pagecache.PageCursor)40 IOException (java.io.IOException)31 UncheckedIOException (java.io.UncheckedIOException)27 PageCursorTracer (org.neo4j.io.pagecache.tracing.cursor.PageCursorTracer)27 Path (java.nio.file.Path)17 PagedFile (org.neo4j.io.pagecache.PagedFile)17 ArrayList (java.util.ArrayList)16 PageCacheTest (org.neo4j.io.pagecache.PageCacheTest)15 MutableLong (org.apache.commons.lang3.mutable.MutableLong)13 MemoryTracker (org.neo4j.memory.MemoryTracker)12 PageCache (org.neo4j.io.pagecache.PageCache)11 ProgressListener (org.neo4j.internal.helpers.progress.ProgressListener)10 DynamicRecord (org.neo4j.kernel.impl.store.record.DynamicRecord)10 List (java.util.List)9 RepeatedTest (org.junit.jupiter.api.RepeatedTest)9 NodeRecord (org.neo4j.kernel.impl.store.record.NodeRecord)9 PageCacheTracer (org.neo4j.io.pagecache.tracing.PageCacheTracer)8