Search in sources :

Example 26 with TransportAddress

use of org.elasticsearch.common.transport.TransportAddress in project elasticsearch by elastic.

the class Netty4HttpServerTransportTests 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) {
            throw new AssertionError();
        }

        @Override
        public void dispatchBadRequest(final RestRequest request, final RestChannel channel, final ThreadContext threadContext, final Throwable cause) {
            causeReference.set(cause);
            try {
                final ElasticsearchException e = new ElasticsearchException("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 = Settings.EMPTY;
    } else {
        maxInitialLineLength = randomIntBetween(1, 8192);
        settings = Settings.builder().put(httpMaxInitialLineLengthSetting.getKey(), maxInitialLineLength + "b").build();
    }
    try (Netty4HttpServerTransport transport = new Netty4HttpServerTransport(settings, networkService, bigArrays, threadPool, xContentRegistry(), dispatcher)) {
        transport.start();
        final TransportAddress remoteAddress = randomFrom(transport.boundAddress.boundAddresses());
        try (Netty4HttpClient client = new Netty4HttpClient()) {
            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.post(remoteAddress.address(), request);
            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"));
        }
    }
    assertNotNull(causeReference.get());
    assertThat(causeReference.get(), instanceOf(TooLongFrameException.class));
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) TooLongFrameException(io.netty.handler.codec.TooLongFrameException) TransportAddress(org.elasticsearch.common.transport.TransportAddress) ByteSizeValue(org.elasticsearch.common.unit.ByteSizeValue) ElasticsearchException(org.elasticsearch.ElasticsearchException) Matchers.containsString(org.hamcrest.Matchers.containsString) Strings.collectionToDelimitedString(org.elasticsearch.common.Strings.collectionToDelimitedString) NullDispatcher(org.elasticsearch.http.NullDispatcher) HttpServerTransport(org.elasticsearch.http.HttpServerTransport) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) Settings(org.elasticsearch.common.settings.Settings) HttpTransportSettings(org.elasticsearch.http.HttpTransportSettings) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) ThreadContext(org.elasticsearch.common.util.concurrent.ThreadContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) RestChannel(org.elasticsearch.rest.RestChannel) IOException(java.io.IOException) RestRequest(org.elasticsearch.rest.RestRequest)

Example 27 with TransportAddress

use of org.elasticsearch.common.transport.TransportAddress in project elasticsearch by elastic.

the class Netty4PipeliningDisabledIT method testThatNettyHttpServerDoesNotSupportPipelining.

public void testThatNettyHttpServerDoesNotSupportPipelining() throws Exception {
    ensureGreen();
    String[] requests = new String[] { "/", "/_nodes/stats", "/", "/_cluster/state", "/", "/_nodes", "/" };
    HttpServerTransport httpServerTransport = internalCluster().getInstance(HttpServerTransport.class);
    TransportAddress[] boundAddresses = httpServerTransport.boundAddress().boundAddresses();
    TransportAddress transportAddress = (TransportAddress) randomFrom(boundAddresses);
    try (Netty4HttpClient nettyHttpClient = new Netty4HttpClient()) {
        Collection<FullHttpResponse> responses = nettyHttpClient.get(transportAddress.address(), requests);
        assertThat(responses, hasSize(requests.length));
        List<String> opaqueIds = new ArrayList<>(Netty4HttpClient.returnOpaqueIds(responses));
        assertResponsesOutOfOrder(opaqueIds);
    }
}
Also used : TransportAddress(org.elasticsearch.common.transport.TransportAddress) ArrayList(java.util.ArrayList) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) HttpServerTransport(org.elasticsearch.http.HttpServerTransport)

Example 28 with TransportAddress

use of org.elasticsearch.common.transport.TransportAddress in project elasticsearch by elastic.

the class Netty4PipeliningEnabledIT method testThatNettyHttpServerSupportsPipelining.

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

Example 29 with TransportAddress

use of org.elasticsearch.common.transport.TransportAddress in project elasticsearch by elastic.

the class AwsEc2UnicastHostsProvider method fetchDynamicNodes.

protected List<DiscoveryNode> fetchDynamicNodes() {
    List<DiscoveryNode> discoNodes = new ArrayList<>();
    DescribeInstancesResult descInstances;
    try {
        // Query EC2 API based on AZ, instance state, and tag.
        // NOTE: we don't filter by security group during the describe instances request for two reasons:
        // 1. differences in VPCs require different parameters during query (ID vs Name)
        // 2. We want to use two different strategies: (all security groups vs. any security groups)
        descInstances = SocketAccess.doPrivileged(() -> client.describeInstances(buildDescribeInstancesRequest()));
    } catch (AmazonClientException e) {
        logger.info("Exception while retrieving instance list from AWS API: {}", e.getMessage());
        logger.debug("Full exception:", e);
        return discoNodes;
    }
    logger.trace("building dynamic unicast discovery nodes...");
    for (Reservation reservation : descInstances.getReservations()) {
        for (Instance instance : reservation.getInstances()) {
            // lets see if we can filter based on groups
            if (!groups.isEmpty()) {
                List<GroupIdentifier> instanceSecurityGroups = instance.getSecurityGroups();
                ArrayList<String> securityGroupNames = new ArrayList<String>();
                ArrayList<String> securityGroupIds = new ArrayList<String>();
                for (GroupIdentifier sg : instanceSecurityGroups) {
                    securityGroupNames.add(sg.getGroupName());
                    securityGroupIds.add(sg.getGroupId());
                }
                if (bindAnyGroup) {
                    // We check if we can find at least one group name or one group id in groups.
                    if (disjoint(securityGroupNames, groups) && disjoint(securityGroupIds, groups)) {
                        logger.trace("filtering out instance {} based on groups {}, not part of {}", instance.getInstanceId(), instanceSecurityGroups, groups);
                        // continue to the next instance
                        continue;
                    }
                } else {
                    // We need tp match all group names or group ids, otherwise we ignore this instance
                    if (!(securityGroupNames.containsAll(groups) || securityGroupIds.containsAll(groups))) {
                        logger.trace("filtering out instance {} based on groups {}, does not include all of {}", instance.getInstanceId(), instanceSecurityGroups, groups);
                        // continue to the next instance
                        continue;
                    }
                }
            }
            String address = null;
            if (hostType.equals(PRIVATE_DNS)) {
                address = instance.getPrivateDnsName();
            } else if (hostType.equals(PRIVATE_IP)) {
                address = instance.getPrivateIpAddress();
            } else if (hostType.equals(PUBLIC_DNS)) {
                address = instance.getPublicDnsName();
            } else if (hostType.equals(PUBLIC_IP)) {
                address = instance.getPublicIpAddress();
            } else if (hostType.startsWith(TAG_PREFIX)) {
                // Reading the node host from its metadata
                String tagName = hostType.substring(TAG_PREFIX.length());
                logger.debug("reading hostname from [{}] instance tag", tagName);
                List<Tag> tags = instance.getTags();
                for (Tag tag : tags) {
                    if (tag.getKey().equals(tagName)) {
                        address = tag.getValue();
                        logger.debug("using [{}] as the instance address", address);
                    }
                }
            } else {
                throw new IllegalArgumentException(hostType + " is unknown for discovery.ec2.host_type");
            }
            if (address != null) {
                try {
                    // we only limit to 1 port per address, makes no sense to ping 100 ports
                    TransportAddress[] addresses = transportService.addressesFromString(address, 1);
                    for (int i = 0; i < addresses.length; i++) {
                        logger.trace("adding {}, address {}, transport_address {}", instance.getInstanceId(), address, addresses[i]);
                        discoNodes.add(new DiscoveryNode(instance.getInstanceId(), "#cloud-" + instance.getInstanceId() + "-" + i, addresses[i], emptyMap(), emptySet(), Version.CURRENT.minimumCompatibilityVersion()));
                    }
                } catch (Exception e) {
                    final String finalAddress = address;
                    logger.warn((Supplier<?>) () -> new ParameterizedMessage("failed to add {}, address {}", instance.getInstanceId(), finalAddress), e);
                }
            } else {
                logger.trace("not adding {}, address is null, host_type {}", instance.getInstanceId(), hostType);
            }
        }
    }
    logger.debug("using dynamic discovery nodes {}", discoNodes);
    return discoNodes;
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) Instance(com.amazonaws.services.ec2.model.Instance) TransportAddress(org.elasticsearch.common.transport.TransportAddress) AmazonClientException(com.amazonaws.AmazonClientException) ArrayList(java.util.ArrayList) Collections.disjoint(java.util.Collections.disjoint) AmazonClientException(com.amazonaws.AmazonClientException) GroupIdentifier(com.amazonaws.services.ec2.model.GroupIdentifier) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Reservation(com.amazonaws.services.ec2.model.Reservation) Supplier(org.apache.logging.log4j.util.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) Tag(com.amazonaws.services.ec2.model.Tag)

Example 30 with TransportAddress

use of org.elasticsearch.common.transport.TransportAddress in project elasticsearch by elastic.

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_timeout"));
        assertThat(e.getMessage(), containsString("[127.0.0.1:9876]"));
    }
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) TransportAddress(org.elasticsearch.common.transport.TransportAddress)

Aggregations

TransportAddress (org.elasticsearch.common.transport.TransportAddress)89 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)32 Settings (org.elasticsearch.common.settings.Settings)29 ArrayList (java.util.ArrayList)24 BoundTransportAddress (org.elasticsearch.common.transport.BoundTransportAddress)24 IOException (java.io.IOException)21 InetSocketAddress (java.net.InetSocketAddress)20 InetAddress (java.net.InetAddress)19 UnknownHostException (java.net.UnknownHostException)12 NamedWriteableRegistry (org.elasticsearch.common.io.stream.NamedWriteableRegistry)12 TransportService (org.elasticsearch.transport.TransportService)12 HashSet (java.util.HashSet)11 TransportClient (org.elasticsearch.client.transport.TransportClient)11 NetworkService (org.elasticsearch.common.network.NetworkService)11 MockTransportService (org.elasticsearch.test.transport.MockTransportService)11 NoneCircuitBreakerService (org.elasticsearch.indices.breaker.NoneCircuitBreakerService)10 HashMap (java.util.HashMap)9 List (java.util.List)9 Logger (org.apache.logging.log4j.Logger)9 MockTcpTransport (org.elasticsearch.transport.MockTcpTransport)9