Search in sources :

Example 36 with CyclicBarrier

use of java.util.concurrent.CyclicBarrier in project elasticsearch by elastic.

the class RemoteClusterConnectionTests method testTriggerUpdatesConcurrently.

public void testTriggerUpdatesConcurrently() throws IOException, InterruptedException {
    List<DiscoveryNode> knownNodes = new CopyOnWriteArrayList<>();
    try (MockTransportService seedTransport = startTransport("seed_node", knownNodes, Version.CURRENT);
        MockTransportService seedTransport1 = startTransport("seed_node_1", knownNodes, Version.CURRENT);
        MockTransportService discoverableTransport = startTransport("discoverable_node", knownNodes, Version.CURRENT)) {
        DiscoveryNode seedNode = seedTransport.getLocalDiscoNode();
        DiscoveryNode discoverableNode = discoverableTransport.getLocalDiscoNode();
        DiscoveryNode seedNode1 = seedTransport1.getLocalDiscoNode();
        knownNodes.add(seedTransport.getLocalDiscoNode());
        knownNodes.add(discoverableTransport.getLocalDiscoNode());
        knownNodes.add(seedTransport1.getLocalDiscoNode());
        Collections.shuffle(knownNodes, random());
        List<DiscoveryNode> seedNodes = Arrays.asList(seedNode1, seedNode);
        Collections.shuffle(seedNodes, random());
        try (MockTransportService service = MockTransportService.createNewService(Settings.EMPTY, Version.CURRENT, threadPool, null)) {
            service.start();
            service.acceptIncomingRequests();
            try (RemoteClusterConnection connection = new RemoteClusterConnection(Settings.EMPTY, "test-cluster", seedNodes, service, Integer.MAX_VALUE, n -> true)) {
                int numThreads = randomIntBetween(4, 10);
                Thread[] threads = new Thread[numThreads];
                CyclicBarrier barrier = new CyclicBarrier(numThreads);
                for (int i = 0; i < threads.length; i++) {
                    final int numConnectionAttempts = randomIntBetween(10, 200);
                    threads[i] = new Thread() {

                        @Override
                        public void run() {
                            try {
                                barrier.await();
                                CountDownLatch latch = new CountDownLatch(numConnectionAttempts);
                                for (int i = 0; i < numConnectionAttempts; i++) {
                                    AtomicBoolean executed = new AtomicBoolean(false);
                                    ActionListener<Void> listener = ActionListener.wrap(x -> {
                                        assertTrue(executed.compareAndSet(false, true));
                                        latch.countDown();
                                    }, x -> {
                                        assertTrue(executed.compareAndSet(false, true));
                                        latch.countDown();
                                        if (x instanceof RejectedExecutionException) {
                                        } else {
                                            throw new AssertionError(x);
                                        }
                                    });
                                    connection.updateSeedNodes(seedNodes, listener);
                                }
                                latch.await();
                            } catch (Exception ex) {
                                throw new AssertionError(ex);
                            }
                        }
                    };
                    threads[i].start();
                }
                for (int i = 0; i < threads.length; i++) {
                    threads[i].join();
                }
                assertTrue(service.nodeConnected(seedNode));
                assertTrue(service.nodeConnected(discoverableNode));
                assertTrue(service.nodeConnected(seedNode1));
                assertTrue(connection.assertNoRunningConnections());
            }
        }
    }
}
Also used : CancellableThreads(org.elasticsearch.common.util.CancellableThreads) Socket(java.net.Socket) Arrays(java.util.Arrays) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) AlreadyConnectedException(java.nio.channels.AlreadyConnectedException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) ClusterSearchShardsRequest(org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsRequest) InetAddress(java.net.InetAddress) ServerSocket(java.net.ServerSocket) ClusterState(org.elasticsearch.cluster.ClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) Settings(org.elasticsearch.common.settings.Settings) ThreadPool(org.elasticsearch.threadpool.ThreadPool) ClusterName(org.elasticsearch.cluster.ClusterName) ESTestCase(org.elasticsearch.test.ESTestCase) MockTransportService(org.elasticsearch.test.transport.MockTransportService) ClusterSearchShardsAction(org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsAction) ClusterSearchShardsGroup(org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsGroup) Collections.emptyMap(java.util.Collections.emptyMap) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) CyclicBarrier(java.util.concurrent.CyclicBarrier) Collections.emptySet(java.util.Collections.emptySet) IOException(java.io.IOException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) InetSocketAddress(java.net.InetSocketAddress) UnknownHostException(java.net.UnknownHostException) MockServerSocket(org.elasticsearch.mocksocket.MockServerSocket) ClusterSearchShardsResponse(org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsResponse) ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) UncheckedIOException(java.io.UncheckedIOException) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Version(org.elasticsearch.Version) SuppressForbidden(org.elasticsearch.common.SuppressForbidden) TransportAddress(org.elasticsearch.common.transport.TransportAddress) TransportConnectionListener(org.elasticsearch.transport.TransportConnectionListener) ClusterStateRequest(org.elasticsearch.action.admin.cluster.state.ClusterStateRequest) ClusterStateAction(org.elasticsearch.action.admin.cluster.state.ClusterStateAction) Collections(java.util.Collections) ActionListener(org.elasticsearch.action.ActionListener) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) MockTransportService(org.elasticsearch.test.transport.MockTransportService) CountDownLatch(java.util.concurrent.CountDownLatch) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) AlreadyConnectedException(java.nio.channels.AlreadyConnectedException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) IOException(java.io.IOException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) UnknownHostException(java.net.UnknownHostException) UncheckedIOException(java.io.UncheckedIOException) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ActionListener(org.elasticsearch.action.ActionListener) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 37 with CyclicBarrier

use of java.util.concurrent.CyclicBarrier in project elasticsearch by elastic.

the class AdapterActionFutureTests method testInterruption.

public void testInterruption() throws Exception {
    final AdapterActionFuture<String, Integer> adapter = new AdapterActionFuture<String, Integer>() {

        @Override
        protected String convert(final Integer listenerResponse) {
            return Objects.toString(listenerResponse);
        }
    };
    // test all possible methods that can be interrupted
    final Runnable runnable = () -> {
        final int method = randomIntBetween(0, 4);
        switch(method) {
            case 0:
                adapter.actionGet();
                break;
            case 1:
                adapter.actionGet("30s");
                break;
            case 2:
                adapter.actionGet(30000);
                break;
            case 3:
                adapter.actionGet(TimeValue.timeValueSeconds(30));
                break;
            case 4:
                adapter.actionGet(30, TimeUnit.SECONDS);
                break;
            default:
                throw new AssertionError(method);
        }
    };
    final CyclicBarrier barrier = new CyclicBarrier(2);
    final Thread main = Thread.currentThread();
    final Thread thread = new Thread(() -> {
        try {
            barrier.await();
        } catch (final BrokenBarrierException | InterruptedException e) {
            throw new RuntimeException(e);
        }
        main.interrupt();
    });
    thread.start();
    final AtomicBoolean interrupted = new AtomicBoolean();
    barrier.await();
    try {
        runnable.run();
    } catch (final IllegalStateException e) {
        interrupted.set(Thread.interrupted());
    }
    // we check this here instead of in the catch block to ensure that the catch block executed
    assertTrue(interrupted.get());
    thread.join();
}
Also used : BrokenBarrierException(java.util.concurrent.BrokenBarrierException) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Example 38 with CyclicBarrier

use of java.util.concurrent.CyclicBarrier in project elasticsearch by elastic.

the class BulkWithUpdatesIT method testFailingVersionedUpdatedOnBulk.

public void testFailingVersionedUpdatedOnBulk() throws Exception {
    createIndex("test");
    index("test", "type", "1", "field", "1");
    final BulkResponse[] responses = new BulkResponse[30];
    final CyclicBarrier cyclicBarrier = new CyclicBarrier(responses.length);
    Thread[] threads = new Thread[responses.length];
    for (int i = 0; i < responses.length; i++) {
        final int threadID = i;
        threads[threadID] = new Thread(() -> {
            try {
                cyclicBarrier.await();
            } catch (Exception e) {
                return;
            }
            BulkRequestBuilder requestBuilder = client().prepareBulk();
            requestBuilder.add(client().prepareUpdate("test", "type", "1").setVersion(1).setDoc(Requests.INDEX_CONTENT_TYPE, "field", threadID));
            responses[threadID] = requestBuilder.get();
        });
        threads[threadID].start();
    }
    for (int i = 0; i < threads.length; i++) {
        threads[i].join();
    }
    int successes = 0;
    for (BulkResponse response : responses) {
        if (!response.hasFailures()) {
            successes++;
        }
    }
    assertThat(successes, equalTo(1));
}
Also used : ScriptException(org.elasticsearch.script.ScriptException) CyclicBarrier(java.util.concurrent.CyclicBarrier)

Example 39 with CyclicBarrier

use of java.util.concurrent.CyclicBarrier in project jetty.project by eclipse.

the class SameNodeLoadTest method testLoad.

@Test
@Slow
public void testLoad() throws Exception {
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
    SessionDataStoreFactory storeFactory = new TestSessionDataStoreFactory();
    String contextPath = "";
    String servletMapping = "/server";
    TestServer server1 = new TestServer(0, -1, 4, cacheFactory, storeFactory);
    server1.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
    try {
        server1.start();
        int port1 = server1.getPort();
        HttpClient client = new HttpClient();
        client.start();
        try {
            String url = "http://localhost:" + port1 + contextPath + servletMapping;
            //create session via first server
            ContentResponse response1 = client.GET(url + "?action=init");
            assertEquals(HttpServletResponse.SC_OK, response1.getStatus());
            String sessionCookie = response1.getHeaders().get("Set-Cookie");
            assertTrue(sessionCookie != null);
            // Mangle the cookie, replacing Path with $Path, etc.
            sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
            //simulate 10 clients making 100 requests each
            ExecutorService executor = Executors.newCachedThreadPool();
            int clientsCount = 10;
            CyclicBarrier barrier = new CyclicBarrier(clientsCount + 1);
            int requestsCount = 100;
            Worker[] workers = new Worker[clientsCount];
            for (int i = 0; i < clientsCount; ++i) {
                workers[i] = new Worker(barrier, client, requestsCount, sessionCookie, url);
                executor.execute(workers[i]);
            }
            // Wait for all workers to be ready
            barrier.await();
            long start = System.nanoTime();
            // Wait for all workers to be done
            barrier.await();
            long end = System.nanoTime();
            long elapsed = TimeUnit.NANOSECONDS.toMillis(end - start);
            System.err.println("Elapsed ms:" + elapsed);
            executor.shutdownNow();
            // Perform one request to get the result
            Request request = client.newRequest(url + "?action=result");
            request.header("Cookie", sessionCookie);
            ContentResponse response2 = request.send();
            assertEquals(HttpServletResponse.SC_OK, response2.getStatus());
            String response = response2.getContentAsString();
            assertEquals(response.trim(), String.valueOf(clientsCount * requestsCount));
        } finally {
            client.stop();
        }
    } finally {
        server1.stop();
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) CyclicBarrier(java.util.concurrent.CyclicBarrier) HttpClient(org.eclipse.jetty.client.HttpClient) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Example 40 with CyclicBarrier

use of java.util.concurrent.CyclicBarrier in project elastic-job by dangdangdotcom.

the class ExecutorServiceHandlerRegistryTest method assertGetExecutorServiceHandlerForConcurrent.

@Test
public void assertGetExecutorServiceHandlerForConcurrent() throws InterruptedException {
    int threadCount = 100;
    CyclicBarrier barrier = new CyclicBarrier(threadCount);
    ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
    CountDownLatch latch = new CountDownLatch(threadCount);
    Set<ExecutorService> set = new CopyOnWriteArraySet<>();
    for (int i = 0; i < threadCount; i++) {
        executorService.submit(new GetExecutorServiceHandlerTask(barrier, latch, set));
    }
    latch.await();
    assertThat(set.size(), is(1));
    assertThat(ExecutorServiceHandlerRegistry.getExecutorServiceHandler("test_job", new DefaultExecutorServiceHandler()), is(set.iterator().next()));
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) DefaultExecutorServiceHandler(com.dangdang.ddframe.job.executor.handler.impl.DefaultExecutorServiceHandler) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) CountDownLatch(java.util.concurrent.CountDownLatch) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Aggregations

CyclicBarrier (java.util.concurrent.CyclicBarrier)366 Test (org.junit.Test)128 CountDownLatch (java.util.concurrent.CountDownLatch)96 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)85 ArrayList (java.util.ArrayList)78 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)75 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)73 IOException (java.io.IOException)66 ExecutorService (java.util.concurrent.ExecutorService)45 Ignite (org.apache.ignite.Ignite)41 AtomicReference (java.util.concurrent.atomic.AtomicReference)40 IgniteException (org.apache.ignite.IgniteException)35 List (java.util.List)33 IgniteCache (org.apache.ignite.IgniteCache)27 Test (org.testng.annotations.Test)27 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)24 TimeoutException (java.util.concurrent.TimeoutException)23 Transaction (org.apache.ignite.transactions.Transaction)23 ExecutionException (java.util.concurrent.ExecutionException)22 Callable (java.util.concurrent.Callable)20