Search in sources :

Example 1 with TransportChannel

use of org.opensearch.transport.TransportChannel in project OpenSearch by opensearch-project.

the class DisruptableMockTransport method onConnectedDuringSend.

protected void onConnectedDuringSend(long requestId, String action, TransportRequest request, DisruptableMockTransport destinationTransport) {
    final RequestHandlerRegistry<TransportRequest> requestHandler = destinationTransport.getRequestHandlers().getHandler(action);
    final DiscoveryNode destination = destinationTransport.getLocalNode();
    final String requestDescription = getRequestDescription(requestId, action, destination);
    final TransportChannel transportChannel = new TransportChannel() {

        @Override
        public String getProfileName() {
            return "default";
        }

        @Override
        public String getChannelType() {
            return "disruptable-mock-transport-channel";
        }

        @Override
        public void sendResponse(final TransportResponse response) {
            execute(new Runnable() {

                @Override
                public void run() {
                    final ConnectionStatus connectionStatus = destinationTransport.getConnectionStatus(getLocalNode());
                    switch(connectionStatus) {
                        case CONNECTED:
                        case BLACK_HOLE_REQUESTS_ONLY:
                            handleResponse(requestId, response);
                            break;
                        case BLACK_HOLE:
                        case DISCONNECTED:
                            logger.trace("delaying response to {}: channel is {}", requestDescription, connectionStatus);
                            onBlackholedDuringSend(requestId, action, destinationTransport);
                            break;
                        default:
                            throw new AssertionError("unexpected status: " + connectionStatus);
                    }
                }

                @Override
                public String toString() {
                    return "response to " + requestDescription;
                }
            });
        }

        @Override
        public void sendResponse(Exception exception) {
            execute(new Runnable() {

                @Override
                public void run() {
                    final ConnectionStatus connectionStatus = destinationTransport.getConnectionStatus(getLocalNode());
                    switch(connectionStatus) {
                        case CONNECTED:
                        case BLACK_HOLE_REQUESTS_ONLY:
                            handleRemoteError(requestId, exception);
                            break;
                        case BLACK_HOLE:
                        case DISCONNECTED:
                            logger.trace("delaying exception response to {}: channel is {}", requestDescription, connectionStatus);
                            onBlackholedDuringSend(requestId, action, destinationTransport);
                            break;
                        default:
                            throw new AssertionError("unexpected status: " + connectionStatus);
                    }
                }

                @Override
                public String toString() {
                    return "error response to " + requestDescription;
                }
            });
        }
    };
    final TransportRequest copiedRequest;
    try {
        copiedRequest = copyWriteable(request, writeableRegistry(), requestHandler::newRequest);
    } catch (IOException e) {
        throw new AssertionError("exception de/serializing request", e);
    }
    try {
        requestHandler.processMessageReceived(copiedRequest, transportChannel);
    } catch (Exception e) {
        try {
            transportChannel.sendResponse(e);
        } catch (Exception ee) {
            logger.warn("failed to send failure", e);
        }
    }
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) TransportRequest(org.opensearch.transport.TransportRequest) TransportChannel(org.opensearch.transport.TransportChannel) IOException(java.io.IOException) TransportResponse(org.opensearch.transport.TransportResponse) IOException(java.io.IOException) ConnectTransportException(org.opensearch.transport.ConnectTransportException) TransportException(org.opensearch.transport.TransportException)

Example 2 with TransportChannel

use of org.opensearch.transport.TransportChannel in project OpenSearch by opensearch-project.

the class DisruptableMockTransportTests method testUnavailableOnExceptionalResponse.

public void testUnavailableOnExceptionalResponse() throws IOException {
    registerRequestHandler(service1, requestHandlerShouldNotBeCalled());
    AtomicReference<TransportChannel> responseHandlerChannel = new AtomicReference<>();
    registerRequestHandler(service2, requestHandlerCaptures(responseHandlerChannel::set));
    send(service1, node2, responseHandlerShouldNotBeCalled());
    deterministicTaskQueue.runAllRunnableTasks();
    assertNotNull(responseHandlerChannel.get());
    blackholedLinks.add(Tuple.tuple(node2, node1));
    responseHandlerChannel.get().sendResponse(new Exception());
    deterministicTaskQueue.runAllRunnableTasks();
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) TransportChannel(org.opensearch.transport.TransportChannel) IOException(java.io.IOException) ConnectTransportException(org.opensearch.transport.ConnectTransportException) TransportException(org.opensearch.transport.TransportException)

Example 3 with TransportChannel

use of org.opensearch.transport.TransportChannel in project OpenSearch by opensearch-project.

the class DisruptableMockTransportTests method testUnavailableOnRequestOnlyReceivesExceptionalResponse.

public void testUnavailableOnRequestOnlyReceivesExceptionalResponse() throws IOException {
    registerRequestHandler(service1, requestHandlerShouldNotBeCalled());
    AtomicReference<TransportChannel> responseHandlerChannel = new AtomicReference<>();
    registerRequestHandler(service2, requestHandlerCaptures(responseHandlerChannel::set));
    AtomicBoolean responseHandlerCalled = new AtomicBoolean();
    send(service1, node2, responseHandlerShouldBeCalledExceptionally(e -> responseHandlerCalled.set(true)));
    deterministicTaskQueue.runAllTasks();
    assertNotNull(responseHandlerChannel.get());
    assertFalse(responseHandlerCalled.get());
    blackholedRequestLinks.add(Tuple.tuple(node1, node2));
    blackholedRequestLinks.add(Tuple.tuple(node2, node1));
    responseHandlerChannel.get().sendResponse(new Exception());
    deterministicTaskQueue.runAllRunnableTasks();
    assertTrue(responseHandlerCalled.get());
}
Also used : TransportRequestHandler(org.opensearch.transport.TransportRequestHandler) ThreadPool(org.opensearch.threadpool.ThreadPool) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Version(org.opensearch.Version) Node(org.opensearch.node.Node) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) ConnectionStatus(org.opensearch.test.disruption.DisruptableMockTransport.ConnectionStatus) Before(org.junit.Before) StreamInput(org.opensearch.common.io.stream.StreamInput) Empty(org.opensearch.transport.TransportResponse.Empty) DeterministicTaskQueue(org.opensearch.cluster.coordination.DeterministicTaskQueue) TransportChannel(org.opensearch.transport.TransportChannel) TransportRequest(org.opensearch.transport.TransportRequest) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) TransportResponseHandler(org.opensearch.transport.TransportResponseHandler) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) IOException(java.io.IOException) TransportResponse(org.opensearch.transport.TransportResponse) TransportService(org.opensearch.transport.TransportService) Tuple(org.opensearch.common.collect.Tuple) TransportAddress(org.opensearch.common.transport.TransportAddress) Consumer(java.util.function.Consumer) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) List(java.util.List) Optional(java.util.Optional) ConnectTransportException(org.opensearch.transport.ConnectTransportException) Collections(java.util.Collections) Matchers.containsString(org.hamcrest.Matchers.containsString) Matchers.endsWith(org.hamcrest.Matchers.endsWith) TransportException(org.opensearch.transport.TransportException) NOOP_TRANSPORT_INTERCEPTOR(org.opensearch.transport.TransportService.NOOP_TRANSPORT_INTERCEPTOR) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) TransportChannel(org.opensearch.transport.TransportChannel) IOException(java.io.IOException) ConnectTransportException(org.opensearch.transport.ConnectTransportException) TransportException(org.opensearch.transport.TransportException)

Example 4 with TransportChannel

use of org.opensearch.transport.TransportChannel in project OpenSearch by opensearch-project.

the class DisruptableMockTransportTests method testDisconnectedOnExceptionalResponse.

public void testDisconnectedOnExceptionalResponse() throws IOException {
    registerRequestHandler(service1, requestHandlerShouldNotBeCalled());
    AtomicReference<TransportChannel> responseHandlerChannel = new AtomicReference<>();
    registerRequestHandler(service2, requestHandlerCaptures(responseHandlerChannel::set));
    AtomicReference<TransportException> responseHandlerException = new AtomicReference<>();
    send(service1, node2, responseHandlerShouldBeCalledExceptionally(responseHandlerException::set));
    deterministicTaskQueue.runAllRunnableTasks();
    assertNotNull(responseHandlerChannel.get());
    assertNull(responseHandlerException.get());
    disconnectedLinks.add(Tuple.tuple(node2, node1));
    responseHandlerChannel.get().sendResponse(new Exception());
    deterministicTaskQueue.runAllTasks();
    assertThat(responseHandlerException.get(), instanceOf(ConnectTransportException.class));
}
Also used : ConnectTransportException(org.opensearch.transport.ConnectTransportException) AtomicReference(java.util.concurrent.atomic.AtomicReference) TransportChannel(org.opensearch.transport.TransportChannel) ConnectTransportException(org.opensearch.transport.ConnectTransportException) TransportException(org.opensearch.transport.TransportException) IOException(java.io.IOException) ConnectTransportException(org.opensearch.transport.ConnectTransportException) TransportException(org.opensearch.transport.TransportException)

Example 5 with TransportChannel

use of org.opensearch.transport.TransportChannel in project OpenSearch by opensearch-project.

the class DisruptableMockTransportTests method testUnavailableOnRequestOnlyReceivesSuccessfulResponse.

public void testUnavailableOnRequestOnlyReceivesSuccessfulResponse() throws IOException {
    registerRequestHandler(service1, requestHandlerShouldNotBeCalled());
    AtomicReference<TransportChannel> responseHandlerChannel = new AtomicReference<>();
    registerRequestHandler(service2, requestHandlerCaptures(responseHandlerChannel::set));
    AtomicBoolean responseHandlerCalled = new AtomicBoolean();
    send(service1, node2, responseHandlerShouldBeCalledNormally(() -> responseHandlerCalled.set(true)));
    deterministicTaskQueue.runAllTasks();
    assertNotNull(responseHandlerChannel.get());
    assertFalse(responseHandlerCalled.get());
    blackholedRequestLinks.add(Tuple.tuple(node1, node2));
    blackholedRequestLinks.add(Tuple.tuple(node2, node1));
    responseHandlerChannel.get().sendResponse(TransportResponse.Empty.INSTANCE);
    deterministicTaskQueue.runAllRunnableTasks();
    assertTrue(responseHandlerCalled.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) TransportChannel(org.opensearch.transport.TransportChannel)

Aggregations

TransportChannel (org.opensearch.transport.TransportChannel)5 IOException (java.io.IOException)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 ConnectTransportException (org.opensearch.transport.ConnectTransportException)4 TransportException (org.opensearch.transport.TransportException)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)2 TransportRequest (org.opensearch.transport.TransportRequest)2 TransportResponse (org.opensearch.transport.TransportResponse)2 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Consumer (java.util.function.Consumer)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Matchers.endsWith (org.hamcrest.Matchers.endsWith)1 Matchers.instanceOf (org.hamcrest.Matchers.instanceOf)1 Before (org.junit.Before)1