Search in sources :

Example 6 with CursorFactory

use of org.neo4j.internal.kernel.api.CursorFactory in project neo4j by neo4j.

the class ParallelRelationshipCursorTransactionStateTestBase method shouldScanAllRelationshipsFromMultipleThreads.

@Test
void shouldScanAllRelationshipsFromMultipleThreads() throws InterruptedException, ExecutionException, KernelException {
    // given
    ExecutorService service = Executors.newFixedThreadPool(4);
    CursorFactory cursors = testSupport.kernelToTest().cursors();
    int size = 128;
    LongArrayList ids = new LongArrayList();
    try (KernelTransaction tx = beginTransaction()) {
        Write write = tx.dataWrite();
        int type = tx.tokenWrite().relationshipTypeGetOrCreateForName("R");
        for (int i = 0; i < size; i++) {
            ids.add(write.relationshipCreate(write.nodeCreate(), type, write.nodeCreate()));
        }
        org.neo4j.internal.kernel.api.Read read = tx.dataRead();
        Scan<RelationshipScanCursor> scan = read.allRelationshipsScan();
        // when
        Future<LongList> future1 = service.submit(singleBatchWorker(scan, () -> cursors.allocateRelationshipScanCursor(NULL), REL_GET, size / 4));
        Future<LongList> future2 = service.submit(singleBatchWorker(scan, () -> cursors.allocateRelationshipScanCursor(NULL), REL_GET, size / 4));
        Future<LongList> future3 = service.submit(singleBatchWorker(scan, () -> cursors.allocateRelationshipScanCursor(NULL), REL_GET, size / 4));
        Future<LongList> future4 = service.submit(singleBatchWorker(scan, () -> cursors.allocateRelationshipScanCursor(NULL), REL_GET, size / 4));
        // then
        LongList ids1 = future1.get();
        LongList ids2 = future2.get();
        LongList ids3 = future3.get();
        LongList ids4 = future4.get();
        TestUtils.assertDistinct(ids1, ids2, ids3, ids4);
        LongList concat = TestUtils.concat(ids1, ids2, ids3, ids4);
        assertEquals(ids.toSortedList(), concat.toSortedList());
        tx.rollback();
    } finally {
        service.shutdown();
        service.awaitTermination(1, TimeUnit.MINUTES);
    }
}
Also used : Write(org.neo4j.internal.kernel.api.Write) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Read(org.neo4j.internal.kernel.api.Read) LongArrayList(org.eclipse.collections.impl.list.mutable.primitive.LongArrayList) LongList(org.eclipse.collections.api.list.primitive.LongList) CursorFactory(org.neo4j.internal.kernel.api.CursorFactory) RelationshipScanCursor(org.neo4j.internal.kernel.api.RelationshipScanCursor) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.jupiter.api.Test)

Example 7 with CursorFactory

use of org.neo4j.internal.kernel.api.CursorFactory in project neo4j by neo4j.

the class ParallelRelationshipCursorTransactionStateTestBase method shouldScanAllRelationshipFromRandomlySizedWorkers.

@Test
void shouldScanAllRelationshipFromRandomlySizedWorkers() throws InterruptedException, KernelException {
    // given
    ExecutorService service = Executors.newFixedThreadPool(4);
    int size = 128;
    LongArrayList ids = new LongArrayList();
    try (KernelTransaction tx = beginTransaction()) {
        Write write = tx.dataWrite();
        int type = tx.tokenWrite().relationshipTypeGetOrCreateForName("R");
        for (int i = 0; i < size; i++) {
            ids.add(write.relationshipCreate(write.nodeCreate(), type, write.nodeCreate()));
        }
        Read read = tx.dataRead();
        Scan<RelationshipScanCursor> scan = read.allRelationshipsScan();
        CursorFactory cursors = testSupport.kernelToTest().cursors();
        // when
        List<Future<LongList>> futures = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            futures.add(service.submit(randomBatchWorker(scan, () -> cursors.allocateRelationshipScanCursor(NULL), REL_GET)));
        }
        // then
        List<LongList> lists = futures.stream().map(TestUtils::unsafeGet).collect(Collectors.toList());
        TestUtils.assertDistinct(lists);
        LongList concat = TestUtils.concat(lists);
        assertEquals(ids.toSortedList(), concat.toSortedList());
        tx.rollback();
    } finally {
        service.shutdown();
        service.awaitTermination(1, TimeUnit.MINUTES);
    }
}
Also used : Write(org.neo4j.internal.kernel.api.Write) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) LongArrayList(org.eclipse.collections.impl.list.mutable.primitive.LongArrayList) ArrayList(java.util.ArrayList) LongArrayList(org.eclipse.collections.impl.list.mutable.primitive.LongArrayList) LongList(org.eclipse.collections.api.list.primitive.LongList) Read(org.neo4j.internal.kernel.api.Read) RelationshipScanCursor(org.neo4j.internal.kernel.api.RelationshipScanCursor) CursorFactory(org.neo4j.internal.kernel.api.CursorFactory) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) Test(org.junit.jupiter.api.Test)

Example 8 with CursorFactory

use of org.neo4j.internal.kernel.api.CursorFactory in project neo4j by neo4j.

the class ParallelRelationshipCursorTransactionStateTestBase method shouldScanAllRelationshipsFromMultipleThreadWithBigSizeHints.

@Test
void shouldScanAllRelationshipsFromMultipleThreadWithBigSizeHints() throws InterruptedException, ExecutionException, KernelException {
    // given
    ExecutorService service = Executors.newFixedThreadPool(4);
    CursorFactory cursors = testSupport.kernelToTest().cursors();
    int size = 128;
    LongArrayList ids = new LongArrayList();
    try (KernelTransaction tx = beginTransaction()) {
        Write write = tx.dataWrite();
        int type = tx.tokenWrite().relationshipTypeGetOrCreateForName("R");
        for (int i = 0; i < size; i++) {
            ids.add(write.relationshipCreate(write.nodeCreate(), type, write.nodeCreate()));
        }
        org.neo4j.internal.kernel.api.Read read = tx.dataRead();
        Scan<RelationshipScanCursor> scan = read.allRelationshipsScan();
        // when
        Supplier<RelationshipScanCursor> allocateCursor = () -> cursors.allocateRelationshipScanCursor(NULL);
        Future<LongList> future1 = service.submit(singleBatchWorker(scan, allocateCursor, REL_GET, 100));
        Future<LongList> future2 = service.submit(singleBatchWorker(scan, allocateCursor, REL_GET, 100));
        Future<LongList> future3 = service.submit(singleBatchWorker(scan, allocateCursor, REL_GET, 100));
        Future<LongList> future4 = service.submit(singleBatchWorker(scan, allocateCursor, REL_GET, 100));
        // then
        LongList ids1 = future1.get();
        LongList ids2 = future2.get();
        LongList ids3 = future3.get();
        LongList ids4 = future4.get();
        TestUtils.assertDistinct(ids1, ids2, ids3, ids4);
        LongList concat = TestUtils.concat(ids1, ids2, ids3, ids4);
        assertEquals(ids.toSortedList(), concat.toSortedList());
    } finally {
        service.shutdown();
        service.awaitTermination(1, TimeUnit.MINUTES);
    }
}
Also used : Write(org.neo4j.internal.kernel.api.Write) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Read(org.neo4j.internal.kernel.api.Read) LongArrayList(org.eclipse.collections.impl.list.mutable.primitive.LongArrayList) LongList(org.eclipse.collections.api.list.primitive.LongList) CursorFactory(org.neo4j.internal.kernel.api.CursorFactory) RelationshipScanCursor(org.neo4j.internal.kernel.api.RelationshipScanCursor) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.jupiter.api.Test)

Example 9 with CursorFactory

use of org.neo4j.internal.kernel.api.CursorFactory in project neo4j by neo4j.

the class ParallelRelationshipCursorTransactionStateTestBase method parallelTxStateScanStressTest.

@Test
void parallelTxStateScanStressTest() throws InterruptedException, KernelException {
    LongSet existingRelationships = createRelationships(77);
    int workers = Runtime.getRuntime().availableProcessors();
    ExecutorService threadPool = Executors.newFixedThreadPool(workers);
    CursorFactory cursors = testSupport.kernelToTest().cursors();
    ThreadLocalRandom random = ThreadLocalRandom.current();
    try {
        for (int i = 0; i < 1000; i++) {
            MutableLongSet allRels = LongSets.mutable.withAll(existingRelationships);
            try (KernelTransaction tx = beginTransaction()) {
                int relationshipsInTx = random.nextInt(100);
                Write write = tx.dataWrite();
                int type = tx.tokenWrite().relationshipTypeGetOrCreateForName("R");
                for (int j = 0; j < relationshipsInTx; j++) {
                    allRels.add(write.relationshipCreate(write.nodeCreate(), type, write.nodeCreate()));
                }
                Scan<RelationshipScanCursor> scan = tx.dataRead().allRelationshipsScan();
                List<Future<LongList>> futures = new ArrayList<>(workers);
                for (int j = 0; j < workers; j++) {
                    futures.add(threadPool.submit(randomBatchWorker(scan, () -> cursors.allocateRelationshipScanCursor(NULL), REL_GET)));
                }
                List<LongList> lists = futures.stream().map(TestUtils::unsafeGet).collect(Collectors.toList());
                TestUtils.assertDistinct(lists);
                LongList concat = TestUtils.concat(lists);
                assertEquals(allRels, LongSets.immutable.withAll(concat), format("relationships=%d, seen=%d, all=%d", relationshipsInTx, concat.size(), allRels.size()));
                assertEquals(allRels.size(), concat.size(), format("relationships=%d", relationshipsInTx));
                tx.rollback();
            }
        }
    } finally {
        threadPool.shutdown();
        threadPool.awaitTermination(1, TimeUnit.MINUTES);
    }
}
Also used : Write(org.neo4j.internal.kernel.api.Write) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) MutableLongSet(org.eclipse.collections.api.set.primitive.MutableLongSet) LongSet(org.eclipse.collections.api.set.primitive.LongSet) ArrayList(java.util.ArrayList) LongArrayList(org.eclipse.collections.impl.list.mutable.primitive.LongArrayList) LongList(org.eclipse.collections.api.list.primitive.LongList) CursorFactory(org.neo4j.internal.kernel.api.CursorFactory) MutableLongSet(org.eclipse.collections.api.set.primitive.MutableLongSet) RelationshipScanCursor(org.neo4j.internal.kernel.api.RelationshipScanCursor) ExecutorService(java.util.concurrent.ExecutorService) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Future(java.util.concurrent.Future) Test(org.junit.jupiter.api.Test)

Example 10 with CursorFactory

use of org.neo4j.internal.kernel.api.CursorFactory in project neo4j by neo4j.

the class ParallelNodeCursorTestBase method shouldScanAllNodesFromMultipleThreads.

@Test
void shouldScanAllNodesFromMultipleThreads() throws InterruptedException, ExecutionException {
    // given
    ExecutorService service = Executors.newFixedThreadPool(4);
    Scan<NodeCursor> scan = read.allNodesScan();
    CursorFactory cursors = testSupport.kernelToTest().cursors();
    try {
        // when
        Future<LongList> future1 = service.submit(singleBatchWorker(scan, () -> cursors.allocateNodeCursor(NULL), NodeCursor::nodeReference, 32));
        Future<LongList> future2 = service.submit(singleBatchWorker(scan, () -> cursors.allocateNodeCursor(NULL), NodeCursor::nodeReference, 32));
        Future<LongList> future3 = service.submit(singleBatchWorker(scan, () -> cursors.allocateNodeCursor(NULL), NodeCursor::nodeReference, 32));
        Future<LongList> future4 = service.submit(singleBatchWorker(scan, () -> cursors.allocateNodeCursor(NULL), NodeCursor::nodeReference, 32));
        // then
        LongList ids1 = future1.get();
        LongList ids2 = future2.get();
        LongList ids3 = future3.get();
        LongList ids4 = future4.get();
        TestUtils.assertDistinct(ids1, ids2, ids3, ids4);
        LongList concat = TestUtils.concat(ids1, ids2, ids3, ids4).toSortedList();
        assertEquals(NODE_IDS, concat);
    } finally {
        service.shutdown();
        service.awaitTermination(1, TimeUnit.MINUTES);
    }
}
Also used : CursorFactory(org.neo4j.internal.kernel.api.CursorFactory) ExecutorService(java.util.concurrent.ExecutorService) NodeCursor(org.neo4j.internal.kernel.api.NodeCursor) LongList(org.eclipse.collections.api.list.primitive.LongList) MutableLongList(org.eclipse.collections.api.list.primitive.MutableLongList) Test(org.junit.jupiter.api.Test)

Aggregations

CursorFactory (org.neo4j.internal.kernel.api.CursorFactory)22 Test (org.junit.jupiter.api.Test)21 ExecutorService (java.util.concurrent.ExecutorService)19 LongList (org.eclipse.collections.api.list.primitive.LongList)19 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)13 MutableLongList (org.eclipse.collections.api.list.primitive.MutableLongList)11 Read (org.neo4j.internal.kernel.api.Read)11 LongArrayList (org.eclipse.collections.impl.list.mutable.primitive.LongArrayList)10 NodeCursor (org.neo4j.internal.kernel.api.NodeCursor)10 ArrayList (java.util.ArrayList)9 Future (java.util.concurrent.Future)9 Write (org.neo4j.internal.kernel.api.Write)9 RelationshipScanCursor (org.neo4j.internal.kernel.api.RelationshipScanCursor)8 NodeLabelIndexCursor (org.neo4j.internal.kernel.api.NodeLabelIndexCursor)5 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)3 MutableLongSet (org.eclipse.collections.api.set.primitive.MutableLongSet)3 LongSet (org.eclipse.collections.api.set.primitive.LongSet)2 TokenWrite (org.neo4j.internal.kernel.api.TokenWrite)2 Degrees (org.neo4j.storageengine.api.Degrees)2 PropertyCursor (org.neo4j.internal.kernel.api.PropertyCursor)1