Search in sources :

Example 1 with ThreadSafePeakMemoryTracker

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

the class BlockBasedIndexPopulatorTest method shouldDeallocateAllAllocatedMemoryOnClose.

@Test
void shouldDeallocateAllAllocatedMemoryOnClose() throws IndexEntryConflictException, IOException {
    // given
    ThreadSafePeakMemoryTracker memoryTracker = new ThreadSafePeakMemoryTracker();
    ByteBufferFactory bufferFactory = new ByteBufferFactory(UnsafeDirectByteBufferAllocator::new, 100);
    BlockBasedIndexPopulator<GenericKey, NativeIndexValue> populator = instantiatePopulator(NO_MONITOR, bufferFactory, memoryTracker);
    boolean closed = false;
    try {
        // when
        Collection<IndexEntryUpdate<?>> updates = batchOfUpdates();
        populator.add(updates, NULL);
        int nextId = updates.size();
        externalUpdates(populator, nextId, nextId + 10);
        nextId = nextId + 10;
        long memoryBeforeScanCompleted = memoryTracker.usedNativeMemory();
        populator.scanCompleted(nullInstance, populationWorkScheduler, NULL);
        externalUpdates(populator, nextId, nextId + 10);
        // then
        assertTrue(memoryTracker.peakMemoryUsage() > memoryBeforeScanCompleted, "expected some memory to have been temporarily allocated in scanCompleted");
        populator.close(true, NULL);
        closed = true;
        bufferFactory.close();
        assertEquals(0, memoryTracker.usedNativeMemory());
    } finally {
        if (!closed) {
            populator.close(true, NULL);
        }
    }
}
Also used : IndexEntryUpdate(org.neo4j.storageengine.api.IndexEntryUpdate) ThreadSafePeakMemoryTracker(org.neo4j.memory.ThreadSafePeakMemoryTracker) ByteBufferFactory(org.neo4j.io.memory.ByteBufferFactory) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with ThreadSafePeakMemoryTracker

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

the class BlockBasedIndexPopulatorTest method shouldFlushTreeOnScanCompleted.

@Test
void shouldFlushTreeOnScanCompleted() throws IndexEntryConflictException, IOException {
    // given
    ThreadSafePeakMemoryTracker memoryTracker = new ThreadSafePeakMemoryTracker();
    ByteBufferFactory bufferFactory = new ByteBufferFactory(UnsafeDirectByteBufferAllocator::new, 100);
    AtomicInteger checkpoints = new AtomicInteger();
    GBPTree.Monitor treeMonitor = new GBPTree.Monitor.Adaptor() {

        @Override
        public void checkpointCompleted() {
            checkpoints.incrementAndGet();
        }
    };
    Monitors monitors = new Monitors(databaseIndexContext.monitors, NullLogProvider.getInstance());
    monitors.addMonitorListener(treeMonitor);
    databaseIndexContext = DatabaseIndexContext.builder(databaseIndexContext).withMonitors(monitors).build();
    BlockBasedIndexPopulator<GenericKey, NativeIndexValue> populator = instantiatePopulator(NO_MONITOR, bufferFactory, memoryTracker);
    try {
        // when
        int numberOfCheckPointsBeforeScanCompleted = checkpoints.get();
        populator.scanCompleted(nullInstance, populationWorkScheduler, NULL);
        // then
        assertEquals(numberOfCheckPointsBeforeScanCompleted + 1, checkpoints.get());
    } finally {
        populator.close(true, NULL);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThreadSafePeakMemoryTracker(org.neo4j.memory.ThreadSafePeakMemoryTracker) Monitors(org.neo4j.monitoring.Monitors) GBPTree(org.neo4j.index.internal.gbptree.GBPTree) ByteBufferFactory(org.neo4j.io.memory.ByteBufferFactory) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with ThreadSafePeakMemoryTracker

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

the class BlockBasedIndexPopulatorTest method shouldBuildNonUniqueSampleAsPartOfScanCompleted.

@Test
void shouldBuildNonUniqueSampleAsPartOfScanCompleted() throws IndexEntryConflictException, IOException {
    // given
    ThreadSafePeakMemoryTracker memoryTracker = new ThreadSafePeakMemoryTracker();
    ByteBufferFactory bufferFactory = new ByteBufferFactory(UnsafeDirectByteBufferAllocator::new, 100);
    BlockBasedIndexPopulator<GenericKey, NativeIndexValue> populator = instantiatePopulator(NO_MONITOR, bufferFactory, memoryTracker);
    Collection<IndexEntryUpdate<?>> populationUpdates = batchOfUpdates();
    populator.add(populationUpdates, NULL);
    // when
    populator.scanCompleted(nullInstance, populationWorkScheduler, NULL);
    // Also a couple of updates afterwards
    int numberOfUpdatesAfterCompleted = 4;
    try (IndexUpdater updater = populator.newPopulatingUpdater(NULL)) {
        for (int i = 0; i < numberOfUpdatesAfterCompleted; i++) {
            updater.process(IndexEntryUpdate.add(10_000 + i, SCHEMA_DESCRIPTOR, intValue(i)));
        }
    }
    populator.close(true, NULL);
    // then
    IndexSample sample = populator.sample(NULL);
    assertEquals(populationUpdates.size(), sample.indexSize());
    assertEquals(populationUpdates.size(), sample.sampleSize());
    assertEquals(populationUpdates.size(), sample.uniqueValues());
    assertEquals(numberOfUpdatesAfterCompleted, sample.updates());
}
Also used : IndexEntryUpdate(org.neo4j.storageengine.api.IndexEntryUpdate) IndexSample(org.neo4j.kernel.api.index.IndexSample) ThreadSafePeakMemoryTracker(org.neo4j.memory.ThreadSafePeakMemoryTracker) IndexUpdater(org.neo4j.kernel.api.index.IndexUpdater) ByteBufferFactory(org.neo4j.io.memory.ByteBufferFactory) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with ThreadSafePeakMemoryTracker

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

the class BlockBasedIndexPopulatorTest method shouldDeallocateAllAllocatedMemoryOnDrop.

@Test
void shouldDeallocateAllAllocatedMemoryOnDrop() throws IndexEntryConflictException, IOException {
    // given
    ThreadSafePeakMemoryTracker memoryTracker = new ThreadSafePeakMemoryTracker();
    ByteBufferFactory bufferFactory = new ByteBufferFactory(UnsafeDirectByteBufferAllocator::new, 100);
    BlockBasedIndexPopulator<GenericKey, NativeIndexValue> populator = instantiatePopulator(NO_MONITOR, bufferFactory, memoryTracker);
    boolean closed = false;
    try {
        // when
        Collection<IndexEntryUpdate<?>> updates = batchOfUpdates();
        populator.add(updates, NULL);
        int nextId = updates.size();
        externalUpdates(populator, nextId, nextId + 10);
        nextId = nextId + 10;
        long memoryBeforeScanCompleted = memoryTracker.usedNativeMemory();
        populator.scanCompleted(nullInstance, populationWorkScheduler, NULL);
        externalUpdates(populator, nextId, nextId + 10);
        // then
        assertTrue(memoryTracker.peakMemoryUsage() > memoryBeforeScanCompleted, "expected some memory to have been temporarily allocated in scanCompleted");
        populator.drop();
        closed = true;
        bufferFactory.close();
        assertEquals(0, memoryTracker.usedNativeMemory());
    } finally {
        if (!closed) {
            populator.close(true, NULL);
        }
    }
}
Also used : IndexEntryUpdate(org.neo4j.storageengine.api.IndexEntryUpdate) ThreadSafePeakMemoryTracker(org.neo4j.memory.ThreadSafePeakMemoryTracker) ByteBufferFactory(org.neo4j.io.memory.ByteBufferFactory) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

Test (org.junit.jupiter.api.Test)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 ByteBufferFactory (org.neo4j.io.memory.ByteBufferFactory)4 ThreadSafePeakMemoryTracker (org.neo4j.memory.ThreadSafePeakMemoryTracker)4 IndexEntryUpdate (org.neo4j.storageengine.api.IndexEntryUpdate)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 GBPTree (org.neo4j.index.internal.gbptree.GBPTree)1 IndexSample (org.neo4j.kernel.api.index.IndexSample)1 IndexUpdater (org.neo4j.kernel.api.index.IndexUpdater)1 Monitors (org.neo4j.monitoring.Monitors)1