Search in sources :

Example 26 with TransportAddress

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

the class EC2RetriesTests method testEC2DiscoveryRetriesOnRateLimiting.

public void testEC2DiscoveryRetriesOnRateLimiting() throws IOException {
    final String accessKey = "ec2_access";
    final List<String> hosts = Collections.singletonList("127.0.0.1:9300");
    final Map<String, Integer> failedRequests = new ConcurrentHashMap<>();
    // retry the same request 5 times at most
    final int maxRetries = randomIntBetween(1, 5);
    httpServer.createContext("/", exchange -> {
        if (exchange.getRequestMethod().equals(HttpMethodName.POST.name())) {
            final String request = Streams.readFully(exchange.getRequestBody()).utf8ToString();
            final String userAgent = exchange.getRequestHeaders().getFirst("User-Agent");
            if (userAgent != null && userAgent.startsWith("aws-sdk-java")) {
                final String auth = exchange.getRequestHeaders().getFirst("Authorization");
                if (auth == null || auth.contains(accessKey) == false) {
                    throw new IllegalArgumentException("wrong access key: " + auth);
                }
                if (failedRequests.compute(exchange.getRequestHeaders().getFirst("Amz-sdk-invocation-id"), (requestId, count) -> (count == null ? 0 : count) + 1) < maxRetries) {
                    exchange.sendResponseHeaders(HttpStatus.SC_SERVICE_UNAVAILABLE, -1);
                    return;
                }
                // Simulate an EC2 DescribeInstancesResponse
                byte[] responseBody = null;
                for (NameValuePair parse : URLEncodedUtils.parse(request, UTF_8)) {
                    if ("Action".equals(parse.getName())) {
                        responseBody = generateDescribeInstancesResponse(hosts.stream().map(address -> new Instance().withPublicIpAddress(address)).collect(Collectors.toList()));
                        break;
                    }
                }
                responseBody = responseBody == null ? new byte[0] : responseBody;
                exchange.getResponseHeaders().set("Content-Type", "text/xml; charset=UTF-8");
                exchange.sendResponseHeaders(HttpStatus.SC_OK, responseBody.length);
                exchange.getResponseBody().write(responseBody);
                return;
            }
        }
        fail("did not send response");
    });
    try (Ec2DiscoveryPlugin plugin = new Ec2DiscoveryPlugin(buildSettings(accessKey))) {
        final SeedHostsProvider seedHostsProvider = plugin.getSeedHostProviders(transportService, networkService).get("ec2").get();
        final SeedHostsResolver resolver = new SeedHostsResolver("test", Settings.EMPTY, transportService, seedHostsProvider);
        resolver.start();
        final List<TransportAddress> addressList = seedHostsProvider.getSeedAddresses(null);
        assertThat(addressList, Matchers.hasSize(1));
        assertThat(addressList.get(0).toString(), is(hosts.get(0)));
        assertThat(failedRequests, aMapWithSize(1));
        assertThat(failedRequests.values().iterator().next(), is(maxRetries));
    }
}
Also used : Matchers.aMapWithSize(org.hamcrest.Matchers.aMapWithSize) NoneCircuitBreakerService(org.opensearch.indices.breaker.NoneCircuitBreakerService) HttpStatus(org.apache.http.HttpStatus) Version(org.opensearch.Version) MockTransportService(org.opensearch.test.transport.MockTransportService) SeedHostsResolver(org.opensearch.discovery.SeedHostsResolver) NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) Streams(org.opensearch.common.io.Streams) Map(java.util.Map) Instance(com.amazonaws.services.ec2.model.Instance) MockNioTransport(org.opensearch.transport.nio.MockNioTransport) SeedHostsProvider(org.opensearch.discovery.SeedHostsProvider) SuppressForbidden(org.opensearch.common.SuppressForbidden) UTF_8(java.nio.charset.StandardCharsets.UTF_8) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Matchers(org.hamcrest.Matchers) Settings(org.opensearch.common.settings.Settings) IOException(java.io.IOException) HttpMethodName(com.amazonaws.http.HttpMethodName) TransportService(org.opensearch.transport.TransportService) Collectors(java.util.stream.Collectors) TransportAddress(org.opensearch.common.transport.TransportAddress) List(java.util.List) URLEncodedUtils(org.apache.http.client.utils.URLEncodedUtils) Matchers.is(org.hamcrest.Matchers.is) NameValuePair(org.apache.http.NameValuePair) Collections(java.util.Collections) PageCacheRecycler(org.opensearch.common.util.PageCacheRecycler) NameValuePair(org.apache.http.NameValuePair) Instance(com.amazonaws.services.ec2.model.Instance) TransportAddress(org.opensearch.common.transport.TransportAddress) SeedHostsResolver(org.opensearch.discovery.SeedHostsResolver) SeedHostsProvider(org.opensearch.discovery.SeedHostsProvider) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 27 with TransportAddress

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

the class AbstractHttpServerTransport method bindServer.

protected void bindServer() {
    // Bind and start to accept incoming connections.
    InetAddress[] hostAddresses;
    try {
        hostAddresses = networkService.resolveBindHostAddresses(bindHosts);
    } catch (IOException e) {
        throw new BindHttpException("Failed to resolve host [" + Arrays.toString(bindHosts) + "]", e);
    }
    List<TransportAddress> boundAddresses = new ArrayList<>(hostAddresses.length);
    for (InetAddress address : hostAddresses) {
        boundAddresses.add(bindAddress(address));
    }
    final InetAddress publishInetAddress;
    try {
        publishInetAddress = networkService.resolvePublishHostAddresses(publishHosts);
    } catch (Exception e) {
        throw new BindTransportException("Failed to resolve publish address", e);
    }
    final int publishPort = resolvePublishPort(settings, boundAddresses, publishInetAddress);
    TransportAddress publishAddress = new TransportAddress(new InetSocketAddress(publishInetAddress, publishPort));
    this.boundAddress = new BoundTransportAddress(boundAddresses.toArray(new TransportAddress[0]), publishAddress);
    logger.info("{}", boundAddress);
}
Also used : BoundTransportAddress(org.opensearch.common.transport.BoundTransportAddress) TransportAddress(org.opensearch.common.transport.TransportAddress) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) BoundTransportAddress(org.opensearch.common.transport.BoundTransportAddress) BindTransportException(org.opensearch.transport.BindTransportException) IOException(java.io.IOException) InetAddress(java.net.InetAddress) CancelledKeyException(java.nio.channels.CancelledKeyException) IOException(java.io.IOException) BindTransportException(org.opensearch.transport.BindTransportException)

Example 28 with TransportAddress

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

the class AbstractHttpServerTransport method bindAddress.

private TransportAddress bindAddress(final InetAddress hostAddress) {
    final AtomicReference<Exception> lastException = new AtomicReference<>();
    final AtomicReference<InetSocketAddress> boundSocket = new AtomicReference<>();
    boolean success = port.iterate(portNumber -> {
        try {
            synchronized (httpServerChannels) {
                HttpServerChannel httpServerChannel = bind(new InetSocketAddress(hostAddress, portNumber));
                httpServerChannels.add(httpServerChannel);
                boundSocket.set(httpServerChannel.getLocalAddress());
            }
        } catch (Exception e) {
            lastException.set(e);
            return false;
        }
        return true;
    });
    if (!success) {
        throw new BindHttpException("Failed to bind to " + NetworkAddress.format(hostAddress, port), lastException.get());
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Bound http to address {{}}", NetworkAddress.format(boundSocket.get()));
    }
    return new TransportAddress(boundSocket.get());
}
Also used : InetSocketAddress(java.net.InetSocketAddress) BoundTransportAddress(org.opensearch.common.transport.BoundTransportAddress) TransportAddress(org.opensearch.common.transport.TransportAddress) AtomicReference(java.util.concurrent.atomic.AtomicReference) CancelledKeyException(java.nio.channels.CancelledKeyException) IOException(java.io.IOException) BindTransportException(org.opensearch.transport.BindTransportException)

Example 29 with TransportAddress

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

the class MockTransportService method extractTransportAddresses.

private static TransportAddress[] extractTransportAddresses(TransportService transportService) {
    HashSet<TransportAddress> transportAddresses = new HashSet<>();
    BoundTransportAddress boundTransportAddress = transportService.boundAddress();
    transportAddresses.addAll(Arrays.asList(boundTransportAddress.boundAddresses()));
    transportAddresses.add(boundTransportAddress.publishAddress());
    return transportAddresses.toArray(new TransportAddress[transportAddresses.size()]);
}
Also used : BoundTransportAddress(org.opensearch.common.transport.BoundTransportAddress) TransportAddress(org.opensearch.common.transport.TransportAddress) BoundTransportAddress(org.opensearch.common.transport.BoundTransportAddress) HashSet(java.util.HashSet)

Example 30 with TransportAddress

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

the class StubbableTransport method openConnection.

@Override
public void openConnection(DiscoveryNode node, ConnectionProfile profile, ActionListener<Connection> listener) {
    TransportAddress address = node.getAddress();
    OpenConnectionBehavior behavior = connectBehaviors.getOrDefault(address, defaultConnectBehavior);
    ActionListener<Connection> wrappedListener = ActionListener.delegateFailure(listener, (delegatedListener, connection) -> delegatedListener.onResponse(new WrappedConnection(connection)));
    if (behavior == null) {
        delegate.openConnection(node, profile, wrappedListener);
    } else {
        behavior.openConnection(delegate, node, profile, wrappedListener);
    }
}
Also used : BoundTransportAddress(org.opensearch.common.transport.BoundTransportAddress) TransportAddress(org.opensearch.common.transport.TransportAddress)

Aggregations

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