Return transport addresses from UnicastHostsProvider (#31426)

With #20695 we removed local transport and there is just TransportAddress now. The
UnicastHostsProvider currently returns DiscoveryNode instances, where, during pinging, we're
actually only making use of the TransportAddress to establish a first connection to the possible new 
node. To simplify the interface, we can just return a list of transport addresses instead, which
means that it's not necessary anymore to create fake node objects in each plugin just to return the
address information.
This commit is contained in:
Yannick Welsch 2018-06-21 16:00:26 +02:00 committed by GitHub
parent 86423f9563
commit da69ab28c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 175 additions and 220 deletions

View File

@ -24,12 +24,10 @@ import com.microsoft.windowsazure.management.compute.models.DeploymentStatus;
import com.microsoft.windowsazure.management.compute.models.HostedServiceGetDetailedResponse; import com.microsoft.windowsazure.management.compute.models.HostedServiceGetDetailedResponse;
import com.microsoft.windowsazure.management.compute.models.InstanceEndpoint; import com.microsoft.windowsazure.management.compute.models.InstanceEndpoint;
import com.microsoft.windowsazure.management.compute.models.RoleInstance; import com.microsoft.windowsazure.management.compute.models.RoleInstance;
import org.elasticsearch.Version;
import org.elasticsearch.cloud.azure.classic.AzureServiceDisableException; import org.elasticsearch.cloud.azure.classic.AzureServiceDisableException;
import org.elasticsearch.cloud.azure.classic.AzureServiceRemoteException; import org.elasticsearch.cloud.azure.classic.AzureServiceRemoteException;
import org.elasticsearch.cloud.azure.classic.management.AzureComputeService; import org.elasticsearch.cloud.azure.classic.management.AzureComputeService;
import org.elasticsearch.cloud.azure.classic.management.AzureComputeService.Discovery; import org.elasticsearch.cloud.azure.classic.management.AzureComputeService.Discovery;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.component.AbstractComponent; import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.network.InetAddresses; import org.elasticsearch.common.network.InetAddresses;
@ -47,9 +45,6 @@ import java.net.InetSocketAddress;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static java.util.Collections.emptyMap;
import static java.util.Collections.emptySet;
public class AzureUnicastHostsProvider extends AbstractComponent implements UnicastHostsProvider { public class AzureUnicastHostsProvider extends AbstractComponent implements UnicastHostsProvider {
public enum HostType { public enum HostType {
@ -104,7 +99,7 @@ public class AzureUnicastHostsProvider extends AbstractComponent implements Unic
private final TimeValue refreshInterval; private final TimeValue refreshInterval;
private long lastRefresh; private long lastRefresh;
private List<DiscoveryNode> cachedDiscoNodes; private List<TransportAddress> dynamicHosts;
private final HostType hostType; private final HostType hostType;
private final String publicEndpointName; private final String publicEndpointName;
private final String deploymentName; private final String deploymentName;
@ -137,30 +132,30 @@ public class AzureUnicastHostsProvider extends AbstractComponent implements Unic
* Setting `cloud.azure.refresh_interval` to `0` will disable caching (default). * Setting `cloud.azure.refresh_interval` to `0` will disable caching (default).
*/ */
@Override @Override
public List<DiscoveryNode> buildDynamicNodes() { public List<TransportAddress> buildDynamicHosts() {
if (refreshInterval.millis() != 0) { if (refreshInterval.millis() != 0) {
if (cachedDiscoNodes != null && if (dynamicHosts != null &&
(refreshInterval.millis() < 0 || (System.currentTimeMillis() - lastRefresh) < refreshInterval.millis())) { (refreshInterval.millis() < 0 || (System.currentTimeMillis() - lastRefresh) < refreshInterval.millis())) {
logger.trace("using cache to retrieve node list"); logger.trace("using cache to retrieve node list");
return cachedDiscoNodes; return dynamicHosts;
} }
lastRefresh = System.currentTimeMillis(); lastRefresh = System.currentTimeMillis();
} }
logger.debug("start building nodes list using Azure API"); logger.debug("start building nodes list using Azure API");
cachedDiscoNodes = new ArrayList<>(); dynamicHosts = new ArrayList<>();
HostedServiceGetDetailedResponse detailed; HostedServiceGetDetailedResponse detailed;
try { try {
detailed = azureComputeService.getServiceDetails(); detailed = azureComputeService.getServiceDetails();
} catch (AzureServiceDisableException e) { } catch (AzureServiceDisableException e) {
logger.debug("Azure discovery service has been disabled. Returning empty list of nodes."); logger.debug("Azure discovery service has been disabled. Returning empty list of nodes.");
return cachedDiscoNodes; return dynamicHosts;
} catch (AzureServiceRemoteException e) { } catch (AzureServiceRemoteException e) {
// We got a remote exception // We got a remote exception
logger.warn("can not get list of azure nodes: [{}]. Returning empty list of nodes.", e.getMessage()); logger.warn("can not get list of azure nodes: [{}]. Returning empty list of nodes.", e.getMessage());
logger.trace("AzureServiceRemoteException caught", e); logger.trace("AzureServiceRemoteException caught", e);
return cachedDiscoNodes; return dynamicHosts;
} }
InetAddress ipAddress = null; InetAddress ipAddress = null;
@ -212,8 +207,7 @@ public class AzureUnicastHostsProvider extends AbstractComponent implements Unic
TransportAddress[] addresses = transportService.addressesFromString(networkAddress, 1); TransportAddress[] addresses = transportService.addressesFromString(networkAddress, 1);
for (TransportAddress address : addresses) { for (TransportAddress address : addresses) {
logger.trace("adding {}, transport_address {}", networkAddress, address); logger.trace("adding {}, transport_address {}", networkAddress, address);
cachedDiscoNodes.add(new DiscoveryNode("#cloud-" + instance.getInstanceName(), address, emptyMap(), dynamicHosts.add(address);
emptySet(), Version.CURRENT.minimumCompatibilityVersion()));
} }
} catch (Exception e) { } catch (Exception e) {
logger.warn("can not convert [{}] to transport address. skipping. [{}]", networkAddress, e.getMessage()); logger.warn("can not convert [{}] to transport address. skipping. [{}]", networkAddress, e.getMessage());
@ -221,9 +215,9 @@ public class AzureUnicastHostsProvider extends AbstractComponent implements Unic
} }
} }
logger.debug("{} node(s) added", cachedDiscoNodes.size()); logger.debug("{} addresses added", dynamicHosts.size());
return cachedDiscoNodes; return dynamicHosts;
} }
protected String resolveInstanceAddress(final HostType hostType, final RoleInstance instance) { protected String resolveInstanceAddress(final HostType hostType, final RoleInstance instance) {

View File

@ -29,8 +29,6 @@ import com.amazonaws.services.ec2.model.Reservation;
import com.amazonaws.services.ec2.model.Tag; import com.amazonaws.services.ec2.model.Tag;
import org.apache.logging.log4j.message.ParameterizedMessage; import org.apache.logging.log4j.message.ParameterizedMessage;
import org.apache.logging.log4j.util.Supplier; import org.apache.logging.log4j.util.Supplier;
import org.elasticsearch.Version;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.common.component.AbstractComponent; import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress; import org.elasticsearch.common.transport.TransportAddress;
@ -46,8 +44,6 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import static java.util.Collections.disjoint; import static java.util.Collections.disjoint;
import static java.util.Collections.emptyMap;
import static java.util.Collections.emptySet;
import static org.elasticsearch.discovery.ec2.AwsEc2Service.HostType.TAG_PREFIX; import static org.elasticsearch.discovery.ec2.AwsEc2Service.HostType.TAG_PREFIX;
import static org.elasticsearch.discovery.ec2.AwsEc2Service.HostType.PRIVATE_DNS; import static org.elasticsearch.discovery.ec2.AwsEc2Service.HostType.PRIVATE_DNS;
import static org.elasticsearch.discovery.ec2.AwsEc2Service.HostType.PRIVATE_IP; import static org.elasticsearch.discovery.ec2.AwsEc2Service.HostType.PRIVATE_IP;
@ -70,7 +66,7 @@ class AwsEc2UnicastHostsProvider extends AbstractComponent implements UnicastHos
private final String hostType; private final String hostType;
private final DiscoNodesCache discoNodes; private final TransportAddressesCache dynamicHosts;
AwsEc2UnicastHostsProvider(Settings settings, TransportService transportService, AwsEc2Service awsEc2Service) { AwsEc2UnicastHostsProvider(Settings settings, TransportService transportService, AwsEc2Service awsEc2Service) {
super(settings); super(settings);
@ -78,7 +74,7 @@ class AwsEc2UnicastHostsProvider extends AbstractComponent implements UnicastHos
this.awsEc2Service = awsEc2Service; this.awsEc2Service = awsEc2Service;
this.hostType = AwsEc2Service.HOST_TYPE_SETTING.get(settings); this.hostType = AwsEc2Service.HOST_TYPE_SETTING.get(settings);
this.discoNodes = new DiscoNodesCache(AwsEc2Service.NODE_CACHE_TIME_SETTING.get(settings)); this.dynamicHosts = new TransportAddressesCache(AwsEc2Service.NODE_CACHE_TIME_SETTING.get(settings));
this.bindAnyGroup = AwsEc2Service.ANY_GROUP_SETTING.get(settings); this.bindAnyGroup = AwsEc2Service.ANY_GROUP_SETTING.get(settings);
this.groups = new HashSet<>(); this.groups = new HashSet<>();
@ -96,13 +92,13 @@ class AwsEc2UnicastHostsProvider extends AbstractComponent implements UnicastHos
} }
@Override @Override
public List<DiscoveryNode> buildDynamicNodes() { public List<TransportAddress> buildDynamicHosts() {
return discoNodes.getOrRefresh(); return dynamicHosts.getOrRefresh();
} }
protected List<DiscoveryNode> fetchDynamicNodes() { protected List<TransportAddress> fetchDynamicNodes() {
final List<DiscoveryNode> discoNodes = new ArrayList<>(); final List<TransportAddress> dynamicHosts = new ArrayList<>();
final DescribeInstancesResult descInstances; final DescribeInstancesResult descInstances;
try (AmazonEc2Reference clientReference = awsEc2Service.client()) { try (AmazonEc2Reference clientReference = awsEc2Service.client()) {
@ -115,7 +111,7 @@ class AwsEc2UnicastHostsProvider extends AbstractComponent implements UnicastHos
} catch (final AmazonClientException e) { } catch (final AmazonClientException e) {
logger.info("Exception while retrieving instance list from AWS API: {}", e.getMessage()); logger.info("Exception while retrieving instance list from AWS API: {}", e.getMessage());
logger.debug("Full exception:", e); logger.debug("Full exception:", e);
return discoNodes; return dynamicHosts;
} }
logger.trace("building dynamic unicast discovery nodes..."); logger.trace("building dynamic unicast discovery nodes...");
@ -179,8 +175,7 @@ class AwsEc2UnicastHostsProvider extends AbstractComponent implements UnicastHos
final TransportAddress[] addresses = transportService.addressesFromString(address, 1); final TransportAddress[] addresses = transportService.addressesFromString(address, 1);
for (int i = 0; i < addresses.length; i++) { for (int i = 0; i < addresses.length; i++) {
logger.trace("adding {}, address {}, transport_address {}", instance.getInstanceId(), address, addresses[i]); logger.trace("adding {}, address {}, transport_address {}", instance.getInstanceId(), address, addresses[i]);
discoNodes.add(new DiscoveryNode(instance.getInstanceId(), "#cloud-" + instance.getInstanceId() + "-" + i, dynamicHosts.add(addresses[i]);
addresses[i], emptyMap(), emptySet(), Version.CURRENT.minimumCompatibilityVersion()));
} }
} catch (final Exception e) { } catch (final Exception e) {
final String finalAddress = address; final String finalAddress = address;
@ -194,9 +189,9 @@ class AwsEc2UnicastHostsProvider extends AbstractComponent implements UnicastHos
} }
} }
logger.debug("using dynamic discovery nodes {}", discoNodes); logger.debug("using dynamic transport addresses {}", dynamicHosts);
return discoNodes; return dynamicHosts;
} }
private DescribeInstancesRequest buildDescribeInstancesRequest() { private DescribeInstancesRequest buildDescribeInstancesRequest() {
@ -222,11 +217,11 @@ class AwsEc2UnicastHostsProvider extends AbstractComponent implements UnicastHos
return describeInstancesRequest; return describeInstancesRequest;
} }
private final class DiscoNodesCache extends SingleObjectCache<List<DiscoveryNode>> { private final class TransportAddressesCache extends SingleObjectCache<List<TransportAddress>> {
private boolean empty = true; private boolean empty = true;
protected DiscoNodesCache(TimeValue refreshInterval) { protected TransportAddressesCache(TimeValue refreshInterval) {
super(refreshInterval, new ArrayList<>()); super(refreshInterval, new ArrayList<>());
} }
@ -236,8 +231,8 @@ class AwsEc2UnicastHostsProvider extends AbstractComponent implements UnicastHos
} }
@Override @Override
protected List<DiscoveryNode> refresh() { protected List<TransportAddress> refresh() {
final List<DiscoveryNode> nodes = fetchDynamicNodes(); final List<TransportAddress> nodes = fetchDynamicNodes();
empty = nodes.isEmpty(); empty = nodes.isEmpty();
return nodes; return nodes;
} }

View File

@ -21,7 +21,6 @@ package org.elasticsearch.discovery.ec2;
import com.amazonaws.services.ec2.model.Tag; import com.amazonaws.services.ec2.model.Tag;
import org.elasticsearch.Version; import org.elasticsearch.Version;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.network.NetworkService; import org.elasticsearch.common.network.NetworkService;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
@ -87,16 +86,16 @@ public class Ec2DiscoveryTests extends ESTestCase {
null); null);
} }
protected List<DiscoveryNode> buildDynamicNodes(Settings nodeSettings, int nodes) { protected List<TransportAddress> buildDynamicHosts(Settings nodeSettings, int nodes) {
return buildDynamicNodes(nodeSettings, nodes, null); return buildDynamicHosts(nodeSettings, nodes, null);
} }
protected List<DiscoveryNode> buildDynamicNodes(Settings nodeSettings, int nodes, List<List<Tag>> tagsList) { protected List<TransportAddress> buildDynamicHosts(Settings nodeSettings, int nodes, List<List<Tag>> tagsList) {
try (Ec2DiscoveryPluginMock plugin = new Ec2DiscoveryPluginMock(Settings.EMPTY, nodes, tagsList)) { try (Ec2DiscoveryPluginMock plugin = new Ec2DiscoveryPluginMock(Settings.EMPTY, nodes, tagsList)) {
AwsEc2UnicastHostsProvider provider = new AwsEc2UnicastHostsProvider(nodeSettings, transportService, plugin.ec2Service); AwsEc2UnicastHostsProvider provider = new AwsEc2UnicastHostsProvider(nodeSettings, transportService, plugin.ec2Service);
List<DiscoveryNode> discoveryNodes = provider.buildDynamicNodes(); List<TransportAddress> dynamicHosts = provider.buildDynamicHosts();
logger.debug("--> nodes found: {}", discoveryNodes); logger.debug("--> addresses found: {}", dynamicHosts);
return discoveryNodes; return dynamicHosts;
} catch (IOException e) { } catch (IOException e) {
fail("Unexpected IOException"); fail("Unexpected IOException");
return null; return null;
@ -107,7 +106,7 @@ public class Ec2DiscoveryTests extends ESTestCase {
int nodes = randomInt(10); int nodes = randomInt(10);
Settings nodeSettings = Settings.builder() Settings nodeSettings = Settings.builder()
.build(); .build();
List<DiscoveryNode> discoveryNodes = buildDynamicNodes(nodeSettings, nodes); List<TransportAddress> discoveryNodes = buildDynamicHosts(nodeSettings, nodes);
assertThat(discoveryNodes, hasSize(nodes)); assertThat(discoveryNodes, hasSize(nodes));
} }
@ -119,12 +118,11 @@ public class Ec2DiscoveryTests extends ESTestCase {
Settings nodeSettings = Settings.builder() Settings nodeSettings = Settings.builder()
.put(AwsEc2Service.HOST_TYPE_SETTING.getKey(), "private_ip") .put(AwsEc2Service.HOST_TYPE_SETTING.getKey(), "private_ip")
.build(); .build();
List<DiscoveryNode> discoveryNodes = buildDynamicNodes(nodeSettings, nodes); List<TransportAddress> transportAddresses = buildDynamicHosts(nodeSettings, nodes);
assertThat(discoveryNodes, hasSize(nodes)); assertThat(transportAddresses, hasSize(nodes));
// We check that we are using here expected address // We check that we are using here expected address
int node = 1; int node = 1;
for (DiscoveryNode discoveryNode : discoveryNodes) { for (TransportAddress address : transportAddresses) {
TransportAddress address = discoveryNode.getAddress();
TransportAddress expected = poorMansDNS.get(AmazonEC2Mock.PREFIX_PRIVATE_IP + node++); TransportAddress expected = poorMansDNS.get(AmazonEC2Mock.PREFIX_PRIVATE_IP + node++);
assertEquals(address, expected); assertEquals(address, expected);
} }
@ -138,12 +136,11 @@ public class Ec2DiscoveryTests extends ESTestCase {
Settings nodeSettings = Settings.builder() Settings nodeSettings = Settings.builder()
.put(AwsEc2Service.HOST_TYPE_SETTING.getKey(), "public_ip") .put(AwsEc2Service.HOST_TYPE_SETTING.getKey(), "public_ip")
.build(); .build();
List<DiscoveryNode> discoveryNodes = buildDynamicNodes(nodeSettings, nodes); List<TransportAddress> dynamicHosts = buildDynamicHosts(nodeSettings, nodes);
assertThat(discoveryNodes, hasSize(nodes)); assertThat(dynamicHosts, hasSize(nodes));
// We check that we are using here expected address // We check that we are using here expected address
int node = 1; int node = 1;
for (DiscoveryNode discoveryNode : discoveryNodes) { for (TransportAddress address : dynamicHosts) {
TransportAddress address = discoveryNode.getAddress();
TransportAddress expected = poorMansDNS.get(AmazonEC2Mock.PREFIX_PUBLIC_IP + node++); TransportAddress expected = poorMansDNS.get(AmazonEC2Mock.PREFIX_PUBLIC_IP + node++);
assertEquals(address, expected); assertEquals(address, expected);
} }
@ -159,13 +156,12 @@ public class Ec2DiscoveryTests extends ESTestCase {
Settings nodeSettings = Settings.builder() Settings nodeSettings = Settings.builder()
.put(AwsEc2Service.HOST_TYPE_SETTING.getKey(), "private_dns") .put(AwsEc2Service.HOST_TYPE_SETTING.getKey(), "private_dns")
.build(); .build();
List<DiscoveryNode> discoveryNodes = buildDynamicNodes(nodeSettings, nodes); List<TransportAddress> dynamicHosts = buildDynamicHosts(nodeSettings, nodes);
assertThat(discoveryNodes, hasSize(nodes)); assertThat(dynamicHosts, hasSize(nodes));
// We check that we are using here expected address // We check that we are using here expected address
int node = 1; int node = 1;
for (DiscoveryNode discoveryNode : discoveryNodes) { for (TransportAddress address : dynamicHosts) {
String instanceId = "node" + node++; String instanceId = "node" + node++;
TransportAddress address = discoveryNode.getAddress();
TransportAddress expected = poorMansDNS.get( TransportAddress expected = poorMansDNS.get(
AmazonEC2Mock.PREFIX_PRIVATE_DNS + instanceId + AmazonEC2Mock.SUFFIX_PRIVATE_DNS); AmazonEC2Mock.PREFIX_PRIVATE_DNS + instanceId + AmazonEC2Mock.SUFFIX_PRIVATE_DNS);
assertEquals(address, expected); assertEquals(address, expected);
@ -182,13 +178,12 @@ public class Ec2DiscoveryTests extends ESTestCase {
Settings nodeSettings = Settings.builder() Settings nodeSettings = Settings.builder()
.put(AwsEc2Service.HOST_TYPE_SETTING.getKey(), "public_dns") .put(AwsEc2Service.HOST_TYPE_SETTING.getKey(), "public_dns")
.build(); .build();
List<DiscoveryNode> discoveryNodes = buildDynamicNodes(nodeSettings, nodes); List<TransportAddress> dynamicHosts = buildDynamicHosts(nodeSettings, nodes);
assertThat(discoveryNodes, hasSize(nodes)); assertThat(dynamicHosts, hasSize(nodes));
// We check that we are using here expected address // We check that we are using here expected address
int node = 1; int node = 1;
for (DiscoveryNode discoveryNode : discoveryNodes) { for (TransportAddress address : dynamicHosts) {
String instanceId = "node" + node++; String instanceId = "node" + node++;
TransportAddress address = discoveryNode.getAddress();
TransportAddress expected = poorMansDNS.get( TransportAddress expected = poorMansDNS.get(
AmazonEC2Mock.PREFIX_PUBLIC_DNS + instanceId + AmazonEC2Mock.SUFFIX_PUBLIC_DNS); AmazonEC2Mock.PREFIX_PUBLIC_DNS + instanceId + AmazonEC2Mock.SUFFIX_PUBLIC_DNS);
assertEquals(address, expected); assertEquals(address, expected);
@ -201,7 +196,7 @@ public class Ec2DiscoveryTests extends ESTestCase {
.build(); .build();
IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> { IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> {
buildDynamicNodes(nodeSettings, 1); buildDynamicHosts(nodeSettings, 1);
}); });
assertThat(exception.getMessage(), containsString("does_not_exist is unknown for discovery.ec2.host_type")); assertThat(exception.getMessage(), containsString("does_not_exist is unknown for discovery.ec2.host_type"));
} }
@ -227,8 +222,8 @@ public class Ec2DiscoveryTests extends ESTestCase {
} }
logger.info("started [{}] instances with [{}] stage=prod tag", nodes, prodInstances); logger.info("started [{}] instances with [{}] stage=prod tag", nodes, prodInstances);
List<DiscoveryNode> discoveryNodes = buildDynamicNodes(nodeSettings, nodes, tagsList); List<TransportAddress> dynamicHosts = buildDynamicHosts(nodeSettings, nodes, tagsList);
assertThat(discoveryNodes, hasSize(prodInstances)); assertThat(dynamicHosts, hasSize(prodInstances));
} }
public void testFilterByMultipleTags() throws InterruptedException { public void testFilterByMultipleTags() throws InterruptedException {
@ -258,8 +253,8 @@ public class Ec2DiscoveryTests extends ESTestCase {
} }
logger.info("started [{}] instances with [{}] stage=prod tag", nodes, prodInstances); logger.info("started [{}] instances with [{}] stage=prod tag", nodes, prodInstances);
List<DiscoveryNode> discoveryNodes = buildDynamicNodes(nodeSettings, nodes, tagsList); List<TransportAddress> dynamicHosts = buildDynamicHosts(nodeSettings, nodes, tagsList);
assertThat(discoveryNodes, hasSize(prodInstances)); assertThat(dynamicHosts, hasSize(prodInstances));
} }
public void testReadHostFromTag() throws InterruptedException, UnknownHostException { public void testReadHostFromTag() throws InterruptedException, UnknownHostException {
@ -285,11 +280,11 @@ public class Ec2DiscoveryTests extends ESTestCase {
} }
logger.info("started [{}] instances", nodes); logger.info("started [{}] instances", nodes);
List<DiscoveryNode> discoveryNodes = buildDynamicNodes(nodeSettings, nodes, tagsList); List<TransportAddress> dynamicHosts = buildDynamicHosts(nodeSettings, nodes, tagsList);
assertThat(discoveryNodes, hasSize(nodes)); assertThat(dynamicHosts, hasSize(nodes));
for (DiscoveryNode discoveryNode : discoveryNodes) { int node = 1;
TransportAddress address = discoveryNode.getAddress(); for (TransportAddress address : dynamicHosts) {
TransportAddress expected = poorMansDNS.get(discoveryNode.getName()); TransportAddress expected = poorMansDNS.get("node" + node++);
assertEquals(address, expected); assertEquals(address, expected);
} }
} }
@ -306,13 +301,13 @@ public class Ec2DiscoveryTests extends ESTestCase {
AwsEc2Service awsEc2Service = new AwsEc2ServiceMock(Settings.EMPTY, 1, null); AwsEc2Service awsEc2Service = new AwsEc2ServiceMock(Settings.EMPTY, 1, null);
DummyEc2HostProvider provider = new DummyEc2HostProvider(Settings.EMPTY, transportService, awsEc2Service) { DummyEc2HostProvider provider = new DummyEc2HostProvider(Settings.EMPTY, transportService, awsEc2Service) {
@Override @Override
protected List<DiscoveryNode> fetchDynamicNodes() { protected List<TransportAddress> fetchDynamicNodes() {
fetchCount++; fetchCount++;
return new ArrayList<>(); return new ArrayList<>();
} }
}; };
for (int i=0; i<3; i++) { for (int i=0; i<3; i++) {
provider.buildDynamicNodes(); provider.buildDynamicHosts();
} }
assertThat(provider.fetchCount, is(3)); assertThat(provider.fetchCount, is(3));
} }
@ -323,18 +318,18 @@ public class Ec2DiscoveryTests extends ESTestCase {
try (Ec2DiscoveryPluginMock plugin = new Ec2DiscoveryPluginMock(Settings.EMPTY)) { try (Ec2DiscoveryPluginMock plugin = new Ec2DiscoveryPluginMock(Settings.EMPTY)) {
DummyEc2HostProvider provider = new DummyEc2HostProvider(builder.build(), transportService, plugin.ec2Service) { DummyEc2HostProvider provider = new DummyEc2HostProvider(builder.build(), transportService, plugin.ec2Service) {
@Override @Override
protected List<DiscoveryNode> fetchDynamicNodes() { protected List<TransportAddress> fetchDynamicNodes() {
fetchCount++; fetchCount++;
return Ec2DiscoveryTests.this.buildDynamicNodes(Settings.EMPTY, 1); return Ec2DiscoveryTests.this.buildDynamicHosts(Settings.EMPTY, 1);
} }
}; };
for (int i=0; i<3; i++) { for (int i=0; i<3; i++) {
provider.buildDynamicNodes(); provider.buildDynamicHosts();
} }
assertThat(provider.fetchCount, is(1)); assertThat(provider.fetchCount, is(1));
Thread.sleep(1_000L); // wait for cache to expire Thread.sleep(1_000L); // wait for cache to expire
for (int i=0; i<3; i++) { for (int i=0; i<3; i++) {
provider.buildDynamicNodes(); provider.buildDynamicHosts();
} }
assertThat(provider.fetchCount, is(2)); assertThat(provider.fetchCount, is(2));
} }

View File

@ -21,8 +21,8 @@ package org.elasticsearch.discovery.file;
import org.apache.logging.log4j.message.ParameterizedMessage; import org.apache.logging.log4j.message.ParameterizedMessage;
import org.apache.logging.log4j.util.Supplier; import org.apache.logging.log4j.util.Supplier;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.common.component.AbstractComponent; import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.discovery.zen.UnicastHostsProvider; import org.elasticsearch.discovery.zen.UnicastHostsProvider;
import org.elasticsearch.env.Environment; import org.elasticsearch.env.Environment;
@ -58,7 +58,6 @@ import static org.elasticsearch.discovery.zen.UnicastZenPing.resolveHostsLists;
class FileBasedUnicastHostsProvider extends AbstractComponent implements UnicastHostsProvider { class FileBasedUnicastHostsProvider extends AbstractComponent implements UnicastHostsProvider {
static final String UNICAST_HOSTS_FILE = "unicast_hosts.txt"; static final String UNICAST_HOSTS_FILE = "unicast_hosts.txt";
static final String UNICAST_HOST_PREFIX = "#zen_file_unicast_host_";
private final TransportService transportService; private final TransportService transportService;
private final ExecutorService executorService; private final ExecutorService executorService;
@ -76,7 +75,7 @@ class FileBasedUnicastHostsProvider extends AbstractComponent implements Unicast
} }
@Override @Override
public List<DiscoveryNode> buildDynamicNodes() { public List<TransportAddress> buildDynamicHosts() {
List<String> hostsList; List<String> hostsList;
try (Stream<String> lines = Files.lines(unicastHostsFilePath)) { try (Stream<String> lines = Files.lines(unicastHostsFilePath)) {
hostsList = lines.filter(line -> line.startsWith("#") == false) // lines starting with `#` are comments hostsList = lines.filter(line -> line.startsWith("#") == false) // lines starting with `#` are comments
@ -91,23 +90,22 @@ class FileBasedUnicastHostsProvider extends AbstractComponent implements Unicast
hostsList = Collections.emptyList(); hostsList = Collections.emptyList();
} }
final List<DiscoveryNode> discoNodes = new ArrayList<>(); final List<TransportAddress> dynamicHosts = new ArrayList<>();
try { try {
discoNodes.addAll(resolveHostsLists( dynamicHosts.addAll(resolveHostsLists(
executorService, executorService,
logger, logger,
hostsList, hostsList,
1, 1,
transportService, transportService,
UNICAST_HOST_PREFIX,
resolveTimeout)); resolveTimeout));
} catch (InterruptedException e) { } catch (InterruptedException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
logger.debug("[discovery-file] Using dynamic discovery nodes {}", discoNodes); logger.debug("[discovery-file] Using dynamic discovery nodes {}", dynamicHosts);
return discoNodes; return dynamicHosts;
} }
} }

View File

@ -19,7 +19,6 @@
package org.elasticsearch.discovery.file; package org.elasticsearch.discovery.file;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.network.NetworkService; import org.elasticsearch.common.network.NetworkService;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
@ -50,7 +49,6 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import static org.elasticsearch.discovery.file.FileBasedUnicastHostsProvider.UNICAST_HOSTS_FILE; import static org.elasticsearch.discovery.file.FileBasedUnicastHostsProvider.UNICAST_HOSTS_FILE;
import static org.elasticsearch.discovery.file.FileBasedUnicastHostsProvider.UNICAST_HOST_PREFIX;
/** /**
* Tests for {@link FileBasedUnicastHostsProvider}. * Tests for {@link FileBasedUnicastHostsProvider}.
@ -104,23 +102,20 @@ public class FileBasedUnicastHostsProviderTests extends ESTestCase {
public void testBuildDynamicNodes() throws Exception { public void testBuildDynamicNodes() throws Exception {
final List<String> hostEntries = Arrays.asList("#comment, should be ignored", "192.168.0.1", "192.168.0.2:9305", "255.255.23.15"); final List<String> hostEntries = Arrays.asList("#comment, should be ignored", "192.168.0.1", "192.168.0.2:9305", "255.255.23.15");
final List<DiscoveryNode> nodes = setupAndRunHostProvider(hostEntries); final List<TransportAddress> nodes = setupAndRunHostProvider(hostEntries);
assertEquals(hostEntries.size() - 1, nodes.size()); // minus 1 because we are ignoring the first line that's a comment assertEquals(hostEntries.size() - 1, nodes.size()); // minus 1 because we are ignoring the first line that's a comment
assertEquals("192.168.0.1", nodes.get(0).getAddress().getAddress()); assertEquals("192.168.0.1", nodes.get(0).getAddress());
assertEquals(9300, nodes.get(0).getAddress().getPort()); assertEquals(9300, nodes.get(0).getPort());
assertEquals(UNICAST_HOST_PREFIX + "192.168.0.1_0#", nodes.get(0).getId()); assertEquals("192.168.0.2", nodes.get(1).getAddress());
assertEquals("192.168.0.2", nodes.get(1).getAddress().getAddress()); assertEquals(9305, nodes.get(1).getPort());
assertEquals(9305, nodes.get(1).getAddress().getPort()); assertEquals("255.255.23.15", nodes.get(2).getAddress());
assertEquals(UNICAST_HOST_PREFIX + "192.168.0.2:9305_0#", nodes.get(1).getId()); assertEquals(9300, nodes.get(2).getPort());
assertEquals("255.255.23.15", nodes.get(2).getAddress().getAddress());
assertEquals(9300, nodes.get(2).getAddress().getPort());
assertEquals(UNICAST_HOST_PREFIX + "255.255.23.15_0#", nodes.get(2).getId());
} }
public void testEmptyUnicastHostsFile() throws Exception { public void testEmptyUnicastHostsFile() throws Exception {
final List<String> hostEntries = Collections.emptyList(); final List<String> hostEntries = Collections.emptyList();
final List<DiscoveryNode> nodes = setupAndRunHostProvider(hostEntries); final List<TransportAddress> addresses = setupAndRunHostProvider(hostEntries);
assertEquals(0, nodes.size()); assertEquals(0, addresses.size());
} }
public void testUnicastHostsDoesNotExist() throws Exception { public void testUnicastHostsDoesNotExist() throws Exception {
@ -129,27 +124,27 @@ public class FileBasedUnicastHostsProviderTests extends ESTestCase {
.build(); .build();
final Environment environment = TestEnvironment.newEnvironment(settings); final Environment environment = TestEnvironment.newEnvironment(settings);
final FileBasedUnicastHostsProvider provider = new FileBasedUnicastHostsProvider(environment, transportService, executorService); final FileBasedUnicastHostsProvider provider = new FileBasedUnicastHostsProvider(environment, transportService, executorService);
final List<DiscoveryNode> nodes = provider.buildDynamicNodes(); final List<TransportAddress> addresses = provider.buildDynamicHosts();
assertEquals(0, nodes.size()); assertEquals(0, addresses.size());
} }
public void testInvalidHostEntries() throws Exception { public void testInvalidHostEntries() throws Exception {
List<String> hostEntries = Arrays.asList("192.168.0.1:9300:9300"); List<String> hostEntries = Arrays.asList("192.168.0.1:9300:9300");
List<DiscoveryNode> nodes = setupAndRunHostProvider(hostEntries); List<TransportAddress> addresses = setupAndRunHostProvider(hostEntries);
assertEquals(0, nodes.size()); assertEquals(0, addresses.size());
} }
public void testSomeInvalidHostEntries() throws Exception { public void testSomeInvalidHostEntries() throws Exception {
List<String> hostEntries = Arrays.asList("192.168.0.1:9300:9300", "192.168.0.1:9301"); List<String> hostEntries = Arrays.asList("192.168.0.1:9300:9300", "192.168.0.1:9301");
List<DiscoveryNode> nodes = setupAndRunHostProvider(hostEntries); List<TransportAddress> addresses = setupAndRunHostProvider(hostEntries);
assertEquals(1, nodes.size()); // only one of the two is valid and will be used assertEquals(1, addresses.size()); // only one of the two is valid and will be used
assertEquals("192.168.0.1", nodes.get(0).getAddress().getAddress()); assertEquals("192.168.0.1", addresses.get(0).getAddress());
assertEquals(9301, nodes.get(0).getAddress().getPort()); assertEquals(9301, addresses.get(0).getPort());
} }
// sets up the config dir, writes to the unicast hosts file in the config dir, // sets up the config dir, writes to the unicast hosts file in the config dir,
// and then runs the file-based unicast host provider to get the list of discovery nodes // and then runs the file-based unicast host provider to get the list of discovery nodes
private List<DiscoveryNode> setupAndRunHostProvider(final List<String> hostEntries) throws IOException { private List<TransportAddress> setupAndRunHostProvider(final List<String> hostEntries) throws IOException {
final Path homeDir = createTempDir(); final Path homeDir = createTempDir();
final Settings settings = Settings.builder() final Settings settings = Settings.builder()
.put(Environment.PATH_HOME_SETTING.getKey(), homeDir) .put(Environment.PATH_HOME_SETTING.getKey(), homeDir)
@ -168,6 +163,6 @@ public class FileBasedUnicastHostsProviderTests extends ESTestCase {
} }
return new FileBasedUnicastHostsProvider( return new FileBasedUnicastHostsProvider(
new Environment(settings, configPath), transportService, executorService).buildDynamicNodes(); new Environment(settings, configPath), transportService, executorService).buildDynamicHosts();
} }
} }

View File

@ -31,9 +31,7 @@ import com.google.api.services.compute.model.Instance;
import com.google.api.services.compute.model.NetworkInterface; import com.google.api.services.compute.model.NetworkInterface;
import org.apache.logging.log4j.message.ParameterizedMessage; import org.apache.logging.log4j.message.ParameterizedMessage;
import org.apache.logging.log4j.util.Supplier; import org.apache.logging.log4j.util.Supplier;
import org.elasticsearch.Version;
import org.elasticsearch.cloud.gce.GceInstancesService; import org.elasticsearch.cloud.gce.GceInstancesService;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.component.AbstractComponent; import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.network.NetworkAddress; import org.elasticsearch.common.network.NetworkAddress;
@ -47,8 +45,6 @@ import org.elasticsearch.discovery.zen.UnicastHostsProvider;
import org.elasticsearch.transport.TransportService; import org.elasticsearch.transport.TransportService;
import static java.util.Collections.emptyList; import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
import static java.util.Collections.emptySet;
public class GceUnicastHostsProvider extends AbstractComponent implements UnicastHostsProvider { public class GceUnicastHostsProvider extends AbstractComponent implements UnicastHostsProvider {
@ -72,7 +68,7 @@ public class GceUnicastHostsProvider extends AbstractComponent implements Unicas
private final TimeValue refreshInterval; private final TimeValue refreshInterval;
private long lastRefresh; private long lastRefresh;
private List<DiscoveryNode> cachedDiscoNodes; private List<TransportAddress> cachedDynamicHosts;
public GceUnicastHostsProvider(Settings settings, GceInstancesService gceInstancesService, public GceUnicastHostsProvider(Settings settings, GceInstancesService gceInstancesService,
TransportService transportService, TransportService transportService,
@ -97,7 +93,7 @@ public class GceUnicastHostsProvider extends AbstractComponent implements Unicas
* Information can be cached using `cloud.gce.refresh_interval` property if needed. * Information can be cached using `cloud.gce.refresh_interval` property if needed.
*/ */
@Override @Override
public List<DiscoveryNode> buildDynamicNodes() { public List<TransportAddress> buildDynamicHosts() {
// We check that needed properties have been set // We check that needed properties have been set
if (this.project == null || this.project.isEmpty() || this.zones == null || this.zones.isEmpty()) { if (this.project == null || this.project.isEmpty() || this.zones == null || this.zones.isEmpty()) {
throw new IllegalArgumentException("one or more gce discovery settings are missing. " + throw new IllegalArgumentException("one or more gce discovery settings are missing. " +
@ -106,16 +102,16 @@ public class GceUnicastHostsProvider extends AbstractComponent implements Unicas
} }
if (refreshInterval.millis() != 0) { if (refreshInterval.millis() != 0) {
if (cachedDiscoNodes != null && if (cachedDynamicHosts != null &&
(refreshInterval.millis() < 0 || (System.currentTimeMillis() - lastRefresh) < refreshInterval.millis())) { (refreshInterval.millis() < 0 || (System.currentTimeMillis() - lastRefresh) < refreshInterval.millis())) {
if (logger.isTraceEnabled()) logger.trace("using cache to retrieve node list"); if (logger.isTraceEnabled()) logger.trace("using cache to retrieve node list");
return cachedDiscoNodes; return cachedDynamicHosts;
} }
lastRefresh = System.currentTimeMillis(); lastRefresh = System.currentTimeMillis();
} }
logger.debug("start building nodes list using GCE API"); logger.debug("start building nodes list using GCE API");
cachedDiscoNodes = new ArrayList<>(); cachedDynamicHosts = new ArrayList<>();
String ipAddress = null; String ipAddress = null;
try { try {
InetAddress inetAddress = networkService.resolvePublishHostAddresses( InetAddress inetAddress = networkService.resolvePublishHostAddresses(
@ -133,7 +129,7 @@ public class GceUnicastHostsProvider extends AbstractComponent implements Unicas
if (instances == null) { if (instances == null) {
logger.trace("no instance found for project [{}], zones [{}].", this.project, this.zones); logger.trace("no instance found for project [{}], zones [{}].", this.project, this.zones);
return cachedDiscoNodes; return cachedDynamicHosts;
} }
for (Instance instance : instances) { for (Instance instance : instances) {
@ -238,8 +234,7 @@ public class GceUnicastHostsProvider extends AbstractComponent implements Unicas
for (TransportAddress transportAddress : addresses) { for (TransportAddress transportAddress : addresses) {
logger.trace("adding {}, type {}, address {}, transport_address {}, status {}", name, type, logger.trace("adding {}, type {}, address {}, transport_address {}, status {}", name, type,
ip_private, transportAddress, status); ip_private, transportAddress, status);
cachedDiscoNodes.add(new DiscoveryNode("#cloud-" + name + "-" + 0, transportAddress, cachedDynamicHosts.add(transportAddress);
emptyMap(), emptySet(), Version.CURRENT.minimumCompatibilityVersion()));
} }
} }
} catch (Exception e) { } catch (Exception e) {
@ -252,9 +247,9 @@ public class GceUnicastHostsProvider extends AbstractComponent implements Unicas
logger.warn("exception caught during discovery", e); logger.warn("exception caught during discovery", e);
} }
logger.debug("{} node(s) added", cachedDiscoNodes.size()); logger.debug("{} addresses added", cachedDynamicHosts.size());
logger.debug("using dynamic discovery nodes {}", cachedDiscoNodes); logger.debug("using transport addresses {}", cachedDynamicHosts);
return cachedDiscoNodes; return cachedDynamicHosts;
} }
} }

View File

@ -21,9 +21,9 @@ package org.elasticsearch.discovery.gce;
import org.elasticsearch.Version; import org.elasticsearch.Version;
import org.elasticsearch.cloud.gce.GceInstancesServiceImpl; import org.elasticsearch.cloud.gce.GceInstancesServiceImpl;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.common.network.NetworkService; import org.elasticsearch.common.network.NetworkService;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.transport.MockTransportService; import org.elasticsearch.test.transport.MockTransportService;
import org.elasticsearch.threadpool.TestThreadPool; import org.elasticsearch.threadpool.TestThreadPool;
@ -40,7 +40,6 @@ import java.util.Locale;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
/** /**
* This test class uses a GCE HTTP Mock system which allows to simulate JSON Responses. * This test class uses a GCE HTTP Mock system which allows to simulate JSON Responses.
@ -105,13 +104,13 @@ public class GceDiscoveryTests extends ESTestCase {
} }
} }
protected List<DiscoveryNode> buildDynamicNodes(GceInstancesServiceImpl gceInstancesService, Settings nodeSettings) { protected List<TransportAddress> buildDynamicNodes(GceInstancesServiceImpl gceInstancesService, Settings nodeSettings) {
GceUnicastHostsProvider provider = new GceUnicastHostsProvider(nodeSettings, gceInstancesService, GceUnicastHostsProvider provider = new GceUnicastHostsProvider(nodeSettings, gceInstancesService,
transportService, new NetworkService(Collections.emptyList())); transportService, new NetworkService(Collections.emptyList()));
List<DiscoveryNode> discoveryNodes = provider.buildDynamicNodes(); List<TransportAddress> dynamicHosts = provider.buildDynamicHosts();
logger.info("--> nodes found: {}", discoveryNodes); logger.info("--> addresses found: {}", dynamicHosts);
return discoveryNodes; return dynamicHosts;
} }
public void testNodesWithDifferentTagsAndNoTagSet() { public void testNodesWithDifferentTagsAndNoTagSet() {
@ -120,8 +119,8 @@ public class GceDiscoveryTests extends ESTestCase {
.put(GceInstancesServiceImpl.ZONE_SETTING.getKey(), "europe-west1-b") .put(GceInstancesServiceImpl.ZONE_SETTING.getKey(), "europe-west1-b")
.build(); .build();
mock = new GceInstancesServiceMock(nodeSettings); mock = new GceInstancesServiceMock(nodeSettings);
List<DiscoveryNode> discoveryNodes = buildDynamicNodes(mock, nodeSettings); List<TransportAddress> dynamicHosts = buildDynamicNodes(mock, nodeSettings);
assertThat(discoveryNodes, hasSize(2)); assertThat(dynamicHosts, hasSize(2));
} }
public void testNodesWithDifferentTagsAndOneTagSet() { public void testNodesWithDifferentTagsAndOneTagSet() {
@ -131,9 +130,8 @@ public class GceDiscoveryTests extends ESTestCase {
.putList(GceUnicastHostsProvider.TAGS_SETTING.getKey(), "elasticsearch") .putList(GceUnicastHostsProvider.TAGS_SETTING.getKey(), "elasticsearch")
.build(); .build();
mock = new GceInstancesServiceMock(nodeSettings); mock = new GceInstancesServiceMock(nodeSettings);
List<DiscoveryNode> discoveryNodes = buildDynamicNodes(mock, nodeSettings); List<TransportAddress> dynamicHosts = buildDynamicNodes(mock, nodeSettings);
assertThat(discoveryNodes, hasSize(1)); assertThat(dynamicHosts, hasSize(1));
assertThat(discoveryNodes.get(0).getId(), is("#cloud-test2-0"));
} }
public void testNodesWithDifferentTagsAndTwoTagSet() { public void testNodesWithDifferentTagsAndTwoTagSet() {
@ -143,9 +141,8 @@ public class GceDiscoveryTests extends ESTestCase {
.putList(GceUnicastHostsProvider.TAGS_SETTING.getKey(), "elasticsearch", "dev") .putList(GceUnicastHostsProvider.TAGS_SETTING.getKey(), "elasticsearch", "dev")
.build(); .build();
mock = new GceInstancesServiceMock(nodeSettings); mock = new GceInstancesServiceMock(nodeSettings);
List<DiscoveryNode> discoveryNodes = buildDynamicNodes(mock, nodeSettings); List<TransportAddress> dynamicHosts = buildDynamicNodes(mock, nodeSettings);
assertThat(discoveryNodes, hasSize(1)); assertThat(dynamicHosts, hasSize(1));
assertThat(discoveryNodes.get(0).getId(), is("#cloud-test2-0"));
} }
public void testNodesWithSameTagsAndNoTagSet() { public void testNodesWithSameTagsAndNoTagSet() {
@ -154,8 +151,8 @@ public class GceDiscoveryTests extends ESTestCase {
.put(GceInstancesServiceImpl.ZONE_SETTING.getKey(), "europe-west1-b") .put(GceInstancesServiceImpl.ZONE_SETTING.getKey(), "europe-west1-b")
.build(); .build();
mock = new GceInstancesServiceMock(nodeSettings); mock = new GceInstancesServiceMock(nodeSettings);
List<DiscoveryNode> discoveryNodes = buildDynamicNodes(mock, nodeSettings); List<TransportAddress> dynamicHosts = buildDynamicNodes(mock, nodeSettings);
assertThat(discoveryNodes, hasSize(2)); assertThat(dynamicHosts, hasSize(2));
} }
public void testNodesWithSameTagsAndOneTagSet() { public void testNodesWithSameTagsAndOneTagSet() {
@ -165,8 +162,8 @@ public class GceDiscoveryTests extends ESTestCase {
.putList(GceUnicastHostsProvider.TAGS_SETTING.getKey(), "elasticsearch") .putList(GceUnicastHostsProvider.TAGS_SETTING.getKey(), "elasticsearch")
.build(); .build();
mock = new GceInstancesServiceMock(nodeSettings); mock = new GceInstancesServiceMock(nodeSettings);
List<DiscoveryNode> discoveryNodes = buildDynamicNodes(mock, nodeSettings); List<TransportAddress> dynamicHosts = buildDynamicNodes(mock, nodeSettings);
assertThat(discoveryNodes, hasSize(2)); assertThat(dynamicHosts, hasSize(2));
} }
public void testNodesWithSameTagsAndTwoTagsSet() { public void testNodesWithSameTagsAndTwoTagsSet() {
@ -176,8 +173,8 @@ public class GceDiscoveryTests extends ESTestCase {
.putList(GceUnicastHostsProvider.TAGS_SETTING.getKey(), "elasticsearch", "dev") .putList(GceUnicastHostsProvider.TAGS_SETTING.getKey(), "elasticsearch", "dev")
.build(); .build();
mock = new GceInstancesServiceMock(nodeSettings); mock = new GceInstancesServiceMock(nodeSettings);
List<DiscoveryNode> discoveryNodes = buildDynamicNodes(mock, nodeSettings); List<TransportAddress> dynamicHosts = buildDynamicNodes(mock, nodeSettings);
assertThat(discoveryNodes, hasSize(2)); assertThat(dynamicHosts, hasSize(2));
} }
public void testMultipleZonesAndTwoNodesInSameZone() { public void testMultipleZonesAndTwoNodesInSameZone() {
@ -186,8 +183,8 @@ public class GceDiscoveryTests extends ESTestCase {
.putList(GceInstancesServiceImpl.ZONE_SETTING.getKey(), "us-central1-a", "europe-west1-b") .putList(GceInstancesServiceImpl.ZONE_SETTING.getKey(), "us-central1-a", "europe-west1-b")
.build(); .build();
mock = new GceInstancesServiceMock(nodeSettings); mock = new GceInstancesServiceMock(nodeSettings);
List<DiscoveryNode> discoveryNodes = buildDynamicNodes(mock, nodeSettings); List<TransportAddress> dynamicHosts = buildDynamicNodes(mock, nodeSettings);
assertThat(discoveryNodes, hasSize(2)); assertThat(dynamicHosts, hasSize(2));
} }
public void testMultipleZonesAndTwoNodesInDifferentZones() { public void testMultipleZonesAndTwoNodesInDifferentZones() {
@ -196,8 +193,8 @@ public class GceDiscoveryTests extends ESTestCase {
.putList(GceInstancesServiceImpl.ZONE_SETTING.getKey(), "us-central1-a", "europe-west1-b") .putList(GceInstancesServiceImpl.ZONE_SETTING.getKey(), "us-central1-a", "europe-west1-b")
.build(); .build();
mock = new GceInstancesServiceMock(nodeSettings); mock = new GceInstancesServiceMock(nodeSettings);
List<DiscoveryNode> discoveryNodes = buildDynamicNodes(mock, nodeSettings); List<TransportAddress> dynamicHosts = buildDynamicNodes(mock, nodeSettings);
assertThat(discoveryNodes, hasSize(2)); assertThat(dynamicHosts, hasSize(2));
} }
/** /**
@ -209,8 +206,8 @@ public class GceDiscoveryTests extends ESTestCase {
.putList(GceInstancesServiceImpl.ZONE_SETTING.getKey(), "us-central1-a", "us-central1-b") .putList(GceInstancesServiceImpl.ZONE_SETTING.getKey(), "us-central1-a", "us-central1-b")
.build(); .build();
mock = new GceInstancesServiceMock(nodeSettings); mock = new GceInstancesServiceMock(nodeSettings);
List<DiscoveryNode> discoveryNodes = buildDynamicNodes(mock, nodeSettings); List<TransportAddress> dynamicHosts = buildDynamicNodes(mock, nodeSettings);
assertThat(discoveryNodes, hasSize(0)); assertThat(dynamicHosts, hasSize(0));
} }
public void testIllegalSettingsMissingAllRequired() { public void testIllegalSettingsMissingAllRequired() {
@ -261,7 +258,7 @@ public class GceDiscoveryTests extends ESTestCase {
.putList(GceInstancesServiceImpl.ZONE_SETTING.getKey(), "europe-west1-b", "us-central1-a") .putList(GceInstancesServiceImpl.ZONE_SETTING.getKey(), "europe-west1-b", "us-central1-a")
.build(); .build();
mock = new GceInstancesServiceMock(nodeSettings); mock = new GceInstancesServiceMock(nodeSettings);
List<DiscoveryNode> discoveryNodes = buildDynamicNodes(mock, nodeSettings); List<TransportAddress> dynamicHosts = buildDynamicNodes(mock, nodeSettings);
assertThat(discoveryNodes, hasSize(1)); assertThat(dynamicHosts, hasSize(1));
} }
} }

View File

@ -19,7 +19,7 @@
package org.elasticsearch.discovery.zen; package org.elasticsearch.discovery.zen;
import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.common.transport.TransportAddress;
import java.util.List; import java.util.List;
@ -31,5 +31,5 @@ public interface UnicastHostsProvider {
/** /**
* Builds the dynamic list of unicast hosts to be used for unicast discovery. * Builds the dynamic list of unicast hosts to be used for unicast discovery.
*/ */
List<DiscoveryNode> buildDynamicNodes(); List<TransportAddress> buildDynamicHosts();
} }

View File

@ -118,9 +118,6 @@ public class UnicastZenPing extends AbstractComponent implements ZenPing {
private final AtomicInteger pingingRoundIdGenerator = new AtomicInteger(); private final AtomicInteger pingingRoundIdGenerator = new AtomicInteger();
// used as a node id prefix for configured unicast host nodes/address
private static final String UNICAST_NODE_PREFIX = "#zen_unicast_";
private final Map<Integer, PingingRound> activePingingRounds = newConcurrentMap(); private final Map<Integer, PingingRound> activePingingRounds = newConcurrentMap();
// a list of temporal responses a node will return for a request (holds responses from other nodes) // a list of temporal responses a node will return for a request (holds responses from other nodes)
@ -184,23 +181,20 @@ public class UnicastZenPing extends AbstractComponent implements ZenPing {
* @param hosts the hosts to resolve * @param hosts the hosts to resolve
* @param limitPortCounts the number of ports to resolve (should be 1 for non-local transport) * @param limitPortCounts the number of ports to resolve (should be 1 for non-local transport)
* @param transportService the transport service * @param transportService the transport service
* @param nodeId_prefix a prefix to use for node ids
* @param resolveTimeout the timeout before returning from hostname lookups * @param resolveTimeout the timeout before returning from hostname lookups
* @return a list of discovery nodes with resolved transport addresses * @return a list of resolved transport addresses
*/ */
public static List<DiscoveryNode> resolveHostsLists( public static List<TransportAddress> resolveHostsLists(
final ExecutorService executorService, final ExecutorService executorService,
final Logger logger, final Logger logger,
final List<String> hosts, final List<String> hosts,
final int limitPortCounts, final int limitPortCounts,
final TransportService transportService, final TransportService transportService,
final String nodeId_prefix,
final TimeValue resolveTimeout) throws InterruptedException { final TimeValue resolveTimeout) throws InterruptedException {
Objects.requireNonNull(executorService); Objects.requireNonNull(executorService);
Objects.requireNonNull(logger); Objects.requireNonNull(logger);
Objects.requireNonNull(hosts); Objects.requireNonNull(hosts);
Objects.requireNonNull(transportService); Objects.requireNonNull(transportService);
Objects.requireNonNull(nodeId_prefix);
Objects.requireNonNull(resolveTimeout); Objects.requireNonNull(resolveTimeout);
if (resolveTimeout.nanos() < 0) { if (resolveTimeout.nanos() < 0) {
throw new IllegalArgumentException("resolve timeout must be non-negative but was [" + resolveTimeout + "]"); throw new IllegalArgumentException("resolve timeout must be non-negative but was [" + resolveTimeout + "]");
@ -213,7 +207,7 @@ public class UnicastZenPing extends AbstractComponent implements ZenPing {
.collect(Collectors.toList()); .collect(Collectors.toList());
final List<Future<TransportAddress[]>> futures = final List<Future<TransportAddress[]>> futures =
executorService.invokeAll(callables, resolveTimeout.nanos(), TimeUnit.NANOSECONDS); executorService.invokeAll(callables, resolveTimeout.nanos(), TimeUnit.NANOSECONDS);
final List<DiscoveryNode> discoveryNodes = new ArrayList<>(); final List<TransportAddress> transportAddresses = new ArrayList<>();
final Set<TransportAddress> localAddresses = new HashSet<>(); final Set<TransportAddress> localAddresses = new HashSet<>();
localAddresses.add(transportService.boundAddress().publishAddress()); localAddresses.add(transportService.boundAddress().publishAddress());
localAddresses.addAll(Arrays.asList(transportService.boundAddress().boundAddresses())); localAddresses.addAll(Arrays.asList(transportService.boundAddress().boundAddresses()));
@ -231,13 +225,7 @@ public class UnicastZenPing extends AbstractComponent implements ZenPing {
final TransportAddress address = addresses[addressId]; final TransportAddress address = addresses[addressId];
// no point in pinging ourselves // no point in pinging ourselves
if (localAddresses.contains(address) == false) { if (localAddresses.contains(address) == false) {
discoveryNodes.add( transportAddresses.add(address);
new DiscoveryNode(
nodeId_prefix + hostname + "_" + addressId + "#",
address,
emptyMap(),
emptySet(),
Version.CURRENT.minimumCompatibilityVersion()));
} }
} }
} catch (final ExecutionException e) { } catch (final ExecutionException e) {
@ -249,7 +237,7 @@ public class UnicastZenPing extends AbstractComponent implements ZenPing {
logger.warn("timed out after [{}] resolving host [{}]", resolveTimeout, hostname); logger.warn("timed out after [{}] resolving host [{}]", resolveTimeout, hostname);
} }
} }
return discoveryNodes; return Collections.unmodifiableList(transportAddresses);
} }
@Override @Override
@ -292,29 +280,28 @@ public class UnicastZenPing extends AbstractComponent implements ZenPing {
protected void ping(final Consumer<PingCollection> resultsConsumer, protected void ping(final Consumer<PingCollection> resultsConsumer,
final TimeValue scheduleDuration, final TimeValue scheduleDuration,
final TimeValue requestDuration) { final TimeValue requestDuration) {
final List<DiscoveryNode> seedNodes; final List<TransportAddress> seedAddresses = new ArrayList<>();
try { try {
seedNodes = resolveHostsLists( seedAddresses.addAll(resolveHostsLists(
unicastZenPingExecutorService, unicastZenPingExecutorService,
logger, logger,
configuredHosts, configuredHosts,
limitPortCounts, limitPortCounts,
transportService, transportService,
UNICAST_NODE_PREFIX, resolveTimeout));
resolveTimeout);
} catch (InterruptedException e) { } catch (InterruptedException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
seedNodes.addAll(hostsProvider.buildDynamicNodes()); seedAddresses.addAll(hostsProvider.buildDynamicHosts());
final DiscoveryNodes nodes = contextProvider.clusterState().nodes(); final DiscoveryNodes nodes = contextProvider.clusterState().nodes();
// add all possible master nodes that were active in the last known cluster configuration // add all possible master nodes that were active in the last known cluster configuration
for (ObjectCursor<DiscoveryNode> masterNode : nodes.getMasterNodes().values()) { for (ObjectCursor<DiscoveryNode> masterNode : nodes.getMasterNodes().values()) {
seedNodes.add(masterNode.value); seedAddresses.add(masterNode.value.getAddress());
} }
final ConnectionProfile connectionProfile = final ConnectionProfile connectionProfile =
ConnectionProfile.buildSingleChannelProfile(TransportRequestOptions.Type.REG, requestDuration, requestDuration); ConnectionProfile.buildSingleChannelProfile(TransportRequestOptions.Type.REG, requestDuration, requestDuration);
final PingingRound pingingRound = new PingingRound(pingingRoundIdGenerator.incrementAndGet(), seedNodes, resultsConsumer, final PingingRound pingingRound = new PingingRound(pingingRoundIdGenerator.incrementAndGet(), seedAddresses, resultsConsumer,
nodes.getLocalNode(), connectionProfile); nodes.getLocalNode(), connectionProfile);
activePingingRounds.put(pingingRound.id(), pingingRound); activePingingRounds.put(pingingRound.id(), pingingRound);
final AbstractRunnable pingSender = new AbstractRunnable() { final AbstractRunnable pingSender = new AbstractRunnable() {
@ -356,17 +343,17 @@ public class UnicastZenPing extends AbstractComponent implements ZenPing {
private final Map<TransportAddress, Connection> tempConnections = new HashMap<>(); private final Map<TransportAddress, Connection> tempConnections = new HashMap<>();
private final KeyedLock<TransportAddress> connectionLock = new KeyedLock<>(true); private final KeyedLock<TransportAddress> connectionLock = new KeyedLock<>(true);
private final PingCollection pingCollection; private final PingCollection pingCollection;
private final List<DiscoveryNode> seedNodes; private final List<TransportAddress> seedAddresses;
private final Consumer<PingCollection> pingListener; private final Consumer<PingCollection> pingListener;
private final DiscoveryNode localNode; private final DiscoveryNode localNode;
private final ConnectionProfile connectionProfile; private final ConnectionProfile connectionProfile;
private AtomicBoolean closed = new AtomicBoolean(false); private AtomicBoolean closed = new AtomicBoolean(false);
PingingRound(int id, List<DiscoveryNode> seedNodes, Consumer<PingCollection> resultsConsumer, DiscoveryNode localNode, PingingRound(int id, List<TransportAddress> seedAddresses, Consumer<PingCollection> resultsConsumer, DiscoveryNode localNode,
ConnectionProfile connectionProfile) { ConnectionProfile connectionProfile) {
this.id = id; this.id = id;
this.seedNodes = Collections.unmodifiableList(new ArrayList<>(seedNodes)); this.seedAddresses = Collections.unmodifiableList(seedAddresses.stream().distinct().collect(Collectors.toList()));
this.pingListener = resultsConsumer; this.pingListener = resultsConsumer;
this.localNode = localNode; this.localNode = localNode;
this.connectionProfile = connectionProfile; this.connectionProfile = connectionProfile;
@ -381,9 +368,9 @@ public class UnicastZenPing extends AbstractComponent implements ZenPing {
return this.closed.get(); return this.closed.get();
} }
public List<DiscoveryNode> getSeedNodes() { public List<TransportAddress> getSeedAddresses() {
ensureOpen(); ensureOpen();
return seedNodes; return seedAddresses;
} }
public Connection getOrConnect(DiscoveryNode node) throws IOException { public Connection getOrConnect(DiscoveryNode node) throws IOException {
@ -457,26 +444,28 @@ public class UnicastZenPing extends AbstractComponent implements ZenPing {
final ClusterState lastState = contextProvider.clusterState(); final ClusterState lastState = contextProvider.clusterState();
final UnicastPingRequest pingRequest = new UnicastPingRequest(pingingRound.id(), timeout, createPingResponse(lastState)); final UnicastPingRequest pingRequest = new UnicastPingRequest(pingingRound.id(), timeout, createPingResponse(lastState));
Set<DiscoveryNode> nodesFromResponses = temporalResponses.stream().map(pingResponse -> { List<TransportAddress> temporalAddresses = temporalResponses.stream().map(pingResponse -> {
assert clusterName.equals(pingResponse.clusterName()) : assert clusterName.equals(pingResponse.clusterName()) :
"got a ping request from a different cluster. expected " + clusterName + " got " + pingResponse.clusterName(); "got a ping request from a different cluster. expected " + clusterName + " got " + pingResponse.clusterName();
return pingResponse.node(); return pingResponse.node().getAddress();
}).collect(Collectors.toSet()); }).collect(Collectors.toList());
// dedup by address
final Map<TransportAddress, DiscoveryNode> uniqueNodesByAddress =
Stream.concat(pingingRound.getSeedNodes().stream(), nodesFromResponses.stream())
.collect(Collectors.toMap(DiscoveryNode::getAddress, Function.identity(), (n1, n2) -> n1));
final Stream<TransportAddress> uniqueAddresses = Stream.concat(pingingRound.getSeedAddresses().stream(),
temporalAddresses.stream()).distinct();
// resolve what we can via the latest cluster state // resolve what we can via the latest cluster state
final Set<DiscoveryNode> nodesToPing = uniqueNodesByAddress.values().stream() final Set<DiscoveryNode> nodesToPing = uniqueAddresses
.map(node -> { .map(address -> {
DiscoveryNode foundNode = lastState.nodes().findByAddress(node.getAddress()); DiscoveryNode foundNode = lastState.nodes().findByAddress(address);
if (foundNode == null) { if (foundNode != null && transportService.nodeConnected(foundNode)) {
return node;
} else {
return foundNode; return foundNode;
} else {
return new DiscoveryNode(
address.toString(),
address,
emptyMap(),
emptySet(),
Version.CURRENT.minimumCompatibilityVersion());
} }
}).collect(Collectors.toSet()); }).collect(Collectors.toSet());

View File

@ -84,7 +84,7 @@ public class SingleNodeDiscoveryIT extends ESIntegTestCase {
internalCluster().getInstance(TransportService.class); internalCluster().getInstance(TransportService.class);
// try to ping the single node directly // try to ping the single node directly
final UnicastHostsProvider provider = final UnicastHostsProvider provider =
() -> Collections.singletonList(nodeTransport.getLocalNode()); () -> Collections.singletonList(nodeTransport.getLocalNode().getAddress());
final CountDownLatch latch = new CountDownLatch(1); final CountDownLatch latch = new CountDownLatch(1);
final DiscoveryNodes nodes = DiscoveryNodes.builder() final DiscoveryNodes nodes = DiscoveryNodes.builder()
.add(nodeTransport.getLocalNode()) .add(nodeTransport.getLocalNode())

View File

@ -408,19 +408,18 @@ public class UnicastZenPingTests extends ESTestCase {
Collections.emptySet()); Collections.emptySet());
closeables.push(transportService); closeables.push(transportService);
final int limitPortCounts = randomIntBetween(1, 10); final int limitPortCounts = randomIntBetween(1, 10);
final List<DiscoveryNode> discoveryNodes = TestUnicastZenPing.resolveHostsLists( final List<TransportAddress> transportAddresses = TestUnicastZenPing.resolveHostsLists(
executorService, executorService,
logger, logger,
Collections.singletonList("127.0.0.1"), Collections.singletonList("127.0.0.1"),
limitPortCounts, limitPortCounts,
transportService, transportService,
"test_",
TimeValue.timeValueSeconds(1)); TimeValue.timeValueSeconds(1));
assertThat(discoveryNodes, hasSize(limitPortCounts)); assertThat(transportAddresses, hasSize(limitPortCounts));
final Set<Integer> ports = new HashSet<>(); final Set<Integer> ports = new HashSet<>();
for (final DiscoveryNode discoveryNode : discoveryNodes) { for (final TransportAddress address : transportAddresses) {
assertTrue(discoveryNode.getAddress().address().getAddress().isLoopbackAddress()); assertTrue(address.address().getAddress().isLoopbackAddress());
ports.add(discoveryNode.getAddress().getPort()); ports.add(address.getPort());
} }
assertThat(ports, equalTo(IntStream.range(9300, 9300 + limitPortCounts).mapToObj(m -> m).collect(Collectors.toSet()))); assertThat(ports, equalTo(IntStream.range(9300, 9300 + limitPortCounts).mapToObj(m -> m).collect(Collectors.toSet())));
} }
@ -453,19 +452,18 @@ public class UnicastZenPingTests extends ESTestCase {
new TransportService(Settings.EMPTY, transport, threadPool, TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, new TransportService(Settings.EMPTY, transport, threadPool, TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null,
Collections.emptySet()); Collections.emptySet());
closeables.push(transportService); closeables.push(transportService);
final List<DiscoveryNode> discoveryNodes = TestUnicastZenPing.resolveHostsLists( final List<TransportAddress> transportAddresses = TestUnicastZenPing.resolveHostsLists(
executorService, executorService,
logger, logger,
Collections.singletonList(NetworkAddress.format(loopbackAddress)), Collections.singletonList(NetworkAddress.format(loopbackAddress)),
10, 10,
transportService, transportService,
"test_",
TimeValue.timeValueSeconds(1)); TimeValue.timeValueSeconds(1));
assertThat(discoveryNodes, hasSize(7)); assertThat(transportAddresses, hasSize(7));
final Set<Integer> ports = new HashSet<>(); final Set<Integer> ports = new HashSet<>();
for (final DiscoveryNode discoveryNode : discoveryNodes) { for (final TransportAddress address : transportAddresses) {
assertTrue(discoveryNode.getAddress().address().getAddress().isLoopbackAddress()); assertTrue(address.address().getAddress().isLoopbackAddress());
ports.add(discoveryNode.getAddress().getPort()); ports.add(address.getPort());
} }
assertThat(ports, equalTo(IntStream.range(9303, 9310).mapToObj(m -> m).collect(Collectors.toSet()))); assertThat(ports, equalTo(IntStream.range(9303, 9310).mapToObj(m -> m).collect(Collectors.toSet())));
} }
@ -505,17 +503,16 @@ public class UnicastZenPingTests extends ESTestCase {
Collections.emptySet()); Collections.emptySet());
closeables.push(transportService); closeables.push(transportService);
final List<DiscoveryNode> discoveryNodes = TestUnicastZenPing.resolveHostsLists( final List<TransportAddress> transportAddresses = TestUnicastZenPing.resolveHostsLists(
executorService, executorService,
logger, logger,
Arrays.asList(hostname), Arrays.asList(hostname),
1, 1,
transportService, transportService,
"test_",
TimeValue.timeValueSeconds(1) TimeValue.timeValueSeconds(1)
); );
assertThat(discoveryNodes, empty()); assertThat(transportAddresses, empty());
verify(logger).warn("failed to resolve host [" + hostname + "]", unknownHostException); verify(logger).warn("failed to resolve host [" + hostname + "]", unknownHostException);
} }
@ -565,16 +562,15 @@ public class UnicastZenPingTests extends ESTestCase {
closeables.push(transportService); closeables.push(transportService);
final TimeValue resolveTimeout = TimeValue.timeValueSeconds(randomIntBetween(1, 3)); final TimeValue resolveTimeout = TimeValue.timeValueSeconds(randomIntBetween(1, 3));
try { try {
final List<DiscoveryNode> discoveryNodes = TestUnicastZenPing.resolveHostsLists( final List<TransportAddress> transportAddresses = TestUnicastZenPing.resolveHostsLists(
executorService, executorService,
logger, logger,
Arrays.asList("hostname1", "hostname2"), Arrays.asList("hostname1", "hostname2"),
1, 1,
transportService, transportService,
"test+",
resolveTimeout); resolveTimeout);
assertThat(discoveryNodes, hasSize(1)); assertThat(transportAddresses, hasSize(1));
verify(logger).trace( verify(logger).trace(
"resolved host [{}] to {}", "hostname1", "resolved host [{}] to {}", "hostname1",
new TransportAddress[]{new TransportAddress(TransportAddress.META_ADDRESS, 9300)}); new TransportAddress[]{new TransportAddress(TransportAddress.META_ADDRESS, 9300)});
@ -732,17 +728,16 @@ public class UnicastZenPingTests extends ESTestCase {
new TransportService(Settings.EMPTY, transport, threadPool, TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, new TransportService(Settings.EMPTY, transport, threadPool, TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null,
Collections.emptySet()); Collections.emptySet());
closeables.push(transportService); closeables.push(transportService);
final List<DiscoveryNode> discoveryNodes = TestUnicastZenPing.resolveHostsLists( final List<TransportAddress> transportAddresses = TestUnicastZenPing.resolveHostsLists(
executorService, executorService,
logger, logger,
Arrays.asList("127.0.0.1:9300:9300", "127.0.0.1:9301"), Arrays.asList("127.0.0.1:9300:9300", "127.0.0.1:9301"),
1, 1,
transportService, transportService,
"test_",
TimeValue.timeValueSeconds(1)); TimeValue.timeValueSeconds(1));
assertThat(discoveryNodes, hasSize(1)); // only one of the two is valid and will be used assertThat(transportAddresses, hasSize(1)); // only one of the two is valid and will be used
assertThat(discoveryNodes.get(0).getAddress().getAddress(), equalTo("127.0.0.1")); assertThat(transportAddresses.get(0).getAddress(), equalTo("127.0.0.1"));
assertThat(discoveryNodes.get(0).getAddress().getPort(), equalTo(9301)); assertThat(transportAddresses.get(0).getPort(), equalTo(9301));
verify(logger).warn(eq("failed to resolve host [127.0.0.1:9300:9300]"), Matchers.any(ExecutionException.class)); verify(logger).warn(eq("failed to resolve host [127.0.0.1:9300:9300]"), Matchers.any(ExecutionException.class));
} }

View File

@ -21,6 +21,7 @@ package org.elasticsearch.test.discovery;
import org.elasticsearch.cluster.ClusterName; import org.elasticsearch.cluster.ClusterName;
import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.util.concurrent.ConcurrentCollections; import org.elasticsearch.common.util.concurrent.ConcurrentCollections;
import org.elasticsearch.discovery.zen.UnicastHostsProvider; import org.elasticsearch.discovery.zen.UnicastHostsProvider;
@ -55,7 +56,7 @@ public final class MockUncasedHostProvider implements UnicastHostsProvider, Clos
} }
@Override @Override
public List<DiscoveryNode> buildDynamicNodes() { public List<TransportAddress> buildDynamicHosts() {
final DiscoveryNode localNode = getNode(); final DiscoveryNode localNode = getNode();
assert localNode != null; assert localNode != null;
synchronized (activeNodesPerCluster) { synchronized (activeNodesPerCluster) {
@ -64,6 +65,7 @@ public final class MockUncasedHostProvider implements UnicastHostsProvider, Clos
.map(MockUncasedHostProvider::getNode) .map(MockUncasedHostProvider::getNode)
.filter(Objects::nonNull) .filter(Objects::nonNull)
.filter(n -> localNode.equals(n) == false) .filter(n -> localNode.equals(n) == false)
.map(DiscoveryNode::getAddress)
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
} }