Search in sources :

Example 16 with TransportAddress

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

the class ClusterRerouteResponseTests method testToXContent.

public void testToXContent() throws IOException {
    DiscoveryNode node0 = new DiscoveryNode("node0", new TransportAddress(TransportAddress.META_ADDRESS, 9000), Version.CURRENT);
    DiscoveryNodes nodes = new DiscoveryNodes.Builder().add(node0).masterNodeId(node0.getId()).build();
    IndexMetadata indexMetadata = IndexMetadata.builder("index").settings(Settings.builder().put(IndexSettings.INDEX_CHECK_ON_STARTUP.getKey(), true).put(IndexSettings.MAX_SCRIPT_FIELDS_SETTING.getKey(), 10).put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).build()).build();
    ImmutableOpenMap.Builder<String, IndexMetadata> openMapBuilder = ImmutableOpenMap.builder();
    openMapBuilder.put("index", indexMetadata);
    Metadata metadata = Metadata.builder().indices(openMapBuilder.build()).build();
    ClusterState clusterState = ClusterState.builder(new ClusterName("test")).nodes(nodes).metadata(metadata).build();
    RoutingExplanations routingExplanations = new RoutingExplanations();
    routingExplanations.add(new RerouteExplanation(new AllocateReplicaAllocationCommand("index", 0, "node0"), Decision.YES));
    ClusterRerouteResponse clusterRerouteResponse = new ClusterRerouteResponse(true, clusterState, routingExplanations);
    {
        XContentBuilder builder = JsonXContent.contentBuilder().prettyPrint();
        clusterRerouteResponse.toXContent(builder, ToXContent.EMPTY_PARAMS);
        assertEquals("{\n" + "  \"acknowledged\" : true,\n" + "  \"state\" : {\n" + "    \"cluster_uuid\" : \"_na_\",\n" + "    \"version\" : 0,\n" + "    \"state_uuid\" : \"" + clusterState.stateUUID() + "\",\n" + "    \"master_node\" : \"node0\",\n" + "    \"cluster_manager_node\" : \"node0\",\n" + "    \"blocks\" : { },\n" + "    \"nodes\" : {\n" + "      \"node0\" : {\n" + "        \"name\" : \"\",\n" + "        \"ephemeral_id\" : \"" + node0.getEphemeralId() + "\",\n" + "        \"transport_address\" : \"0.0.0.0:9000\",\n" + "        \"attributes\" : { }\n" + "      }\n" + "    },\n" + "    \"metadata\" : {\n" + "      \"cluster_uuid\" : \"_na_\",\n" + "      \"cluster_uuid_committed\" : false,\n" + "      \"cluster_coordination\" : {\n" + "        \"term\" : 0,\n" + "        \"last_committed_config\" : [ ],\n" + "        \"last_accepted_config\" : [ ],\n" + "        \"voting_config_exclusions\" : [ ]\n" + "      },\n" + "      \"templates\" : { },\n" + "      \"indices\" : {\n" + "        \"index\" : {\n" + "          \"version\" : 1,\n" + "          \"mapping_version\" : 1,\n" + "          \"settings_version\" : 1,\n" + "          \"aliases_version\" : 1,\n" + "          \"routing_num_shards\" : 1,\n" + "          \"state\" : \"open\",\n" + "          \"settings\" : {\n" + "            \"index\" : {\n" + "              \"shard\" : {\n" + "                \"check_on_startup\" : \"true\"\n" + "              },\n" + "              \"number_of_shards\" : \"1\",\n" + "              \"number_of_replicas\" : \"0\",\n" + "              \"version\" : {\n" + "                \"created\" : \"" + Version.CURRENT.id + "\"\n" + "              },\n" + "              \"max_script_fields\" : \"10\"\n" + "            }\n" + "          },\n" + "          \"mappings\" : { },\n" + "          \"aliases\" : [ ],\n" + "          \"primary_terms\" : {\n" + "            \"0\" : 0\n" + "          },\n" + "          \"in_sync_allocations\" : {\n" + "            \"0\" : [ ]\n" + "          },\n" + "          \"rollover_info\" : { },\n" + "          \"system\" : false\n" + "        }\n" + "      },\n" + "      \"index-graveyard\" : {\n" + "        \"tombstones\" : [ ]\n" + "      }\n" + "    },\n" + "    \"routing_table\" : {\n" + "      \"indices\" : { }\n" + "    },\n" + "    \"routing_nodes\" : {\n" + "      \"unassigned\" : [ ],\n" + "      \"nodes\" : {\n" + "        \"node0\" : [ ]\n" + "      }\n" + "    }\n" + "  }\n" + "}", Strings.toString(builder));
    }
    {
        XContentBuilder builder = JsonXContent.contentBuilder().prettyPrint();
        Map<String, String> params = new HashMap<>();
        params.put("explain", "true");
        params.put("metric", "version,cluster_manager_node");
        clusterRerouteResponse.toXContent(builder, new ToXContent.MapParams(params));
        assertEquals("{\n" + "  \"acknowledged\" : true,\n" + "  \"state\" : {\n" + "    \"cluster_uuid\" : \"_na_\",\n" + "    \"version\" : 0,\n" + "    \"state_uuid\" : \"" + clusterState.stateUUID() + "\",\n" + "    \"cluster_manager_node\" : \"node0\"\n" + "  },\n" + "  \"explanations\" : [\n" + "    {\n" + "      \"command\" : \"allocate_replica\",\n" + "      \"parameters\" : {\n" + "        \"index\" : \"index\",\n" + "        \"shard\" : 0,\n" + "        \"node\" : \"node0\"\n" + "      },\n" + "      \"decisions\" : [\n" + "        {\n" + "          \"decider\" : null,\n" + "          \"decision\" : \"YES\",\n" + "          \"explanation\" : \"none\"\n" + "        }\n" + "      ]\n" + "    }\n" + "  ]\n" + "}", Strings.toString(builder));
    }
    {
        XContentBuilder builder = JsonXContent.contentBuilder().prettyPrint();
        Map<String, String> params = new HashMap<>();
        params.put("metric", "metadata");
        params.put("settings_filter", "index.number*,index.version.created");
        clusterRerouteResponse.toXContent(builder, new ToXContent.MapParams(params));
        assertEquals("{\n" + "  \"acknowledged\" : true,\n" + "  \"state\" : {\n" + "    \"cluster_uuid\" : \"_na_\",\n" + "    \"metadata\" : {\n" + "      \"cluster_uuid\" : \"_na_\",\n" + "      \"cluster_uuid_committed\" : false,\n" + "      \"cluster_coordination\" : {\n" + "        \"term\" : 0,\n" + "        \"last_committed_config\" : [ ],\n" + "        \"last_accepted_config\" : [ ],\n" + "        \"voting_config_exclusions\" : [ ]\n" + "      },\n" + "      \"templates\" : { },\n" + "      \"indices\" : {\n" + "        \"index\" : {\n" + "          \"version\" : 1,\n" + "          \"mapping_version\" : 1,\n" + "          \"settings_version\" : 1,\n" + "          \"aliases_version\" : 1,\n" + "          \"routing_num_shards\" : 1,\n" + "          \"state\" : \"open\",\n" + "          \"settings\" : {\n" + "            \"index\" : {\n" + "              \"max_script_fields\" : \"10\",\n" + "              \"shard\" : {\n" + "                \"check_on_startup\" : \"true\"\n" + "              }\n" + "            }\n" + "          },\n" + "          \"mappings\" : { },\n" + "          \"aliases\" : [ ],\n" + "          \"primary_terms\" : {\n" + "            \"0\" : 0\n" + "          },\n" + "          \"in_sync_allocations\" : {\n" + "            \"0\" : [ ]\n" + "          },\n" + "          \"rollover_info\" : { },\n" + "          \"system\" : false\n" + "        }\n" + "      },\n" + "      \"index-graveyard\" : {\n" + "        \"tombstones\" : [ ]\n" + "      }\n" + "    }\n" + "  }\n" + "}", Strings.toString(builder));
    }
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) RoutingExplanations(org.opensearch.cluster.routing.allocation.RoutingExplanations) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) TransportAddress(org.opensearch.common.transport.TransportAddress) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) AllocateReplicaAllocationCommand(org.opensearch.cluster.routing.allocation.command.AllocateReplicaAllocationCommand) RerouteExplanation(org.opensearch.cluster.routing.allocation.RerouteExplanation) ImmutableOpenMap(org.opensearch.common.collect.ImmutableOpenMap) ClusterName(org.opensearch.cluster.ClusterName) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) ImmutableOpenMap(org.opensearch.common.collect.ImmutableOpenMap) HashMap(java.util.HashMap) Map(java.util.Map) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 17 with TransportAddress

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

the class ExceptionSerializationTests method testConnectTransportException.

public void testConnectTransportException() throws IOException {
    TransportAddress transportAddress = buildNewFakeTransportAddress();
    DiscoveryNode node = new DiscoveryNode("thenode", transportAddress, emptyMap(), emptySet(), Version.CURRENT);
    ConnectTransportException ex = serialize(new ConnectTransportException(node, "msg", "action", null));
    assertEquals("[][" + transportAddress.toString() + "][action] msg", ex.getMessage());
    assertEquals(node, ex.node());
    assertEquals("action", ex.action());
    assertNull(ex.getCause());
    ex = serialize(new ConnectTransportException(node, "msg", "action", new NullPointerException()));
    assertEquals("[][" + transportAddress + "][action] msg", ex.getMessage());
    assertEquals(node, ex.node());
    assertEquals("action", ex.action());
    assertTrue(ex.getCause() instanceof NullPointerException);
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) ConnectTransportException(org.opensearch.transport.ConnectTransportException) TransportAddress(org.opensearch.common.transport.TransportAddress)

Example 18 with TransportAddress

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

the class NioHttpServerTransportTests method testReadTimeout.

public void testReadTimeout() throws Exception {
    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("Should not have received a dispatched request");
        }

        @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("Should not have received a dispatched request");
        }
    };
    Settings settings = createBuilderWithPort().put(HttpTransportSettings.SETTING_HTTP_READ_TIMEOUT.getKey(), new TimeValue(randomIntBetween(100, 300))).build();
    try (NioHttpServerTransport transport = new NioHttpServerTransport(settings, networkService, bigArrays, pageRecycler, threadPool, xContentRegistry(), dispatcher, new NioGroupFactory(settings, logger), new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS))) {
        transport.start();
        final TransportAddress remoteAddress = randomFrom(transport.boundAddress().boundAddresses());
        try (NioHttpClient client = new NioHttpClient()) {
            NioSocketChannel channel = null;
            try {
                CountDownLatch channelClosedLatch = new CountDownLatch(1);
                channel = client.connect(remoteAddress.address());
                channel.addCloseListener((r, t) -> channelClosedLatch.countDown());
                assertTrue("Channel should be closed due to read timeout", channelClosedLatch.await(1, TimeUnit.MINUTES));
            } finally {
                if (channel != null) {
                    channel.close();
                }
            }
        }
    }
}
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) HttpServerTransport(org.opensearch.http.HttpServerTransport) NullDispatcher(org.opensearch.http.NullDispatcher) CountDownLatch(java.util.concurrent.CountDownLatch) NioSocketChannel(org.opensearch.nio.NioSocketChannel) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) RestRequest(org.opensearch.rest.RestRequest) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) NioGroupFactory(org.opensearch.transport.nio.NioGroupFactory) Settings(org.opensearch.common.settings.Settings) HttpTransportSettings(org.opensearch.http.HttpTransportSettings) ClusterSettings(org.opensearch.common.settings.ClusterSettings) TimeValue(org.opensearch.common.unit.TimeValue)

Example 19 with TransportAddress

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

the class NioHttpServerTransportTests 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 (NioHttpServerTransport transport = new NioHttpServerTransport(settings, networkService, bigArrays, pageRecycler, threadPool, xContentRegistry(), dispatcher, new NioGroupFactory(settings, logger), new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS))) {
        transport.start();
        final TransportAddress remoteAddress = randomFrom(transport.boundAddress().boundAddresses());
        try (NioHttpClient client = new NioHttpClient()) {
            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) 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) 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) NioGroupFactory(org.opensearch.transport.nio.NioGroupFactory)

Example 20 with TransportAddress

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

the class NioHttpServerTransportTests method testBadRequest.

public void testBadRequest() throws InterruptedException {
    final AtomicReference<Throwable> causeReference = new AtomicReference<>();
    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) {
            causeReference.set(cause);
            try {
                final OpenSearchException e = new OpenSearchException("you sent a bad request and you should feel bad");
                channel.sendResponse(new BytesRestResponse(channel, BAD_REQUEST, e));
            } catch (final IOException e) {
                throw new AssertionError(e);
            }
        }
    };
    final Settings settings;
    final int maxInitialLineLength;
    final Setting<ByteSizeValue> httpMaxInitialLineLengthSetting = HttpTransportSettings.SETTING_HTTP_MAX_INITIAL_LINE_LENGTH;
    if (randomBoolean()) {
        maxInitialLineLength = httpMaxInitialLineLengthSetting.getDefault(Settings.EMPTY).bytesAsInt();
        settings = createSettings();
    } else {
        maxInitialLineLength = randomIntBetween(1, 8192);
        settings = createBuilderWithPort().put(httpMaxInitialLineLengthSetting.getKey(), maxInitialLineLength + "b").build();
    }
    try (NioHttpServerTransport transport = new NioHttpServerTransport(settings, networkService, bigArrays, pageRecycler, threadPool, xContentRegistry(), dispatcher, new NioGroupFactory(settings, logger), new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS))) {
        transport.start();
        final TransportAddress remoteAddress = randomFrom(transport.boundAddress().boundAddresses());
        try (NioHttpClient client = new NioHttpClient()) {
            final String url = "/" + new String(new byte[maxInitialLineLength], Charset.forName("UTF-8"));
            final FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, url);
            final FullHttpResponse response = client.send(remoteAddress.address(), request);
            try {
                assertThat(response.status(), equalTo(HttpResponseStatus.BAD_REQUEST));
                assertThat(new String(response.content().array(), Charset.forName("UTF-8")), containsString("you sent a bad request and you should feel bad"));
            } finally {
                response.release();
            }
        }
    }
    assertNotNull(causeReference.get());
    assertThat(causeReference.get(), instanceOf(TooLongFrameException.class));
}
Also used : ClusterSettings(org.opensearch.common.settings.ClusterSettings) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) TooLongFrameException(io.netty.handler.codec.TooLongFrameException) TransportAddress(org.opensearch.common.transport.TransportAddress) ByteSizeValue(org.opensearch.common.unit.ByteSizeValue) Matchers.containsString(org.hamcrest.Matchers.containsString) HttpServerTransport(org.opensearch.http.HttpServerTransport) NullDispatcher(org.opensearch.http.NullDispatcher) BytesRestResponse(org.opensearch.rest.BytesRestResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) NioGroupFactory(org.opensearch.transport.nio.NioGroupFactory) Settings(org.opensearch.common.settings.Settings) HttpTransportSettings(org.opensearch.http.HttpTransportSettings) ClusterSettings(org.opensearch.common.settings.ClusterSettings) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) RestChannel(org.opensearch.rest.RestChannel) IOException(java.io.IOException) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) RestRequest(org.opensearch.rest.RestRequest) OpenSearchException(org.opensearch.OpenSearchException)

Aggregations

TransportAddress (org.opensearch.common.transport.TransportAddress)175 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)61 Settings (org.opensearch.common.settings.Settings)57 IOException (java.io.IOException)32 BoundTransportAddress (org.opensearch.common.transport.BoundTransportAddress)31 ArrayList (java.util.ArrayList)28 Version (org.opensearch.Version)28 HashSet (java.util.HashSet)25 List (java.util.List)25 InetAddress (java.net.InetAddress)24 CountDownLatch (java.util.concurrent.CountDownLatch)21 ThreadPool (org.opensearch.threadpool.ThreadPool)21 ClusterSettings (org.opensearch.common.settings.ClusterSettings)20 ClusterName (org.opensearch.cluster.ClusterName)17 Set (java.util.Set)16 TimeUnit (java.util.concurrent.TimeUnit)16 PlainActionFuture (org.opensearch.action.support.PlainActionFuture)16 HttpServerTransport (org.opensearch.http.HttpServerTransport)16 InetSocketAddress (java.net.InetSocketAddress)15 OpenSearchTestCase (org.opensearch.test.OpenSearchTestCase)15