Search in sources :

Example 21 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)

Example 22 with AbstractRunnable

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

the class FollowersChecker method handleFollowerCheck.

private void handleFollowerCheck(FollowerCheckRequest request, TransportChannel transportChannel) throws IOException {
    final StatusInfo statusInfo = nodeHealthService.getHealth();
    if (statusInfo.getStatus() == UNHEALTHY) {
        final String message = "handleFollowerCheck: node is unhealthy [" + statusInfo.getInfo() + "], rejecting " + statusInfo.getInfo();
        logger.debug(message);
        throw new NodeHealthCheckFailureException(message);
    }
    final FastResponseState responder = this.fastResponseState;
    if (responder.mode == Mode.FOLLOWER && responder.term == request.term) {
        logger.trace("responding to {} on fast path", request);
        transportChannel.sendResponse(Empty.INSTANCE);
        return;
    }
    if (request.term < responder.term) {
        throw new CoordinationStateRejectedException("rejecting " + request + " since local state is " + this);
    }
    transportService.getThreadPool().generic().execute(new AbstractRunnable() {

        @Override
        protected void doRun() throws IOException {
            logger.trace("responding to {} on slow path", request);
            try {
                handleRequestAndUpdateState.accept(request);
            } catch (Exception e) {
                transportChannel.sendResponse(e);
                return;
            }
            transportChannel.sendResponse(Empty.INSTANCE);
        }

        @Override
        public void onFailure(Exception e) {
            logger.debug(new ParameterizedMessage("exception while responding to {}", request), e);
        }

        @Override
        public String toString() {
            return "slow path response to " + request;
        }
    });
}
Also used : AbstractRunnable(org.opensearch.common.util.concurrent.AbstractRunnable) StatusInfo(org.opensearch.monitor.StatusInfo) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) IOException(java.io.IOException) IOException(java.io.IOException) ConnectTransportException(org.opensearch.transport.ConnectTransportException) TransportException(org.opensearch.transport.TransportException)

Example 23 with AbstractRunnable

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

the class NioSelectorTests method executeOnNewThread.

private static void executeOnNewThread(CheckedRunnable<Exception> runnable) throws InterruptedException {
    final Thread thread = new Thread(new AbstractRunnable() {

        @Override
        protected void doRun() throws Exception {
            runnable.run();
        }

        @Override
        public void onFailure(Exception e) {
            throw new AssertionError(e);
        }
    });
    thread.start();
    thread.join();
}
Also used : AbstractRunnable(org.opensearch.common.util.concurrent.AbstractRunnable) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) ClosedSelectorException(java.nio.channels.ClosedSelectorException)

Example 24 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.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 25 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)

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