Search in sources :

Example 1 with WorkerCommand

use of org.neo4j.test.OtherThreadExecutor.WorkerCommand in project neo4j by neo4j.

the class TicketedProcessingTest method shouldReturnTicketsInOrder.

@Test
public void shouldReturnTicketsInOrder() throws Exception {
    // GIVEN
    int items = 1_000;
    ParkStrategy park = new ParkStrategy.Park(2, MILLISECONDS);
    BiFunction<Integer, Void, Integer> processor = (from, ignore) -> {
        if (ThreadLocalRandom.current().nextFloat() < 0.01f) {
            park.park(Thread.currentThread());
        }
        return from * 2;
    };
    int processorCount = Runtime.getRuntime().availableProcessors();
    Future<Void> assertions;
    try (TicketedProcessing<Integer, Void, Integer> processing = new TicketedProcessing<>("Doubler", processorCount, processor, () -> null)) {
        processing.processors(processorCount - processing.processors(0));
        // WHEN
        assertions = t2.execute(new WorkerCommand<Void, Void>() {

            @Override
            public Void doWork(Void state) throws Exception {
                for (int i = 0; i < items; i++) {
                    Integer next = processing.next();
                    assertNotNull(next);
                    assertEquals(i * 2, next.intValue());
                }
                assertNull(processing.next());
                return null;
            }
        });
        for (int i = 0; i < items; i++) {
            processing.submit(i);
        }
    }
    // THEN
    assertions.get();
}
Also used : Assert.assertNotNull(org.junit.Assert.assertNotNull) BiFunction(java.util.function.BiFunction) OtherThreadRule(org.neo4j.test.rule.concurrent.OtherThreadRule) Test(org.junit.Test) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Integer.parseInt(java.lang.Integer.parseInt) WorkerCommand(org.neo4j.test.OtherThreadExecutor.WorkerCommand) CountDownLatch(java.util.concurrent.CountDownLatch) Future(java.util.concurrent.Future) Rule(org.junit.Rule) Assert.assertNull(org.junit.Assert.assertNull) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) DoubleLatch.awaitLatch(org.neo4j.test.DoubleLatch.awaitLatch) ParkStrategy(org.neo4j.unsafe.impl.batchimport.executor.ParkStrategy) Assert.assertEquals(org.junit.Assert.assertEquals) WorkerCommand(org.neo4j.test.OtherThreadExecutor.WorkerCommand) ParkStrategy(org.neo4j.unsafe.impl.batchimport.executor.ParkStrategy) Test(org.junit.Test)

Example 2 with WorkerCommand

use of org.neo4j.test.OtherThreadExecutor.WorkerCommand in project neo4j by neo4j.

the class PropertyConstraintsStressIT method performInserts.

/**
     * Inserts a bunch of new nodes with the label and property key currently set in the fields in this class, where
     * running this method twice will insert nodes with duplicate property values, assuming property key or label has
     * not changed.
     */
private WorkerCommand<Object, Integer> performInserts(final HighlyAvailableGraphDatabase slave, final boolean constraintCompliant) {
    return new WorkerCommand<Object, Integer>() {

        @Override
        public Integer doWork(Object state) throws Exception {
            int i = 0;
            try {
                for (; i < 100; i++) {
                    try (Transaction tx = slave.beginTx()) {
                        constraintOps.createEntity(slave, labelOrRelType, property, "value" + i, constraintCompliant);
                        tx.success();
                    }
                }
            } catch (TransactionFailureException | TransientTransactionFailureException e) {
            // Swallowed on purpose, we except it to fail sometimes due to either
            //  - constraint violation on master
            //  - concurrent schema operation on master
            } catch (ConstraintViolationException e) {
            // Constraint violation detected on slave while building transaction
            } catch (ComException e) {
            // Happens sometimes, cause:
            // - The lock session requested to start is already in use.
            //   Please retry your request in a few seconds.
            }
            return i;
        }
    };
}
Also used : ComException(org.neo4j.com.ComException) WorkerCommand(org.neo4j.test.OtherThreadExecutor.WorkerCommand) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) TransientTransactionFailureException(org.neo4j.graphdb.TransientTransactionFailureException) Transaction(org.neo4j.graphdb.Transaction) ConstraintViolationException(org.neo4j.graphdb.ConstraintViolationException) TransientTransactionFailureException(org.neo4j.graphdb.TransientTransactionFailureException)

Example 3 with WorkerCommand

use of org.neo4j.test.OtherThreadExecutor.WorkerCommand in project neo4j by neo4j.

the class PropertyConstraintsStressIT method createPropertyExistenceConstraintCommand.

private static WorkerCommand<Object, Boolean> createPropertyExistenceConstraintCommand(final GraphDatabaseService db, final String query) {
    return new WorkerCommand<Object, Boolean>() {

        @Override
        public Boolean doWork(Object state) throws Exception {
            boolean constraintCreationFailed = false;
            try (Transaction tx = db.beginTx()) {
                db.execute(query);
                tx.success();
            } catch (QueryExecutionException e) {
                System.out.println("Constraint failed: " + e.getMessage());
                if (Exceptions.rootCause(e) instanceof ConstraintValidationException) {
                    // Unable to create constraint since it is not consistent with existing data
                    constraintCreationFailed = true;
                } else {
                    throw e;
                }
            }
            if (!constraintCreationFailed) {
                System.out.println("Constraint created: " + query);
            }
            return constraintCreationFailed;
        }
    };
}
Also used : WorkerCommand(org.neo4j.test.OtherThreadExecutor.WorkerCommand) QueryExecutionException(org.neo4j.graphdb.QueryExecutionException) Transaction(org.neo4j.graphdb.Transaction) ConstraintValidationException(org.neo4j.kernel.api.exceptions.schema.ConstraintValidationException)

Aggregations

WorkerCommand (org.neo4j.test.OtherThreadExecutor.WorkerCommand)3 Transaction (org.neo4j.graphdb.Transaction)2 Integer.parseInt (java.lang.Integer.parseInt)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Future (java.util.concurrent.Future)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1 MILLISECONDS (java.util.concurrent.TimeUnit.MILLISECONDS)1 BiFunction (java.util.function.BiFunction)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.assertNotNull (org.junit.Assert.assertNotNull)1 Assert.assertNull (org.junit.Assert.assertNull)1 Rule (org.junit.Rule)1 Test (org.junit.Test)1 ComException (org.neo4j.com.ComException)1 ConstraintViolationException (org.neo4j.graphdb.ConstraintViolationException)1 QueryExecutionException (org.neo4j.graphdb.QueryExecutionException)1 TransactionFailureException (org.neo4j.graphdb.TransactionFailureException)1 TransientTransactionFailureException (org.neo4j.graphdb.TransientTransactionFailureException)1 ConstraintValidationException (org.neo4j.kernel.api.exceptions.schema.ConstraintValidationException)1 DoubleLatch.awaitLatch (org.neo4j.test.DoubleLatch.awaitLatch)1