Search in sources :

Example 36 with AbstractRunnable

use of org.opensearch.common.util.concurrent.AbstractRunnable in project OpenSearch by opensearch-project.

the class HandshakingTransportAddressConnector method connectToRemoteMasterNode.

@Override
public void connectToRemoteMasterNode(TransportAddress transportAddress, ActionListener<DiscoveryNode> listener) {
    transportService.getThreadPool().generic().execute(new AbstractRunnable() {

        private final AbstractRunnable thisConnectionAttempt = this;

        @Override
        protected void doRun() {
            // We could skip this if the transportService were already connected to the given address, but the savings would be minimal
            // so we open a new connection anyway.
            final DiscoveryNode targetNode = new DiscoveryNode("", transportAddress.toString(), // generated deterministically for reproducible tests
            UUIDs.randomBase64UUID(Randomness.get()), transportAddress.address().getHostString(), transportAddress.getAddress(), transportAddress, emptyMap(), emptySet(), Version.CURRENT.minimumCompatibilityVersion());
            logger.trace("[{}] opening probe connection", thisConnectionAttempt);
            transportService.openConnection(targetNode, ConnectionProfile.buildSingleChannelProfile(Type.REG, probeConnectTimeout, probeHandshakeTimeout, TimeValue.MINUS_ONE, null), new ActionListener<Connection>() {

                @Override
                public void onResponse(Connection connection) {
                    logger.trace("[{}] opened probe connection", thisConnectionAttempt);
                    // use NotifyOnceListener to make sure the following line does not result in onFailure being called when
                    // the connection is closed in the onResponse handler
                    transportService.handshake(connection, probeHandshakeTimeout.millis(), new NotifyOnceListener<DiscoveryNode>() {

                        @Override
                        protected void innerOnResponse(DiscoveryNode remoteNode) {
                            try {
                                // success means (amongst other things) that the cluster names match
                                logger.trace("[{}] handshake successful: {}", thisConnectionAttempt, remoteNode);
                                IOUtils.closeWhileHandlingException(connection);
                                if (remoteNode.equals(transportService.getLocalNode())) {
                                    listener.onFailure(new ConnectTransportException(remoteNode, "local node found"));
                                } else if (remoteNode.isMasterNode() == false) {
                                    listener.onFailure(new ConnectTransportException(remoteNode, "non-cluster-manager-eligible node found"));
                                } else {
                                    transportService.connectToNode(remoteNode, new ActionListener<Void>() {

                                        @Override
                                        public void onResponse(Void ignored) {
                                            logger.trace("[{}] completed full connection with [{}]", thisConnectionAttempt, remoteNode);
                                            listener.onResponse(remoteNode);
                                        }

                                        @Override
                                        public void onFailure(Exception e) {
                                            // we opened a connection and successfully performed a handshake, so we're definitely
                                            // talking to a cluster-manager-eligible node with a matching cluster name and a good
                                            // version,
                                            // but the attempt to open a full connection to its publish address failed; a common
                                            // reason is that the remote node is listening on 0.0.0.0 but has made an inappropriate
                                            // choice for its publish address.
                                            logger.warn(new ParameterizedMessage("[{}] completed handshake with [{}] but followup connection failed", thisConnectionAttempt, remoteNode), e);
                                            listener.onFailure(e);
                                        }
                                    });
                                }
                            } catch (Exception e) {
                                listener.onFailure(e);
                            }
                        }

                        @Override
                        protected void innerOnFailure(Exception e) {
                            // we opened a connection and successfully performed a low-level handshake, so we were definitely
                            // talking to an Elasticsearch node, but the high-level handshake failed indicating some kind of
                            // mismatched configurations (e.g. cluster name) that the user should address
                            logger.warn(new ParameterizedMessage("handshake failed for [{}]", thisConnectionAttempt), e);
                            IOUtils.closeWhileHandlingException(connection);
                            listener.onFailure(e);
                        }
                    });
                }

                @Override
                public void onFailure(Exception e) {
                    listener.onFailure(e);
                }
            });
        }

        @Override
        public void onFailure(Exception e) {
            listener.onFailure(e);
        }

        @Override
        public String toString() {
            return "connectToRemoteMasterNode[" + transportAddress + "]";
        }
    });
}
Also used : AbstractRunnable(org.opensearch.common.util.concurrent.AbstractRunnable) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) ActionListener(org.opensearch.action.ActionListener) ConnectTransportException(org.opensearch.transport.ConnectTransportException) Connection(org.opensearch.transport.Transport.Connection) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) ConnectTransportException(org.opensearch.transport.ConnectTransportException)

Example 37 with AbstractRunnable

use of org.opensearch.common.util.concurrent.AbstractRunnable in project OpenSearch by opensearch-project.

the class AsyncBulkByScrollActionTests method testThreadPoolRejectionsAbortRequest.

/**
 * Mimicks a ThreadPool rejecting execution of the task.
 */
public void testThreadPoolRejectionsAbortRequest() throws Exception {
    worker.rethrottle(1);
    setupClient(new TestThreadPool(getTestName()) {

        @Override
        public ScheduledCancellable schedule(Runnable command, TimeValue delay, String name) {
            // While we're here we can check that the sleep made it through
            assertThat(delay.nanos(), greaterThan(0L));
            assertThat(delay.seconds(), lessThanOrEqualTo(10L));
            final OpenSearchRejectedExecutionException exception = new OpenSearchRejectedExecutionException("test");
            if (command instanceof AbstractRunnable) {
                ((AbstractRunnable) command).onRejection(exception);
                return null;
            } else {
                throw exception;
            }
        }
    });
    ScrollableHitSource.Response response = new ScrollableHitSource.Response(false, emptyList(), 0, emptyList(), null);
    simulateScrollResponse(new DummyAsyncBulkByScrollAction(), System.nanoTime(), 10, response);
    ExecutionException e = expectThrows(ExecutionException.class, () -> listener.get());
    assertThat(e.getCause(), instanceOf(OpenSearchRejectedExecutionException.class));
    assertThat(e.getCause(), hasToString(containsString("test")));
    assertThat(client.scrollsCleared, contains(scrollId));
    // When the task is rejected we don't increment the throttled timer
    assertEquals(timeValueMillis(0), testTask.getStatus().getThrottled());
}
Also used : AbstractRunnable(org.opensearch.common.util.concurrent.AbstractRunnable) OpenSearchRejectedExecutionException(org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Matchers.containsString(org.hamcrest.Matchers.containsString) TestUtil.randomSimpleString(org.apache.lucene.tests.util.TestUtil.randomSimpleString) TestThreadPool(org.opensearch.threadpool.TestThreadPool) IndexResponse(org.opensearch.action.index.IndexResponse) ActionResponse(org.opensearch.action.ActionResponse) DocWriteResponse(org.opensearch.action.DocWriteResponse) UpdateResponse(org.opensearch.action.update.UpdateResponse) DeleteResponse(org.opensearch.action.delete.DeleteResponse) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse) BulkItemResponse(org.opensearch.action.bulk.BulkItemResponse) ClearScrollResponse(org.opensearch.action.search.ClearScrollResponse) SearchResponse(org.opensearch.action.search.SearchResponse) BulkResponse(org.opensearch.action.bulk.BulkResponse) AbstractRunnable(org.opensearch.common.util.concurrent.AbstractRunnable) OpenSearchRejectedExecutionException(org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) SearchPhaseExecutionException(org.opensearch.action.search.SearchPhaseExecutionException) TimeValue(org.opensearch.common.unit.TimeValue)

Example 38 with AbstractRunnable

use of org.opensearch.common.util.concurrent.AbstractRunnable in project OpenSearch by opensearch-project.

the class EvilThreadPoolTests method checkExecutionError.

private void checkExecutionError(Consumer<Runnable> runner) throws InterruptedException {
    logger.info("checking error for {}", runner);
    final Runnable runnable;
    if (randomBoolean()) {
        runnable = () -> {
            throw new Error("future error");
        };
    } else {
        runnable = new AbstractRunnable() {

            @Override
            public void onFailure(Exception e) {
            }

            @Override
            protected void doRun() {
                throw new Error("future error");
            }
        };
    }
    runExecutionTest(runner, runnable, true, o -> {
        assertTrue(o.isPresent());
        assertThat(o.get(), instanceOf(Error.class));
        assertThat(o.get(), hasToString(containsString("future error")));
    });
}
Also used : AbstractRunnable(org.opensearch.common.util.concurrent.AbstractRunnable) AbstractRunnable(org.opensearch.common.util.concurrent.AbstractRunnable)

Example 39 with AbstractRunnable

use of org.opensearch.common.util.concurrent.AbstractRunnable in project OpenSearch by opensearch-project.

the class EvilThreadPoolTests method checkExecutionException.

private void checkExecutionException(Consumer<Runnable> runner, boolean expectException) throws InterruptedException {
    final Runnable runnable;
    final boolean willThrow;
    if (randomBoolean()) {
        logger.info("checking direct exception for {}", runner);
        runnable = () -> {
            throw new IllegalStateException("future exception");
        };
        willThrow = expectException;
    } else {
        logger.info("checking abstract runnable exception for {}", runner);
        runnable = new AbstractRunnable() {

            @Override
            public void onFailure(Exception e) {
            }

            @Override
            protected void doRun() {
                throw new IllegalStateException("future exception");
            }
        };
        willThrow = false;
    }
    runExecutionTest(runner, runnable, willThrow, o -> {
        assertEquals(willThrow, o.isPresent());
        if (willThrow) {
            if (o.get() instanceof Error)
                throw (Error) o.get();
            assertThat(o.get(), instanceOf(IllegalStateException.class));
            assertThat(o.get(), hasToString(containsString("future exception")));
        }
    });
}
Also used : AbstractRunnable(org.opensearch.common.util.concurrent.AbstractRunnable) AbstractRunnable(org.opensearch.common.util.concurrent.AbstractRunnable)

Example 40 with AbstractRunnable

use of org.opensearch.common.util.concurrent.AbstractRunnable in project OpenSearch by opensearch-project.

the class RecoveryIT method asyncIndexDocs.

private Future<Void> asyncIndexDocs(String index, final int idStart, final int numDocs) throws IOException {
    PlainActionFuture<Void> future = new PlainActionFuture<>();
    Thread background = new Thread(new AbstractRunnable() {

        @Override
        public void onFailure(Exception e) {
            future.onFailure(e);
        }

        @Override
        protected void doRun() throws Exception {
            indexDocs(index, idStart, numDocs);
            future.onResponse(null);
        }
    });
    background.start();
    return future;
}
Also used : AbstractRunnable(org.opensearch.common.util.concurrent.AbstractRunnable) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) IOException(java.io.IOException) ResponseException(org.opensearch.client.ResponseException)

Aggregations

AbstractRunnable (org.opensearch.common.util.concurrent.AbstractRunnable)42 IOException (java.io.IOException)29 OpenSearchException (org.opensearch.OpenSearchException)14 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)12 AlreadyClosedException (org.apache.lucene.store.AlreadyClosedException)11 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)10 CountDownLatch (java.util.concurrent.CountDownLatch)10 UncheckedIOException (java.io.UncheckedIOException)9 TimeValue (org.opensearch.common.unit.TimeValue)9 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)8 UnknownHostException (java.net.UnknownHostException)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)7 ExecutionException (java.util.concurrent.ExecutionException)6 ActionListener (org.opensearch.action.ActionListener)6 StreamInput (org.opensearch.common.io.stream.StreamInput)6 CyclicBarrier (java.util.concurrent.CyclicBarrier)5 OpenSearchRejectedExecutionException (org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException)5 IndexNotFoundException (org.opensearch.index.IndexNotFoundException)5 ThreadPool (org.opensearch.threadpool.ThreadPool)5 ArrayList (java.util.ArrayList)4