Skip to content

Switch role pluggability to use SPI #54370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions plugins/examples/discovery-node-role-extension/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.
*/

apply plugin: 'elasticsearch.testclusters'
apply plugin: 'elasticsearch.esplugin'

esplugin {
name 'discovery-node-role-extension'
description 'an example DiscoveryNodeRoleExtension'
classname 'org.elasticsearch.example.discovery_node_role_extension.AdditionalRolePlugin'
licenseFile rootProject.file('licenses/APACHE-LICENSE-2.0.txt')
noticeFile rootProject.file('NOTICE.txt')
}

testClusters.integTest {
setting 'node.additional', 'true'
testDistribution = 'OSS'
}

test.enabled = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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.example.discovery_node_role_extension;

import org.elasticsearch.cluster.node.DiscoveryNodeRole;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.plugins.Plugin;

import java.util.List;
import java.util.Set;

public class AdditionalRolePlugin extends Plugin {

static final Setting<Boolean> NODE_ADDITIONAL_SETTING = Setting.boolSetting("node.additional", false, Setting.Property.NodeScope);

@Override
public List<Setting<?>> getSettings() {
return List.of(NODE_ADDITIONAL_SETTING);
}

public static DiscoveryNodeRole ADDITIONAL_ROLE = new DiscoveryNodeRole("additional", "a") {

@Override
protected Setting<Boolean> roleSetting() {
return NODE_ADDITIONAL_SETTING;
}

};

public static class DiscoveryNodeRoleExtension implements org.elasticsearch.cluster.node.DiscoveryNodeRoleExtension {

@Override
public Set<DiscoveryNodeRole> roles() {
return Set.of(ADDITIONAL_ROLE);
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.elasticsearch.example.discovery_node_role_extension.AdditionalRolePlugin$DiscoveryNodeRoleExtension
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.examples.discovery_node_role_extension;

import com.carrotsearch.randomizedtesting.annotations.Name;
import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;
import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate;
import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase;

public class DiscoveryNodeRoleExtensionClientYamlTestSuiteIT extends ESClientYamlSuiteTestCase {

public DiscoveryNodeRoleExtensionClientYamlTestSuiteIT(@Name("yaml") final ClientYamlTestCandidate testCandidate) {
super(testCandidate);
}

@ParametersFactory
public static Iterable<Object[]> parameters() throws Exception {
return ESClientYamlSuiteTestCase.createParameters();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"additional role":
- skip:
reason: "contains"
features: contains

- do:
cluster.state: {}

# get master node id
- set: { master_node: master }

- do:
nodes.info: {}

- match: { nodes.$master.roles.0: "additional" }
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.SortedSet;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;


/**
Expand Down Expand Up @@ -456,20 +458,35 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws

private static Map<String, DiscoveryNodeRole> roleNameToPossibleRoles;

public static void setPossibleRoles(final Set<DiscoveryNodeRole> possibleRoles) {
public static Set<String> getPossibleRoleNames() {
return roleNameToPossibleRoles.keySet();
}

static {
setPossibleRoles(DiscoveryNodeRole.BUILT_IN_ROLES);
}

static void setPossibleRoles(final Set<DiscoveryNodeRole> possibleRoles) {
final Map<String, DiscoveryNodeRole> roleNameToPossibleRoles =
possibleRoles.stream().collect(Collectors.toUnmodifiableMap(DiscoveryNodeRole::roleName, Function.identity()));
possibleRoles.stream().collect(Collectors.toUnmodifiableMap(DiscoveryNodeRole::roleName, Function.identity()));
// collect the abbreviation names into a map to ensure that there are not any duplicate abbreviations
final Map<String, DiscoveryNodeRole> roleNameAbbreviationToPossibleRoles = roleNameToPossibleRoles.values()
.stream()
.collect(Collectors.toUnmodifiableMap(DiscoveryNodeRole::roleNameAbbreviation, Function.identity()));
.stream()
.collect(Collectors.toUnmodifiableMap(DiscoveryNodeRole::roleNameAbbreviation, Function.identity()));
assert roleNameToPossibleRoles.size() == roleNameAbbreviationToPossibleRoles.size() :
"roles by name [" + roleNameToPossibleRoles + "], roles by name abbreviation [" + roleNameAbbreviationToPossibleRoles + "]";
"roles by name [" + roleNameToPossibleRoles + "], roles by name abbreviation [" + roleNameAbbreviationToPossibleRoles + "]";
DiscoveryNode.roleNameToPossibleRoles = roleNameToPossibleRoles;
}

public static Set<String> getPossibleRoleNames() {
return roleNameToPossibleRoles.keySet();
public static void reloadSPI(final ClassLoader loader) {
for (final DiscoveryNodeRoleExtension extension : ServiceLoader.load(DiscoveryNodeRoleExtension.class, loader)) {
final Set<DiscoveryNodeRole> possibleRoles = Stream.concat(
roleNameToPossibleRoles.values().stream(),
extension.roles().stream())
.collect(Collectors.toUnmodifiableSet());
setPossibleRoles(possibleRoles);
}

}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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.cluster.node;

import java.util.Set;

public interface DiscoveryNodeRoleExtension {

Set<DiscoveryNodeRole> roles();

}
9 changes: 0 additions & 9 deletions server/src/main/java/org/elasticsearch/node/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
import org.elasticsearch.cluster.metadata.MetaDataIndexUpgradeService;
import org.elasticsearch.cluster.metadata.TemplateUpgradeService;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.node.DiscoveryNodeRole;
import org.elasticsearch.cluster.routing.BatchedRerouteService;
import org.elasticsearch.cluster.routing.RerouteService;
import org.elasticsearch.cluster.routing.allocation.DiskThresholdMonitor;
Expand Down Expand Up @@ -320,14 +319,6 @@ protected Node(final Environment initialEnvironment,
this.pluginsService = new PluginsService(tmpSettings, initialEnvironment.configFile(), initialEnvironment.modulesFile(),
initialEnvironment.pluginsFile(), classpathPlugins);
final Settings settings = pluginsService.updatedSettings();
final Set<DiscoveryNodeRole> possibleRoles = Stream.concat(
DiscoveryNodeRole.BUILT_IN_ROLES.stream(),
pluginsService.filterPlugins(Plugin.class)
.stream()
.map(Plugin::getRoles)
.flatMap(Set::stream))
.collect(Collectors.toSet());
DiscoveryNode.setPossibleRoles(possibleRoles);
localNodeFactory = new LocalNodeFactory(settings, nodeEnvironment.nodeId());

// create the environment based on the finalized (processed) view of the settings
Expand Down
6 changes: 0 additions & 6 deletions server/src/main/java/org/elasticsearch/plugins/Plugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
import org.elasticsearch.cluster.metadata.IndexTemplateMetaData;
import org.elasticsearch.cluster.node.DiscoveryNodeRole;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.component.LifecycleComponent;
import org.elasticsearch.common.io.stream.NamedWriteable;
Expand All @@ -47,7 +46,6 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.UnaryOperator;

/**
Expand Down Expand Up @@ -179,10 +177,6 @@ public List<ExecutorBuilder<?>> getExecutorBuilders(Settings settings) {
*/
public List<BootstrapCheck> getBootstrapChecks() { return Collections.emptyList(); }

public Set<DiscoveryNodeRole> getRoles() {
return Set.of();
}

/**
* Close the resources opened by this plugin.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.elasticsearch.Version;
import org.elasticsearch.action.admin.cluster.node.info.PluginsAndModules;
import org.elasticsearch.bootstrap.JarHell;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.io.FileSystemUtils;
Expand Down Expand Up @@ -511,6 +512,7 @@ private Plugin loadBundle(Bundle bundle, Map<String, Plugin> loaded) {
// note: already asserted above that extended plugins are loaded and extensible
ExtensiblePlugin.class.cast(loaded.get(extendedPluginName)).reloadSPI(loader);
}
DiscoveryNode.reloadSPI(loader);

Class<? extends Plugin> pluginClass = loadPluginClass(bundle.plugin.getClassname(), loader);
Plugin plugin = loadPlugin(pluginClass, settings, configPath);
Expand Down
Loading