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();
}
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());
}
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);
}
}
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();
}
}
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);
}
}
}
Aggregations