Search in sources :

Example 1 with AtomicReferenceArray

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()));
}
Also used : BrokenBarrierException(java.util.concurrent.BrokenBarrierException) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicReferenceArray(java.util.concurrent.atomic.AtomicReferenceArray) ExecutionException(java.util.concurrent.ExecutionException) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 2 with AtomicReferenceArray

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()));
}
Also used : ArrayList(java.util.ArrayList) AtomicReferenceArray(java.util.concurrent.atomic.AtomicReferenceArray) FailedNodeException(org.elasticsearch.action.FailedNodeException)

Example 3 with AtomicReferenceArray

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();
}
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 4 with AtomicReferenceArray

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());
    }
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) JoinConfig(com.hazelcast.config.JoinConfig) Config(com.hazelcast.config.Config) MulticastConfig(com.hazelcast.config.MulticastConfig) NetworkConfig(com.hazelcast.config.NetworkConfig) TcpIpConfig(com.hazelcast.config.TcpIpConfig) AtomicReferenceArray(java.util.concurrent.atomic.AtomicReferenceArray) ExecutorService(java.util.concurrent.ExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 5 with AtomicReferenceArray

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);
    }
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) HashMap(java.util.HashMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JoinConfig(com.hazelcast.config.JoinConfig) Config(com.hazelcast.config.Config) MulticastConfig(com.hazelcast.config.MulticastConfig) NetworkConfig(com.hazelcast.config.NetworkConfig) TcpIpConfig(com.hazelcast.config.TcpIpConfig) AtomicReferenceArray(java.util.concurrent.atomic.AtomicReferenceArray) ExecutorService(java.util.concurrent.ExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

AtomicReferenceArray (java.util.concurrent.atomic.AtomicReferenceArray)31 CountDownLatch (java.util.concurrent.CountDownLatch)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 RubyObject (org.jruby.RubyObject)6 IRubyObject (org.jruby.runtime.builtin.IRubyObject)6 Config (com.hazelcast.config.Config)4 HazelcastInstance (com.hazelcast.core.HazelcastInstance)4 Random (java.util.Random)4 Test (org.junit.Test)4 Thread.currentThread (java.lang.Thread.currentThread)3 ExecutorService (java.util.concurrent.ExecutorService)3 JoinConfig (com.hazelcast.config.JoinConfig)2 MulticastConfig (com.hazelcast.config.MulticastConfig)2 NetworkConfig (com.hazelcast.config.NetworkConfig)2 TcpIpConfig (com.hazelcast.config.TcpIpConfig)2 IMap (com.hazelcast.core.IMap)2 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)2 QuickTest (com.hazelcast.test.annotation.QuickTest)2 ArrayList (java.util.ArrayList)2