Search in sources :

Example 61 with AtomicLong

use of java.util.concurrent.atomic.AtomicLong in project neo4j by neo4j.

the class ConfigurableIOLimiterTest method createIOLimiter.

private void createIOLimiter(Config config) {
    pauseNanosCounter = new AtomicLong();
    ObjLongConsumer<Object> pauseNanos = (blocker, nanos) -> pauseNanosCounter.getAndAdd(nanos);
    limiter = new ConfigurableIOLimiter(config, pauseNanos);
}
Also used : Config(org.neo4j.kernel.configuration.Config) Test(org.junit.Test) IOException(java.io.IOException) IOLimiter(org.neo4j.io.pagecache.IOLimiter) TimeUnit(java.util.concurrent.TimeUnit) Assert.assertThat(org.junit.Assert.assertThat) AtomicLong(java.util.concurrent.atomic.AtomicLong) ObjLongConsumer(java.util.function.ObjLongConsumer) Map(java.util.Map) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Matchers.is(org.hamcrest.Matchers.is) GraphDatabaseSettings(org.neo4j.graphdb.factory.GraphDatabaseSettings) MapUtil.stringMap(org.neo4j.helpers.collection.MapUtil.stringMap) Flushable(java.io.Flushable) AtomicLong(java.util.concurrent.atomic.AtomicLong)

Example 62 with AtomicLong

use of java.util.concurrent.atomic.AtomicLong in project neo4j by neo4j.

the class KernelTransactionImplementationTest method shouldCallCloseListenerOnCloseWhenCommitting.

@Test
public void shouldCallCloseListenerOnCloseWhenCommitting() throws Exception {
    // given
    AtomicLong closeTxId = new AtomicLong(Long.MIN_VALUE);
    KernelTransactionImplementation tx = newTransaction(securityContext());
    tx.registerCloseListener(closeTxId::set);
    // when
    if (isWriteTx) {
        tx.upgradeToDataWrites();
        tx.txState().nodeDoCreate(42L);
    }
    tx.success();
    tx.close();
    // then
    assertThat(closeTxId.get(), isWriteTx ? greaterThan(BASE_TX_ID) : equalTo(0L));
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Test(org.junit.Test)

Example 63 with AtomicLong

use of java.util.concurrent.atomic.AtomicLong in project neo4j by neo4j.

the class LegacyBatchIndexApplierTest method shouldOrderTransactionsMakingLegacyIndexChanges.

@Test
public void shouldOrderTransactionsMakingLegacyIndexChanges() throws Throwable {
    // GIVEN
    Map<String, Integer> names = MapUtil.genericMap("first", 0, "second", 1);
    Map<String, Integer> keys = MapUtil.genericMap("key", 0);
    String applierName = "test-applier";
    LegacyIndexApplierLookup applierLookup = mock(LegacyIndexApplierLookup.class);
    when(applierLookup.newApplier(anyString(), anyBoolean())).thenReturn(mock(TransactionApplier.class));
    IndexConfigStore config = newIndexConfigStore(names, applierName);
    // WHEN multiple legacy index transactions are running, they should be done in order
    SynchronizedArrayIdOrderingQueue queue = new SynchronizedArrayIdOrderingQueue(10);
    final AtomicLong lastAppliedTxId = new AtomicLong(-1);
    Race race = new Race();
    for (long i = 0; i < 100; i++) {
        final long txId = i;
        race.addContestant(() -> {
            try (LegacyBatchIndexApplier applier = new LegacyBatchIndexApplier(config, applierLookup, queue, INTERNAL)) {
                TransactionToApply txToApply = new TransactionToApply(new PhysicalTransactionRepresentation(new ArrayList<>()));
                FakeCommitment commitment = new FakeCommitment(txId, mock(TransactionIdStore.class));
                commitment.setHasLegacyIndexChanges(true);
                txToApply.commitment(commitment, txId);
                TransactionApplier txApplier = applier.startTx(txToApply);
                // Make sure threads are unordered
                Thread.sleep(ThreadLocalRandom.current().nextInt(5));
                // THEN
                assertTrue(lastAppliedTxId.compareAndSet(txId - 1, txId));
                // Closing manually instead of using try-with-resources since we have no additional work to do in
                // txApplier
                txApplier.close();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });
        queue.offer(txId);
    }
    race.go();
}
Also used : TransactionIdStore(org.neo4j.kernel.impl.transaction.log.TransactionIdStore) IndexConfigStore(org.neo4j.kernel.impl.index.IndexConfigStore) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) AtomicLong(java.util.concurrent.atomic.AtomicLong) SynchronizedArrayIdOrderingQueue(org.neo4j.kernel.impl.util.SynchronizedArrayIdOrderingQueue) Race(org.neo4j.test.Race) FakeCommitment(org.neo4j.kernel.impl.transaction.log.FakeCommitment) PhysicalTransactionRepresentation(org.neo4j.kernel.impl.transaction.log.PhysicalTransactionRepresentation) Test(org.junit.Test)

Example 64 with AtomicLong

use of java.util.concurrent.atomic.AtomicLong in project neo4j by neo4j.

the class Neo4jJobSchedulerTest method shouldRunWithDelay.

@Test
public void shouldRunWithDelay() throws Throwable {
    // Given
    life.start();
    final AtomicLong runTime = new AtomicLong();
    final CountDownLatch latch = new CountDownLatch(1);
    long time = System.nanoTime();
    scheduler.schedule(new JobScheduler.Group("group", POOLED), new Runnable() {

        @Override
        public void run() {
            runTime.set(System.nanoTime());
            latch.countDown();
        }
    }, 100, TimeUnit.MILLISECONDS);
    latch.await();
    assertTrue(time + TimeUnit.MILLISECONDS.toNanos(100) <= runTime.get());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 65 with AtomicLong

use of java.util.concurrent.atomic.AtomicLong in project neo4j by neo4j.

the class ArrayQueueOutOfOrderSequenceTest method shouldKeepItsCoolWhenMultipleThreadsAreHammeringIt.

@Test
public void shouldKeepItsCoolWhenMultipleThreadsAreHammeringIt() throws Exception {
    // An interesting note is that during tests the call to sequence#offer made no difference
    // in performance, so there seems to be no visible penalty in using ArrayQueueOutOfOrderSequence.
    // GIVEN a sequence with intentionally low starting queue size
    final AtomicLong numberSource = new AtomicLong();
    final OutOfOrderSequence sequence = new ArrayQueueOutOfOrderSequence(numberSource.get(), 5, new long[1]);
    final AtomicBoolean end = new AtomicBoolean();
    // and a bunch of threads that will start offering numbers at the same time
    final CountDownLatch startSignal = new CountDownLatch(1);
    Thread[] threads = new Thread[1];
    for (int i = 0; i < threads.length; i++) {
        threads[i] = new Thread() {

            @Override
            public void run() {
                await(startSignal);
                while (!end.get()) {
                    long number = numberSource.incrementAndGet();
                    offer(sequence, number, new long[] { number + 2 });
                }
            }
        };
    }
    // WHEN
    for (Thread thread : threads) {
        thread.start();
    }
    startSignal.countDown();
    while (numberSource.get() < 10_000_000) {
        sleep(1);
        yield();
    }
    end.set(true);
    for (Thread thread : threads) {
        thread.join();
    }
    // THEN
    long lastNumber = numberSource.get();
    assertGet(sequence, lastNumber, new long[] { lastNumber + 2 });
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) ArrayQueueOutOfOrderSequence(org.neo4j.kernel.impl.util.ArrayQueueOutOfOrderSequence) ArrayQueueOutOfOrderSequence(org.neo4j.kernel.impl.util.ArrayQueueOutOfOrderSequence) OutOfOrderSequence(org.neo4j.kernel.impl.util.OutOfOrderSequence) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

AtomicLong (java.util.concurrent.atomic.AtomicLong)2292 Test (org.junit.Test)986 ArrayList (java.util.ArrayList)300 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)273 IOException (java.io.IOException)254 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)250 List (java.util.List)222 HashMap (java.util.HashMap)212 Map (java.util.Map)209 CountDownLatch (java.util.concurrent.CountDownLatch)185 AtomicReference (java.util.concurrent.atomic.AtomicReference)174 HashSet (java.util.HashSet)106 Arrays (java.util.Arrays)101 File (java.io.File)99 Test (org.junit.jupiter.api.Test)98 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)95 Set (java.util.Set)94 TimeUnit (java.util.concurrent.TimeUnit)91 Collections (java.util.Collections)88 Random (java.util.Random)85