Search in sources :

Example 6 with BrokenBarrierException

use of java.util.concurrent.BrokenBarrierException in project druid by druid-io.

the class NamedIntrospectionHandler method testConcurrencyStartStoooooooooop.

@Test
public void testConcurrencyStartStoooooooooop() throws Exception {
    lookupReferencesManager.stop();
    lookupReferencesManager.start();
    final CyclicBarrier cyclicBarrier = new CyclicBarrier(CONCURRENT_THREADS);
    final Runnable start = new Runnable() {

        @Override
        public void run() {
            try {
                cyclicBarrier.await();
            } catch (InterruptedException | BrokenBarrierException e) {
                throw Throwables.propagate(e);
            }
            lookupReferencesManager.stop();
        }
    };
    final Collection<ListenableFuture<?>> futures = new ArrayList<>(CONCURRENT_THREADS);
    for (int i = 0; i < CONCURRENT_THREADS; ++i) {
        futures.add(executorService.submit(start));
    }
    Futures.allAsList(futures).get(100, TimeUnit.MILLISECONDS);
    for (ListenableFuture future : futures) {
        Assert.assertNull(future.get());
    }
}
Also used : BrokenBarrierException(java.util.concurrent.BrokenBarrierException) ArrayList(java.util.ArrayList) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Example 7 with BrokenBarrierException

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

the class AbstractSimpleTransportTestCase method testConcurrentSendRespondAndDisconnect.

public void testConcurrentSendRespondAndDisconnect() throws BrokenBarrierException, InterruptedException {
    Set<Exception> sendingErrors = ConcurrentCollections.newConcurrentSet();
    Set<Exception> responseErrors = ConcurrentCollections.newConcurrentSet();
    serviceA.registerRequestHandler("test", TestRequest::new, randomBoolean() ? ThreadPool.Names.SAME : ThreadPool.Names.GENERIC, (request, channel) -> {
        try {
            channel.sendResponse(new TestResponse());
        } catch (Exception e) {
            logger.info("caught exception while responding", e);
            responseErrors.add(e);
        }
    });
    final TransportRequestHandler<TestRequest> ignoringRequestHandler = (request, channel) -> {
        try {
            channel.sendResponse(new TestResponse());
        } catch (Exception e) {
            // we don't really care what's going on B, we're testing through A
            logger.trace("caught exception while responding from node B", e);
        }
    };
    serviceB.registerRequestHandler("test", TestRequest::new, ThreadPool.Names.SAME, ignoringRequestHandler);
    int halfSenders = scaledRandomIntBetween(3, 10);
    final CyclicBarrier go = new CyclicBarrier(halfSenders * 2 + 1);
    final CountDownLatch done = new CountDownLatch(halfSenders * 2);
    for (int i = 0; i < halfSenders; i++) {
        // B senders just generated activity so serciveA can respond, we don't test what's going on there
        final int sender = i;
        threadPool.executor(ThreadPool.Names.GENERIC).execute(new AbstractRunnable() {

            @Override
            public void onFailure(Exception e) {
                logger.trace("caught exception while sending from B", e);
            }

            @Override
            protected void doRun() throws Exception {
                go.await();
                for (int iter = 0; iter < 10; iter++) {
                    PlainActionFuture<TestResponse> listener = new PlainActionFuture<>();
                    final String info = sender + "_B_" + iter;
                    serviceB.sendRequest(nodeA, "test", new TestRequest(info), new ActionListenerResponseHandler<>(listener, TestResponse::new));
                    try {
                        listener.actionGet();
                    } catch (Exception e) {
                        logger.trace((Supplier<?>) () -> new ParameterizedMessage("caught exception while sending to node {}", nodeA), e);
                    }
                }
            }

            @Override
            public void onAfter() {
                done.countDown();
            }
        });
    }
    for (int i = 0; i < halfSenders; i++) {
        final int sender = i;
        threadPool.executor(ThreadPool.Names.GENERIC).execute(new AbstractRunnable() {

            @Override
            public void onFailure(Exception e) {
                logger.error("unexpected error", e);
                sendingErrors.add(e);
            }

            @Override
            protected void doRun() throws Exception {
                go.await();
                for (int iter = 0; iter < 10; iter++) {
                    PlainActionFuture<TestResponse> listener = new PlainActionFuture<>();
                    final String info = sender + "_" + iter;
                    // capture now
                    final DiscoveryNode node = nodeB;
                    try {
                        serviceA.sendRequest(node, "test", new TestRequest(info), new ActionListenerResponseHandler<>(listener, TestResponse::new));
                        try {
                            listener.actionGet();
                        } catch (ConnectTransportException e) {
                        // ok!
                        } catch (Exception e) {
                            logger.error((Supplier<?>) () -> new ParameterizedMessage("caught exception while sending to node {}", node), e);
                            sendingErrors.add(e);
                        }
                    } catch (NodeNotConnectedException ex) {
                    // ok
                    }
                }
            }

            @Override
            public void onAfter() {
                done.countDown();
            }
        });
    }
    go.await();
    for (int i = 0; i <= 10; i++) {
        if (i % 3 == 0) {
            // simulate restart of nodeB
            serviceB.close();
            MockTransportService newService = buildService("TS_B_" + i, version1, null);
            newService.registerRequestHandler("test", TestRequest::new, ThreadPool.Names.SAME, ignoringRequestHandler);
            serviceB = newService;
            nodeB = newService.getLocalDiscoNode();
            serviceB.connectToNode(nodeA);
            serviceA.connectToNode(nodeB);
        } else if (serviceA.nodeConnected(nodeB)) {
            serviceA.disconnectFromNode(nodeB);
        } else {
            serviceA.connectToNode(nodeB);
        }
    }
    done.await();
    assertThat("found non connection errors while sending", sendingErrors, empty());
    assertThat("found non connection errors while responding", responseErrors, empty());
}
Also used : ElasticsearchException(org.elasticsearch.ElasticsearchException) StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) Arrays(java.util.Arrays) BigArrays(org.elasticsearch.common.util.BigArrays) ConcurrentCollections(org.elasticsearch.common.util.concurrent.ConcurrentCollections) InetAddress(java.net.InetAddress) ServerSocket(java.net.ServerSocket) Settings(org.elasticsearch.common.settings.Settings) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) Map(java.util.Map) ThreadPool(org.elasticsearch.threadpool.ThreadPool) CyclicBarrier(java.util.concurrent.CyclicBarrier) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) Set(java.util.Set) InetSocketAddress(java.net.InetSocketAddress) Matchers.startsWith(org.hamcrest.Matchers.startsWith) UncheckedIOException(java.io.UncheckedIOException) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) List(java.util.List) Version(org.elasticsearch.Version) TransportAddress(org.elasticsearch.common.transport.TransportAddress) Supplier(org.apache.logging.log4j.util.Supplier) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Socket(java.net.Socket) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) NetworkService(org.elasticsearch.common.network.NetworkService) ActionListenerResponseHandler(org.elasticsearch.action.ActionListenerResponseHandler) NoneCircuitBreakerService(org.elasticsearch.indices.breaker.NoneCircuitBreakerService) NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry) TimeValue(org.elasticsearch.common.unit.TimeValue) Node(org.elasticsearch.node.Node) ESTestCase(org.elasticsearch.test.ESTestCase) MockTransportService(org.elasticsearch.test.transport.MockTransportService) Before(org.junit.Before) Collections.emptyMap(java.util.Collections.emptyMap) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) Matchers.empty(org.hamcrest.Matchers.empty) Collections.emptySet(java.util.Collections.emptySet) Semaphore(java.util.concurrent.Semaphore) IOUtils(org.apache.lucene.util.IOUtils) IOException(java.io.IOException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) MockServerSocket(org.elasticsearch.mocksocket.MockServerSocket) VersionUtils(org.elasticsearch.test.VersionUtils) CollectionUtil(org.apache.lucene.util.CollectionUtil) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) ExceptionsHelper(org.elasticsearch.ExceptionsHelper) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) Constants(org.apache.lucene.util.Constants) StreamInput(org.elasticsearch.common.io.stream.StreamInput) Collections(java.util.Collections) AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) MockTransportService(org.elasticsearch.test.transport.MockTransportService) CountDownLatch(java.util.concurrent.CountDownLatch) ElasticsearchException(org.elasticsearch.ElasticsearchException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) ExecutionException(java.util.concurrent.ExecutionException) CyclicBarrier(java.util.concurrent.CyclicBarrier) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) ActionListenerResponseHandler(org.elasticsearch.action.ActionListenerResponseHandler) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage)

Example 8 with BrokenBarrierException

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

the class CacheTests method testDependentKeyDeadlock.

public void testDependentKeyDeadlock() throws BrokenBarrierException, InterruptedException {
    class Key {

        private final int key;

        Key(int key) {
            this.key = key;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o)
                return true;
            if (o == null || getClass() != o.getClass())
                return false;
            Key key1 = (Key) o;
            return key == key1.key;
        }

        @Override
        public int hashCode() {
            return key % 2;
        }
    }
    int numberOfThreads = randomIntBetween(2, 32);
    final Cache<Key, Integer> cache = CacheBuilder.<Key, Integer>builder().build();
    CopyOnWriteArrayList<ExecutionException> failures = new CopyOnWriteArrayList<>();
    CyclicBarrier barrier = new CyclicBarrier(1 + numberOfThreads);
    CountDownLatch deadlockLatch = new CountDownLatch(numberOfThreads);
    List<Thread> threads = new ArrayList<>();
    for (int i = 0; i < numberOfThreads; i++) {
        Thread thread = new Thread(() -> {
            try {
                try {
                    barrier.await();
                } catch (BrokenBarrierException | InterruptedException e) {
                    throw new AssertionError(e);
                }
                Random random = new Random(random().nextLong());
                for (int j = 0; j < numberOfEntries; j++) {
                    Key key = new Key(random.nextInt(numberOfEntries));
                    try {
                        cache.computeIfAbsent(key, k -> {
                            if (k.key == 0) {
                                return 0;
                            } else {
                                Integer value = cache.get(new Key(k.key / 2));
                                return value != null ? value : 0;
                            }
                        });
                    } catch (ExecutionException e) {
                        failures.add(e);
                        break;
                    }
                }
            } finally {
                // successfully avoided deadlock, release the main thread
                deadlockLatch.countDown();
            }
        });
        threads.add(thread);
        thread.start();
    }
    AtomicBoolean deadlock = new AtomicBoolean();
    assert !deadlock.get();
    // start a watchdog service
    ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
    scheduler.scheduleAtFixedRate(() -> {
        Set<Long> ids = threads.stream().map(t -> t.getId()).collect(Collectors.toSet());
        ThreadMXBean mxBean = ManagementFactory.getThreadMXBean();
        long[] deadlockedThreads = mxBean.findDeadlockedThreads();
        if (!deadlock.get() && deadlockedThreads != null) {
            for (long deadlockedThread : deadlockedThreads) {
                // ensure that we detected deadlock on our threads
                if (ids.contains(deadlockedThread)) {
                    deadlock.set(true);
                    // release the main test thread to fail the test
                    for (int i = 0; i < numberOfThreads; i++) {
                        deadlockLatch.countDown();
                    }
                    break;
                }
            }
        }
    }, 1, 1, TimeUnit.SECONDS);
    // everything is setup, release the hounds
    barrier.await();
    // wait for either deadlock to be detected or the threads to terminate
    deadlockLatch.await();
    // shutdown the watchdog service
    scheduler.shutdown();
    assertThat(failures, is(empty()));
    assertFalse("deadlock", deadlock.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) Random(java.util.Random) ArrayList(java.util.ArrayList) AtomicReferenceArray(java.util.concurrent.atomic.AtomicReferenceArray) HashSet(java.util.HashSet) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Map(java.util.Map) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ESTestCase(org.elasticsearch.test.ESTestCase) ManagementFactory(java.lang.management.ManagementFactory) Before(org.junit.Before) CyclicBarrier(java.util.concurrent.CyclicBarrier) Matchers.empty(org.hamcrest.Matchers.empty) Set(java.util.Set) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) ThreadMXBean(java.lang.management.ThreadMXBean) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Matchers.is(org.hamcrest.Matchers.is) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Random(java.util.Random) ExecutionException(java.util.concurrent.ExecutionException) ThreadMXBean(java.lang.management.ThreadMXBean) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 9 with BrokenBarrierException

use of java.util.concurrent.BrokenBarrierException 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 10 with BrokenBarrierException

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

the class NodeJoinControllerTests method testElectionWithConcurrentJoins.

public void testElectionWithConcurrentJoins() throws InterruptedException, BrokenBarrierException {
    DiscoveryNodes.Builder nodesBuilder = DiscoveryNodes.builder(clusterService.state().nodes()).masterNodeId(null);
    setState(clusterService, ClusterState.builder(clusterService.state()).nodes(nodesBuilder));
    nodeJoinController.startElectionContext();
    Thread[] threads = new Thread[3 + randomInt(5)];
    final int requiredJoins = randomInt(threads.length);
    ArrayList<DiscoveryNode> nodes = new ArrayList<>();
    nodes.add(clusterService.localNode());
    final CyclicBarrier barrier = new CyclicBarrier(threads.length + 1);
    final List<Throwable> backgroundExceptions = new CopyOnWriteArrayList<>();
    for (int i = 0; i < threads.length; i++) {
        final DiscoveryNode node = newNode(i, true);
        final int iterations = rarely() ? randomIntBetween(1, 4) : 1;
        nodes.add(node);
        threads[i] = new Thread(new AbstractRunnable() {

            @Override
            public void onFailure(Exception e) {
                logger.error("unexpected error in join thread", e);
                backgroundExceptions.add(e);
            }

            @Override
            protected void doRun() throws Exception {
                barrier.await();
                for (int i = 0; i < iterations; i++) {
                    logger.debug("{} joining", node);
                    joinNode(node);
                }
            }
        }, "t_" + i);
        threads[i].start();
    }
    barrier.await();
    logger.info("--> waiting to be elected as master (required joins [{}])", requiredJoins);
    final AtomicReference<Throwable> failure = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    nodeJoinController.waitToBeElectedAsMaster(requiredJoins, TimeValue.timeValueHours(30), new NodeJoinController.ElectionCallback() {

        @Override
        public void onElectedAsMaster(ClusterState state) {
            assertThat("callback called with elected as master, but state disagrees", state.nodes().isLocalNodeElectedMaster(), equalTo(true));
            latch.countDown();
        }

        @Override
        public void onFailure(Throwable t) {
            logger.error("unexpected error while waiting to be elected as master", t);
            failure.set(t);
            latch.countDown();
        }
    });
    latch.await();
    ExceptionsHelper.reThrowIfNotNull(failure.get());
    logger.info("--> waiting for joins to complete");
    for (Thread thread : threads) {
        thread.join();
    }
    assertNodesInCurrentState(nodes);
}
Also used : AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) ClusterState(org.elasticsearch.cluster.ClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) NotMasterException(org.elasticsearch.cluster.NotMasterException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) ExecutionException(java.util.concurrent.ExecutionException) CyclicBarrier(java.util.concurrent.CyclicBarrier) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Aggregations

BrokenBarrierException (java.util.concurrent.BrokenBarrierException)62 CyclicBarrier (java.util.concurrent.CyclicBarrier)53 Test (org.junit.Test)25 ArrayList (java.util.ArrayList)16 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)15 IOException (java.io.IOException)14 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)14 CountDownLatch (java.util.concurrent.CountDownLatch)12 ExecutionException (java.util.concurrent.ExecutionException)11 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 List (java.util.List)9 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)9 HashMap (java.util.HashMap)8 HashSet (java.util.HashSet)8 Map (java.util.Map)8 TimeoutException (java.util.concurrent.TimeoutException)8 Random (java.util.Random)7 YarnConfiguration (org.apache.hadoop.yarn.conf.YarnConfiguration)7 Set (java.util.Set)6 TimeUnit (java.util.concurrent.TimeUnit)6