Search in sources :

Example 71 with TransportAddress

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

the class Netty4BadRequestTests method testBadParameterEncoding.

public void testBadParameterEncoding() throws Exception {
    final HttpServerTransport.Dispatcher dispatcher = new HttpServerTransport.Dispatcher() {

        @Override
        public void dispatchRequest(RestRequest request, RestChannel channel, ThreadContext threadContext) {
            fail();
        }

        @Override
        public void dispatchBadRequest(RestChannel channel, ThreadContext threadContext, Throwable cause) {
            try {
                final Exception e = cause instanceof Exception ? (Exception) cause : new OpenSearchException(cause);
                channel.sendResponse(new BytesRestResponse(channel, RestStatus.BAD_REQUEST, e));
            } catch (final IOException e) {
                throw new UncheckedIOException(e);
            }
        }
    };
    Settings settings = Settings.builder().put(HttpTransportSettings.SETTING_HTTP_PORT.getKey(), getPortRange()).build();
    try (HttpServerTransport httpServerTransport = new Netty4HttpServerTransport(settings, networkService, bigArrays, threadPool, xContentRegistry(), dispatcher, new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), new SharedGroupFactory(Settings.EMPTY))) {
        httpServerTransport.start();
        final TransportAddress transportAddress = randomFrom(httpServerTransport.boundAddress().boundAddresses());
        try (Netty4HttpClient nettyHttpClient = new Netty4HttpClient()) {
            final Collection<FullHttpResponse> responses = nettyHttpClient.get(transportAddress.address(), "/_cluster/settings?pretty=%");
            try {
                assertThat(responses, hasSize(1));
                assertThat(responses.iterator().next().status().code(), equalTo(400));
                final Collection<String> responseBodies = Netty4HttpClient.returnHttpResponseBodies(responses);
                assertThat(responseBodies, hasSize(1));
                assertThat(responseBodies.iterator().next(), containsString("\"type\":\"bad_parameter_exception\""));
                assertThat(responseBodies.iterator().next(), containsString("\"reason\":\"java.lang.IllegalArgumentException: unterminated escape sequence at end of string: %\""));
            } finally {
                responses.forEach(ReferenceCounted::release);
            }
        }
    }
}
Also used : ClusterSettings(org.opensearch.common.settings.ClusterSettings) TransportAddress(org.opensearch.common.transport.TransportAddress) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) RestChannel(org.opensearch.rest.RestChannel) UncheckedIOException(java.io.UncheckedIOException) SharedGroupFactory(org.opensearch.transport.SharedGroupFactory) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) Matchers.containsString(org.hamcrest.Matchers.containsString) HttpServerTransport(org.opensearch.http.HttpServerTransport) OpenSearchException(org.opensearch.OpenSearchException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) RestRequest(org.opensearch.rest.RestRequest) BytesRestResponse(org.opensearch.rest.BytesRestResponse) OpenSearchException(org.opensearch.OpenSearchException) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) HttpTransportSettings(org.opensearch.http.HttpTransportSettings) ClusterSettings(org.opensearch.common.settings.ClusterSettings) Settings(org.opensearch.common.settings.Settings) ReferenceCounted(io.netty.util.ReferenceCounted)

Example 72 with TransportAddress

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

the class SimpleNetty4TransportTests method testConnectException.

public void testConnectException() throws UnknownHostException {
    try {
        serviceA.connectToNode(new DiscoveryNode("C", new TransportAddress(InetAddress.getByName("localhost"), 9876), emptyMap(), emptySet(), Version.CURRENT));
        fail("Expected ConnectTransportException");
    } catch (ConnectTransportException e) {
        assertThat(e.getMessage(), containsString("connect_exception"));
        assertThat(e.getMessage(), containsString("[127.0.0.1:9876]"));
    }
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) ConnectTransportException(org.opensearch.transport.ConnectTransportException) TransportAddress(org.opensearch.common.transport.TransportAddress)

Example 73 with TransportAddress

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

the class Netty4TransportPublishAddressIT method testDifferentPorts.

public void testDifferentPorts() throws Exception {
    if (!NetworkUtils.SUPPORTS_V6) {
        return;
    }
    logger.info("--> starting a node on ipv4 only");
    Settings ipv4Settings = Settings.builder().put("network.host", "127.0.0.1").build();
    // should bind 127.0.0.1:XYZ
    String ipv4OnlyNode = internalCluster().startNode(ipv4Settings);
    logger.info("--> starting a node on ipv4 and ipv6");
    Settings bothSettings = Settings.builder().put("network.host", "_local_").build();
    // should bind [::1]:XYZ and 127.0.0.1:XYZ+1
    internalCluster().startNode(bothSettings);
    logger.info("--> waiting for the cluster to declare itself stable");
    // fails if port of publish address does not match corresponding bound address
    ensureStableCluster(2);
    logger.info("--> checking if boundAddress matching publishAddress has same port");
    NodesInfoResponse nodesInfoResponse = client().admin().cluster().prepareNodesInfo().get();
    for (NodeInfo nodeInfo : nodesInfoResponse.getNodes()) {
        BoundTransportAddress boundTransportAddress = nodeInfo.getInfo(TransportInfo.class).getAddress();
        if (nodeInfo.getNode().getName().equals(ipv4OnlyNode)) {
            assertThat(boundTransportAddress.boundAddresses().length, equalTo(1));
            assertThat(boundTransportAddress.boundAddresses()[0].getPort(), equalTo(boundTransportAddress.publishAddress().getPort()));
        } else {
            assertThat(boundTransportAddress.boundAddresses().length, greaterThan(1));
            for (TransportAddress boundAddress : boundTransportAddress.boundAddresses()) {
                assertThat(boundAddress, instanceOf(TransportAddress.class));
                TransportAddress inetBoundAddress = boundAddress;
                if (inetBoundAddress.address().getAddress() instanceof Inet4Address) {
                    // IPv4 address is preferred publish address for _local_
                    assertThat(inetBoundAddress.getPort(), equalTo(boundTransportAddress.publishAddress().getPort()));
                }
            }
        }
    }
}
Also used : NodesInfoResponse(org.opensearch.action.admin.cluster.node.info.NodesInfoResponse) Inet4Address(java.net.Inet4Address) TransportInfo(org.opensearch.transport.TransportInfo) NodeInfo(org.opensearch.action.admin.cluster.node.info.NodeInfo) BoundTransportAddress(org.opensearch.common.transport.BoundTransportAddress) TransportAddress(org.opensearch.common.transport.TransportAddress) BoundTransportAddress(org.opensearch.common.transport.BoundTransportAddress) Settings(org.opensearch.common.settings.Settings)

Example 74 with TransportAddress

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

the class Netty4TransportMultiPortIntegrationIT method testThatInfosAreExposed.

@Network
public void testThatInfosAreExposed() throws Exception {
    NodesInfoResponse response = client().admin().cluster().prepareNodesInfo().clear().addMetric(TRANSPORT.metricName()).get();
    for (NodeInfo nodeInfo : response.getNodes()) {
        assertThat(nodeInfo.getInfo(TransportInfo.class).getProfileAddresses().keySet(), hasSize(1));
        assertThat(nodeInfo.getInfo(TransportInfo.class).getProfileAddresses(), hasKey("client1"));
        BoundTransportAddress boundTransportAddress = nodeInfo.getInfo(TransportInfo.class).getProfileAddresses().get("client1");
        for (TransportAddress transportAddress : boundTransportAddress.boundAddresses()) {
            assertThat(transportAddress, instanceOf(TransportAddress.class));
        }
        // bound addresses
        for (TransportAddress transportAddress : boundTransportAddress.boundAddresses()) {
            assertThat(transportAddress, instanceOf(TransportAddress.class));
            assertThat(transportAddress.address().getPort(), is(allOf(greaterThanOrEqualTo(randomPort), lessThanOrEqualTo(randomPort + 10))));
        }
        // publish address
        assertThat(boundTransportAddress.publishAddress(), instanceOf(TransportAddress.class));
        TransportAddress publishAddress = boundTransportAddress.publishAddress();
        assertThat(NetworkAddress.format(publishAddress.address().getAddress()), is("127.0.0.7"));
        assertThat(publishAddress.address().getPort(), is(4321));
    }
}
Also used : NodesInfoResponse(org.opensearch.action.admin.cluster.node.info.NodesInfoResponse) TransportInfo(org.opensearch.transport.TransportInfo) NodeInfo(org.opensearch.action.admin.cluster.node.info.NodeInfo) BoundTransportAddress(org.opensearch.common.transport.BoundTransportAddress) TransportAddress(org.opensearch.common.transport.TransportAddress) BoundTransportAddress(org.opensearch.common.transport.BoundTransportAddress) Network(org.opensearch.test.junit.annotations.Network)

Example 75 with TransportAddress

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

the class NioPipeliningIT method testThatNioHttpServerSupportsPipelining.

public void testThatNioHttpServerSupportsPipelining() throws Exception {
    String[] requests = new String[] { "/", "/_nodes/stats", "/", "/_cluster/state", "/" };
    HttpServerTransport httpServerTransport = internalCluster().getInstance(HttpServerTransport.class);
    TransportAddress[] boundAddresses = httpServerTransport.boundAddress().boundAddresses();
    TransportAddress transportAddress = randomFrom(boundAddresses);
    try (NioHttpClient nettyHttpClient = new NioHttpClient()) {
        Collection<FullHttpResponse> responses = nettyHttpClient.get(transportAddress.address(), requests);
        assertThat(responses, hasSize(5));
        Collection<String> opaqueIds = NioHttpClient.returnOpaqueIds(responses);
        assertOpaqueIdsInOrder(opaqueIds);
    }
}
Also used : TransportAddress(org.opensearch.common.transport.TransportAddress) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) HttpServerTransport(org.opensearch.http.HttpServerTransport)

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