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);
}
}
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);
});
}
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);
}
}
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);
}
}
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());
}
Aggregations