Search in sources :

Example 36 with MemoryTracker

use of org.neo4j.memory.MemoryTracker in project neo4j by neo4j.

the class FabricServicesBootstrap method createBoltDatabaseManagementServiceProvider.

public BoltGraphDatabaseManagementServiceSPI createBoltDatabaseManagementServiceProvider(BoltGraphDatabaseManagementServiceSPI kernelDatabaseManagementService, DatabaseManagementService managementService, Monitors monitors, SystemNanoClock clock) {
    FabricExecutor fabricExecutor = dependencies.resolveDependency(FabricExecutor.class);
    TransactionManager transactionManager = dependencies.resolveDependency(TransactionManager.class);
    FabricDatabaseManager fabricDatabaseManager = dependencies.resolveDependency(FabricDatabaseManager.class);
    var serverConfig = dependencies.resolveDependency(Config.class);
    var transactionIdTracker = new TransactionIdTracker(managementService, monitors, clock);
    var databaseManager = (DatabaseManager<DatabaseContext>) dependencies.resolveDependency(DatabaseManager.class);
    var databaseIdRepository = databaseManager.databaseIdRepository();
    var transactionBookmarkManagerFactory = dependencies.resolveDependency(TransactionBookmarkManagerFactory.class);
    var localGraphTransactionIdTracker = new LocalGraphTransactionIdTracker(transactionIdTracker, databaseIdRepository, serverConfig);
    var fabricDatabaseManagementService = dependencies.satisfyDependency(new BoltFabricDatabaseManagementService(fabricExecutor, fabricConfig, transactionManager, fabricDatabaseManager, localGraphTransactionIdTracker, transactionBookmarkManagerFactory));
    return new BoltGraphDatabaseManagementServiceSPI() {

        @Override
        public BoltGraphDatabaseServiceSPI database(String databaseName, MemoryTracker memoryTracker) throws UnavailableException, DatabaseNotFoundException {
            if (fabricDatabaseManager.hasMultiGraphCapabilities(databaseName)) {
                return fabricDatabaseManagementService.database(databaseName, memoryTracker);
            }
            return kernelDatabaseManagementService.database(databaseName, memoryTracker);
        }

        @Override
        public Optional<CustomBookmarkFormatParser> getCustomBookmarkFormatParser() {
            return fabricDatabaseManagementService.getCustomBookmarkFormatParser();
        }
    };
}
Also used : BoltFabricDatabaseManagementService(org.neo4j.fabric.bolt.BoltFabricDatabaseManagementService) FabricExecutor(org.neo4j.fabric.executor.FabricExecutor) DatabaseManager(org.neo4j.dbms.database.DatabaseManager) FabricDatabaseManager(org.neo4j.fabric.FabricDatabaseManager) TransactionManager(org.neo4j.fabric.transaction.TransactionManager) CustomBookmarkFormatParser(org.neo4j.bolt.dbapi.CustomBookmarkFormatParser) FabricDatabaseManager(org.neo4j.fabric.FabricDatabaseManager) LocalGraphTransactionIdTracker(org.neo4j.fabric.bookmark.LocalGraphTransactionIdTracker) TransactionIdTracker(org.neo4j.bolt.txtracking.TransactionIdTracker) LocalGraphTransactionIdTracker(org.neo4j.fabric.bookmark.LocalGraphTransactionIdTracker) BoltGraphDatabaseManagementServiceSPI(org.neo4j.bolt.dbapi.BoltGraphDatabaseManagementServiceSPI) MemoryTracker(org.neo4j.memory.MemoryTracker)

Example 37 with MemoryTracker

use of org.neo4j.memory.MemoryTracker in project neo4j by neo4j.

the class GBPTreeGenericCountsStoreTest method shouldNotApplyTransactionOnCreatedCountsStoreDuringRecovery.

@Test
void shouldNotApplyTransactionOnCreatedCountsStoreDuringRecovery() throws IOException {
    // given
    int labelId = 123;
    incrementNodeCount(BASE_TX_ID + 1, labelId, 4);
    countsStore.checkpoint(NULL);
    incrementNodeCount(BASE_TX_ID + 2, labelId, -2);
    closeCountsStore();
    deleteCountsStore();
    GBPTreeCountsStore.Monitor monitor = mock(GBPTreeCountsStore.Monitor.class);
    // instantiate, but don't start
    instantiateCountsStore(new Rebuilder() {

        @Override
        public void rebuild(CountUpdater updater, CursorContext cursorContext, MemoryTracker memoryTracker) {
            updater.increment(nodeKey(labelId), 2);
        }

        @Override
        public long lastCommittedTxId() {
            return BASE_TX_ID + 2;
        }
    }, writable(), monitor);
    // when doing recovery of the last transaction (since this is on an empty counts store then making the count negative, i.e. 0 - 2)
    // applying this negative delta would have failed in the updater.
    incrementNodeCount(BASE_TX_ID + 2, labelId, -2);
    verify(monitor).ignoredTransaction(BASE_TX_ID + 2);
    countsStore.start(NULL, INSTANCE);
    // then
    assertEquals(2, countsStore.read(nodeKey(labelId), NULL));
}
Also used : CursorContext(org.neo4j.io.pagecache.context.CursorContext) Rebuilder(org.neo4j.internal.counts.GBPTreeGenericCountsStore.Rebuilder) MemoryTracker(org.neo4j.memory.MemoryTracker) Test(org.junit.jupiter.api.Test)

Example 38 with MemoryTracker

use of org.neo4j.memory.MemoryTracker in project neo4j by neo4j.

the class DatabaseIT method shutdownStopMustOnlyReleaseMemoryOnce.

@Test
void shutdownStopMustOnlyReleaseMemoryOnce() throws Exception {
    MemoryTracker otherMemoryTracker = getOtherMemoryTracker();
    long beforeShutdown = otherMemoryTracker.usedNativeMemory();
    database.shutdown();
    long afterShutdown = otherMemoryTracker.usedNativeMemory();
    assertThat(afterShutdown).isLessThan(beforeShutdown);
    database.stop();
    long afterStop = otherMemoryTracker.usedNativeMemory();
    assertEquals(afterStop, afterShutdown);
}
Also used : MemoryTracker(org.neo4j.memory.MemoryTracker) Test(org.junit.jupiter.api.Test)

Example 39 with MemoryTracker

use of org.neo4j.memory.MemoryTracker in project neo4j by neo4j.

the class BatchingNeoStoresTest method shouldRebuildCountsStoreEvenIfExistsInEmptyDb.

@Test
void shouldRebuildCountsStoreEvenIfExistsInEmptyDb() throws IOException {
    // given
    try (GBPTreeCountsStore countsStore = new GBPTreeCountsStore(pageCache, databaseLayout.countStore(), fileSystem, RecoveryCleanupWorkCollector.immediate(), CountsBuilder.EMPTY, writable(), PageCacheTracer.NULL, GBPTreeCountsStore.NO_MONITOR, DEFAULT_DATABASE_NAME, 1_000)) {
        countsStore.start(NULL, INSTANCE);
        countsStore.checkpoint(NULL);
    }
    // when
    try (BatchingNeoStores stores = BatchingNeoStores.batchingNeoStoresWithExternalPageCache(fileSystem, pageCache, PageCacheTracer.NULL, databaseLayout, LATEST_RECORD_FORMATS, Configuration.DEFAULT, NullLogService.getInstance(), EMPTY, Config.defaults(), INSTANCE)) {
        stores.createNew();
        stores.buildCountsStore(new CountsBuilder() {

            @Override
            public void initialize(CountsAccessor.Updater updater, CursorContext cursorContext, MemoryTracker memoryTracker) {
                updater.incrementNodeCount(1, 10);
                updater.incrementNodeCount(2, 20);
                updater.incrementRelationshipCount(ANY_LABEL, 1, 2, 30);
                updater.incrementRelationshipCount(1, 2, ANY_LABEL, 50);
            }

            @Override
            public long lastCommittedTxId() {
                return BASE_TX_ID + 1;
            }
        }, PageCacheTracer.NULL, NULL, INSTANCE);
    }
    // then
    try (GBPTreeCountsStore countsStore = new GBPTreeCountsStore(pageCache, databaseLayout.countStore(), fileSystem, RecoveryCleanupWorkCollector.immediate(), CountsBuilder.EMPTY, writable(), PageCacheTracer.NULL, GBPTreeCountsStore.NO_MONITOR, DEFAULT_DATABASE_NAME, 1_000)) {
        assertEquals(10, countsStore.nodeCount(1, NULL));
        assertEquals(20, countsStore.nodeCount(2, NULL));
        assertEquals(30, countsStore.relationshipCount(ANY_LABEL, 1, 2, NULL));
        assertEquals(50, countsStore.relationshipCount(1, 2, ANY_LABEL, NULL));
    }
}
Also used : GBPTreeCountsStore(org.neo4j.internal.counts.GBPTreeCountsStore) CountsAccessor(org.neo4j.counts.CountsAccessor) CursorContext(org.neo4j.io.pagecache.context.CursorContext) CountsBuilder(org.neo4j.internal.counts.CountsBuilder) MemoryTracker(org.neo4j.memory.MemoryTracker) Test(org.junit.jupiter.api.Test)

Example 40 with MemoryTracker

use of org.neo4j.memory.MemoryTracker in project neo4j by neo4j.

the class HeapTrackingLongEnumerationListTest method addRemoveScenario.

@Test
void addRemoveScenario() {
    MemoryTracker memoryTracker = new LocalMemoryTracker();
    HeapTrackingLongEnumerationList<Long> table = HeapTrackingLongEnumerationList.create(memoryTracker, 4);
    assertEmpty(table);
    table.add(0L);
    table.add(1L);
    table.add(2L);
    assertContainsOnly(table, 0, 2);
    table.remove(0L);
    table.remove(1L);
    assertContainsOnly(table, 2);
    table.remove(2);
    assertEmpty(table);
    table.add(3L);
    table.add(4L);
    table.add(5L);
    assertContainsOnly(table, 3, 5);
    table.remove(5);
    assertContainsOnly(table, 3, 4);
    table.remove(4);
    assertContainsOnly(table, 3);
    long measured1 = meter.measureDeep(table) - measuredMemoryTracker;
    assertEquals(measured1, memoryTracker.estimatedHeapMemory() + HeapEstimator.LONG_SIZE);
    table.add(6L);
    assertContainsOnly(table, new long[] { 3L, 6L });
    long measured2 = meter.measureDeep(table) - measuredMemoryTracker;
    assertEquals(measured2, memoryTracker.estimatedHeapMemory() + 2 * HeapEstimator.LONG_SIZE);
    assertEquals(HeapEstimator.LONG_SIZE, measured2 - measured1);
    // New chunk needed
    table.add(7L);
    assertContainsOnly(table, new long[] { 3L, 6, 7L });
    // New chunk allocated because of poor alignment in tail chunks
    table.add(8L);
    assertContainsOnly(table, new long[] { 3L, 6L, 7L, 8L });
    table.remove(6L);
    assertContainsOnly(table, new long[] { 3L, 7L, 8L });
    // Should recycle chunk
    table.remove(3L);
    assertContainsOnly(table, new long[] { 7L, 8L });
    // Should recycle chunk
    table.remove(7L);
    assertContainsOnly(table, 8L);
    // Memory should be back to one chunk + one value
    long measured3 = meter.measureDeep(table) - measuredMemoryTracker;
    assertEquals(measured3, memoryTracker.estimatedHeapMemory() + HeapEstimator.LONG_SIZE);
    assertEquals(measured1, measured3);
    table.add(9L);
    table.add(10L);
    table.add(11L);
    assertContainsOnly(table, 8, 11);
    table.remove(8L);
    table.remove(10L);
    table.remove(11L);
    assertContainsOnly(table, 9);
    table.add(12L);
    assertContainsOnly(table, new long[] { 9L, 12L });
    table.remove(9L);
    assertContainsOnly(table, 12);
    // Memory should be one chunk + one value
    long measured4 = meter.measureDeep(table) - measuredMemoryTracker;
    assertEquals(measured4, memoryTracker.estimatedHeapMemory() + HeapEstimator.LONG_SIZE);
    assertEquals(measured3, measured4);
    table.close();
}
Also used : LocalMemoryTracker(org.neo4j.memory.LocalMemoryTracker) LocalMemoryTracker(org.neo4j.memory.LocalMemoryTracker) MemoryTracker(org.neo4j.memory.MemoryTracker) Test(org.junit.jupiter.api.Test)

Aggregations

MemoryTracker (org.neo4j.memory.MemoryTracker)44 Test (org.junit.jupiter.api.Test)22 LocalMemoryTracker (org.neo4j.memory.LocalMemoryTracker)10 CursorContext (org.neo4j.io.pagecache.context.CursorContext)9 Value (org.neo4j.values.storable.Value)6 Transaction (org.neo4j.graphdb.Transaction)5 InternalTransaction (org.neo4j.kernel.impl.coreapi.InternalTransaction)5 Map (java.util.Map)4 Optional (java.util.Optional)4 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)4 SchemaDescriptor (org.neo4j.internal.schema.SchemaDescriptor)4 Log (org.neo4j.logging.Log)4 ValueTuple (org.neo4j.values.storable.ValueTuple)4 URI (java.net.URI)3 ArrayList (java.util.ArrayList)3 Collections.emptyMap (java.util.Collections.emptyMap)3 Objects.requireNonNullElse (java.util.Objects.requireNonNullElse)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 IOException (java.io.IOException)2 Arrays (java.util.Arrays)2