Search in sources :

Example 66 with TransportAddress

use of org.opensearch.common.transport.TransportAddress in project OpenSearch by opensearch-project.

the class DisruptableMockTransportTests method initTransports.

@Before
public void initTransports() {
    node1 = new DiscoveryNode("node1", buildNewFakeTransportAddress(), Version.CURRENT);
    node2 = new DiscoveryNode("node2", buildNewFakeTransportAddress(), Version.CURRENT);
    disconnectedLinks = new HashSet<>();
    blackholedLinks = new HashSet<>();
    blackholedRequestLinks = new HashSet<>();
    List<DisruptableMockTransport> transports = new ArrayList<>();
    deterministicTaskQueue = new DeterministicTaskQueue(Settings.builder().put(Node.NODE_NAME_SETTING.getKey(), "dummy").build(), random());
    final DisruptableMockTransport transport1 = new DisruptableMockTransport(node1, logger, deterministicTaskQueue) {

        @Override
        protected ConnectionStatus getConnectionStatus(DiscoveryNode destination) {
            return DisruptableMockTransportTests.this.getConnectionStatus(getLocalNode(), destination);
        }

        @Override
        protected Optional<DisruptableMockTransport> getDisruptableMockTransport(TransportAddress address) {
            return transports.stream().filter(t -> t.getLocalNode().getAddress().equals(address)).findAny();
        }

        @Override
        protected void execute(Runnable runnable) {
            deterministicTaskQueue.scheduleNow(runnable);
        }
    };
    final DisruptableMockTransport transport2 = new DisruptableMockTransport(node2, logger, deterministicTaskQueue) {

        @Override
        protected ConnectionStatus getConnectionStatus(DiscoveryNode destination) {
            return DisruptableMockTransportTests.this.getConnectionStatus(getLocalNode(), destination);
        }

        @Override
        protected Optional<DisruptableMockTransport> getDisruptableMockTransport(TransportAddress address) {
            return transports.stream().filter(t -> t.getLocalNode().getAddress().equals(address)).findAny();
        }

        @Override
        protected void execute(Runnable runnable) {
            deterministicTaskQueue.scheduleNow(runnable);
        }
    };
    transports.add(transport1);
    transports.add(transport2);
    service1 = transport1.createTransportService(Settings.EMPTY, deterministicTaskQueue.getThreadPool(), NOOP_TRANSPORT_INTERCEPTOR, a -> node1, null, Collections.emptySet());
    service2 = transport2.createTransportService(Settings.EMPTY, deterministicTaskQueue.getThreadPool(), NOOP_TRANSPORT_INTERCEPTOR, a -> node2, null, Collections.emptySet());
    service1.start();
    service2.start();
    final PlainActionFuture<Void> fut1 = new PlainActionFuture<>();
    service1.connectToNode(node2, fut1);
    final PlainActionFuture<Void> fut2 = new PlainActionFuture<>();
    service2.connectToNode(node1, fut2);
    deterministicTaskQueue.runAllTasksInTimeOrder();
    assertTrue(fut1.isDone());
    assertTrue(fut2.isDone());
}
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) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) DeterministicTaskQueue(org.opensearch.cluster.coordination.DeterministicTaskQueue) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) TransportAddress(org.opensearch.common.transport.TransportAddress) ArrayList(java.util.ArrayList) Before(org.junit.Before)

Example 67 with TransportAddress

use of org.opensearch.common.transport.TransportAddress in project OpenSearch by opensearch-project.

the class Netty4HttpServerTransportTests method testBindUnavailableAddress.

public void testBindUnavailableAddress() {
    Settings initialSettings = createSettings();
    try (Netty4HttpServerTransport transport = new Netty4HttpServerTransport(initialSettings, networkService, bigArrays, threadPool, xContentRegistry(), new NullDispatcher(), clusterSettings, new SharedGroupFactory(Settings.EMPTY))) {
        transport.start();
        TransportAddress remoteAddress = randomFrom(transport.boundAddress().boundAddresses());
        Settings settings = Settings.builder().put("http.port", remoteAddress.getPort()).put("network.host", remoteAddress.getAddress()).build();
        try (Netty4HttpServerTransport otherTransport = new Netty4HttpServerTransport(settings, networkService, bigArrays, threadPool, xContentRegistry(), new NullDispatcher(), clusterSettings, new SharedGroupFactory(settings))) {
            BindHttpException bindHttpException = expectThrows(BindHttpException.class, otherTransport::start);
            assertEquals("Failed to bind to " + NetworkAddress.format(remoteAddress.address()), bindHttpException.getMessage());
        }
    }
}
Also used : NullDispatcher(org.opensearch.http.NullDispatcher) TransportAddress(org.opensearch.common.transport.TransportAddress) SharedGroupFactory(org.opensearch.transport.SharedGroupFactory) BindHttpException(org.opensearch.http.BindHttpException) Settings(org.opensearch.common.settings.Settings) HttpTransportSettings(org.opensearch.http.HttpTransportSettings) ClusterSettings(org.opensearch.common.settings.ClusterSettings)

Example 68 with TransportAddress

use of org.opensearch.common.transport.TransportAddress in project OpenSearch by opensearch-project.

the class Netty4HttpServerTransportTests method runExpectHeaderTest.

private void runExpectHeaderTest(final Settings settings, final String expectation, final int contentLength, final HttpResponseStatus expectedStatus) throws InterruptedException {
    final HttpServerTransport.Dispatcher dispatcher = new HttpServerTransport.Dispatcher() {

        @Override
        public void dispatchRequest(RestRequest request, RestChannel channel, ThreadContext threadContext) {
            channel.sendResponse(new BytesRestResponse(OK, BytesRestResponse.TEXT_CONTENT_TYPE, new BytesArray("done")));
        }

        @Override
        public void dispatchBadRequest(RestChannel channel, ThreadContext threadContext, Throwable cause) {
            logger.error(new ParameterizedMessage("--> Unexpected bad request [{}]", FakeRestRequest.requestToString(channel.request())), cause);
            throw new AssertionError();
        }
    };
    try (Netty4HttpServerTransport transport = new Netty4HttpServerTransport(settings, networkService, bigArrays, threadPool, xContentRegistry(), dispatcher, clusterSettings, new SharedGroupFactory(settings))) {
        transport.start();
        final TransportAddress remoteAddress = randomFrom(transport.boundAddress().boundAddresses());
        try (Netty4HttpClient client = new Netty4HttpClient()) {
            final FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
            request.headers().set(HttpHeaderNames.EXPECT, expectation);
            HttpUtil.setContentLength(request, contentLength);
            final FullHttpResponse response = client.send(remoteAddress.address(), request);
            try {
                assertThat(response.status(), equalTo(expectedStatus));
                if (expectedStatus.equals(HttpResponseStatus.CONTINUE)) {
                    final FullHttpRequest continuationRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/", Unpooled.EMPTY_BUFFER);
                    final FullHttpResponse continuationResponse = client.send(remoteAddress.address(), continuationRequest);
                    try {
                        assertThat(continuationResponse.status(), is(HttpResponseStatus.OK));
                        assertThat(new String(ByteBufUtil.getBytes(continuationResponse.content()), StandardCharsets.UTF_8), is("done"));
                    } finally {
                        continuationResponse.release();
                    }
                }
            } finally {
                response.release();
            }
        }
    }
}
Also used : BytesArray(org.opensearch.common.bytes.BytesArray) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) TransportAddress(org.opensearch.common.transport.TransportAddress) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) RestChannel(org.opensearch.rest.RestChannel) SharedGroupFactory(org.opensearch.transport.SharedGroupFactory) Matchers.containsString(org.hamcrest.Matchers.containsString) HttpServerTransport(org.opensearch.http.HttpServerTransport) NullDispatcher(org.opensearch.http.NullDispatcher) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) RestRequest(org.opensearch.rest.RestRequest) BytesRestResponse(org.opensearch.rest.BytesRestResponse) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse)

Example 69 with TransportAddress

use of org.opensearch.common.transport.TransportAddress in project OpenSearch by opensearch-project.

the class Netty4HttpServerTransportTests method testCorsRequest.

public void testCorsRequest() throws InterruptedException {
    final HttpServerTransport.Dispatcher dispatcher = new HttpServerTransport.Dispatcher() {

        @Override
        public void dispatchRequest(final RestRequest request, final RestChannel channel, final ThreadContext threadContext) {
            logger.error("--> Unexpected successful request [{}]", FakeRestRequest.requestToString(request));
            throw new AssertionError();
        }

        @Override
        public void dispatchBadRequest(final RestChannel channel, final ThreadContext threadContext, final Throwable cause) {
            logger.error(new ParameterizedMessage("--> Unexpected bad request [{}]", FakeRestRequest.requestToString(channel.request())), cause);
            throw new AssertionError();
        }
    };
    final Settings settings = createBuilderWithPort().put(SETTING_CORS_ENABLED.getKey(), true).put(SETTING_CORS_ALLOW_ORIGIN.getKey(), "test-cors.org").build();
    try (Netty4HttpServerTransport transport = new Netty4HttpServerTransport(settings, networkService, bigArrays, threadPool, xContentRegistry(), dispatcher, new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), new SharedGroupFactory(settings))) {
        transport.start();
        final TransportAddress remoteAddress = randomFrom(transport.boundAddress().boundAddresses());
        // Test pre-flight request
        try (Netty4HttpClient client = new Netty4HttpClient()) {
            final FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.OPTIONS, "/");
            request.headers().add(CorsHandler.ORIGIN, "test-cors.org");
            request.headers().add(CorsHandler.ACCESS_CONTROL_REQUEST_METHOD, "POST");
            final FullHttpResponse response = client.send(remoteAddress.address(), request);
            try {
                assertThat(response.status(), equalTo(HttpResponseStatus.OK));
                assertThat(response.headers().get(CorsHandler.ACCESS_CONTROL_ALLOW_ORIGIN), equalTo("test-cors.org"));
                assertThat(response.headers().get(CorsHandler.VARY), equalTo(CorsHandler.ORIGIN));
                assertTrue(response.headers().contains(CorsHandler.DATE));
            } finally {
                response.release();
            }
        }
        // Test short-circuited request
        try (Netty4HttpClient client = new Netty4HttpClient()) {
            final FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/");
            request.headers().add(CorsHandler.ORIGIN, "google.com");
            final FullHttpResponse response = client.send(remoteAddress.address(), request);
            try {
                assertThat(response.status(), equalTo(HttpResponseStatus.FORBIDDEN));
            } finally {
                response.release();
            }
        }
    }
}
Also used : ClusterSettings(org.opensearch.common.settings.ClusterSettings) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) TransportAddress(org.opensearch.common.transport.TransportAddress) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) RestChannel(org.opensearch.rest.RestChannel) SharedGroupFactory(org.opensearch.transport.SharedGroupFactory) HttpServerTransport(org.opensearch.http.HttpServerTransport) NullDispatcher(org.opensearch.http.NullDispatcher) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) RestRequest(org.opensearch.rest.RestRequest) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) Settings(org.opensearch.common.settings.Settings) HttpTransportSettings(org.opensearch.http.HttpTransportSettings) ClusterSettings(org.opensearch.common.settings.ClusterSettings)

Example 70 with TransportAddress

use of org.opensearch.common.transport.TransportAddress in project OpenSearch by opensearch-project.

the class Netty4HttpServerPipeliningTests method testThatHttpPipeliningWorks.

public void testThatHttpPipeliningWorks() throws Exception {
    final Settings settings = Settings.builder().put("http.port", "0").build();
    try (HttpServerTransport httpServerTransport = new CustomNettyHttpServerTransport(settings)) {
        httpServerTransport.start();
        final TransportAddress transportAddress = randomFrom(httpServerTransport.boundAddress().boundAddresses());
        final int numberOfRequests = randomIntBetween(4, 16);
        final List<String> requests = new ArrayList<>(numberOfRequests);
        for (int i = 0; i < numberOfRequests; i++) {
            if (rarely()) {
                requests.add("/slow/" + i);
            } else {
                requests.add("/" + i);
            }
        }
        try (Netty4HttpClient nettyHttpClient = new Netty4HttpClient()) {
            Collection<FullHttpResponse> responses = nettyHttpClient.get(transportAddress.address(), requests.toArray(new String[] {}));
            try {
                Collection<String> responseBodies = Netty4HttpClient.returnHttpResponseBodies(responses);
                assertThat(responseBodies, contains(requests.toArray()));
            } finally {
                responses.forEach(ReferenceCounted::release);
            }
        }
    }
}
Also used : TransportAddress(org.opensearch.common.transport.TransportAddress) ArrayList(java.util.ArrayList) HttpServerTransport(org.opensearch.http.HttpServerTransport) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) ClusterSettings(org.opensearch.common.settings.ClusterSettings) Settings(org.opensearch.common.settings.Settings) ReferenceCounted(io.netty.util.ReferenceCounted)

Aggregations

TransportAddress (org.opensearch.common.transport.TransportAddress)129 Settings (org.opensearch.common.settings.Settings)51 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)43 BoundTransportAddress (org.opensearch.common.transport.BoundTransportAddress)31 Version (org.opensearch.Version)26 ArrayList (java.util.ArrayList)24 IOException (java.io.IOException)22 List (java.util.List)21 InetAddress (java.net.InetAddress)20 HashSet (java.util.HashSet)20 CountDownLatch (java.util.concurrent.CountDownLatch)20 ClusterSettings (org.opensearch.common.settings.ClusterSettings)20 ThreadPool (org.opensearch.threadpool.ThreadPool)17 Matchers.containsString (org.hamcrest.Matchers.containsString)16 HttpServerTransport (org.opensearch.http.HttpServerTransport)16 Set (java.util.Set)15 TimeUnit (java.util.concurrent.TimeUnit)15 AtomicReference (java.util.concurrent.atomic.AtomicReference)15 OpenSearchTestCase (org.opensearch.test.OpenSearchTestCase)15 MockTransportService (org.opensearch.test.transport.MockTransportService)15