Search in sources :

Example 11 with BrokenBarrierException

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

the class InternalEngineTests method testSequenceNumberAdvancesToMaxSeqOnEngineOpenOnPrimary.

public void testSequenceNumberAdvancesToMaxSeqOnEngineOpenOnPrimary() throws BrokenBarrierException, InterruptedException, IOException {
    engine.close();
    final int docs = randomIntBetween(1, 32);
    InternalEngine initialEngine = null;
    try {
        final CountDownLatch latch = new CountDownLatch(1);
        final CyclicBarrier barrier = new CyclicBarrier(2);
        final AtomicBoolean skip = new AtomicBoolean();
        final AtomicLong expectedLocalCheckpoint = new AtomicLong(SequenceNumbersService.NO_OPS_PERFORMED);
        final List<Thread> threads = new ArrayList<>();
        final SequenceNumbersService seqNoService = new SequenceNumbersService(shardId, defaultSettings, SequenceNumbersService.NO_OPS_PERFORMED, SequenceNumbersService.NO_OPS_PERFORMED, SequenceNumbersService.UNASSIGNED_SEQ_NO) {

            @Override
            public long generateSeqNo() {
                final long seqNo = super.generateSeqNo();
                if (skip.get()) {
                    try {
                        barrier.await();
                        latch.await();
                    } catch (BrokenBarrierException | InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                } else {
                    if (expectedLocalCheckpoint.get() + 1 == seqNo) {
                        expectedLocalCheckpoint.set(seqNo);
                    }
                }
                return seqNo;
            }
        };
        initialEngine = createEngine(defaultSettings, store, primaryTranslogDir, newMergePolicy(), null, () -> seqNoService);
        final InternalEngine finalInitialEngine = initialEngine;
        for (int i = 0; i < docs; i++) {
            final String id = Integer.toString(i);
            final ParsedDocument doc = testParsedDocument(id, "test", null, testDocumentWithTextField(), SOURCE, null);
            skip.set(randomBoolean());
            final Thread thread = new Thread(() -> {
                try {
                    finalInitialEngine.index(indexForDoc(doc));
                } catch (IOException e) {
                    throw new AssertionError(e);
                }
            });
            thread.start();
            if (skip.get()) {
                threads.add(thread);
                barrier.await();
            } else {
                thread.join();
            }
        }
        assertThat(initialEngine.seqNoService().getLocalCheckpoint(), equalTo(expectedLocalCheckpoint.get()));
        assertThat(initialEngine.seqNoService().getMaxSeqNo(), equalTo((long) (docs - 1)));
        initialEngine.flush(true, true);
        latch.countDown();
        for (final Thread thread : threads) {
            thread.join();
        }
    } finally {
        IOUtils.close(initialEngine);
    }
    try (Engine recoveringEngine = new InternalEngine(copy(initialEngine.config(), EngineConfig.OpenMode.OPEN_INDEX_AND_TRANSLOG))) {
        assertThat(recoveringEngine.seqNoService().getLocalCheckpoint(), greaterThanOrEqualTo((long) (docs - 1)));
    }
}
Also used : BrokenBarrierException(java.util.concurrent.BrokenBarrierException) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) LongPoint(org.apache.lucene.document.LongPoint) CyclicBarrier(java.util.concurrent.CyclicBarrier) SequenceNumbersService(org.elasticsearch.index.seqno.SequenceNumbersService) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument)

Example 12 with BrokenBarrierException

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

the class TasksIT method testNodeNotFoundButTaskFound.

public void testNodeNotFoundButTaskFound() throws Exception {
    // Save a fake task that looks like it is from a node that isn't part of the cluster
    CyclicBarrier b = new CyclicBarrier(2);
    TaskResultsService resultsService = internalCluster().getInstance(TaskResultsService.class);
    resultsService.storeResult(new TaskResult(new TaskInfo(new TaskId("fake", 1), "test", "test", "", null, 0, 0, false, TaskId.EMPTY_TASK_ID), new RuntimeException("test")), new ActionListener<Void>() {

        @Override
        public void onResponse(Void response) {
            try {
                b.await();
            } catch (InterruptedException | BrokenBarrierException e) {
                onFailure(e);
            }
        }

        @Override
        public void onFailure(Exception e) {
            throw new RuntimeException(e);
        }
    });
    b.await();
    // Now we can find it!
    GetTaskResponse response = expectFinishedTask(new TaskId("fake:1"));
    assertEquals("test", response.getTask().getTask().getAction());
    assertNotNull(response.getTask().getError());
    assertNull(response.getTask().getResponse());
}
Also used : TaskInfo(org.elasticsearch.tasks.TaskInfo) TaskId(org.elasticsearch.tasks.TaskId) TaskResult(org.elasticsearch.tasks.TaskResult) GetTaskResponse(org.elasticsearch.action.admin.cluster.node.tasks.get.GetTaskResponse) TaskResultsService(org.elasticsearch.tasks.TaskResultsService) FailedNodeException(org.elasticsearch.action.FailedNodeException) ResourceNotFoundException(org.elasticsearch.ResourceNotFoundException) ReceiveTimeoutTransportException(org.elasticsearch.transport.ReceiveTimeoutTransportException) ElasticsearchTimeoutException(org.elasticsearch.ElasticsearchTimeoutException) IOException(java.io.IOException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) CyclicBarrier(java.util.concurrent.CyclicBarrier)

Example 13 with BrokenBarrierException

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

the class RemoteClusterConnectionTests method testCloseWhileConcurrentlyConnecting.

public void testCloseWhileConcurrentlyConnecting() throws IOException, InterruptedException, BrokenBarrierException {
    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 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 + 1);
                for (int i = 0; i < threads.length; i++) {
                    final int numConnectionAttempts = randomIntBetween(10, 100);
                    threads[i] = new Thread() {

                        @Override
                        public void run() {
                            try {
                                barrier.await();
                                CountDownLatch latch = new CountDownLatch(numConnectionAttempts);
                                for (int i = 0; i < numConnectionAttempts; i++) {
                                    AtomicReference<RuntimeException> executed = new AtomicReference<>();
                                    ActionListener<Void> listener = ActionListener.wrap(x -> {
                                        if (executed.compareAndSet(null, new RuntimeException())) {
                                            latch.countDown();
                                        } else {
                                            throw new AssertionError("shit's been called twice", executed.get());
                                        }
                                    }, x -> {
                                        if (executed.compareAndSet(null, new RuntimeException())) {
                                            latch.countDown();
                                        } else {
                                            throw new AssertionError("shit's been called twice", executed.get());
                                        }
                                        if (x instanceof RejectedExecutionException || x instanceof AlreadyClosedException || x instanceof CancellableThreads.ExecutionCancelledException) {
                                        } else {
                                            throw new AssertionError(x);
                                        }
                                    });
                                    connection.updateSeedNodes(seedNodes, listener);
                                }
                                latch.await();
                            } catch (Exception ex) {
                                throw new AssertionError(ex);
                            }
                        }
                    };
                    threads[i].start();
                }
                barrier.await();
                connection.close();
            }
        }
    }
}
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) AtomicReference(java.util.concurrent.atomic.AtomicReference) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) 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) ActionListener(org.elasticsearch.action.ActionListener) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 14 with BrokenBarrierException

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

use of java.util.concurrent.BrokenBarrierException in project jersey by jersey.

the class ParallelTest method testParallel.

@Test
public void testParallel() throws BrokenBarrierException, InterruptedException, TimeoutException {
    final ScheduledExecutorService executor = Executors.newScheduledThreadPool(PARALLEL_CLIENTS);
    try {
        final WebTarget target = target();
        for (int i = 1; i <= PARALLEL_CLIENTS; i++) {
            final int id = i;
            executor.submit(new Runnable() {

                @Override
                public void run() {
                    try {
                        startBarrier.await();
                        Response response;
                        response = target.path(PATH).request().get();
                        assertEquals("GET", response.readEntity(String.class));
                        receivedCounter.incrementAndGet();
                    } catch (InterruptedException ex) {
                        Thread.currentThread().interrupt();
                        LOGGER.log(Level.WARNING, "Client thread " + id + " interrupted.", ex);
                    } catch (BrokenBarrierException ex) {
                        LOGGER.log(Level.INFO, "Client thread " + id + " failed on broken barrier.", ex);
                    } catch (Throwable t) {
                        t.printStackTrace();
                        LOGGER.log(Level.WARNING, "Client thread " + id + " failed on unexpected exception.", t);
                    } finally {
                        doneLatch.countDown();
                    }
                }
            });
        }
        startBarrier.await(1, TimeUnit.SECONDS);
        assertTrue("Waiting for clients to finish has timed out.", doneLatch.await(5 * getAsyncTimeoutMultiplier(), TimeUnit.SECONDS));
        assertEquals("Resource counter", PARALLEL_CLIENTS, resourceCounter.get());
        assertEquals("Received counter", PARALLEL_CLIENTS, receivedCounter.get());
    } finally {
        executor.shutdownNow();
        Assert.assertTrue("Executor termination", executor.awaitTermination(5, TimeUnit.SECONDS));
    }
}
Also used : Response(javax.ws.rs.core.Response) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) WebTarget(javax.ws.rs.client.WebTarget) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

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