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));
}
}
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);
}
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());
}
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()]);
}
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);
}
}
Aggregations