Search in sources :

Example 1 with SeedHostsProvider

use of org.opensearch.discovery.SeedHostsProvider 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)

Aggregations

HttpMethodName (com.amazonaws.http.HttpMethodName)1 Instance (com.amazonaws.services.ec2.model.Instance)1 IOException (java.io.IOException)1 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Collectors (java.util.stream.Collectors)1 HttpStatus (org.apache.http.HttpStatus)1 NameValuePair (org.apache.http.NameValuePair)1 URLEncodedUtils (org.apache.http.client.utils.URLEncodedUtils)1 Matchers (org.hamcrest.Matchers)1 Matchers.aMapWithSize (org.hamcrest.Matchers.aMapWithSize)1 Matchers.is (org.hamcrest.Matchers.is)1 Version (org.opensearch.Version)1 SuppressForbidden (org.opensearch.common.SuppressForbidden)1 Streams (org.opensearch.common.io.Streams)1 NamedWriteableRegistry (org.opensearch.common.io.stream.NamedWriteableRegistry)1 Settings (org.opensearch.common.settings.Settings)1