Search in sources :

Example 1 with BinaryLatch

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

the class CheckPointerImplTest method tryCheckPointMustWaitForOnGoingCheckPointsToCompleteAsLongAsTimeoutPredicateIsFalse.

@Test
void tryCheckPointMustWaitForOnGoingCheckPointsToCompleteAsLongAsTimeoutPredicateIsFalse() throws Exception {
    mockTxIdStore();
    CheckPointerImpl checkPointer = checkPointer();
    BinaryLatch arriveFlushAndForce = new BinaryLatch();
    BinaryLatch finishFlushAndForce = new BinaryLatch();
    doAnswer(invocation -> {
        arriveFlushAndForce.release();
        finishFlushAndForce.await();
        return null;
    }).when(forceOperation).flushAndForce(any());
    Thread forceCheckPointThread = new Thread(() -> {
        try {
            checkPointer.forceCheckPoint(INFO);
        } catch (Throwable e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    });
    forceCheckPointThread.start();
    // Wait for force-thread to arrive in flushAndForce().
    arriveFlushAndForce.await();
    BooleanSupplier predicate = mock(BooleanSupplier.class);
    when(predicate.getAsBoolean()).thenReturn(false, false, true);
    // We decided to not wait for the on-going check point to finish.
    assertThat(checkPointer.tryCheckPoint(INFO, predicate)).isEqualTo(-1L);
    // Let the flushAndForce complete.
    finishFlushAndForce.release();
    forceCheckPointThread.join();
    assertThat(checkPointer.tryCheckPoint(INFO, predicate)).isEqualTo(this.transactionId);
}
Also used : BooleanSupplier(java.util.function.BooleanSupplier) BinaryLatch(org.neo4j.util.concurrent.BinaryLatch) Test(org.junit.jupiter.api.Test)

Example 2 with BinaryLatch

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

the class CentralJobSchedulerTest method shouldListActiveGroups.

@Test
void shouldListActiveGroups() {
    life.start();
    assertEquals(List.of(), scheduler.activeGroups().map(ag -> ag.group).collect(toList()));
    BinaryLatch firstLatch = new BinaryLatch();
    scheduler.schedule(Group.CHECKPOINT, NOT_MONITORED, firstLatch::release);
    firstLatch.await();
    assertEquals(List.of(Group.CHECKPOINT), scheduler.activeGroups().map(ag -> ag.group).collect(toList()));
}
Also used : BinaryLatch(org.neo4j.util.concurrent.BinaryLatch) Test(org.junit.jupiter.api.Test)

Example 3 with BinaryLatch

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

the class CentralJobSchedulerTest method longRunningScheduledJobsMustNotDelayOtherLongRunningJobs.

@Test
void longRunningScheduledJobsMustNotDelayOtherLongRunningJobs() {
    life.start();
    List<JobHandle<?>> handles = new ArrayList<>(30);
    AtomicLong startedCounter = new AtomicLong();
    BinaryLatch blockLatch = new BinaryLatch();
    Runnable task = () -> {
        startedCounter.incrementAndGet();
        blockLatch.await();
    };
    for (int i = 0; i < 10; i++) {
        handles.add(scheduler.schedule(Group.INDEX_POPULATION, NOT_MONITORED, task, 0, TimeUnit.MILLISECONDS));
    }
    for (int i = 0; i < 10; i++) {
        handles.add(scheduler.scheduleRecurring(Group.INDEX_POPULATION, NOT_MONITORED, task, Integer.MAX_VALUE, TimeUnit.MILLISECONDS));
    }
    for (int i = 0; i < 10; i++) {
        handles.add(scheduler.scheduleRecurring(Group.INDEX_POPULATION, NOT_MONITORED, task, 0, Integer.MAX_VALUE, TimeUnit.MILLISECONDS));
    }
    long deadline = TimeUnit.SECONDS.toNanos(10) + System.nanoTime();
    do {
        if (startedCounter.get() == handles.size()) {
            // All jobs got started. We're good!
            blockLatch.release();
            for (JobHandle<?> handle : handles) {
                handle.cancel();
            }
            return;
        }
    } while (System.nanoTime() < deadline);
    fail("Only managed to start " + startedCounter.get() + " tasks in 10 seconds, when " + handles.size() + " was expected.");
}
Also used : JobHandle(org.neo4j.scheduler.JobHandle) AtomicLong(java.util.concurrent.atomic.AtomicLong) ArrayList(java.util.ArrayList) BinaryLatch(org.neo4j.util.concurrent.BinaryLatch) Test(org.junit.jupiter.api.Test)

Example 4 with BinaryLatch

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

the class FulltextProceduresTest method concurrentPopulationAndUpdatesToAnEventuallyConsistentIndexMustEventuallyResultInCorrectIndexState.

@Test
void concurrentPopulationAndUpdatesToAnEventuallyConsistentIndexMustEventuallyResultInCorrectIndexState() throws Exception {
    String oldValue = "red";
    String newValue = "green";
    // First we create the nodes and relationships with the property value "red".
    LongHashSet nodeIds = new LongHashSet();
    LongHashSet relIds = new LongHashSet();
    generateNodesAndRelationshipsWithProperty(200, nodeIds, relIds, oldValue);
    // Then, in two concurrent transactions, we create our indexes AND change all the property values to "green".
    CountDownLatch readyLatch = new CountDownLatch(2);
    BinaryLatch startLatch = new BinaryLatch();
    Runnable createIndexes = () -> {
        readyLatch.countDown();
        startLatch.await();
        try (Transaction tx = db.beginTx()) {
            tx.execute(format(NODE_CREATE, DEFAULT_NODE_IDX_NAME, asStrList(LABEL.name()), asStrList(PROP) + EVENTUALLY_CONSISTENT));
            tx.execute(format(RELATIONSHIP_CREATE, DEFAULT_REL_IDX_NAME, asStrList(REL.name()), asStrList(PROP) + EVENTUALLY_CONSISTENT));
            tx.commit();
        }
    };
    Runnable makeAllEntitiesGreen = () -> {
        try (Transaction tx = db.beginTx()) {
            // Prepare our transaction state first.
            nodeIds.forEach(nodeId -> tx.getNodeById(nodeId).setProperty(PROP, newValue));
            relIds.forEach(relId -> tx.getRelationshipById(relId).setProperty(PROP, newValue));
            // Okay, NOW we're ready to race!
            readyLatch.countDown();
            startLatch.await();
            tx.commit();
        }
    };
    ExecutorService executor = Executors.newFixedThreadPool(2);
    Future<?> future1 = executor.submit(createIndexes);
    Future<?> future2 = executor.submit(makeAllEntitiesGreen);
    readyLatch.await();
    startLatch.release();
    // Finally, when everything has settled down, we should see that all of the nodes and relationships are indexed with the value "green".
    try {
        future1.get();
        future2.get();
        awaitIndexesOnline();
        try (Transaction tx = db.beginTx()) {
            tx.execute(AWAIT_REFRESH).close();
        }
        assertQueryFindsIds(db, true, DEFAULT_NODE_IDX_NAME, newValue, nodeIds);
        assertQueryFindsIds(db, false, DEFAULT_REL_IDX_NAME, newValue, relIds);
    } finally {
        IOUtils.closeAllSilently(executor::shutdown);
    }
}
Also used : Arrays(java.util.Arrays) ResourceIterator(org.neo4j.graphdb.ResourceIterator) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Config(org.neo4j.configuration.Config) Value(org.neo4j.values.storable.Value) FulltextIndexProviderFactory(org.neo4j.kernel.impl.index.schema.FulltextIndexProviderFactory) NullLogProvider(org.neo4j.logging.NullLogProvider) DatabaseLayout(org.neo4j.io.layout.DatabaseLayout) Future(java.util.concurrent.Future) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) LongHashSet(org.eclipse.collections.impl.set.mutable.primitive.LongHashSet) FulltextProcedures(org.neo4j.procedure.builtin.FulltextProcedures) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) IndexSettingImpl(org.neo4j.graphdb.schema.IndexSettingImpl) ThreadTestUtils(org.neo4j.test.ThreadTestUtils) Transaction(org.neo4j.graphdb.Transaction) BinaryLatch(org.neo4j.util.concurrent.BinaryLatch) MethodSource(org.junit.jupiter.params.provider.MethodSource) QueryExecutionException(org.neo4j.graphdb.QueryExecutionException) Result(org.neo4j.graphdb.Result) RepeatedPropertyInSchemaException(org.neo4j.kernel.api.exceptions.schema.RepeatedPropertyInSchemaException) Set(java.util.Set) DROP(org.neo4j.kernel.api.impl.fulltext.FulltextIndexProceduresUtil.DROP) Arguments(org.junit.jupiter.params.provider.Arguments) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) String.format(java.lang.String.format) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Stream(java.util.stream.Stream) RepeatedLabelInSchemaException(org.neo4j.kernel.api.exceptions.schema.RepeatedLabelInSchemaException) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) RelationshipType(org.neo4j.graphdb.RelationshipType) LongHashSet.newSetWith(org.eclipse.collections.impl.set.mutable.primitive.LongHashSet.newSetWith) ConsistencyCheckService(org.neo4j.consistency.ConsistencyCheckService) SHOW_FULLTEXT_INDEXES(org.neo4j.kernel.api.impl.fulltext.FulltextIndexProceduresUtil.SHOW_FULLTEXT_INDEXES) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) RELATIONSHIP_CREATE(org.neo4j.kernel.api.impl.fulltext.FulltextIndexProceduresUtil.RELATIONSHIP_CREATE) CsvSource(org.junit.jupiter.params.provider.CsvSource) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Label(org.neo4j.graphdb.Label) AWAIT_REFRESH(org.neo4j.kernel.api.impl.fulltext.FulltextIndexProceduresUtil.AWAIT_REFRESH) DB_AWAIT_INDEX(org.neo4j.kernel.api.impl.fulltext.FulltextIndexProceduresUtil.DB_AWAIT_INDEX) IOUtils(org.neo4j.io.IOUtils) ArrayUtils(org.apache.commons.lang3.ArrayUtils) EnumSource(org.junit.jupiter.params.provider.EnumSource) HashMap(java.util.HashMap) NODE_CREATE(org.neo4j.kernel.api.impl.fulltext.FulltextIndexProceduresUtil.NODE_CREATE) TreeSet(java.util.TreeSet) Node(org.neo4j.graphdb.Node) IndexType(org.neo4j.graphdb.schema.IndexType) HashSet(java.util.HashSet) Iterables(org.neo4j.internal.helpers.collection.Iterables) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) NON_ASCII_LETTERS(org.neo4j.kernel.api.impl.fulltext.analyzer.StandardFoldingAnalyzer.NON_ASCII_LETTERS) ExecutorService(java.util.concurrent.ExecutorService) LIST_AVAILABLE_ANALYZERS(org.neo4j.kernel.api.impl.fulltext.FulltextIndexProceduresUtil.LIST_AVAILABLE_ANALYZERS) Files(java.nio.file.Files) Iterables.stream(org.neo4j.internal.helpers.collection.Iterables.stream) ProgressMonitorFactory(org.neo4j.internal.helpers.progress.ProgressMonitorFactory) InputStreamReader(java.io.InputStreamReader) MutableLongSet(org.eclipse.collections.api.set.primitive.MutableLongSet) TimeUnit(java.util.concurrent.TimeUnit) Assertions.assertArrayEquals(org.junit.jupiter.api.Assertions.assertArrayEquals) RepeatedRelationshipTypeInSchemaException(org.neo4j.kernel.api.exceptions.schema.RepeatedRelationshipTypeInSchemaException) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Collectors.toList(java.util.stream.Collectors.toList) ASCIIFoldingFilter(org.apache.lucene.analysis.miscellaneous.ASCIIFoldingFilter) Relationship(org.neo4j.graphdb.Relationship) FulltextIndexProceduresUtil.asStrList(org.neo4j.kernel.api.impl.fulltext.FulltextIndexProceduresUtil.asStrList) BufferedReader(java.io.BufferedReader) IndexDefinition(org.neo4j.graphdb.schema.IndexDefinition) IndexSetting(org.neo4j.graphdb.schema.IndexSetting) ConsistencyFlags(org.neo4j.consistency.checking.full.ConsistencyFlags) LongHashSet(org.eclipse.collections.impl.set.mutable.primitive.LongHashSet) Transaction(org.neo4j.graphdb.Transaction) ExecutorService(java.util.concurrent.ExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) BinaryLatch(org.neo4j.util.concurrent.BinaryLatch) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with BinaryLatch

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

the class ForsetiFalseDeadlockTest method runTest.

private static void runTest(Fixture fixture) throws InterruptedException, java.util.concurrent.ExecutionException {
    int iterations = fixture.iterations();
    ResourceType resourceType = fixture.createResourceType();
    Locks manager = fixture.createLockManager(resourceType);
    try (Locks.Client a = manager.newClient();
        Locks.Client b = manager.newClient()) {
        a.initialize(NoLeaseClient.INSTANCE, 1, EmptyMemoryTracker.INSTANCE, Config.defaults());
        b.initialize(NoLeaseClient.INSTANCE, 2, EmptyMemoryTracker.INSTANCE, Config.defaults());
        BinaryLatch startLatch = new BinaryLatch();
        BlockedCallable callA = new BlockedCallable(startLatch, () -> workloadA(fixture, a, resourceType, iterations));
        BlockedCallable callB = new BlockedCallable(startLatch, () -> workloadB(fixture, b, resourceType, iterations));
        Future<Void> futureA = executor.submit(callA);
        Future<Void> futureB = executor.submit(callB);
        callA.awaitBlocked();
        callB.awaitBlocked();
        startLatch.release();
        futureA.get();
        futureB.get();
    } finally {
        manager.close();
    }
}
Also used : ResourceType(org.neo4j.lock.ResourceType) Locks(org.neo4j.kernel.impl.locking.Locks) BinaryLatch(org.neo4j.util.concurrent.BinaryLatch)

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