Search in sources :

Example 16 with ThreadLocalRandom

use of java.util.concurrent.ThreadLocalRandom in project neo4j by neo4j.

the class KernelTransactionsTest method shouldBeAbleToSnapshotDuringHeavyLoad.

@Test
public void shouldBeAbleToSnapshotDuringHeavyLoad() throws Throwable {
    // GIVEN
    final KernelTransactions transactions = newKernelTransactions();
    Race race = new Race();
    final int threads = 50;
    final AtomicBoolean end = new AtomicBoolean();
    final AtomicReferenceArray<KernelTransactionsSnapshot> snapshots = new AtomicReferenceArray<>(threads);
    // Representing "transaction" threads
    for (int i = 0; i < threads; i++) {
        final int threadIndex = i;
        race.addContestant(() -> {
            ThreadLocalRandom random = ThreadLocalRandom.current();
            while (!end.get()) {
                try (KernelTransaction transaction = getKernelTransaction(transactions)) {
                    parkNanos(MILLISECONDS.toNanos(random.nextInt(3)));
                    if (snapshots.get(threadIndex) == null) {
                        snapshots.set(threadIndex, transactions.get());
                        parkNanos(MILLISECONDS.toNanos(random.nextInt(3)));
                    }
                } catch (TransactionFailureException e) {
                    throw new RuntimeException(e);
                }
            }
        });
    }
    // Just checks snapshots
    race.addContestant(() -> {
        ThreadLocalRandom random = ThreadLocalRandom.current();
        int snapshotsLeft = 1_000;
        while (snapshotsLeft > 0) {
            int threadIndex = random.nextInt(threads);
            KernelTransactionsSnapshot snapshot = snapshots.get(threadIndex);
            if (snapshot != null && snapshot.allClosed()) {
                snapshotsLeft--;
                snapshots.set(threadIndex, null);
            }
        }
        // End condition of this test can be described as:
        //   when 1000 snapshots have been seen as closed.
        // setting this boolean to true will have all other threads end as well so that race.go() will end
        end.set(true);
    });
    // WHEN
    race.go();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) TransactionFailureException(org.neo4j.kernel.api.exceptions.TransactionFailureException) Race(org.neo4j.test.Race) AtomicReferenceArray(java.util.concurrent.atomic.AtomicReferenceArray) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Test(org.junit.Test)

Example 17 with ThreadLocalRandom

use of java.util.concurrent.ThreadLocalRandom in project neo4j by neo4j.

the class TransactionRepresentationCommitProcessIT method commitDuringContinuousCheckpointing.

@Test(timeout = 15000)
public void commitDuringContinuousCheckpointing() throws Exception {
    final Index<Node> index;
    try (Transaction tx = db.beginTx()) {
        index = db.index().forNodes(INDEX_NAME, stringMap(IndexManager.PROVIDER, DummyIndexExtensionFactory.IDENTIFIER));
        tx.success();
    }
    final AtomicBoolean done = new AtomicBoolean();
    Workers<Runnable> workers = new Workers<>(getClass().getSimpleName());
    for (int i = 0; i < TOTAL_ACTIVE_THREADS; i++) {
        workers.start(new Runnable() {

            private final ThreadLocalRandom random = ThreadLocalRandom.current();

            @Override
            public void run() {
                while (!done.get()) {
                    try (Transaction tx = db.beginTx()) {
                        Node node = db.createNode();
                        index.add(node, "key", node.getId());
                        tx.success();
                    }
                    randomSleep();
                }
            }

            private void randomSleep() {
                try {
                    Thread.sleep(random.nextInt(50));
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
    }
    Thread.sleep(SECONDS.toMillis(2));
    done.set(true);
    workers.awaitAndThrowOnError(RuntimeException.class);
    NeoStores neoStores = getDependency(RecordStorageEngine.class).testAccessNeoStores();
    assertThat("Count store should be rotated once at least", neoStores.getCounts().txId(), greaterThan(0L));
    long lastRotationTx = getDependency(CheckPointer.class).forceCheckPoint(new SimpleTriggerInfo("test"));
    assertEquals("NeoStore last closed transaction id should be equal last count store rotation transaction id.", neoStores.getMetaDataStore().getLastClosedTransactionId(), lastRotationTx);
    assertEquals("Last closed transaction should be last rotated tx in count store", neoStores.getMetaDataStore().getLastClosedTransactionId(), neoStores.getCounts().txId());
}
Also used : Node(org.neo4j.graphdb.Node) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SimpleTriggerInfo(org.neo4j.kernel.impl.transaction.log.checkpoint.SimpleTriggerInfo) Transaction(org.neo4j.graphdb.Transaction) Workers(org.neo4j.unsafe.impl.batchimport.cache.idmapping.string.Workers) RecordStorageEngine(org.neo4j.kernel.impl.storageengine.impl.recordstorage.RecordStorageEngine) NeoStores(org.neo4j.kernel.impl.store.NeoStores) CheckPointer(org.neo4j.kernel.impl.transaction.log.checkpoint.CheckPointer) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Test(org.junit.Test)

Example 18 with ThreadLocalRandom

use of java.util.concurrent.ThreadLocalRandom in project neo4j by neo4j.

the class SimpleRandomMutation method perform.

@Override
public void perform() {
    ThreadLocalRandom rng = ThreadLocalRandom.current();
    long nodeId = rng.nextLong(nodeCount);
    String value = NAMES[rng.nextInt(NAMES.length)];
    if (rng.nextDouble() < 0.01) {
        rareMutation.perform(nodeId, value);
    } else {
        commonMutation.perform(nodeId, value);
    }
}
Also used : ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom)

Example 19 with ThreadLocalRandom

use of java.util.concurrent.ThreadLocalRandom in project neo4j by neo4j.

the class BinaryLatchTest method stressLatch.

@Test(timeout = 60000)
public void stressLatch() throws Exception {
    final AtomicReference<BinaryLatch> latchRef = new AtomicReference<>(new BinaryLatch());
    Runnable awaiter = new Runnable() {

        @Override
        public void run() {
            BinaryLatch latch;
            while ((latch = latchRef.get()) != null) {
                latch.await();
            }
        }
    };
    int awaiters = 6;
    Future<?>[] futures = new Future<?>[awaiters];
    for (int i = 0; i < awaiters; i++) {
        futures[i] = executor.submit(awaiter);
    }
    ThreadLocalRandom rng = ThreadLocalRandom.current();
    for (int i = 0; i < 500000; i++) {
        latchRef.getAndSet(new BinaryLatch()).release();
        spinwaitu(rng.nextLong(0, 10));
    }
    latchRef.getAndSet(null).release();
    // None of the tasks we started should get stuck, e.g. miss a release signal:
    for (Future<?> future : futures) {
        future.get();
    }
}
Also used : Future(java.util.concurrent.Future) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 20 with ThreadLocalRandom

use of java.util.concurrent.ThreadLocalRandom in project neo4j by neo4j.

the class RaftLogVerificationIT method randomAppendAndTruncate.

@Test
public void randomAppendAndTruncate() throws Exception {
    ThreadLocalRandom tlr = ThreadLocalRandom.current();
    // given
    for (int i = 0; i < operations(); i++) {
        final int finalAppendIndex = tlr.nextInt(10) + 1;
        int appendIndex = finalAppendIndex;
        while (appendIndex-- > 0) {
            raftLog.append(new RaftLogEntry(i, valueOf(i)));
        }
        // truncate index must be strictly less than append index
        int truncateIndex = tlr.nextInt(finalAppendIndex);
        while (truncateIndex-- > 0) {
            raftLog.truncate(truncateIndex);
        }
    }
}
Also used : ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Test(org.junit.Test)

Aggregations

ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)186 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)49 Ignite (org.apache.ignite.Ignite)47 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)45 IgniteCache (org.apache.ignite.IgniteCache)33 Test (org.junit.Test)28 ArrayList (java.util.ArrayList)26 IgniteException (org.apache.ignite.IgniteException)26 Transaction (org.apache.ignite.transactions.Transaction)24 CacheException (javax.cache.CacheException)21 HashMap (java.util.HashMap)16 Map (java.util.Map)15 IgniteTransactions (org.apache.ignite.IgniteTransactions)13 CountDownLatch (java.util.concurrent.CountDownLatch)12 TreeMap (java.util.TreeMap)11 Callable (java.util.concurrent.Callable)11 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)10 AtomicLong (java.util.concurrent.atomic.AtomicLong)9 LongAdder (java.util.concurrent.atomic.LongAdder)9 ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)9