Move custom name resolver registration to the NetworkModule
As explained in https://github.com/elastic/elasticsearch/pull/15765#discussion_r65804713
This commit is contained in:
parent
11ec3a4af6
commit
2a9d7f68a1
|
@ -45,6 +45,9 @@ import org.elasticsearch.transport.Transport;
|
|||
import org.elasticsearch.transport.TransportService;
|
||||
import org.elasticsearch.transport.local.LocalTransport;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
/**
|
||||
* A module to handle registering and binding all network related classes.
|
||||
*/
|
||||
|
@ -143,8 +146,18 @@ public class NetworkModule extends AbstractModule {
|
|||
return allocationCommandRegistry;
|
||||
}
|
||||
|
||||
private final List<NetworkService.CustomNameResolver> customNameResolvers = new CopyOnWriteArrayList<>();
|
||||
|
||||
/**
|
||||
* Add a custom name resolver.
|
||||
*/
|
||||
public void addCustomNameResolver(NetworkService.CustomNameResolver customNameResolver) {
|
||||
customNameResolvers.add(customNameResolver);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configure() {
|
||||
networkService.setCustomNameResolvers(customNameResolvers);
|
||||
bind(NetworkService.class).toInstance(networkService);
|
||||
bind(NamedWriteableRegistry.class).toInstance(namedWriteableRegistry);
|
||||
transportServiceTypes.bindType(binder(), settings, TRANSPORT_SERVICE_TYPE_KEY, "default");
|
||||
|
|
|
@ -90,18 +90,15 @@ public class NetworkService extends AbstractComponent {
|
|||
InetAddress[] resolveIfPossible(String value) throws IOException;
|
||||
}
|
||||
|
||||
private final List<CustomNameResolver> customNameResolvers = new CopyOnWriteArrayList<>();
|
||||
private List<CustomNameResolver> customNameResolvers;
|
||||
|
||||
public NetworkService(Settings settings) {
|
||||
super(settings);
|
||||
IfConfig.logIfNecessary();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a custom name resolver.
|
||||
*/
|
||||
public void addCustomNameResolver(CustomNameResolver customNameResolver) {
|
||||
customNameResolvers.add(customNameResolver);
|
||||
public void setCustomNameResolvers(List<CustomNameResolver> customNameResolvers) {
|
||||
this.customNameResolvers = customNameResolvers;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -120,11 +117,13 @@ public class NetworkService extends AbstractComponent {
|
|||
// if we have settings use them (we have a fallback to GLOBAL_NETWORK_HOST_SETTING inline
|
||||
bindHosts = GLOBAL_NETWORK_BINDHOST_SETTING.get(settings).toArray(Strings.EMPTY_ARRAY);
|
||||
} else {
|
||||
// next check any registered custom resolvers
|
||||
for (CustomNameResolver customNameResolver : customNameResolvers) {
|
||||
InetAddress addresses[] = customNameResolver.resolveDefault();
|
||||
if (addresses != null) {
|
||||
return addresses;
|
||||
// next check any registered custom resolvers if any
|
||||
if (customNameResolvers != null) {
|
||||
for (CustomNameResolver customNameResolver : customNameResolvers) {
|
||||
InetAddress addresses[] = customNameResolver.resolveDefault();
|
||||
if (addresses != null) {
|
||||
return addresses;
|
||||
}
|
||||
}
|
||||
}
|
||||
// we know it's not here. get the defaults
|
||||
|
@ -166,11 +165,13 @@ public class NetworkService extends AbstractComponent {
|
|||
// if we have settings use them (we have a fallback to GLOBAL_NETWORK_HOST_SETTING inline
|
||||
publishHosts = GLOBAL_NETWORK_PUBLISHHOST_SETTING.get(settings).toArray(Strings.EMPTY_ARRAY);
|
||||
} else {
|
||||
// next check any registered custom resolvers
|
||||
for (CustomNameResolver customNameResolver : customNameResolvers) {
|
||||
InetAddress addresses[] = customNameResolver.resolveDefault();
|
||||
if (addresses != null) {
|
||||
return addresses[0];
|
||||
// next check any registered custom resolvers if any
|
||||
if (customNameResolvers != null) {
|
||||
for (CustomNameResolver customNameResolver : customNameResolvers) {
|
||||
InetAddress addresses[] = customNameResolver.resolveDefault();
|
||||
if (addresses != null) {
|
||||
return addresses[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
// we know it's not here. get the defaults
|
||||
|
@ -229,11 +230,13 @@ public class NetworkService extends AbstractComponent {
|
|||
private InetAddress[] resolveInternal(String host) throws IOException {
|
||||
if ((host.startsWith("#") && host.endsWith("#")) || (host.startsWith("_") && host.endsWith("_"))) {
|
||||
host = host.substring(1, host.length() - 1);
|
||||
// allow custom resolvers to have special names
|
||||
for (CustomNameResolver customNameResolver : customNameResolvers) {
|
||||
InetAddress addresses[] = customNameResolver.resolveIfPossible(host);
|
||||
if (addresses != null) {
|
||||
return addresses;
|
||||
// next check any registered custom resolvers if any
|
||||
if (customNameResolvers != null) {
|
||||
for (CustomNameResolver customNameResolver : customNameResolvers) {
|
||||
InetAddress addresses[] = customNameResolver.resolveIfPossible(host);
|
||||
if (addresses != null) {
|
||||
return addresses;
|
||||
}
|
||||
}
|
||||
}
|
||||
switch (host) {
|
||||
|
|
|
@ -54,10 +54,8 @@ public class AwsEc2ServiceImpl extends AbstractLifecycleComponent implements Aws
|
|||
private AmazonEC2Client client;
|
||||
|
||||
@Inject
|
||||
public AwsEc2ServiceImpl(Settings settings, NetworkService networkService) {
|
||||
public AwsEc2ServiceImpl(Settings settings) {
|
||||
super(settings);
|
||||
// add specific ec2 name resolver
|
||||
networkService.addCustomNameResolver(new Ec2NameResolver(settings));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -38,10 +38,12 @@ import org.elasticsearch.SpecialPermission;
|
|||
import org.elasticsearch.cloud.aws.AwsEc2Service;
|
||||
import org.elasticsearch.cloud.aws.AwsEc2ServiceImpl;
|
||||
import org.elasticsearch.cloud.aws.Ec2Module;
|
||||
import org.elasticsearch.cloud.aws.network.Ec2NameResolver;
|
||||
import org.elasticsearch.common.component.LifecycleComponent;
|
||||
import org.elasticsearch.common.inject.Module;
|
||||
import org.elasticsearch.common.logging.ESLogger;
|
||||
import org.elasticsearch.common.logging.Loggers;
|
||||
import org.elasticsearch.common.network.NetworkModule;
|
||||
import org.elasticsearch.common.settings.Setting;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.discovery.DiscoveryModule;
|
||||
|
@ -105,6 +107,11 @@ public class Ec2DiscoveryPlugin extends Plugin {
|
|||
discoveryModule.addUnicastHostProvider(EC2, AwsEc2UnicastHostsProvider.class);
|
||||
}
|
||||
|
||||
public void onModule(NetworkModule networkModule) {
|
||||
logger.debug("Register _ec2_, _ec2:xxx_ network names");
|
||||
networkModule.addCustomNameResolver(new Ec2NameResolver(settings));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Setting<?>> getSettings() {
|
||||
return Arrays.asList(
|
||||
|
|
|
@ -26,6 +26,7 @@ import org.elasticsearch.test.ESTestCase;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.util.Collections;
|
||||
|
||||
import static org.hamcrest.Matchers.arrayContaining;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
|
@ -43,7 +44,7 @@ public class Ec2NetworkTests extends ESTestCase {
|
|||
.build();
|
||||
|
||||
NetworkService networkService = new NetworkService(nodeSettings);
|
||||
networkService.addCustomNameResolver(new Ec2NameResolver(nodeSettings));
|
||||
networkService.setCustomNameResolvers(Collections.singletonList(new Ec2NameResolver(nodeSettings)));
|
||||
// TODO we need to replace that with a mock. For now we check the URL we are supposed to reach.
|
||||
try {
|
||||
networkService.resolveBindHostAddresses(null);
|
||||
|
@ -61,7 +62,7 @@ public class Ec2NetworkTests extends ESTestCase {
|
|||
.build();
|
||||
|
||||
NetworkService networkService = new NetworkService(nodeSettings);
|
||||
networkService.addCustomNameResolver(new Ec2NameResolver(nodeSettings));
|
||||
networkService.setCustomNameResolvers(Collections.singletonList(new Ec2NameResolver(nodeSettings)));
|
||||
// TODO we need to replace that with a mock. For now we check the URL we are supposed to reach.
|
||||
try {
|
||||
networkService.resolveBindHostAddresses(null);
|
||||
|
@ -79,7 +80,7 @@ public class Ec2NetworkTests extends ESTestCase {
|
|||
.build();
|
||||
|
||||
NetworkService networkService = new NetworkService(nodeSettings);
|
||||
networkService.addCustomNameResolver(new Ec2NameResolver(nodeSettings));
|
||||
networkService.setCustomNameResolvers(Collections.singletonList(new Ec2NameResolver(nodeSettings)));
|
||||
// TODO we need to replace that with a mock. For now we check the URL we are supposed to reach.
|
||||
try {
|
||||
networkService.resolveBindHostAddresses(null);
|
||||
|
@ -97,7 +98,7 @@ public class Ec2NetworkTests extends ESTestCase {
|
|||
.build();
|
||||
|
||||
NetworkService networkService = new NetworkService(nodeSettings);
|
||||
networkService.addCustomNameResolver(new Ec2NameResolver(nodeSettings));
|
||||
networkService.setCustomNameResolvers(Collections.singletonList(new Ec2NameResolver(nodeSettings)));
|
||||
// TODO we need to replace that with a mock. For now we check the URL we are supposed to reach.
|
||||
try {
|
||||
networkService.resolveBindHostAddresses(null);
|
||||
|
@ -115,7 +116,7 @@ public class Ec2NetworkTests extends ESTestCase {
|
|||
.build();
|
||||
|
||||
NetworkService networkService = new NetworkService(nodeSettings);
|
||||
networkService.addCustomNameResolver(new Ec2NameResolver(nodeSettings));
|
||||
networkService.setCustomNameResolvers(Collections.singletonList(new Ec2NameResolver(nodeSettings)));
|
||||
// TODO we need to replace that with a mock. For now we check the URL we are supposed to reach.
|
||||
try {
|
||||
networkService.resolveBindHostAddresses(null);
|
||||
|
@ -133,7 +134,7 @@ public class Ec2NetworkTests extends ESTestCase {
|
|||
.build();
|
||||
|
||||
NetworkService networkService = new NetworkService(nodeSettings);
|
||||
networkService.addCustomNameResolver(new Ec2NameResolver(nodeSettings));
|
||||
networkService.setCustomNameResolvers(Collections.singletonList(new Ec2NameResolver(nodeSettings)));
|
||||
// TODO we need to replace that with a mock. For now we check the URL we are supposed to reach.
|
||||
try {
|
||||
networkService.resolveBindHostAddresses(null);
|
||||
|
@ -151,7 +152,7 @@ public class Ec2NetworkTests extends ESTestCase {
|
|||
.build();
|
||||
|
||||
NetworkService networkService = new NetworkService(nodeSettings);
|
||||
networkService.addCustomNameResolver(new Ec2NameResolver(nodeSettings));
|
||||
networkService.setCustomNameResolvers(Collections.singletonList(new Ec2NameResolver(nodeSettings)));
|
||||
// TODO we need to replace that with a mock. For now we check the URL we are supposed to reach.
|
||||
try {
|
||||
networkService.resolveBindHostAddresses(null);
|
||||
|
@ -170,7 +171,7 @@ public class Ec2NetworkTests extends ESTestCase {
|
|||
.build();
|
||||
|
||||
NetworkService networkService = new NetworkService(nodeSettings);
|
||||
networkService.addCustomNameResolver(new Ec2NameResolver(nodeSettings));
|
||||
networkService.setCustomNameResolvers(Collections.singletonList(new Ec2NameResolver(nodeSettings)));
|
||||
InetAddress[] addresses = networkService.resolveBindHostAddresses(null);
|
||||
assertThat(addresses, arrayContaining(networkService.resolveBindHostAddresses(new String[] { "_local_" })));
|
||||
}
|
||||
|
|
|
@ -54,9 +54,8 @@ public class GceMetadataServiceImpl extends AbstractLifecycleComponent {
|
|||
private HttpTransport gceHttpTransport;
|
||||
|
||||
@Inject
|
||||
public GceMetadataServiceImpl(Settings settings, NetworkService networkService) {
|
||||
public GceMetadataServiceImpl(Settings settings) {
|
||||
super(settings);
|
||||
networkService.addCustomNameResolver(new GceNameResolver(settings, this));
|
||||
}
|
||||
|
||||
protected synchronized HttpTransport getGceHttpTransport() throws GeneralSecurityException, IOException {
|
||||
|
|
|
@ -27,7 +27,6 @@ import org.elasticsearch.common.settings.Settings;
|
|||
public class GceModule extends AbstractModule {
|
||||
// pkg private so tests can override with mock
|
||||
static Class<? extends GceInstancesService> computeServiceImpl = GceInstancesServiceImpl.class;
|
||||
static Class<? extends GceMetadataServiceImpl> metadataServiceImpl = GceMetadataServiceImpl.class;
|
||||
|
||||
protected final Settings settings;
|
||||
protected final ESLogger logger = Loggers.getLogger(GceModule.class);
|
||||
|
@ -40,14 +39,9 @@ public class GceModule extends AbstractModule {
|
|||
return computeServiceImpl;
|
||||
}
|
||||
|
||||
public static Class<? extends GceMetadataServiceImpl> getMetadataServiceImpl() {
|
||||
return metadataServiceImpl;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configure() {
|
||||
logger.debug("configure GceModule (bind compute and metadata services)");
|
||||
logger.debug("configure GceModule (bind compute service)");
|
||||
bind(GceInstancesService.class).to(computeServiceImpl).asEagerSingleton();
|
||||
bind(GceMetadataServiceImpl.class).asEagerSingleton();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,11 +23,14 @@ import com.google.api.client.http.HttpHeaders;
|
|||
import com.google.api.client.util.ClassInfo;
|
||||
import org.elasticsearch.SpecialPermission;
|
||||
import org.elasticsearch.cloud.gce.GceInstancesService;
|
||||
import org.elasticsearch.cloud.gce.GceMetadataServiceImpl;
|
||||
import org.elasticsearch.cloud.gce.GceModule;
|
||||
import org.elasticsearch.cloud.gce.network.GceNameResolver;
|
||||
import org.elasticsearch.common.component.LifecycleComponent;
|
||||
import org.elasticsearch.common.inject.Module;
|
||||
import org.elasticsearch.common.logging.ESLogger;
|
||||
import org.elasticsearch.common.logging.Loggers;
|
||||
import org.elasticsearch.common.network.NetworkModule;
|
||||
import org.elasticsearch.common.settings.Setting;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.settings.SettingsModule;
|
||||
|
@ -85,10 +88,9 @@ public class GceDiscoveryPlugin extends Plugin {
|
|||
@Override
|
||||
@SuppressWarnings("rawtypes") // Supertype uses raw type
|
||||
public Collection<Class<? extends LifecycleComponent>> getGuiceServiceClasses() {
|
||||
logger.debug("Register gce compute and metadata services");
|
||||
logger.debug("Register gce compute service");
|
||||
Collection<Class<? extends LifecycleComponent>> services = new ArrayList<>();
|
||||
services.add(GceModule.getComputeServiceImpl());
|
||||
services.add(GceModule.getMetadataServiceImpl());
|
||||
return services;
|
||||
}
|
||||
|
||||
|
@ -98,6 +100,11 @@ public class GceDiscoveryPlugin extends Plugin {
|
|||
discoveryModule.addUnicastHostProvider(GCE, GceUnicastHostsProvider.class);
|
||||
}
|
||||
|
||||
public void onModule(NetworkModule networkModule) {
|
||||
logger.debug("Register _gce_, _gce:xxx network names");
|
||||
networkModule.addCustomNameResolver(new GceNameResolver(settings, new GceMetadataServiceImpl(settings)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Setting<?>> getSettings() {
|
||||
return Arrays.asList(
|
||||
|
|
|
@ -34,8 +34,8 @@ public class GceMetadataServiceMock extends GceMetadataServiceImpl {
|
|||
|
||||
protected HttpTransport mockHttpTransport;
|
||||
|
||||
public GceMetadataServiceMock(Settings settings, NetworkService networkService) {
|
||||
super(settings, networkService);
|
||||
public GceMetadataServiceMock(Settings settings) {
|
||||
super(settings);
|
||||
this.mockHttpTransport = GceMockUtils.configureMock();
|
||||
}
|
||||
|
||||
|
|
|
@ -26,6 +26,8 @@ import org.elasticsearch.test.ESTestCase;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.hamcrest.Matchers.arrayContaining;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
|
@ -104,8 +106,8 @@ public class GceNetworkTests extends ESTestCase {
|
|||
.build();
|
||||
|
||||
NetworkService networkService = new NetworkService(nodeSettings);
|
||||
GceMetadataServiceMock mock = new GceMetadataServiceMock(nodeSettings, networkService);
|
||||
networkService.addCustomNameResolver(new GceNameResolver(nodeSettings, mock));
|
||||
GceMetadataServiceMock mock = new GceMetadataServiceMock(nodeSettings);
|
||||
networkService.setCustomNameResolvers(Collections.singletonList(new GceNameResolver(nodeSettings, mock)));
|
||||
try {
|
||||
InetAddress[] addresses = networkService.resolveBindHostAddresses(null);
|
||||
if (expected == null) {
|
||||
|
|
Loading…
Reference in New Issue