From 314f0749aeb26c1937be56f51eb9ba7101168fd6 Mon Sep 17 00:00:00 2001 From: David Pilato Date: Sat, 26 Sep 2015 11:13:50 +0200 Subject: [PATCH] discovery-ec2 plugin should check `discovery.type` As done in #13809 and in Azure, we should check that `discovery.type` is set to `ec2` before starting services. Closes #13581. --- .../elasticsearch/cloud/aws/Ec2Module.java | 17 +++++++ .../discovery/ec2/Ec2Discovery.java | 4 +- .../discovery/ec2/Ec2DiscoveryPlugin.java | 16 ++++++- .../ec2/Ec2DiscoverySettingsTests.java | 44 +++++++++++++++++++ 4 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 plugins/discovery-ec2/src/test/java/org/elasticsearch/discovery/ec2/Ec2DiscoverySettingsTests.java diff --git a/plugins/discovery-ec2/src/main/java/org/elasticsearch/cloud/aws/Ec2Module.java b/plugins/discovery-ec2/src/main/java/org/elasticsearch/cloud/aws/Ec2Module.java index 4029e1b6326..09a0116fc6d 100644 --- a/plugins/discovery-ec2/src/main/java/org/elasticsearch/cloud/aws/Ec2Module.java +++ b/plugins/discovery-ec2/src/main/java/org/elasticsearch/cloud/aws/Ec2Module.java @@ -20,6 +20,9 @@ package org.elasticsearch.cloud.aws; import org.elasticsearch.common.inject.AbstractModule; +import org.elasticsearch.common.logging.ESLogger; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.discovery.ec2.Ec2Discovery; public class Ec2Module extends AbstractModule { @@ -27,4 +30,18 @@ public class Ec2Module extends AbstractModule { protected void configure() { bind(AwsEc2Service.class).to(AwsEc2ServiceImpl.class).asEagerSingleton(); } + + /** + * Check if discovery is meant to start + * @return true if we can start discovery features + */ + public static boolean isEc2DiscoveryActive(Settings settings, ESLogger logger) { + // User set discovery.type: ec2 + if (!Ec2Discovery.EC2.equalsIgnoreCase(settings.get("discovery.type"))) { + logger.trace("discovery.type not set to {}", Ec2Discovery.EC2); + return false; + } + + return true; + } } diff --git a/plugins/discovery-ec2/src/main/java/org/elasticsearch/discovery/ec2/Ec2Discovery.java b/plugins/discovery-ec2/src/main/java/org/elasticsearch/discovery/ec2/Ec2Discovery.java index b599541604c..e94b7618d12 100755 --- a/plugins/discovery-ec2/src/main/java/org/elasticsearch/discovery/ec2/Ec2Discovery.java +++ b/plugins/discovery-ec2/src/main/java/org/elasticsearch/discovery/ec2/Ec2Discovery.java @@ -21,8 +21,6 @@ package org.elasticsearch.discovery.ec2; import org.elasticsearch.cluster.ClusterName; import org.elasticsearch.cluster.ClusterService; -import org.elasticsearch.cluster.settings.ClusterDynamicSettings; -import org.elasticsearch.cluster.settings.DynamicSettings; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.discovery.DiscoverySettings; @@ -38,6 +36,8 @@ import org.elasticsearch.transport.TransportService; */ public class Ec2Discovery extends ZenDiscovery { + public static final String EC2 = "ec2"; + @Inject public Ec2Discovery(Settings settings, ClusterName clusterName, ThreadPool threadPool, TransportService transportService, ClusterService clusterService, NodeSettingsService nodeSettingsService, ZenPingService pingService, diff --git a/plugins/discovery-ec2/src/main/java/org/elasticsearch/plugin/discovery/ec2/Ec2DiscoveryPlugin.java b/plugins/discovery-ec2/src/main/java/org/elasticsearch/plugin/discovery/ec2/Ec2DiscoveryPlugin.java index 36a854489c4..4cf876677a2 100644 --- a/plugins/discovery-ec2/src/main/java/org/elasticsearch/plugin/discovery/ec2/Ec2DiscoveryPlugin.java +++ b/plugins/discovery-ec2/src/main/java/org/elasticsearch/plugin/discovery/ec2/Ec2DiscoveryPlugin.java @@ -23,6 +23,9 @@ import org.elasticsearch.cloud.aws.AwsEc2ServiceImpl; import org.elasticsearch.cloud.aws.Ec2Module; 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.settings.Settings; import org.elasticsearch.discovery.DiscoveryModule; import org.elasticsearch.discovery.ec2.AwsEc2UnicastHostsProvider; import org.elasticsearch.discovery.ec2.Ec2Discovery; @@ -55,6 +58,13 @@ public class Ec2DiscoveryPlugin extends Plugin { }); } + private final Settings settings; + protected final ESLogger logger = Loggers.getLogger(Ec2DiscoveryPlugin.class); + + public Ec2DiscoveryPlugin(Settings settings) { + this.settings = settings; + } + @Override public String name() { return "discovery-ec2"; @@ -80,7 +90,9 @@ public class Ec2DiscoveryPlugin extends Plugin { } public void onModule(DiscoveryModule discoveryModule) { - discoveryModule.addDiscoveryType("ec2", Ec2Discovery.class); - discoveryModule.addUnicastHostProvider(AwsEc2UnicastHostsProvider.class); + if (Ec2Module.isEc2DiscoveryActive(settings, logger)) { + discoveryModule.addDiscoveryType("ec2", Ec2Discovery.class); + discoveryModule.addUnicastHostProvider(AwsEc2UnicastHostsProvider.class); + } } } diff --git a/plugins/discovery-ec2/src/test/java/org/elasticsearch/discovery/ec2/Ec2DiscoverySettingsTests.java b/plugins/discovery-ec2/src/test/java/org/elasticsearch/discovery/ec2/Ec2DiscoverySettingsTests.java new file mode 100644 index 00000000000..f0dfe960c80 --- /dev/null +++ b/plugins/discovery-ec2/src/test/java/org/elasticsearch/discovery/ec2/Ec2DiscoverySettingsTests.java @@ -0,0 +1,44 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.discovery.ec2; + +import org.elasticsearch.cloud.aws.Ec2Module; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.test.ESTestCase; + +import static org.hamcrest.Matchers.is; + +public class Ec2DiscoverySettingsTests extends ESTestCase { + + public void testDiscoveryReady() { + Settings settings = Settings.builder() + .put("discovery.type", "ec2") + .build(); + boolean discoveryReady = Ec2Module.isEc2DiscoveryActive(settings, logger); + assertThat(discoveryReady, is(true)); + } + + public void testDiscoveryNotReady() { + Settings settings = Settings.EMPTY; + boolean discoveryReady = Ec2Module.isEc2DiscoveryActive(settings, logger); + assertThat(discoveryReady, is(false)); + } + +}