-
Notifications
You must be signed in to change notification settings - Fork 25.7k
[Stable plugin API] Load plugin named components #89969
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2a2179b
Scanning for Stable Plugin components
pgomulka f59692a
small cleanup
pgomulka c508977
remove example use
pgomulka 515071b
Update docs/changelog/89969.yaml
pgomulka 58726df
fix javadoc
pgomulka 5927de1
Merge branch 'plugin_scanning_just_files' of github.com:pgomulka/elas…
pgomulka e722af2
code review followup
pgomulka fadaf34
path to resources
pgomulka 6534f34
move file
pgomulka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| pr: 89969 | ||
| summary: "[Stable plugin API] Load plugin named components" | ||
| area: Infra/Plugins | ||
| type: enhancement | ||
| issues: [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
server/src/main/java/org/elasticsearch/plugins/scanners/ExtensibleFileReader.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
| * in compliance with, at your election, the Elastic License 2.0 or the Server | ||
| * Side Public License, v 1. | ||
| */ | ||
|
|
||
| package org.elasticsearch.plugins.scanners; | ||
|
|
||
| import org.elasticsearch.logging.LogManager; | ||
| import org.elasticsearch.logging.Logger; | ||
| import org.elasticsearch.xcontent.XContentParser; | ||
| import org.elasticsearch.xcontent.XContentParserConfiguration; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| import static org.elasticsearch.xcontent.XContentType.JSON; | ||
|
|
||
| public class ExtensibleFileReader { | ||
| private static final Logger logger = LogManager.getLogger(ExtensibleFileReader.class); | ||
|
|
||
| private String extensibleFile; | ||
|
|
||
| public ExtensibleFileReader(String extensibleFile) { | ||
| this.extensibleFile = extensibleFile; | ||
| } | ||
|
|
||
| public Map<String, String> readFromFile() { | ||
| Map<String, String> res = new HashMap<>(); | ||
| // todo should it be BufferedInputStream ? | ||
| try (InputStream in = getClass().getClassLoader().getResourceAsStream(extensibleFile)) { | ||
| if (in != null) { | ||
| try (XContentParser parser = JSON.xContent().createParser(XContentParserConfiguration.EMPTY, in)) { | ||
| // TODO should we validate the classes actually exist? | ||
| return parser.mapStrings(); | ||
| } | ||
| } | ||
| } catch (IOException e) { | ||
| logger.error("failed reading extensible file", e); | ||
| } | ||
| return res; | ||
| } | ||
|
|
||
| } |
86 changes: 86 additions & 0 deletions
86
server/src/main/java/org/elasticsearch/plugins/scanners/ExtensiblesRegistry.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
| * in compliance with, at your election, the Elastic License 2.0 or the Server | ||
| * Side Public License, v 1. | ||
| */ | ||
|
|
||
| package org.elasticsearch.plugins.scanners; | ||
|
|
||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| import static org.elasticsearch.core.Strings.format; | ||
|
|
||
| /** | ||
| * A registry of Extensible interfaces/classes read from extensibles.json file. | ||
| * The file is generated during Elasticsearch built time (or commited) | ||
| * basing on the classes declared in stable plugins api (i.e. plugin-analysis-api) | ||
| * | ||
| * This file is present in server jar. | ||
| * a class/interface is directly extensible when is marked with @Extensible annotation | ||
| * a class/interface can be indirectly extensible when it extends/implements a directly extensible class | ||
| * | ||
| * Information about extensible interfaces/classes are stored in a map where: | ||
| * key and value are the same cannonical name of the class that is directly marked with @Extensible | ||
| * or | ||
| * key: a cannonical name of the class that is indirectly extensible but extends another extensible class (directly/indirectly) | ||
| * value: cannonical name of the class that is directly extensible | ||
| * | ||
| * The reason for indirectly extensible classes is to allow stable plugin apis to create hierarchies | ||
| * | ||
| * Example: | ||
| * <pre> | ||
| * @Extensible | ||
| * interface E{ | ||
| * public void foo(); | ||
| * } | ||
| * interface Eprim extends E{ | ||
| * } | ||
| * | ||
| * class Aclass implements E{ | ||
| * | ||
| * } | ||
| * | ||
| * @Extensible | ||
| * class E2 { | ||
| * public void bar(){} | ||
| * } | ||
| * </pre> | ||
| * the content of extensibles.json should be | ||
| * { | ||
| * "E" : "E", | ||
| * "Eprim" : "E", | ||
| * "A" : "E", | ||
| * "E2" : "E2" | ||
| * } | ||
| * | ||
| * @see org.elasticsearch.plugin.api.Extensible | ||
| */ | ||
| public class ExtensiblesRegistry { | ||
|
|
||
| private static final Logger logger = LogManager.getLogger(ExtensiblesRegistry.class); | ||
|
|
||
| private static final String EXTENSIBLES_FILE = "org/elasticsearch/plugins/scanner/extensibles.json"; | ||
| public static final ExtensiblesRegistry INSTANCE = new ExtensiblesRegistry(EXTENSIBLES_FILE); | ||
|
|
||
| // classname (potentially extending/implementing extensible) to interface/class annotated with extensible | ||
| private final Map<String, String> loadedExtensible; | ||
|
|
||
| ExtensiblesRegistry(String extensiblesFile) { | ||
| ExtensibleFileReader extensibleFileReader = new ExtensibleFileReader(extensiblesFile); | ||
|
|
||
| this.loadedExtensible = extensibleFileReader.readFromFile(); | ||
| if (loadedExtensible.size() > 0) { | ||
| logger.debug(() -> format("Loaded extensible from cache file %s", loadedExtensible)); | ||
| } | ||
| } | ||
|
|
||
| public boolean hasExtensible(String extensibleName) { | ||
| return loadedExtensible.containsKey(extensibleName); | ||
| } | ||
|
|
||
| } |
38 changes: 38 additions & 0 deletions
38
server/src/main/java/org/elasticsearch/plugins/scanners/NameToPluginInfo.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
| * in compliance with, at your election, the Elastic License 2.0 or the Server | ||
| * Side Public License, v 1. | ||
| */ | ||
|
|
||
| package org.elasticsearch.plugins.scanners; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| public record NameToPluginInfo(Map<String, PluginInfo> nameToPluginInfoMap) { | ||
|
|
||
| public NameToPluginInfo() { | ||
| this(new HashMap<>()); | ||
| } | ||
|
|
||
| public NameToPluginInfo put(String name, PluginInfo pluginInfo) { | ||
| nameToPluginInfoMap.put(name, pluginInfo); | ||
| return this; | ||
| } | ||
|
|
||
| public void putAll(Map<String, PluginInfo> namedPluginInfoMap) { | ||
| this.nameToPluginInfoMap.putAll(namedPluginInfoMap); | ||
| } | ||
|
|
||
| public NameToPluginInfo put(NameToPluginInfo nameToPluginInfo) { | ||
| putAll(nameToPluginInfo.nameToPluginInfoMap); | ||
| return this; | ||
| } | ||
|
|
||
| public PluginInfo getForPluginName(String pluginName) { | ||
| return nameToPluginInfoMap.get(pluginName); | ||
| } | ||
|
|
||
| } |
111 changes: 111 additions & 0 deletions
111
server/src/main/java/org/elasticsearch/plugins/scanners/NamedComponentReader.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
| * in compliance with, at your election, the Elastic License 2.0 or the Server | ||
| * Side Public License, v 1. | ||
| */ | ||
|
|
||
| package org.elasticsearch.plugins.scanners; | ||
|
|
||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
| import org.elasticsearch.core.Strings; | ||
| import org.elasticsearch.plugins.PluginBundle; | ||
| import org.elasticsearch.xcontent.XContentParserConfiguration; | ||
|
|
||
| import java.io.BufferedInputStream; | ||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.stream.Stream; | ||
|
|
||
| import static java.util.Collections.emptyMap; | ||
| import static org.elasticsearch.xcontent.XContentType.JSON; | ||
|
|
||
| /** | ||
| * Reads named components declared by a plugin in a cache file. | ||
| * Cache file is expected to be present in plugin's lib directory | ||
| * <p> | ||
| * The content of a cache file is a JSON representation of a map where: | ||
| * keys -> name of the extensible interface (a class/interface marked with @Extensible) | ||
| * values -> a map of name to implementation class name | ||
| */ | ||
| public class NamedComponentReader { | ||
|
|
||
| private Logger logger = LogManager.getLogger(NamedComponentReader.class); | ||
| private static final String NAMED_COMPONENTS_FILE_NAME = "named_components.json"; | ||
| /** | ||
| * a registry of known classes marked or indirectly marked (extending marked class) with @Extensible | ||
| */ | ||
| private final ExtensiblesRegistry extensiblesRegistry; | ||
|
|
||
| public NamedComponentReader() { | ||
| this(ExtensiblesRegistry.INSTANCE); | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's chain this constructor, so we only have one substantive constructor body, e.g. |
||
|
|
||
| NamedComponentReader(ExtensiblesRegistry extensiblesRegistry) { | ||
| this.extensiblesRegistry = extensiblesRegistry; | ||
| } | ||
|
|
||
| public Map<String, NameToPluginInfo> findNamedComponents(PluginBundle bundle, ClassLoader pluginClassLoader) { | ||
| Path pluginDir = bundle.getDir(); | ||
| return findNamedComponents(pluginDir, pluginClassLoader); | ||
| } | ||
|
|
||
| // scope for testing | ||
| Map<String, NameToPluginInfo> findNamedComponents(Path pluginDir, ClassLoader pluginClassLoader) { | ||
| try { | ||
| Path namedComponent = findNamedComponentCacheFile(pluginDir); | ||
| if (namedComponent != null) { | ||
| Map<String, NameToPluginInfo> namedComponents = readFromFile(namedComponent, pluginClassLoader); | ||
| logger.debug(() -> Strings.format("Plugin in dir %s declared named components %s.", pluginDir, namedComponents)); | ||
|
|
||
| return namedComponents; | ||
| } | ||
| logger.debug(() -> Strings.format("No named component defined in plugin dir %s", pluginDir)); | ||
| } catch (IOException e) { | ||
| logger.error("unable to read named components", e); | ||
| } | ||
| return emptyMap(); | ||
| } | ||
|
|
||
| private Path findNamedComponentCacheFile(Path pluginDir) throws IOException { | ||
| try (Stream<Path> list = Files.list(pluginDir)) { | ||
| return list.filter(p -> p.getFileName().toString().equals(NAMED_COMPONENTS_FILE_NAME)).findFirst().orElse(null); | ||
| } | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| Map<String, NameToPluginInfo> readFromFile(Path namedComponent, ClassLoader pluginClassLoader) throws IOException { | ||
| Map<String, NameToPluginInfo> res = new HashMap<>(); | ||
|
|
||
| try ( | ||
| var json = new BufferedInputStream(Files.newInputStream(namedComponent)); | ||
| var parser = JSON.xContent().createParser(XContentParserConfiguration.EMPTY, json) | ||
| ) { | ||
| Map<String, Object> map = parser.map(); | ||
| for (Map.Entry<String, Object> fileAsMap : map.entrySet()) { | ||
| String extensibleInterface = fileAsMap.getKey(); | ||
| validateExtensible(extensibleInterface); | ||
| Map<String, Object> components = (Map<String, Object>) fileAsMap.getValue(); | ||
| for (Map.Entry<String, Object> nameToComponent : components.entrySet()) { | ||
| String name = nameToComponent.getKey(); | ||
| String value = (String) nameToComponent.getValue(); | ||
|
|
||
| res.computeIfAbsent(extensibleInterface, k -> new NameToPluginInfo()) | ||
| .put(name, new PluginInfo(name, value, pluginClassLoader)); | ||
| } | ||
| } | ||
| } | ||
| return res; | ||
| } | ||
|
|
||
| private void validateExtensible(String extensibleInterface) { | ||
| if (extensiblesRegistry.hasExtensible(extensibleInterface) == false) { | ||
| throw new IllegalStateException("Unknown extensible name " + extensibleInterface); | ||
| } | ||
| } | ||
| } | ||
13 changes: 13 additions & 0 deletions
13
server/src/main/java/org/elasticsearch/plugins/scanners/PluginInfo.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
| * in compliance with, at your election, the Elastic License 2.0 or the Server | ||
| * Side Public License, v 1. | ||
| */ | ||
|
|
||
| package org.elasticsearch.plugins.scanners; | ||
|
|
||
| record PluginInfo(String name, String className, ClassLoader loader) { | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this field used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
at this scope of the PR it is not. We do not perform the scanning and we also do not register known extensibles (it will be its primary use). In NamedComponentReader it could be used for validating the read file. will add some simple validation