Search in sources :

Example 76 with Releasable

use of org.opensearch.common.lease.Releasable in project OpenSearch by opensearch-project.

the class TransportService method sendRequest.

/**
 * Sends a request on the specified connection. If there is a failure sending the request, the specified handler is invoked.
 *
 * @param connection the connection to send the request on
 * @param action     the name of the action
 * @param request    the request
 * @param options    the options for this request
 * @param handler    the response handler
 * @param <T>        the type of the transport response
 */
public final <T extends TransportResponse> void sendRequest(final Transport.Connection connection, final String action, final TransportRequest request, final TransportRequestOptions options, final TransportResponseHandler<T> handler) {
    try {
        final TransportResponseHandler<T> delegate;
        if (request.getParentTask().isSet()) {
            // TODO: capture the connection instead so that we can cancel child tasks on the remote connections.
            final Releasable unregisterChildNode = taskManager.registerChildNode(request.getParentTask().getId(), connection.getNode());
            delegate = new TransportResponseHandler<T>() {

                @Override
                public void handleResponse(T response) {
                    unregisterChildNode.close();
                    handler.handleResponse(response);
                }

                @Override
                public void handleException(TransportException exp) {
                    unregisterChildNode.close();
                    handler.handleException(exp);
                }

                @Override
                public String executor() {
                    return handler.executor();
                }

                @Override
                public T read(StreamInput in) throws IOException {
                    return handler.read(in);
                }

                @Override
                public String toString() {
                    return getClass().getName() + "/[" + action + "]:" + handler.toString();
                }
            };
        } else {
            delegate = handler;
        }
        asyncSender.sendRequest(connection, action, request, options, delegate);
    } catch (final Exception ex) {
        // the caller might not handle this so we invoke the handler
        final TransportException te;
        if (ex instanceof TransportException) {
            te = (TransportException) ex;
        } else {
            te = new TransportException("failure to send", ex);
        }
        handler.handleException(te);
    }
}
Also used : StreamInput(org.opensearch.common.io.stream.StreamInput) Releasable(org.opensearch.common.lease.Releasable) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) OpenSearchRejectedExecutionException(org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException) NodeClosedException(org.opensearch.node.NodeClosedException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Example 77 with Releasable

use of org.opensearch.common.lease.Releasable in project OpenSearch by opensearch-project.

the class InboundMessage method takeBreakerReleaseControl.

public Releasable takeBreakerReleaseControl() {
    final Releasable toReturn = breakerRelease;
    breakerRelease = null;
    if (toReturn != null) {
        return toReturn;
    } else {
        return () -> {
        };
    }
}
Also used : Releasable(org.opensearch.common.lease.Releasable)

Example 78 with Releasable

use of org.opensearch.common.lease.Releasable in project OpenSearch by opensearch-project.

the class TaskManagerTests method testTrackingChannelTask.

public void testTrackingChannelTask() throws Exception {
    final TaskManager taskManager = new TaskManager(Settings.EMPTY, threadPool, Collections.emptySet());
    Set<Task> cancelledTasks = ConcurrentCollections.newConcurrentSet();
    taskManager.setTaskCancellationService(new TaskCancellationService(mock(TransportService.class)) {

        @Override
        void cancelTaskAndDescendants(CancellableTask task, String reason, boolean waitForCompletion, ActionListener<Void> listener) {
            assertThat(reason, equalTo("channel was closed"));
            assertFalse(waitForCompletion);
            assertTrue("task [" + task + "] was cancelled already", cancelledTasks.add(task));
        }
    });
    Map<TcpChannel, Set<Task>> pendingTasks = new HashMap<>();
    Set<Task> expectedCancelledTasks = new HashSet<>();
    FakeTcpChannel[] channels = new FakeTcpChannel[randomIntBetween(1, 10)];
    List<Releasable> stopTrackingTasks = new ArrayList<>();
    for (int i = 0; i < channels.length; i++) {
        channels[i] = new SingleThreadedTcpChannel();
    }
    int iterations = randomIntBetween(1, 200);
    for (int i = 0; i < iterations; i++) {
        final List<Releasable> subset = randomSubsetOf(stopTrackingTasks);
        stopTrackingTasks.removeAll(subset);
        Releasables.close(subset);
        final FakeTcpChannel channel = randomFrom(channels);
        final Task task = taskManager.register("transport", "test", new CancellableRequest(Integer.toString(i)));
        if (channel.isOpen() && randomBoolean()) {
            channel.close();
            expectedCancelledTasks.addAll(pendingTasks.getOrDefault(channel, Collections.emptySet()));
        }
        final Releasable stopTracking = taskManager.startTrackingCancellableChannelTask(channel, (CancellableTask) task);
        if (channel.isOpen()) {
            pendingTasks.computeIfAbsent(channel, k -> new HashSet<>()).add(task);
            stopTrackingTasks.add(() -> {
                stopTracking.close();
                assertTrue(pendingTasks.get(channel).remove(task));
                expectedCancelledTasks.remove(task);
            });
        } else {
            expectedCancelledTasks.add(task);
        }
    }
    assertBusy(() -> assertThat(expectedCancelledTasks, everyItem(in(cancelledTasks))), 30, TimeUnit.SECONDS);
    for (FakeTcpChannel channel : channels) {
        channel.close();
    }
    assertThat(taskManager.numberOfChannelPendingTaskTrackers(), equalTo(0));
}
Also used : ThreadPool(org.opensearch.threadpool.ThreadPool) TestThreadPool(org.opensearch.threadpool.TestThreadPool) HashMap(java.util.HashMap) Releasable(org.opensearch.common.lease.Releasable) Releasables(org.opensearch.common.lease.Releasables) ArrayList(java.util.ArrayList) ConcurrentCollections(org.opensearch.common.util.concurrent.ConcurrentCollections) HashSet(java.util.HashSet) Matchers.everyItem(org.hamcrest.Matchers.everyItem) After(org.junit.After) Map(java.util.Map) ActionListener(org.opensearch.action.ActionListener) TransportTasksActionTests(org.opensearch.action.admin.cluster.node.tasks.TransportTasksActionTests) Before(org.junit.Before) TimeValue(org.opensearch.common.unit.TimeValue) TransportRequest(org.opensearch.transport.TransportRequest) Iterator(java.util.Iterator) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) TcpChannel(org.opensearch.transport.TcpChannel) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) FakeTcpChannel(org.opensearch.transport.FakeTcpChannel) TransportService(org.opensearch.transport.TransportService) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Phaser(java.util.concurrent.Phaser) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Collections(java.util.Collections) Matchers.in(org.hamcrest.Matchers.in) Mockito.mock(org.mockito.Mockito.mock) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FakeTcpChannel(org.opensearch.transport.FakeTcpChannel) TcpChannel(org.opensearch.transport.TcpChannel) FakeTcpChannel(org.opensearch.transport.FakeTcpChannel) Releasable(org.opensearch.common.lease.Releasable) HashSet(java.util.HashSet)

Example 79 with Releasable

use of org.opensearch.common.lease.Releasable in project OpenSearch by opensearch-project.

the class InboundPipelineTests method testDecodeExceptionIsPropagated.

public void testDecodeExceptionIsPropagated() throws IOException {
    BiConsumer<TcpChannel, InboundMessage> messageHandler = (c, m) -> {
    };
    final StatsTracker statsTracker = new StatsTracker();
    final LongSupplier millisSupplier = () -> TimeValue.nsecToMSec(System.nanoTime());
    final InboundDecoder decoder = new InboundDecoder(Version.CURRENT, PageCacheRecycler.NON_RECYCLING_INSTANCE);
    final Supplier<CircuitBreaker> breaker = () -> new NoopCircuitBreaker("test");
    final InboundAggregator aggregator = new InboundAggregator(breaker, (Predicate<String>) action -> true);
    final InboundPipeline pipeline = new InboundPipeline(statsTracker, millisSupplier, decoder, aggregator, messageHandler);
    try (BytesStreamOutput streamOutput = new BytesStreamOutput()) {
        String actionName = "actionName";
        final Version invalidVersion = Version.CURRENT.minimumCompatibilityVersion().minimumCompatibilityVersion();
        final String value = randomAlphaOfLength(1000);
        final boolean isRequest = randomBoolean();
        final long requestId = randomNonNegativeLong();
        OutboundMessage message;
        if (isRequest) {
            message = new OutboundMessage.Request(threadContext, new String[0], new TestRequest(value), invalidVersion, actionName, requestId, false, false);
        } else {
            message = new OutboundMessage.Response(threadContext, Collections.emptySet(), new TestResponse(value), invalidVersion, requestId, false, false);
        }
        final BytesReference reference = message.serialize(streamOutput);
        try (ReleasableBytesReference releasable = ReleasableBytesReference.wrap(reference)) {
            expectThrows(IllegalStateException.class, () -> pipeline.handleBytes(new FakeTcpChannel(), releasable));
        }
        // Pipeline cannot be reused after uncaught exception
        final IllegalStateException ise = expectThrows(IllegalStateException.class, () -> pipeline.handleBytes(new FakeTcpChannel(), ReleasableBytesReference.wrap(BytesArray.EMPTY)));
        assertEquals("Pipeline state corrupted by uncaught exception", ise.getMessage());
    }
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) LongSupplier(java.util.function.LongSupplier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Version(org.opensearch.Version) Releasable(org.opensearch.common.lease.Releasable) CircuitBreaker(org.opensearch.common.breaker.CircuitBreaker) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) ReleasableBytesReference(org.opensearch.common.bytes.ReleasableBytesReference) BiConsumer(java.util.function.BiConsumer) CircuitBreakingException(org.opensearch.common.breaker.CircuitBreakingException) NoopCircuitBreaker(org.opensearch.common.breaker.NoopCircuitBreaker) TimeValue(org.opensearch.common.unit.TimeValue) Streams(org.opensearch.core.internal.io.Streams) Predicate(java.util.function.Predicate) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Settings(org.opensearch.common.settings.Settings) IOException(java.io.IOException) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) Tuple(org.opensearch.common.collect.Tuple) Objects(java.util.Objects) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) List(java.util.List) BytesArray(org.opensearch.common.bytes.BytesArray) Collections(java.util.Collections) PageCacheRecycler(org.opensearch.common.util.PageCacheRecycler) TestCircuitBreaker(org.opensearch.common.breaker.TestCircuitBreaker) CircuitBreaker(org.opensearch.common.breaker.CircuitBreaker) NoopCircuitBreaker(org.opensearch.common.breaker.NoopCircuitBreaker) TestCircuitBreaker(org.opensearch.common.breaker.TestCircuitBreaker) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) Version(org.opensearch.Version) NoopCircuitBreaker(org.opensearch.common.breaker.NoopCircuitBreaker) BytesReference(org.opensearch.common.bytes.BytesReference) ReleasableBytesReference(org.opensearch.common.bytes.ReleasableBytesReference) ReleasableBytesReference(org.opensearch.common.bytes.ReleasableBytesReference) LongSupplier(java.util.function.LongSupplier)

Example 80 with Releasable

use of org.opensearch.common.lease.Releasable in project OpenSearch by opensearch-project.

the class InboundPipelineTests method testEnsureBodyIsNotPrematurelyReleased.

public void testEnsureBodyIsNotPrematurelyReleased() throws IOException {
    BiConsumer<TcpChannel, InboundMessage> messageHandler = (c, m) -> {
    };
    final StatsTracker statsTracker = new StatsTracker();
    final LongSupplier millisSupplier = () -> TimeValue.nsecToMSec(System.nanoTime());
    final InboundDecoder decoder = new InboundDecoder(Version.CURRENT, PageCacheRecycler.NON_RECYCLING_INSTANCE);
    final Supplier<CircuitBreaker> breaker = () -> new NoopCircuitBreaker("test");
    final InboundAggregator aggregator = new InboundAggregator(breaker, (Predicate<String>) action -> true);
    final InboundPipeline pipeline = new InboundPipeline(statsTracker, millisSupplier, decoder, aggregator, messageHandler);
    try (BytesStreamOutput streamOutput = new BytesStreamOutput()) {
        String actionName = "actionName";
        final Version version = Version.CURRENT;
        final String value = randomAlphaOfLength(1000);
        final boolean isRequest = randomBoolean();
        final long requestId = randomNonNegativeLong();
        OutboundMessage message;
        if (isRequest) {
            message = new OutboundMessage.Request(threadContext, new String[0], new TestRequest(value), version, actionName, requestId, false, false);
        } else {
            message = new OutboundMessage.Response(threadContext, Collections.emptySet(), new TestResponse(value), version, requestId, false, false);
        }
        final BytesReference reference = message.serialize(streamOutput);
        final int fixedHeaderSize = TcpHeader.headerSize(Version.CURRENT);
        final int variableHeaderSize = reference.getInt(fixedHeaderSize - 4);
        final int totalHeaderSize = fixedHeaderSize + variableHeaderSize;
        final AtomicBoolean bodyReleased = new AtomicBoolean(false);
        for (int i = 0; i < totalHeaderSize - 1; ++i) {
            try (ReleasableBytesReference slice = ReleasableBytesReference.wrap(reference.slice(i, 1))) {
                pipeline.handleBytes(new FakeTcpChannel(), slice);
            }
        }
        final Releasable releasable = () -> bodyReleased.set(true);
        final int from = totalHeaderSize - 1;
        final BytesReference partHeaderPartBody = reference.slice(from, reference.length() - from - 1);
        try (ReleasableBytesReference slice = new ReleasableBytesReference(partHeaderPartBody, releasable)) {
            pipeline.handleBytes(new FakeTcpChannel(), slice);
        }
        assertFalse(bodyReleased.get());
        try (ReleasableBytesReference slice = new ReleasableBytesReference(reference.slice(reference.length() - 1, 1), releasable)) {
            pipeline.handleBytes(new FakeTcpChannel(), slice);
        }
        assertTrue(bodyReleased.get());
    }
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) LongSupplier(java.util.function.LongSupplier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Version(org.opensearch.Version) Releasable(org.opensearch.common.lease.Releasable) CircuitBreaker(org.opensearch.common.breaker.CircuitBreaker) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) ReleasableBytesReference(org.opensearch.common.bytes.ReleasableBytesReference) BiConsumer(java.util.function.BiConsumer) CircuitBreakingException(org.opensearch.common.breaker.CircuitBreakingException) NoopCircuitBreaker(org.opensearch.common.breaker.NoopCircuitBreaker) TimeValue(org.opensearch.common.unit.TimeValue) Streams(org.opensearch.core.internal.io.Streams) Predicate(java.util.function.Predicate) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Settings(org.opensearch.common.settings.Settings) IOException(java.io.IOException) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) Tuple(org.opensearch.common.collect.Tuple) Objects(java.util.Objects) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) List(java.util.List) BytesArray(org.opensearch.common.bytes.BytesArray) Collections(java.util.Collections) PageCacheRecycler(org.opensearch.common.util.PageCacheRecycler) TestCircuitBreaker(org.opensearch.common.breaker.TestCircuitBreaker) CircuitBreaker(org.opensearch.common.breaker.CircuitBreaker) NoopCircuitBreaker(org.opensearch.common.breaker.NoopCircuitBreaker) TestCircuitBreaker(org.opensearch.common.breaker.TestCircuitBreaker) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) Version(org.opensearch.Version) NoopCircuitBreaker(org.opensearch.common.breaker.NoopCircuitBreaker) BytesReference(org.opensearch.common.bytes.BytesReference) ReleasableBytesReference(org.opensearch.common.bytes.ReleasableBytesReference) ReleasableBytesReference(org.opensearch.common.bytes.ReleasableBytesReference) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Releasable(org.opensearch.common.lease.Releasable) LongSupplier(java.util.function.LongSupplier)

Aggregations

Releasable (org.opensearch.common.lease.Releasable)161 ShardId (org.opensearch.index.shard.ShardId)50 IOException (java.io.IOException)45 CountDownLatch (java.util.concurrent.CountDownLatch)36 Settings (org.opensearch.common.settings.Settings)35 IndexingPressurePerShardStats (org.opensearch.index.stats.IndexingPressurePerShardStats)32 ExecutionException (java.util.concurrent.ExecutionException)30 ArrayList (java.util.ArrayList)28 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)28 AlreadyClosedException (org.apache.lucene.store.AlreadyClosedException)27 OpenSearchException (org.opensearch.OpenSearchException)25 ThreadPool (org.opensearch.threadpool.ThreadPool)25 PlainActionFuture (org.opensearch.action.support.PlainActionFuture)24 ActionListener (org.opensearch.action.ActionListener)23 IndexRequest (org.opensearch.action.index.IndexRequest)22 ShardRouting (org.opensearch.cluster.routing.ShardRouting)22 IndexingPressureStats (org.opensearch.index.stats.IndexingPressureStats)22 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)21 List (java.util.List)20 Translog (org.opensearch.index.translog.Translog)19