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