Search in sources :

Example 61 with NodeCursor

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

the class ParallelNodeCursorTestBase method shouldScanAllNodesFromMultipleThreadWithBigSizeHints.

@Test
void shouldScanAllNodesFromMultipleThreadWithBigSizeHints() throws InterruptedException, ExecutionException {
    // given
    ExecutorService service = Executors.newFixedThreadPool(4);
    Scan<NodeCursor> scan = read.allNodesScan();
    CursorFactory cursors = testSupport.kernelToTest().cursors();
    try {
        // when
        Supplier<NodeCursor> allocateNodeCursor = () -> cursors.allocateNodeCursor(NULL);
        Future<LongList> future1 = service.submit(singleBatchWorker(scan, allocateNodeCursor, NODE_GET, 100));
        Future<LongList> future2 = service.submit(singleBatchWorker(scan, allocateNodeCursor, NODE_GET, 100));
        Future<LongList> future3 = service.submit(singleBatchWorker(scan, allocateNodeCursor, NODE_GET, 100));
        Future<LongList> future4 = service.submit(singleBatchWorker(scan, allocateNodeCursor, NODE_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).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)

Example 62 with NodeCursor

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

the class ParallelNodeCursorTransactionStateTestBase method shouldReserveBatchFromTxState.

@Test
void shouldReserveBatchFromTxState() throws TransactionFailureException, InvalidTransactionTypeKernelException {
    try (KernelTransaction tx = beginTransaction()) {
        for (int i = 0; i < 11; i++) {
            tx.dataWrite().nodeCreate();
        }
        try (NodeCursor cursor = tx.cursors().allocateNodeCursor(NULL)) {
            Scan<NodeCursor> scan = tx.dataRead().allNodesScan();
            assertTrue(scan.reserveBatch(cursor, 5));
            assertEquals(5, count(cursor));
            assertTrue(scan.reserveBatch(cursor, 4));
            assertEquals(4, count(cursor));
            assertTrue(scan.reserveBatch(cursor, 6));
            assertEquals(2, count(cursor));
            // now we should have fetched all nodes
            while (scan.reserveBatch(cursor, 3)) {
                assertFalse(cursor.next());
            }
        }
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) NodeCursor(org.neo4j.internal.kernel.api.NodeCursor) Test(org.junit.jupiter.api.Test)

Example 63 with NodeCursor

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

the class ParallelNodeCursorTransactionStateTestBase method parallelTxStateScanStressTest.

@Test
void parallelTxStateScanStressTest() throws InvalidTransactionTypeKernelException, TransactionFailureException, InterruptedException {
    LongSet existingNodes = createNodes(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 allNodes = LongSets.mutable.withAll(existingNodes);
            try (KernelTransaction tx = beginTransaction()) {
                int nodeInTx = random.nextInt(100);
                for (int j = 0; j < nodeInTx; j++) {
                    allNodes.add(tx.dataWrite().nodeCreate());
                }
                Scan<NodeCursor> scan = tx.dataRead().allNodesScan();
                List<Future<LongList>> futures = new ArrayList<>(workers);
                for (int j = 0; j < workers; j++) {
                    futures.add(threadPool.submit(randomBatchWorker(scan, () -> cursors.allocateNodeCursor(NULL), NODE_GET)));
                }
                List<LongList> lists = futures.stream().map(TestUtils::unsafeGet).collect(Collectors.toList());
                TestUtils.assertDistinct(lists);
                LongList concat = TestUtils.concat(lists);
                assertEquals(allNodes, LongSets.immutable.withAll(concat), format("nodes=%d, seen=%d, all=%d", nodeInTx, concat.size(), allNodes.size()));
                assertEquals(allNodes.size(), concat.size(), format("nodes=%d", nodeInTx));
                tx.rollback();
            }
        }
    } finally {
        threadPool.shutdown();
        threadPool.awaitTermination(1, TimeUnit.MINUTES);
    }
}
Also used : 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) NodeCursor(org.neo4j.internal.kernel.api.NodeCursor) LongList(org.eclipse.collections.api.list.primitive.LongList) CursorFactory(org.neo4j.internal.kernel.api.CursorFactory) MutableLongSet(org.eclipse.collections.api.set.primitive.MutableLongSet) ExecutorService(java.util.concurrent.ExecutorService) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Future(java.util.concurrent.Future) Test(org.junit.jupiter.api.Test)

Example 64 with NodeCursor

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

the class ParallelNodeCursorTransactionStateTestBase method scanShouldNotSeeDeletedNode.

@Test
void scanShouldNotSeeDeletedNode() throws Exception {
    int size = 100;
    MutableLongSet created = LongSets.mutable.empty();
    MutableLongSet deleted = LongSets.mutable.empty();
    try (KernelTransaction tx = beginTransaction()) {
        Write write = tx.dataWrite();
        for (int i = 0; i < size; i++) {
            created.add(write.nodeCreate());
            deleted.add(write.nodeCreate());
        }
        tx.commit();
    }
    try (KernelTransaction tx = beginTransaction()) {
        deleted.each(new CheckedLongProcedure() {

            @Override
            public void safeValue(long item) throws Exception {
                tx.dataWrite().nodeDelete(item);
            }
        });
        try (NodeCursor cursor = tx.cursors().allocateNodeCursor(NULL)) {
            Scan<NodeCursor> scan = tx.dataRead().allNodesScan();
            MutableLongSet seen = LongSets.mutable.empty();
            while (scan.reserveBatch(cursor, 17)) {
                while (cursor.next()) {
                    long nodeId = cursor.nodeReference();
                    assertTrue(seen.add(nodeId));
                    assertTrue(created.remove(nodeId));
                }
            }
            assertTrue(created.isEmpty());
        }
    }
}
Also used : Write(org.neo4j.internal.kernel.api.Write) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) MutableLongSet(org.eclipse.collections.api.set.primitive.MutableLongSet) CheckedLongProcedure(org.eclipse.collections.impl.block.procedure.checked.primitive.CheckedLongProcedure) NodeCursor(org.neo4j.internal.kernel.api.NodeCursor) InvalidTransactionTypeKernelException(org.neo4j.internal.kernel.api.exceptions.InvalidTransactionTypeKernelException) ExecutionException(java.util.concurrent.ExecutionException) TransactionFailureException(org.neo4j.internal.kernel.api.exceptions.TransactionFailureException) Test(org.junit.jupiter.api.Test)

Example 65 with NodeCursor

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

the class ParallelNodeCursorTransactionStateTestBase method shouldScanAllNodesFromMultipleThreadWithBigSizeHints.

@Test
void shouldScanAllNodesFromMultipleThreadWithBigSizeHints() throws InterruptedException, ExecutionException, TransactionFailureException, InvalidTransactionTypeKernelException {
    // 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();
        for (int i = 0; i < size; i++) {
            ids.add(write.nodeCreate());
        }
        org.neo4j.internal.kernel.api.Read read = tx.dataRead();
        Scan<NodeCursor> scan = read.allNodesScan();
        // when
        Supplier<NodeCursor> allocateCursor = () -> cursors.allocateNodeCursor(NULL);
        Future<LongList> future1 = service.submit(singleBatchWorker(scan, allocateCursor, NODE_GET, 100));
        Future<LongList> future2 = service.submit(singleBatchWorker(scan, allocateCursor, NODE_GET, 100));
        Future<LongList> future3 = service.submit(singleBatchWorker(scan, allocateCursor, NODE_GET, 100));
        Future<LongList> future4 = service.submit(singleBatchWorker(scan, allocateCursor, NODE_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) NodeCursor(org.neo4j.internal.kernel.api.NodeCursor) LongList(org.eclipse.collections.api.list.primitive.LongList) CursorFactory(org.neo4j.internal.kernel.api.CursorFactory) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.jupiter.api.Test)

Aggregations

NodeCursor (org.neo4j.internal.kernel.api.NodeCursor)113 Test (org.junit.jupiter.api.Test)85 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)62 PropertyCursor (org.neo4j.internal.kernel.api.PropertyCursor)30 RelationshipTraversalCursor (org.neo4j.internal.kernel.api.RelationshipTraversalCursor)26 Read (org.neo4j.internal.kernel.api.Read)20 Write (org.neo4j.internal.kernel.api.Write)17 Degrees (org.neo4j.storageengine.api.Degrees)11 CursorFactory (org.neo4j.internal.kernel.api.CursorFactory)10 ArrayList (java.util.ArrayList)8 ExecutorService (java.util.concurrent.ExecutorService)7 LongList (org.eclipse.collections.api.list.primitive.LongList)7 LongArrayList (org.eclipse.collections.impl.list.mutable.primitive.LongArrayList)7 CachingExpandInto (org.neo4j.internal.kernel.api.helpers.CachingExpandInto)6 TokenWrite (org.neo4j.internal.kernel.api.TokenWrite)5 RelationshipSelection (org.neo4j.storageengine.api.RelationshipSelection)5 MutableLongSet (org.eclipse.collections.api.set.primitive.MutableLongSet)4 Future (java.util.concurrent.Future)3 MutableLongList (org.eclipse.collections.api.list.primitive.MutableLongList)3 NotFoundException (org.neo4j.graphdb.NotFoundException)3