Search in sources :

Example 26 with BinaryLatch

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

the class TinyLockManager method tryLock.

public boolean tryLock(int recordId) {
    Integer record = recordId;
    BinaryLatch myLatch = new BinaryLatch();
    BinaryLatch existingLatch = map.putIfAbsent(record, myLatch);
    return existingLatch == null;
}
Also used : BinaryLatch(org.neo4j.util.concurrent.BinaryLatch)

Example 27 with BinaryLatch

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

the class LatchMapTest method takeOrAwaitLatchMustNotLetUnrelatedLatchesConflictTooMuch.

@ValueSource(ints = { LatchMap.DEFAULT_FAULT_LOCK_STRIPING, 1 << 10, 1 << 11 })
@ParameterizedTest
void takeOrAwaitLatchMustNotLetUnrelatedLatchesConflictTooMuch(int size) throws Exception {
    LatchMap latches = new LatchMap(size);
    BinaryLatch latch = latches.takeOrAwaitLatch(42);
    assertThat(latch).isNotNull();
    ExecutorService executor = null;
    try {
        executor = Executors.newSingleThreadExecutor();
        Future<BinaryLatch> future = executor.submit(() -> latches.takeOrAwaitLatch(33));
        assertThat(future.get(30, TimeUnit.SECONDS)).isNotNull();
        latch.release();
    } finally {
        if (executor != null) {
            executor.shutdown();
        }
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) BinaryLatch(org.neo4j.util.concurrent.BinaryLatch) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 28 with BinaryLatch

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

the class LabelsAcceptanceTest method shouldListAllLabelsInUseEvenWhenExclusiveLabelLocksAreTaken.

@Test
void shouldListAllLabelsInUseEvenWhenExclusiveLabelLocksAreTaken() {
    assertTimeoutPreemptively(Duration.ofSeconds(30), () -> {
        // Given
        createNode(db, Labels.MY_LABEL);
        Node node = createNode(db, Labels.MY_OTHER_LABEL);
        try (Transaction tx = db.beginTx()) {
            tx.getNodeById(node.getId()).delete();
            tx.commit();
        }
        BinaryLatch indexCreateStarted = new BinaryLatch();
        BinaryLatch indexCreateAllowToFinish = new BinaryLatch();
        Thread indexCreator = new Thread(() -> {
            try (Transaction tx = db.beginTx()) {
                tx.schema().indexFor(Labels.MY_LABEL).on("prop").create();
                indexCreateStarted.release();
                indexCreateAllowToFinish.await();
                tx.commit();
            }
        });
        indexCreator.start();
        // When
        indexCreateStarted.await();
        List<Label> labels;
        try (Transaction tx = db.beginTx()) {
            labels = asList(tx.getAllLabelsInUse());
        }
        indexCreateAllowToFinish.release();
        indexCreator.join();
        // Then
        assertEquals(1, labels.size());
        assertThat(map(Label::name, labels)).contains(Labels.MY_LABEL.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 29 with BinaryLatch

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

the class CheckPointerImplTest method verifyAsyncActionCausesConcurrentFlushingRush.

private void verifyAsyncActionCausesConcurrentFlushingRush(ThrowingConsumer<CheckPointerImpl, IOException> asyncAction) throws Exception {
    AtomicLong limitDisableCounter = new AtomicLong();
    AtomicLong observedRushCount = new AtomicLong();
    BinaryLatch backgroundCheckPointStartedLatch = new BinaryLatch();
    BinaryLatch forceCheckPointStartLatch = new BinaryLatch();
    limiter = new EmptyIOController() {

        @Override
        public void disable() {
            limitDisableCounter.getAndIncrement();
            forceCheckPointStartLatch.release();
        }

        @Override
        public void enable() {
            limitDisableCounter.getAndDecrement();
        }

        @Override
        public boolean isEnabled() {
            return limitDisableCounter.get() != 0;
        }
    };
    mockTxIdStore();
    CheckPointerImpl checkPointer = checkPointer();
    doAnswer(invocation -> {
        backgroundCheckPointStartedLatch.release();
        forceCheckPointStartLatch.await();
        long newValue = limitDisableCounter.get();
        observedRushCount.set(newValue);
        return null;
    }).when(forceOperation).flushAndForce(any());
    Future<Object> forceCheckPointer = forkFuture(() -> {
        backgroundCheckPointStartedLatch.await();
        asyncAction.accept(checkPointer);
        return null;
    });
    when(threshold.isCheckPointingNeeded(anyLong(), anyLong(), eq(INFO))).thenReturn(true);
    checkPointer.checkPointIfNeeded(INFO);
    forceCheckPointer.get();
    assertThat(observedRushCount.get()).isEqualTo(1L);
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) EmptyIOController(org.neo4j.io.pagecache.EmptyIOController) BinaryLatch(org.neo4j.util.concurrent.BinaryLatch)

Example 30 with BinaryLatch

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

the class IndexUpdateSink method awaitUpdateApplication.

public void awaitUpdateApplication() {
    BinaryLatch updateLatch = new BinaryLatch();
    scheduler.schedule(Group.INDEX_UPDATING, JobMonitoringParams.NOT_MONITORED, updateLatch::release);
    updateLatch.await();
}
Also used : 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