-
Notifications
You must be signed in to change notification settings - Fork 25k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make EC2 Discovery Plugin Retry Requests #50550
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8945922
Make EC2 Discovery Plugin Retry Requests
original-brownbear 7a5b21f
nicer
original-brownbear 9c8c589
nicer
original-brownbear 2d835a8
nicer
original-brownbear d015576
default retry backoff
original-brownbear ba20261
Merge remote-tracking branch 'elastic/master' into 50462
original-brownbear fb28d07
Merge remote-tracking branch 'elastic/master' into 50462
original-brownbear ec56d95
CR: comments
original-brownbear File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
266 changes: 266 additions & 0 deletions
266
plugins/discovery-ec2/src/test/java/org/elasticsearch/discovery/ec2/EC2RetriesTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,266 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.discovery.ec2; | ||
|
||
import com.amazonaws.http.HttpMethodName; | ||
import com.sun.net.httpserver.HttpServer; | ||
import org.apache.http.HttpStatus; | ||
import org.apache.http.NameValuePair; | ||
import org.apache.http.client.utils.URLEncodedUtils; | ||
import org.elasticsearch.Version; | ||
import org.elasticsearch.common.SuppressForbidden; | ||
import org.elasticsearch.common.io.stream.NamedWriteableRegistry; | ||
import org.elasticsearch.common.network.InetAddresses; | ||
import org.elasticsearch.common.network.NetworkService; | ||
import org.elasticsearch.common.settings.MockSecureSettings; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.common.transport.TransportAddress; | ||
import org.elasticsearch.common.util.PageCacheRecycler; | ||
import org.elasticsearch.core.internal.io.IOUtils; | ||
import org.elasticsearch.discovery.SeedHostsProvider; | ||
import org.elasticsearch.discovery.SeedHostsResolver; | ||
import org.elasticsearch.indices.breaker.NoneCircuitBreakerService; | ||
import org.elasticsearch.mocksocket.MockHttpServer; | ||
import org.elasticsearch.test.ESTestCase; | ||
import org.elasticsearch.test.transport.MockTransportService; | ||
import org.elasticsearch.threadpool.TestThreadPool; | ||
import org.elasticsearch.threadpool.ThreadPool; | ||
import org.elasticsearch.transport.TransportService; | ||
import org.elasticsearch.transport.nio.MockNioTransport; | ||
import org.hamcrest.Matchers; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
|
||
import javax.xml.XMLConstants; | ||
import javax.xml.stream.XMLOutputFactory; | ||
import javax.xml.stream.XMLStreamWriter; | ||
|
||
import java.io.IOException; | ||
import java.io.StringWriter; | ||
import java.net.InetAddress; | ||
import java.net.InetSocketAddress; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.UUID; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
import static org.hamcrest.Matchers.aMapWithSize; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
@SuppressForbidden(reason = "use a http server") | ||
public class EC2RetriesTests extends ESTestCase { | ||
|
||
private HttpServer httpServer; | ||
|
||
private ThreadPool threadPool; | ||
|
||
private MockTransportService transportService; | ||
|
||
private NetworkService networkService = new NetworkService(Collections.emptyList()); | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
httpServer = MockHttpServer.createHttp(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0); | ||
httpServer.start(); | ||
threadPool = new TestThreadPool(EC2RetriesTests.class.getName()); | ||
final MockNioTransport transport = new MockNioTransport(Settings.EMPTY, Version.CURRENT, threadPool, networkService, | ||
PageCacheRecycler.NON_RECYCLING_INSTANCE, new NamedWriteableRegistry(Collections.emptyList()), | ||
new NoneCircuitBreakerService()); | ||
transportService = | ||
new MockTransportService(Settings.EMPTY, transport, threadPool, TransportService.NOOP_TRANSPORT_INTERCEPTOR, null); | ||
super.setUp(); | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
try { | ||
IOUtils.close(transportService, () -> terminate(threadPool), () -> httpServer.stop(0)); | ||
} finally { | ||
super.tearDown(); | ||
} | ||
} | ||
|
||
public void testEC2DiscoveryRetriesOnRateLimiting() throws IOException { | ||
final String accessKey = "ec2_access"; | ||
final List<String> hosts = List.of("127.0.0.1:9000"); | ||
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 = new String(exchange.getRequestBody().readAllBytes(), UTF_8); | ||
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) -> Objects.requireNonNullElse(count, 0) + 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); | ||
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); | ||
DaveCTurner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return; | ||
} | ||
} | ||
original-brownbear marked this conversation as resolved.
Show resolved
Hide resolved
|
||
fail("did not send response"); | ||
}); | ||
|
||
final InetSocketAddress address = httpServer.getAddress(); | ||
final String endpoint = "http://" + InetAddresses.toUriString(address.getAddress()) + ":" + address.getPort(); | ||
final MockSecureSettings mockSecure = new MockSecureSettings(); | ||
mockSecure.setString(Ec2ClientSettings.ACCESS_KEY_SETTING.getKey(), accessKey); | ||
mockSecure.setString(Ec2ClientSettings.SECRET_KEY_SETTING.getKey(), "ec2_secret"); | ||
try (Ec2DiscoveryPlugin plugin = new Ec2DiscoveryPlugin( | ||
Settings.builder().put(Ec2ClientSettings.ENDPOINT_SETTING.getKey(), endpoint).setSecureSettings(mockSecure).build())) { | ||
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(resolver); | ||
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)); | ||
} | ||
} | ||
|
||
/** | ||
* Generates a XML response that describe the EC2 instances | ||
* TODO: org.elasticsearch.discovery.ec2.AmazonEC2Fixture uses pretty much the same code. We should dry up that test fixture. | ||
*/ | ||
private byte[] generateDescribeInstancesResponse(List<String> nodes) { | ||
final XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newFactory(); | ||
xmlOutputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true); | ||
|
||
final StringWriter out = new StringWriter(); | ||
XMLStreamWriter sw; | ||
try { | ||
sw = xmlOutputFactory.createXMLStreamWriter(out); | ||
sw.writeStartDocument(); | ||
|
||
String namespace = "http://ec2.amazonaws.com/doc/2013-02-01/"; | ||
sw.setDefaultNamespace(namespace); | ||
sw.writeStartElement(XMLConstants.DEFAULT_NS_PREFIX, "DescribeInstancesResponse", namespace); | ||
{ | ||
sw.writeStartElement("requestId"); | ||
sw.writeCharacters(UUID.randomUUID().toString()); | ||
sw.writeEndElement(); | ||
|
||
sw.writeStartElement("reservationSet"); | ||
{ | ||
for (String address : nodes) { | ||
sw.writeStartElement("item"); | ||
{ | ||
sw.writeStartElement("reservationId"); | ||
sw.writeCharacters(UUID.randomUUID().toString()); | ||
sw.writeEndElement(); | ||
|
||
sw.writeStartElement("instancesSet"); | ||
{ | ||
sw.writeStartElement("item"); | ||
{ | ||
sw.writeStartElement("instanceId"); | ||
sw.writeCharacters(UUID.randomUUID().toString()); | ||
sw.writeEndElement(); | ||
|
||
sw.writeStartElement("imageId"); | ||
sw.writeCharacters(UUID.randomUUID().toString()); | ||
sw.writeEndElement(); | ||
|
||
sw.writeStartElement("instanceState"); | ||
{ | ||
sw.writeStartElement("code"); | ||
sw.writeCharacters("16"); | ||
sw.writeEndElement(); | ||
|
||
sw.writeStartElement("name"); | ||
sw.writeCharacters("running"); | ||
sw.writeEndElement(); | ||
} | ||
sw.writeEndElement(); | ||
|
||
sw.writeStartElement("privateDnsName"); | ||
sw.writeCharacters(address); | ||
sw.writeEndElement(); | ||
|
||
sw.writeStartElement("dnsName"); | ||
sw.writeCharacters(address); | ||
sw.writeEndElement(); | ||
|
||
sw.writeStartElement("instanceType"); | ||
sw.writeCharacters("m1.medium"); | ||
sw.writeEndElement(); | ||
|
||
sw.writeStartElement("placement"); | ||
{ | ||
sw.writeStartElement("availabilityZone"); | ||
sw.writeCharacters("use-east-1e"); | ||
sw.writeEndElement(); | ||
|
||
sw.writeEmptyElement("groupName"); | ||
|
||
sw.writeStartElement("tenancy"); | ||
sw.writeCharacters("default"); | ||
sw.writeEndElement(); | ||
} | ||
sw.writeEndElement(); | ||
|
||
sw.writeStartElement("privateIpAddress"); | ||
sw.writeCharacters(address); | ||
sw.writeEndElement(); | ||
|
||
sw.writeStartElement("ipAddress"); | ||
sw.writeCharacters(address); | ||
sw.writeEndElement(); | ||
} | ||
sw.writeEndElement(); | ||
} | ||
sw.writeEndElement(); | ||
} | ||
sw.writeEndElement(); | ||
} | ||
sw.writeEndElement(); | ||
} | ||
sw.writeEndElement(); | ||
|
||
sw.writeEndDocument(); | ||
sw.flush(); | ||
} | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
return out.toString().getBytes(UTF_8); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Admittedly quite a bit of code, but I'm hoping we can just extend the REST layer mocking here and dry things up against the fixture step-by-step like we did for S3 and the other cloud providers in the repository tests to get rid of all our complicated mocking of actual AWS SDK classes and just mock on the REST layer.