use of java.util.concurrent.atomic.AtomicReferenceArray in project elasticsearch by elastic.
the class CacheTests method testComputeIfAbsentCallsOnce.
public void testComputeIfAbsentCallsOnce() throws BrokenBarrierException, InterruptedException {
int numberOfThreads = randomIntBetween(2, 32);
final Cache<Integer, String> cache = CacheBuilder.<Integer, String>builder().build();
AtomicReferenceArray flags = new AtomicReferenceArray(numberOfEntries);
for (int j = 0; j < numberOfEntries; j++) {
flags.set(j, false);
}
CopyOnWriteArrayList<ExecutionException> failures = new CopyOnWriteArrayList<>();
CyclicBarrier barrier = new CyclicBarrier(1 + numberOfThreads);
for (int i = 0; i < numberOfThreads; i++) {
Thread thread = new Thread(() -> {
try {
barrier.await();
for (int j = 0; j < numberOfEntries; j++) {
try {
cache.computeIfAbsent(j, key -> {
assertTrue(flags.compareAndSet(key, false, true));
return Integer.toString(key);
});
} catch (ExecutionException e) {
failures.add(e);
break;
}
}
barrier.await();
} catch (BrokenBarrierException | InterruptedException e) {
throw new AssertionError(e);
}
});
thread.start();
}
// wait for all threads to be ready
barrier.await();
// wait for all threads to finish
barrier.await();
assertThat(failures, is(empty()));
}
use of java.util.concurrent.atomic.AtomicReferenceArray in project elasticsearch by elastic.
the class TransportNodesActionTests method testNewResponse.
public void testNewResponse() {
TestTransportNodesAction action = getTestTransportNodesAction();
TestNodesRequest request = new TestNodesRequest();
List<TestNodeResponse> expectedNodeResponses = mockList(TestNodeResponse::new, randomIntBetween(0, 2));
expectedNodeResponses.add(new TestNodeResponse());
List<BaseNodeResponse> nodeResponses = new ArrayList<>(expectedNodeResponses);
// This should be ignored:
nodeResponses.add(new OtherNodeResponse());
List<FailedNodeException> failures = mockList(() -> new FailedNodeException(randomAsciiOfLength(8), randomAsciiOfLength(8), new IllegalStateException(randomAsciiOfLength(8))), randomIntBetween(0, 2));
List<Object> allResponses = new ArrayList<>(expectedNodeResponses);
allResponses.addAll(failures);
Collections.shuffle(allResponses, random());
AtomicReferenceArray<?> atomicArray = new AtomicReferenceArray<>(allResponses.toArray());
TestNodesResponse response = action.newResponse(request, atomicArray);
assertSame(request, response.request);
// note: I shuffled the overall list, so it's not possible to guarantee that it's in the right order
assertTrue(expectedNodeResponses.containsAll(response.getNodes()));
assertTrue(failures.containsAll(response.failures()));
}
use of java.util.concurrent.atomic.AtomicReferenceArray 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.atomic.AtomicReferenceArray in project hazelcast by hazelcast.
the class JoinStressTest method testJoinWithManyNodes.
public void testJoinWithManyNodes(final boolean multicast) throws InterruptedException {
final int nodeCount = 20;
final int basePort = 12301;
final CountDownLatch latch = new CountDownLatch(nodeCount);
final AtomicReferenceArray<HazelcastInstance> instances = new AtomicReferenceArray<HazelcastInstance>(nodeCount);
ExecutorService ex = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2);
for (int i = 0; i < nodeCount; i++) {
final int portSeed = i;
ex.execute(new Runnable() {
public void run() {
sleepRandom(1, 1000);
Config config = new Config();
initNetworkConfig(config.getNetworkConfig(), basePort, portSeed, multicast, nodeCount);
HazelcastInstance h = Hazelcast.newHazelcastInstance(config);
instances.set(portSeed, h);
latch.countDown();
}
});
}
try {
latch.await(200, TimeUnit.SECONDS);
} finally {
ex.shutdown();
}
for (int i = 0; i < nodeCount; i++) {
HazelcastInstance hz = instances.get(i);
assertNotNull(hz);
assertEquals(nodeCount, hz.getCluster().getMembers().size());
}
}
use of java.util.concurrent.atomic.AtomicReferenceArray in project hazelcast by hazelcast.
the class JoinStressTest method testJoinWithManyNodesMultipleGroups.
private void testJoinWithManyNodesMultipleGroups(final boolean multicast) throws InterruptedException {
final int nodeCount = 10;
final int groupCount = 3;
final int basePort = 12301;
final CountDownLatch latch = new CountDownLatch(nodeCount);
final AtomicReferenceArray<HazelcastInstance> instances = new AtomicReferenceArray<HazelcastInstance>(nodeCount);
final Map<String, AtomicInteger> groups = new HashMap<String, AtomicInteger>(groupCount);
for (int i = 0; i < groupCount; i++) {
groups.put("group-" + i, new AtomicInteger(0));
}
ExecutorService ex = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2);
for (int i = 0; i < nodeCount; i++) {
final int portSeed = i;
ex.execute(new Runnable() {
public void run() {
sleepRandom(1, 1000);
Config config = new Config();
String name = "group-" + (int) (Math.random() * groupCount);
config.getGroupConfig().setName(name);
groups.get(name).incrementAndGet();
initNetworkConfig(config.getNetworkConfig(), basePort, portSeed, multicast, nodeCount);
HazelcastInstance h = Hazelcast.newHazelcastInstance(config);
instances.set(portSeed, h);
latch.countDown();
}
});
}
try {
latch.await(200, TimeUnit.SECONDS);
} finally {
ex.shutdown();
}
for (int i = 0; i < nodeCount; i++) {
HazelcastInstance hz = instances.get(i);
assertNotNull(hz);
int clusterSize = hz.getCluster().getMembers().size();
String groupName = hz.getConfig().getGroupConfig().getName();
int shouldBeClusterSize = groups.get(groupName).get();
assertEquals(groupName + ": ", shouldBeClusterSize, clusterSize);
}
}
Aggregations