Search in sources :

Example 1 with ByteBufferFactory

use of org.neo4j.io.memory.ByteBufferFactory 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 ByteBufferFactory

use of org.neo4j.io.memory.ByteBufferFactory 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 ByteBufferFactory

use of org.neo4j.io.memory.ByteBufferFactory in project neo4j by neo4j.

the class BlockBasedIndexPopulatorTest method shouldFailOnBatchAddedTooLargeValue.

@Test
void shouldFailOnBatchAddedTooLargeValue() throws IOException {
    // / given
    ByteBufferFactory bufferFactory = new ByteBufferFactory(UnsafeDirectByteBufferAllocator::new, SUFFICIENTLY_LARGE_BUFFER_SIZE);
    BlockBasedIndexPopulator<GenericKey, NativeIndexValue> populator = instantiatePopulator(NO_MONITOR, bufferFactory, INSTANCE);
    try {
        int size = populator.tree.keyValueSizeCap() + 1;
        assertThrows(IllegalArgumentException.class, () -> populator.add(singletonList(IndexEntryUpdate.add(0, INDEX_DESCRIPTOR, generateStringValueResultingInIndexEntrySize(layout(), size))), NULL));
    } finally {
        populator.close(false, NULL);
    }
}
Also used : ByteBufferFactory(org.neo4j.io.memory.ByteBufferFactory) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with ByteBufferFactory

use of org.neo4j.io.memory.ByteBufferFactory 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 5 with ByteBufferFactory

use of org.neo4j.io.memory.ByteBufferFactory 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

ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)8 ByteBufferFactory (org.neo4j.io.memory.ByteBufferFactory)8 Test (org.junit.jupiter.api.Test)6 ThreadSafePeakMemoryTracker (org.neo4j.memory.ThreadSafePeakMemoryTracker)4 IndexUpdater (org.neo4j.kernel.api.index.IndexUpdater)3 IndexEntryUpdate (org.neo4j.storageengine.api.IndexEntryUpdate)3 ValueSource (org.junit.jupiter.params.provider.ValueSource)2 Value (org.neo4j.values.storable.Value)2 Values.intValue (org.neo4j.values.storable.Values.intValue)2 Values.stringValue (org.neo4j.values.storable.Values.stringValue)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 GBPTree (org.neo4j.index.internal.gbptree.GBPTree)1 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)1 IndexSample (org.neo4j.kernel.api.index.IndexSample)1 Monitors (org.neo4j.monitoring.Monitors)1 Race (org.neo4j.test.Race)1