Search in sources :

Example 11 with BinaryLatch

use of org.neo4j.util.concurrent.BinaryLatch in project neo4j by neo4j.

the class LabelsAcceptanceTest method shouldListAllRelationshipTypesInUseEvenWhenExclusiveRelationshipTypeLocksAreTaken.

@Test
void shouldListAllRelationshipTypesInUseEvenWhenExclusiveRelationshipTypeLocksAreTaken() {
    assertTimeoutPreemptively(Duration.ofSeconds(30), () -> {
        // Given
        RelationshipType relType = RelationshipType.withName("REL");
        Node node = createNode(db, Labels.MY_LABEL);
        try (Transaction tx = db.beginTx()) {
            tx.getNodeById(node.getId()).createRelationshipTo(node, relType).setProperty("prop", "val");
            tx.commit();
        }
        BinaryLatch indexCreateStarted = new BinaryLatch();
        BinaryLatch indexCreateAllowToFinish = new BinaryLatch();
        Thread indexCreator = new Thread(() -> {
            try (Transaction tx = db.beginTx()) {
                tx.execute("CREATE FULLTEXT INDEX myIndex FOR ()-[r:REL]-() ON EACH [r.prop]").close();
                indexCreateStarted.release();
                indexCreateAllowToFinish.await();
                tx.commit();
            }
        });
        indexCreator.start();
        // When
        indexCreateStarted.await();
        List<RelationshipType> relTypes;
        try (Transaction transaction = db.beginTx()) {
            relTypes = asList(transaction.getAllRelationshipTypesInUse());
        }
        indexCreateAllowToFinish.release();
        indexCreator.join();
        // Then
        assertEquals(1, relTypes.size());
        assertThat(map(RelationshipType::name, relTypes)).contains(relType.name());
    });
}
Also used : InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) BinaryLatch(org.neo4j.util.concurrent.BinaryLatch) Test(org.junit.jupiter.api.Test)

Example 12 with BinaryLatch

use of org.neo4j.util.concurrent.BinaryLatch in project neo4j by neo4j.

the class EventuallyConsistentFulltextProceduresTest method fulltextIndexesMustBeEventuallyConsistentByDefaultWhenThisIsConfigured.

@Test
void fulltextIndexesMustBeEventuallyConsistentByDefaultWhenThisIsConfigured() throws InterruptedException {
    try (Transaction tx = db.beginTx()) {
        tx.execute(format(NODE_CREATE, DEFAULT_NODE_IDX_NAME, asStrList(LABEL.name()), asStrList(PROP, "otherprop")));
        tx.execute(format(RELATIONSHIP_CREATE, DEFAULT_REL_IDX_NAME, asStrList(REL.name()), asStrList(PROP)));
        tx.commit();
    }
    awaitIndexesOnline();
    // Prevent index updates from being applied to eventually consistent indexes.
    BinaryLatch indexUpdateBlocker = new BinaryLatch();
    db.getDependencyResolver().resolveDependency(JobScheduler.class).schedule(Group.INDEX_UPDATING, NOT_MONITORED, indexUpdateBlocker::await);
    LongHashSet nodeIds = new LongHashSet();
    long relId;
    try {
        try (Transaction tx = db.beginTx()) {
            Node node1 = tx.createNode(LABEL);
            node1.setProperty(PROP, "bla bla");
            Node node2 = tx.createNode(LABEL);
            node2.setProperty("otherprop", "bla bla");
            Relationship relationship = node1.createRelationshipTo(node2, REL);
            relationship.setProperty(PROP, "bla bla");
            nodeIds.add(node1.getId());
            nodeIds.add(node2.getId());
            relId = relationship.getId();
            tx.commit();
        }
        // Index updates are still blocked for eventually consistent indexes, so we should not find anything at this point.
        assertQueryFindsIdsInOrder(db, true, DEFAULT_NODE_IDX_NAME, "bla");
        assertQueryFindsIdsInOrder(db, false, DEFAULT_REL_IDX_NAME, "bla");
    } finally {
        // Uncork the eventually consistent fulltext index updates.
        Thread.sleep(10);
        indexUpdateBlocker.release();
    }
    // And wait for them to apply.
    try (Transaction transaction = db.beginTx()) {
        transaction.execute(AWAIT_REFRESH).close();
        transaction.commit();
    }
    // Now we should see our data.
    assertQueryFindsIds(db, true, DEFAULT_NODE_IDX_NAME, "bla", nodeIds);
    assertQueryFindsIds(db, false, DEFAULT_REL_IDX_NAME, "bla", newSetWith(relId));
}
Also used : JobScheduler(org.neo4j.scheduler.JobScheduler) LongHashSet(org.eclipse.collections.impl.set.mutable.primitive.LongHashSet) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) BinaryLatch(org.neo4j.util.concurrent.BinaryLatch) Test(org.junit.jupiter.api.Test)

Example 13 with BinaryLatch

use of org.neo4j.util.concurrent.BinaryLatch in project neo4j by neo4j.

the class TimeBasedTaskSchedulerTest method longRunningTasksMustNotDelayExecutionOfOtherTasks.

@Test
void longRunningTasksMustNotDelayExecutionOfOtherTasks() throws Exception {
    BinaryLatch latch = new BinaryLatch();
    Runnable longRunning = latch::await;
    Runnable shortRunning = semaphore::release;
    scheduler.submit(Group.STORAGE_MAINTENANCE, longRunning, 100, 100);
    scheduler.submit(Group.STORAGE_MAINTENANCE, shortRunning, 100, 100);
    for (int i = 0; i < 4; i++) {
        clock.forward(100, TimeUnit.NANOSECONDS);
        scheduler.tick();
        assertSemaphoreAcquire();
    }
    latch.release();
}
Also used : BinaryLatch(org.neo4j.util.concurrent.BinaryLatch) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Example 14 with BinaryLatch

use of org.neo4j.util.concurrent.BinaryLatch in project neo4j by neo4j.

the class CentralJobSchedulerTest method shutDownMustKillCancelledJobs.

@Test
@Timeout(value = 60)
void shutDownMustKillCancelledJobs() {
    life.start();
    BinaryLatch startLatch = new BinaryLatch();
    BinaryLatch stopLatch = new BinaryLatch();
    scheduler.schedule(Group.INDEX_POPULATION, NOT_MONITORED, () -> {
        try {
            startLatch.release();
            Thread.sleep(100_000);
        } catch (InterruptedException e) {
            stopLatch.release();
            throw new RuntimeException(e);
        }
    });
    startLatch.await();
    scheduler.shutdown();
    stopLatch.await();
}
Also used : BinaryLatch(org.neo4j.util.concurrent.BinaryLatch) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 15 with BinaryLatch

use of org.neo4j.util.concurrent.BinaryLatch in project neo4j by neo4j.

the class CentralJobSchedulerTest method scheduledTasksThatThrowsShouldStop.

@Test
@Timeout(value = 60)
void scheduledTasksThatThrowsShouldStop() {
    life.start();
    BinaryLatch triggerLatch = new BinaryLatch();
    AtomicBoolean canceled = new AtomicBoolean();
    RuntimeException boom = new RuntimeException("boom");
    AtomicInteger triggerCounter = new AtomicInteger();
    Runnable job = () -> {
        try {
            triggerCounter.incrementAndGet();
            throw boom;
        } finally {
            triggerLatch.release();
        }
    };
    JobHandle<?> jobHandle = scheduler.scheduleRecurring(Group.INDEX_POPULATION, NOT_MONITORED, job, 1, MILLISECONDS);
    jobHandle.registerCancelListener(() -> canceled.set(true));
    triggerLatch.await();
    assertThat(triggerCounter.get()).isGreaterThanOrEqualTo(1);
    assertFalse(canceled.get());
    jobHandle.cancel();
    assertTrue(canceled.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BinaryLatch(org.neo4j.util.concurrent.BinaryLatch) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Aggregations

BinaryLatch (org.neo4j.util.concurrent.BinaryLatch)31 Test (org.junit.jupiter.api.Test)21 RepeatedTest (org.junit.jupiter.api.RepeatedTest)5 Transaction (org.neo4j.graphdb.Transaction)5 ExecutorService (java.util.concurrent.ExecutorService)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 Node (org.neo4j.graphdb.Node)4 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)4 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 Timeout (org.junit.jupiter.api.Timeout)3 ValueSource (org.junit.jupiter.params.provider.ValueSource)3 InternalTransaction (org.neo4j.kernel.impl.coreapi.InternalTransaction)3 Flushable (java.io.Flushable)2 Path (java.nio.file.Path)2 ArrayList (java.util.ArrayList)2 Future (java.util.concurrent.Future)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 RelationshipType (org.neo4j.graphdb.RelationshipType)2