Search in sources :

Example 86 with CopyOnWriteArrayList

use of java.util.concurrent.CopyOnWriteArrayList in project spring-framework by spring-projects.

the class AbstractCacheTests method testCacheGetSynchronized.

/**
	 * Test that a call to get with a Callable concurrently properly synchronize the
	 * invocations.
	 */
@Test
public void testCacheGetSynchronized() throws InterruptedException {
    T cache = getCache();
    final AtomicInteger counter = new AtomicInteger();
    final List<Object> results = new CopyOnWriteArrayList<>();
    final CountDownLatch latch = new CountDownLatch(10);
    String key = createRandomKey();
    Runnable run = () -> {
        try {
            Integer value = cache.get(key, () -> {
                // make sure the thread will overlap
                Thread.sleep(50);
                return counter.incrementAndGet();
            });
            results.add(value);
        } finally {
            latch.countDown();
        }
    };
    for (int i = 0; i < 10; i++) {
        new Thread(run).start();
    }
    latch.await();
    assertEquals(10, results.size());
    // Only one method got invoked
    results.forEach(r -> assertThat(r, is(1)));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.Test)

Example 87 with CopyOnWriteArrayList

use of java.util.concurrent.CopyOnWriteArrayList in project spring-framework by spring-projects.

the class DefaultLifecycleProcessorTests method singleLifecycleShutdown.

@Test
public void singleLifecycleShutdown() throws Exception {
    CopyOnWriteArrayList<Lifecycle> stoppedBeans = new CopyOnWriteArrayList<>();
    Lifecycle bean = new TestLifecycleBean(null, stoppedBeans);
    StaticApplicationContext context = new StaticApplicationContext();
    context.getBeanFactory().registerSingleton("bean", bean);
    context.refresh();
    assertFalse(bean.isRunning());
    bean.start();
    assertTrue(bean.isRunning());
    context.stop();
    assertEquals(1, stoppedBeans.size());
    assertFalse(bean.isRunning());
    assertEquals(bean, stoppedBeans.get(0));
}
Also used : Lifecycle(org.springframework.context.Lifecycle) SmartLifecycle(org.springframework.context.SmartLifecycle) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.Test)

Example 88 with CopyOnWriteArrayList

use of java.util.concurrent.CopyOnWriteArrayList in project spring-framework by spring-projects.

the class DefaultLifecycleProcessorTests method mixedShutdown.

@Test
public void mixedShutdown() throws Exception {
    CopyOnWriteArrayList<Lifecycle> stoppedBeans = new CopyOnWriteArrayList<>();
    Lifecycle bean1 = TestLifecycleBean.forShutdownTests(stoppedBeans);
    Lifecycle bean2 = TestSmartLifecycleBean.forShutdownTests(500, 200, stoppedBeans);
    Lifecycle bean3 = TestSmartLifecycleBean.forShutdownTests(Integer.MAX_VALUE, 100, stoppedBeans);
    Lifecycle bean4 = TestLifecycleBean.forShutdownTests(stoppedBeans);
    Lifecycle bean5 = TestSmartLifecycleBean.forShutdownTests(1, 200, stoppedBeans);
    Lifecycle bean6 = TestSmartLifecycleBean.forShutdownTests(-1, 100, stoppedBeans);
    Lifecycle bean7 = TestSmartLifecycleBean.forShutdownTests(Integer.MIN_VALUE, 300, stoppedBeans);
    StaticApplicationContext context = new StaticApplicationContext();
    context.getBeanFactory().registerSingleton("bean1", bean1);
    context.getBeanFactory().registerSingleton("bean2", bean2);
    context.getBeanFactory().registerSingleton("bean3", bean3);
    context.getBeanFactory().registerSingleton("bean4", bean4);
    context.getBeanFactory().registerSingleton("bean5", bean5);
    context.getBeanFactory().registerSingleton("bean6", bean6);
    context.getBeanFactory().registerSingleton("bean7", bean7);
    context.refresh();
    assertTrue(bean2.isRunning());
    assertTrue(bean3.isRunning());
    assertTrue(bean5.isRunning());
    assertTrue(bean6.isRunning());
    assertTrue(bean7.isRunning());
    assertFalse(bean1.isRunning());
    assertFalse(bean4.isRunning());
    bean1.start();
    bean4.start();
    assertTrue(bean1.isRunning());
    assertTrue(bean4.isRunning());
    context.stop();
    assertFalse(bean1.isRunning());
    assertFalse(bean2.isRunning());
    assertFalse(bean3.isRunning());
    assertFalse(bean4.isRunning());
    assertFalse(bean5.isRunning());
    assertFalse(bean6.isRunning());
    assertFalse(bean7.isRunning());
    assertEquals(7, stoppedBeans.size());
    assertEquals(Integer.MAX_VALUE, getPhase(stoppedBeans.get(0)));
    assertEquals(500, getPhase(stoppedBeans.get(1)));
    assertEquals(1, getPhase(stoppedBeans.get(2)));
    assertEquals(0, getPhase(stoppedBeans.get(3)));
    assertEquals(0, getPhase(stoppedBeans.get(4)));
    assertEquals(-1, getPhase(stoppedBeans.get(5)));
    assertEquals(Integer.MIN_VALUE, getPhase(stoppedBeans.get(6)));
}
Also used : Lifecycle(org.springframework.context.Lifecycle) SmartLifecycle(org.springframework.context.SmartLifecycle) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.Test)

Example 89 with CopyOnWriteArrayList

use of java.util.concurrent.CopyOnWriteArrayList in project undertow by undertow-io.

the class HttpClientTestCase method testSimpleBasic.

@Test
public void testSimpleBasic() throws Exception {
    //
    DefaultServer.setRootHandler(SIMPLE_MESSAGE_HANDLER);
    final UndertowClient client = createClient();
    final List<ClientResponse> responses = new CopyOnWriteArrayList<>();
    final CountDownLatch latch = new CountDownLatch(10);
    final ClientConnection connection = client.connect(ADDRESS, worker, DefaultServer.getBufferPool(), OptionMap.EMPTY).get();
    try {
        connection.getIoThread().execute(new Runnable() {

            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    final ClientRequest request = new ClientRequest().setMethod(Methods.GET).setPath("/");
                    request.getRequestHeaders().put(Headers.HOST, DefaultServer.getHostAddress());
                    connection.sendRequest(request, createClientCallback(responses, latch));
                }
            }
        });
        latch.await(10, TimeUnit.SECONDS);
        Assert.assertEquals(10, responses.size());
        for (final ClientResponse response : responses) {
            Assert.assertEquals(message, response.getAttachment(RESPONSE_BODY));
        }
    } finally {
        IoUtils.safeClose(connection);
    }
}
Also used : ClientResponse(io.undertow.client.ClientResponse) UndertowClient(io.undertow.client.UndertowClient) ClientConnection(io.undertow.client.ClientConnection) CountDownLatch(java.util.concurrent.CountDownLatch) ClientRequest(io.undertow.client.ClientRequest) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.Test)

Example 90 with CopyOnWriteArrayList

use of java.util.concurrent.CopyOnWriteArrayList in project voldemort by voldemort.

the class ThreadPoolRoutedStore method put.

@Override
public void put(final ByteArray key, final Versioned<byte[]> versioned, final byte[] transforms) throws VoldemortException {
    long startNs = System.nanoTime();
    StoreUtils.assertValidKey(key);
    final List<Node> nodes = availableNodes(routingStrategy.routeRequest(key.get()));
    // quickly fail if there aren't enough nodes to meet the requirement
    final int numNodes = nodes.size();
    if (numNodes < this.storeDef.getRequiredWrites())
        throw new InsufficientOperationalNodesException("Only " + numNodes + " nodes in preference list, but " + this.storeDef.getRequiredWrites() + " writes required.");
    // A count of the number of successful operations
    final AtomicInteger successes = new AtomicInteger(0);
    // A list of thrown exceptions, indicating the number of failures
    final List<Exception> failures = new CopyOnWriteArrayList<Exception>();
    // If requiredWrites > 0 then do a single blocking write to the first
    // live node in the preference list if this node throws an
    // ObsoleteVersionException allow it to propagate
    Node master = null;
    int currentNode = 0;
    Versioned<byte[]> versionedCopy = null;
    for (; currentNode < numNodes; currentNode++) {
        Node current = nodes.get(currentNode);
        long startNsLocal = System.nanoTime();
        try {
            versionedCopy = incremented(versioned, current.getId());
            innerStores.get(current.getId()).put(key, versionedCopy, transforms);
            successes.getAndIncrement();
            recordSuccess(current, startNsLocal);
            master = current;
            break;
        } catch (UnreachableStoreException e) {
            recordException(current, startNsLocal, e);
            failures.add(e);
        } catch (VoldemortApplicationException e) {
            throw e;
        } catch (Exception e) {
            failures.add(e);
        }
    }
    if (successes.get() < 1)
        throw new InsufficientOperationalNodesException("No master node succeeded!", failures);
    else
        currentNode++;
    // A semaphore indicating the number of completed operations
    // Once inititialized all permits are acquired, after that
    // permits are released when an operation is completed.
    // semaphore.acquire(n) waits for n operations to complete
    final Versioned<byte[]> finalVersionedCopy = versionedCopy;
    final Semaphore semaphore = new Semaphore(0, false);
    // Add the operations to the pool
    int attempts = 0;
    for (; currentNode < numNodes; currentNode++) {
        attempts++;
        final Node node = nodes.get(currentNode);
        this.executor.execute(new Runnable() {

            @Override
            public void run() {
                long startNsLocal = System.nanoTime();
                try {
                    innerStores.get(node.getId()).put(key, finalVersionedCopy, transforms);
                    successes.incrementAndGet();
                    recordSuccess(node, startNsLocal);
                } catch (UnreachableStoreException e) {
                    recordException(node, startNsLocal, e);
                    failures.add(e);
                } catch (ObsoleteVersionException e) {
                // ignore this completely here
                // this means that a higher version was able
                // to write on this node and should be termed as clean
                // success.
                } catch (VoldemortApplicationException e) {
                    throw e;
                } catch (Exception e) {
                    logger.warn("Error in PUT on node " + node.getId() + "(" + node.getHost() + ")", e);
                    failures.add(e);
                } finally {
                    // signal that the operation is complete
                    semaphore.release();
                }
            }
        });
    }
    // Block until we get enough completions
    int blockCount = Math.min(storeDef.getPreferredWrites() - 1, attempts);
    boolean noTimeout = blockOnPut(startNs, semaphore, 0, blockCount, successes, storeDef.getPreferredWrites());
    if (successes.get() < storeDef.getRequiredWrites()) {
        /*
             * We don't have enough required writes, but we haven't timed out
             * yet, so block a little more if there are healthy nodes that can
             * help us achieve our target.
             */
        if (noTimeout) {
            int startingIndex = blockCount - 1;
            blockCount = Math.max(storeDef.getPreferredWrites() - 1, attempts);
            blockOnPut(startNs, semaphore, startingIndex, blockCount, successes, storeDef.getRequiredWrites());
        }
        if (successes.get() < storeDef.getRequiredWrites())
            throw new InsufficientOperationalNodesException(successes.get() + " writes succeeded, but " + this.storeDef.getRequiredWrites() + " are required.", failures);
    }
    // Okay looks like it worked, increment the version for the caller
    VectorClock versionedClock = (VectorClock) versioned.getVersion();
    versionedClock.incrementVersion(master.getId(), time.getMilliseconds());
}
Also used : VoldemortApplicationException(voldemort.VoldemortApplicationException) Node(voldemort.cluster.Node) VectorClock(voldemort.versioning.VectorClock) Semaphore(java.util.concurrent.Semaphore) ObsoleteVersionException(voldemort.versioning.ObsoleteVersionException) InsufficientOperationalNodesException(voldemort.store.InsufficientOperationalNodesException) VoldemortException(voldemort.VoldemortException) VoldemortApplicationException(voldemort.VoldemortApplicationException) UnreachableStoreException(voldemort.store.UnreachableStoreException) ExecutionException(java.util.concurrent.ExecutionException) ObsoleteVersionException(voldemort.versioning.ObsoleteVersionException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InsufficientOperationalNodesException(voldemort.store.InsufficientOperationalNodesException) UnreachableStoreException(voldemort.store.UnreachableStoreException) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Aggregations

CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)97 CountDownLatch (java.util.concurrent.CountDownLatch)38 IOException (java.io.IOException)23 ArrayList (java.util.ArrayList)23 List (java.util.List)23 Test (org.junit.Test)20 ExecutionException (java.util.concurrent.ExecutionException)15 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)13 CyclicBarrier (java.util.concurrent.CyclicBarrier)11 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)10 MockTransportService (org.elasticsearch.test.transport.MockTransportService)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 HashMap (java.util.HashMap)8 AtomicReference (java.util.concurrent.atomic.AtomicReference)7 HashSet (java.util.HashSet)6 Map (java.util.Map)6 TimeUnit (java.util.concurrent.TimeUnit)6 Settings (org.elasticsearch.common.settings.Settings)6 Config (com.hazelcast.config.Config)5