Search in sources :

Example 46 with CursorContext

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

the class KernelTransactionImplementationTest method reportTransactionStatistics.

@Test
void reportTransactionStatistics() {
    KernelTransactionImplementation transaction = newTransaction(100);
    transaction.memoryTracker().allocateHeap(13);
    transaction.memoryTracker().allocateNative(14);
    KernelTransactionImplementation.Statistics statistics = new KernelTransactionImplementation.Statistics(transaction, new AtomicReference<>(new ThreadBasedCpuClock()), false);
    PredictablePageCursorTracer tracer = new PredictablePageCursorTracer();
    statistics.init(2, new CursorContext(tracer));
    assertEquals(2, statistics.cpuTimeMillis());
    assertEquals(13, statistics.estimatedHeapMemory());
    assertEquals(14, statistics.usedNativeMemory());
    assertEquals(0, statistics.heapAllocatedBytes());
    assertEquals(1, statistics.totalTransactionPageCacheFaults());
    assertEquals(4, statistics.totalTransactionPageCacheHits());
    statistics.addWaitingTime(1);
    assertEquals(1, statistics.getWaitingTimeNanos(0));
    transaction.memoryTracker().releaseNative(14);
    statistics.reset();
    transaction.memoryTracker().reset();
    statistics.init(4, new CursorContext(tracer));
    assertEquals(4, statistics.cpuTimeMillis());
    assertEquals(0, statistics.estimatedHeapMemory());
    assertEquals(0, statistics.usedNativeMemory());
    assertEquals(0, statistics.heapAllocatedBytes());
    assertEquals(2, statistics.totalTransactionPageCacheFaults());
    assertEquals(6, statistics.totalTransactionPageCacheHits());
    assertEquals(0, statistics.getWaitingTimeNanos(0));
}
Also used : CursorContext(org.neo4j.io.pagecache.context.CursorContext) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 47 with CursorContext

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

the class IdGeneratorMigrator method migrateIdFiles.

private void migrateIdFiles(DatabaseLayout directoryLayout, DatabaseLayout migrationLayout, RecordFormats oldFormat, RecordFormats newFormat, ProgressReporter progress, CursorContext cursorContext) throws IOException {
    // The store .id files needs to be migrated. At this point some of them have been sort-of-migrated, i.e. merely ported
    // to the new format, but just got the highId and nothing else. Regardless we want to do a proper migration here,
    // which not only means creating an empty id file w/ only highId. No, it also means scanning the stores and properly
    // updating its freelist so that from this point no ids will be lost, ever.
    // For every store type: if store is in migrationLayout use that, else use the one from the dbLayout because it will
    // be of the current format already. Then scan it and create the .id file in the migration layout.
    List<StoreType> storesInDbDirectory = new ArrayList<>();
    List<StoreType> storesInMigrationDirectory = new ArrayList<>();
    for (StoreType storeType : StoreType.values()) {
        // See if it exists in migration directory, otherwise it must be in the db directory
        List<StoreType> list = fileSystem.fileExists(migrationLayout.file(storeType.getDatabaseFile())) ? storesInMigrationDirectory : storesInDbDirectory;
        list.add(storeType);
    }
    progress.start(storesInDbDirectory.size() + storesInMigrationDirectory.size());
    // Rebuild the .id files from the legacy stores that haven't been upgraded, i.e. if they remained unchanged
    // Make them end up in upgrade/<store>.id so that they won't overwrite the origin .id file before the upgrade is completed
    IdGeneratorFactory rebuiltIdGeneratorsFromOldStore = new DefaultIdGeneratorFactory(fileSystem, immediate(), directoryLayout.getDatabaseName()) {

        @Override
        public IdGenerator open(PageCache pageCache, Path filename, IdType idType, LongSupplier highIdScanner, long maxId, DatabaseReadOnlyChecker readOnlyChecker, Config config, CursorContext cursorContext, ImmutableSet<OpenOption> openOptions) throws IOException {
            Path redirectedFilename = migrationLayout.databaseDirectory().resolve(filename.getFileName().toString());
            return super.open(pageCache, redirectedFilename, idType, highIdScanner, maxId, readOnlyChecker, config, cursorContext, openOptions);
        }

        @Override
        public IdGenerator create(PageCache pageCache, Path fileName, IdType idType, long highId, boolean throwIfFileExists, long maxId, DatabaseReadOnlyChecker readOnlyChecker, Config config, CursorContext cursorContext, ImmutableSet<OpenOption> openOptions) {
            throw new IllegalStateException("The store file should exist and therefore all calls should be to open, not create");
        }
    };
    startAndTriggerRebuild(directoryLayout, oldFormat, rebuiltIdGeneratorsFromOldStore, storesInDbDirectory, progress, cursorContext);
    // Build the ones from the migration directory, those stores that have been migrated
    // Before doing this we will have to create empty stores for those that are missing, otherwise some of the stores
    // that we need to open will complain because some of their "sub-stores" doesn't exist. They will be empty, it's fine...
    // and we will not read from them at all. They will just sit there and allow their parent store to be opened.
    // We'll remove them after we have built the id files
    IdGeneratorFactory rebuiltIdGeneratorsFromNewStore = new DefaultIdGeneratorFactory(fileSystem, immediate(), migrationLayout.getDatabaseName());
    Set<Path> placeHolderStoreFiles = createEmptyPlaceHolderStoreFiles(migrationLayout, newFormat);
    startAndTriggerRebuild(migrationLayout, newFormat, rebuiltIdGeneratorsFromNewStore, storesInMigrationDirectory, progress, cursorContext);
    for (Path emptyPlaceHolderStoreFile : placeHolderStoreFiles) {
        fileSystem.deleteFile(emptyPlaceHolderStoreFile);
    }
}
Also used : Path(java.nio.file.Path) Config(org.neo4j.configuration.Config) ArrayList(java.util.ArrayList) DefaultIdGeneratorFactory(org.neo4j.internal.id.DefaultIdGeneratorFactory) IdGeneratorFactory(org.neo4j.internal.id.IdGeneratorFactory) ScanOnOpenReadOnlyIdGeneratorFactory(org.neo4j.internal.id.ScanOnOpenReadOnlyIdGeneratorFactory) DefaultIdGeneratorFactory(org.neo4j.internal.id.DefaultIdGeneratorFactory) CursorContext(org.neo4j.io.pagecache.context.CursorContext) IdType(org.neo4j.internal.id.IdType) StoreType(org.neo4j.kernel.impl.store.StoreType) ImmutableSet(org.eclipse.collections.api.set.ImmutableSet) DatabaseReadOnlyChecker(org.neo4j.configuration.helpers.DatabaseReadOnlyChecker) LongSupplier(java.util.function.LongSupplier) PageCache(org.neo4j.io.pagecache.PageCache)

Example 48 with CursorContext

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

the class IdGeneratorMigrator method migrate.

@Override
public void migrate(DatabaseLayout directoryLayout, DatabaseLayout migrationLayout, ProgressReporter progress, String versionToMigrateFrom, String versionToMigrateTo, IndexImporterFactory indexImporterFactory) throws IOException {
    RecordFormats oldFormat = selectForVersion(versionToMigrateFrom);
    RecordFormats newFormat = selectForVersion(versionToMigrateTo);
    if (requiresIdFilesMigration(oldFormat, newFormat)) {
        try (var cursorContext = new CursorContext(cacheTracer.createPageCursorTracer(ID_GENERATOR_MIGRATION_TAG))) {
            migrateIdFiles(directoryLayout, migrationLayout, oldFormat, newFormat, progress, cursorContext);
        }
    }
}
Also used : RecordFormats(org.neo4j.kernel.impl.store.format.RecordFormats) CursorContext(org.neo4j.io.pagecache.context.CursorContext)

Example 49 with CursorContext

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

the class MuninnPageCacheTest method doNotMarkContextAsDirtyWhenAnyEvictedPageHaveModificationTransactionLowerThenReader.

@Test
void doNotMarkContextAsDirtyWhenAnyEvictedPageHaveModificationTransactionLowerThenReader() throws IOException {
    TestVersionContext versionContext = new TestVersionContext(() -> 15);
    try (MuninnPageCache pageCache = createPageCache(fs, 2, PageCacheTracer.NULL);
        PagedFile pagedFile = map(pageCache, file("a"), 8);
        CursorContext cursorContext = new CursorContext(PageCursorTracer.NULL, versionContext)) {
        versionContext.initWrite(3);
        try (PageCursor cursor = pagedFile.io(0, PF_SHARED_WRITE_LOCK, cursorContext)) {
            assertTrue(cursor.next());
            cursor.putLong(3);
        }
        versionContext.initWrite(13);
        try (PageCursor cursor = pagedFile.io(1, PF_SHARED_WRITE_LOCK, cursorContext)) {
            assertTrue(cursor.next());
            cursor.putLong(4);
        }
        evictAllPages(pageCache);
        versionContext.initRead();
        try (PageCursor cursor = pagedFile.io(0, PF_SHARED_READ_LOCK, cursorContext)) {
            assertTrue(cursor.next());
            assertEquals(3, cursor.getLong());
            assertFalse(versionContext.isDirty());
        }
    }
}
Also used : PagedFile(org.neo4j.io.pagecache.PagedFile) CursorContext(org.neo4j.io.pagecache.context.CursorContext) PageCursor(org.neo4j.io.pagecache.PageCursor) PageCacheTest(org.neo4j.io.pagecache.PageCacheTest) Test(org.junit.jupiter.api.Test)

Example 50 with CursorContext

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

the class MuninnPageCacheTest method pageModificationTracksHighestModifierTransactionId.

@Test
void pageModificationTracksHighestModifierTransactionId() throws IOException {
    TestVersionContext versionContext = new TestVersionContext(() -> 0);
    try (MuninnPageCache pageCache = createPageCache(fs, 2, PageCacheTracer.NULL);
        PagedFile pagedFile = map(pageCache, file("a"), 8);
        CursorContext cursorContext = new CursorContext(PageCursorTracer.NULL, versionContext)) {
        versionContext.initWrite(1);
        try (PageCursor cursor = pagedFile.io(0, PF_SHARED_WRITE_LOCK, cursorContext)) {
            assertTrue(cursor.next());
            cursor.putLong(1);
        }
        versionContext.initWrite(12);
        try (PageCursor cursor = pagedFile.io(0, PF_SHARED_WRITE_LOCK, cursorContext)) {
            assertTrue(cursor.next());
            cursor.putLong(2);
        }
        versionContext.initWrite(7);
        try (PageCursor cursor = pagedFile.io(0, PF_SHARED_WRITE_LOCK, cursorContext)) {
            assertTrue(cursor.next());
            cursor.putLong(3);
        }
        try (PageCursor cursor = pagedFile.io(0, PF_SHARED_READ_LOCK, cursorContext)) {
            assertTrue(cursor.next());
            MuninnPageCursor pageCursor = (MuninnPageCursor) cursor;
            assertEquals(12, pageCursor.pagedFile.getLastModifiedTxId(pageCursor.pinnedPageRef));
            assertEquals(3, cursor.getLong());
        }
    }
}
Also used : PagedFile(org.neo4j.io.pagecache.PagedFile) CursorContext(org.neo4j.io.pagecache.context.CursorContext) PageCursor(org.neo4j.io.pagecache.PageCursor) PageCacheTest(org.neo4j.io.pagecache.PageCacheTest) 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