Skip to content
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

Enable testing for ExtensiblePlugins using classpath plugins #16908

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
44154b5
Make extended plugins optional
cwperks Oct 15, 2024
9501854
Make extended plugins optional
cwperks Oct 15, 2024
9b3f406
Merge branch 'main' into optional-extended-plugins
cwperks Dec 13, 2024
44a4042
Load extensions for classpath plugins
cwperks Dec 14, 2024
7b4d7d7
Ensure only single instance for each classpath extension
cwperks Dec 14, 2024
1614706
Add test for classpath plugin extended plugin loading
cwperks Dec 22, 2024
4176c46
Enable testing for ExtensiblePlugins using classpath plugins
cwperks Dec 23, 2024
ef041b0
Merge branch 'main' into extend-classpath-plugins
cwperks Dec 24, 2024
09d1c82
Merge branch 'main' into extend-classpath-plugins
cwperks Dec 31, 2024
f840850
Add to CHANGELOG
cwperks Dec 31, 2024
69ceaab
Merge branch 'main' into extend-classpath-plugins
cwperks Jan 3, 2025
0fbe489
Merge branch 'main' into extend-classpath-plugins
cwperks Jan 6, 2025
7a07307
Merge branch 'extend-classpath-plugins' of https://github.com/cwperks…
cwperks Jan 6, 2025
7ddd9a3
Define PluginInfos from test files for classpath plugins
cwperks Jan 8, 2025
07df814
Merge branch 'main' into extend-classpath-plugins
cwperks Jan 21, 2025
df48c29
Add testing constructor for PluginsService
cwperks Jan 21, 2025
a4937bd
Single call to loadExtensions
cwperks Jan 21, 2025
49ec944
Merge branch 'main' into extend-classpath-plugins
cwperks Jan 22, 2025
4cf427c
Reduce number of MockNode constructors
cwperks Jan 22, 2025
f9bc2ae
Create 2 public constructors for MockNode
cwperks Jan 22, 2025
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Support searching from doc_value using termQueryCaseInsensitive/termQuery in flat_object/keyword field([#16974](https://github.com/opensearch-project/OpenSearch/pull/16974/))
- Added a new `time` field to replace the deprecated `getTime` field in `GetStats`. ([#17009](https://github.com/opensearch-project/OpenSearch/pull/17009))
- Improve performance of the bitmap filtering([#16936](https://github.com/opensearch-project/OpenSearch/pull/16936/))
- Enable testing for ExtensiblePlugins using classpath plugins ([#16908](https://github.com/opensearch-project/OpenSearch/pull/16908))

### Dependencies
- Bump `com.google.cloud:google-cloud-core-http` from 2.23.0 to 2.47.0 ([#16504](https://github.com/opensearch-project/OpenSearch/pull/16504))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.plugins;

import org.opensearch.test.OpenSearchIntegTestCase;

import java.io.IOException;
import java.util.Collection;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.hamcrest.Matchers.equalTo;

@OpenSearchIntegTestCase.ClusterScope(scope = OpenSearchIntegTestCase.Scope.TEST, numDataNodes = 0)
public class ClasspathPluginIT extends OpenSearchIntegTestCase {

public interface SampleExtension {}

public static class SampleExtensiblePlugin extends Plugin implements ExtensiblePlugin {
public SampleExtensiblePlugin() {}

@Override
public void loadExtensions(ExtensiblePlugin.ExtensionLoader loader) {
int nLoaded = 0;
for (SampleExtension e : loader.loadExtensions(SampleExtension.class)) {
nLoaded++;
}

assertThat(nLoaded, equalTo(1));
}
}

public static class SampleExtendingPlugin extends Plugin implements SampleExtension {
public SampleExtendingPlugin() {}
};

@Override
protected Collection<Class<? extends Plugin>> nodePlugins() {
return Stream.concat(super.nodePlugins().stream(), Stream.of(SampleExtensiblePlugin.class, SampleExtendingPlugin.class))
.collect(Collectors.toList());
}

@Override
protected Map<Class<? extends Plugin>, Class<? extends Plugin>> extendedPlugins() {
return Map.of(SampleExtendingPlugin.class, SampleExtensiblePlugin.class);
}

public void testPluginExtensionWithClasspathPlugins() throws IOException {
internalCluster().startNode();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#
# SPDX-License-Identifier: Apache-2.0
#
# The OpenSearch Contributors require contributions made to
# this file be licensed under the Apache-2.0 license or a
# compatible open source license.

org.opensearch.plugins.ClasspathPluginIT$SampleExtendingPlugin
7 changes: 2 additions & 5 deletions server/src/main/java/org/opensearch/node/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@
import org.opensearch.plugins.NetworkPlugin;
import org.opensearch.plugins.PersistentTaskPlugin;
import org.opensearch.plugins.Plugin;
import org.opensearch.plugins.PluginInfo;
import org.opensearch.plugins.PluginsService;
import org.opensearch.plugins.RepositoryPlugin;
import org.opensearch.plugins.ScriptPlugin;
Expand Down Expand Up @@ -460,11 +461,7 @@ public Node(Environment environment) {
* @param forbidPrivateIndexSettings whether or not private index settings are forbidden when creating an index; this is used in the
* test framework for tests that rely on being able to set private settings
*/
protected Node(
final Environment initialEnvironment,
Collection<Class<? extends Plugin>> classpathPlugins,
boolean forbidPrivateIndexSettings
) {
protected Node(final Environment initialEnvironment, Collection<PluginInfo> classpathPlugins, boolean forbidPrivateIndexSettings) {
final List<Closeable> resourcesToClose = new ArrayList<>(); // register everything we need to release in the case of an error
boolean success = false;
try {
Expand Down
72 changes: 52 additions & 20 deletions server/src/main/java/org/opensearch/plugins/PluginsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,48 @@
*/
public PluginsService(
Settings settings,
Path configPath,
Path modulesDirectory,
Path pluginsDirectory,
Collection<Class<? extends Plugin>> classpathPlugins
) {
reta marked this conversation as resolved.
Show resolved Hide resolved
// Used for testing
this(
settings,
null,
modulesDirectory,
pluginsDirectory,
classpathPlugins.stream()
.map(
p -> new PluginInfo(
p.getName(),
"classpath plugin",
"NA",
Version.CURRENT,
"1.8",
p.getName(),
null,
Collections.emptyList(),
false
)
)
.collect(Collectors.toList())
);
}

/**
* Constructs a new PluginService
* @param settings The settings of the system
* @param modulesDirectory The directory modules exist in, or null if modules should not be loaded from the filesystem
* @param pluginsDirectory The directory plugins exist in, or null if plugins should not be loaded from the filesystem
* @param classpathPlugins Plugins that exist in the classpath which should be loaded
*/
@SuppressWarnings("unchecked")
public PluginsService(
reta marked this conversation as resolved.
Show resolved Hide resolved
Settings settings,
Path configPath,
Path modulesDirectory,
Path pluginsDirectory,
Collection<PluginInfo> classpathPlugins
) {
this.settings = settings;
this.configPath = configPath;
Expand All @@ -140,25 +178,19 @@
// we need to build a List of plugins for checking mandatory plugins
final List<String> pluginsNames = new ArrayList<>();
// first we load plugins that are on the classpath. this is for tests
for (Class<? extends Plugin> pluginClass : classpathPlugins) {
Plugin plugin = loadPlugin(pluginClass, settings, configPath);
PluginInfo pluginInfo = new PluginInfo(
pluginClass.getName(),
"classpath plugin",
"NA",
Version.CURRENT,
"1.8",
pluginClass.getName(),
null,
Collections.emptyList(),
false
);
if (logger.isTraceEnabled()) {
logger.trace("plugin loaded from classpath [{}]", pluginInfo);
for (PluginInfo pluginInfo : classpathPlugins) {
try {
Class<? extends Plugin> pluginClazz = (Class<? extends Plugin>) Class.forName(pluginInfo.getClassname());
Plugin plugin = loadPlugin(pluginClazz, settings, configPath);
if (logger.isTraceEnabled()) {
logger.trace("plugin loaded from classpath [{}]", pluginInfo);
}
pluginsLoaded.add(new Tuple<>(pluginInfo, plugin));
pluginsList.add(pluginInfo);
pluginsNames.add(pluginInfo.getName());
} catch (ClassNotFoundException e) {
logger.error("Failed to load classpath plugin: " + pluginInfo.getClassname());

Check warning on line 192 in server/src/main/java/org/opensearch/plugins/PluginsService.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/plugins/PluginsService.java#L191-L192

Added lines #L191 - L192 were not covered by tests
}
pluginsLoaded.add(new Tuple<>(pluginInfo, plugin));
pluginsList.add(pluginInfo);
pluginsNames.add(pluginInfo.getName());
}

Set<Bundle> seenBundles = new LinkedHashSet<>();
Expand Down Expand Up @@ -196,6 +228,7 @@

List<Tuple<PluginInfo, Plugin>> loaded = loadBundles(seenBundles);
pluginsLoaded.addAll(loaded);
loadExtensions(pluginsLoaded);

this.info = new PluginsAndModules(pluginsList, modulesList);
this.plugins = Collections.unmodifiableList(pluginsLoaded);
Expand Down Expand Up @@ -552,7 +585,6 @@
plugins.add(new Tuple<>(bundle.plugin, plugin));
}

loadExtensions(plugins);
return Collections.unmodifiableList(plugins);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,7 @@ private Node startNode() throws NodeValidationException {

Node node = new MockNode(
settings,
Arrays.asList(MockNioTransportPlugin.class, MockHttpTransport.TestPlugin.class, InternalSettingsPlugin.class),
true
Arrays.asList(MockNioTransportPlugin.class, MockHttpTransport.TestPlugin.class, InternalSettingsPlugin.class)
);
node.start();
return node;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,7 @@ public Settings additionalSettings() {
public static class FilterablePlugin extends Plugin implements ScriptPlugin {}

static PluginsService newPluginsService(Settings settings, Class<? extends Plugin>... classpathPlugins) {
return new PluginsService(
settings,
null,
null,
TestEnvironment.newEnvironment(settings).pluginsDir(),
Arrays.asList(classpathPlugins)
);
return new PluginsService(settings, null, TestEnvironment.newEnvironment(settings).pluginsDir(), Arrays.asList(classpathPlugins));
}

public void testAdditionalSettings() {
Expand Down
49 changes: 31 additions & 18 deletions test/framework/src/main/java/org/opensearch/node/MockNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

package org.opensearch.node;

import org.opensearch.Version;
import org.opensearch.client.node.NodeClient;
import org.opensearch.cluster.ClusterInfoService;
import org.opensearch.cluster.MockInternalClusterInfoService;
Expand All @@ -51,6 +52,7 @@
import org.opensearch.http.HttpServerTransport;
import org.opensearch.indices.IndicesService;
import org.opensearch.plugins.Plugin;
import org.opensearch.plugins.PluginInfo;
import org.opensearch.script.MockScriptService;
import org.opensearch.script.ScriptContext;
import org.opensearch.script.ScriptEngine;
Expand All @@ -76,6 +78,7 @@
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* A node for testing which allows:
Expand All @@ -86,23 +89,20 @@
*/
public class MockNode extends Node {

private final Collection<Class<? extends Plugin>> classpathPlugins;
private final Collection<PluginInfo> classpathPlugins;

public MockNode(final Settings settings, final Collection<Class<? extends Plugin>> classpathPlugins) {
this(settings, classpathPlugins, true);
}

public MockNode(
final Settings settings,
final Collection<Class<? extends Plugin>> classpathPlugins,
private MockNode(
final Environment environment,
final Collection<PluginInfo> classpathPlugins,
final boolean forbidPrivateIndexSettings
) {
this(settings, classpathPlugins, null, forbidPrivateIndexSettings);
super(environment, classpathPlugins, forbidPrivateIndexSettings);
this.classpathPlugins = classpathPlugins;
}

public MockNode(
final Settings settings,
final Collection<Class<? extends Plugin>> classpathPlugins,
final Collection<PluginInfo> classpathPlugins,
final Path configPath,
final boolean forbidPrivateIndexSettings
) {
Expand All @@ -113,19 +113,32 @@ public MockNode(
);
}

private MockNode(
final Environment environment,
final Collection<Class<? extends Plugin>> classpathPlugins,
final boolean forbidPrivateIndexSettings
) {
super(environment, classpathPlugins, forbidPrivateIndexSettings);
this.classpathPlugins = classpathPlugins;
public MockNode(final Settings settings, final Collection<Class<? extends Plugin>> classpathPlugins) {
this(
InternalSettingsPreparer.prepareEnvironment(settings, Collections.emptyMap(), null, () -> "mock_ node"),
classpathPlugins.stream()
.map(
p -> new PluginInfo(
p.getName(),
"classpath plugin",
"NA",
Version.CURRENT,
"1.8",
p.getName(),
null,
Collections.emptyList(),
false
)
)
.collect(Collectors.toList()),
true
);
}

/**
* The classpath plugins this node was constructed with.
*/
public Collection<Class<? extends Plugin>> getClasspathPlugins() {
public Collection<PluginInfo> getClasspathPlugins() {
return classpathPlugins;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ private static class ServiceHolder implements Closeable {
throw new AssertionError("node.name must be set");
});
PluginsService pluginsService;
pluginsService = new PluginsService(nodeSettings, null, env.modulesDir(), env.pluginsDir(), plugins);
pluginsService = new PluginsService(nodeSettings, env.modulesDir(), env.pluginsDir(), plugins);

client = (Client) Proxy.newProxyInstance(Client.class.getClassLoader(), new Class[] { Client.class }, clientInvocationHandler);
ScriptModule scriptModule = createScriptModule(pluginsService.filterPlugins(ScriptPlugin.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.OpenSearchException;
import org.opensearch.Version;
import org.opensearch.action.admin.cluster.node.info.NodeInfo;
import org.opensearch.action.admin.cluster.node.info.NodesInfoResponse;
import org.opensearch.action.admin.cluster.node.stats.NodeStats;
Expand All @@ -50,6 +51,7 @@
import org.opensearch.http.HttpInfo;
import org.opensearch.node.MockNode;
import org.opensearch.plugins.Plugin;
import org.opensearch.plugins.PluginInfo;
import org.opensearch.transport.nio.MockNioTransportPlugin;

import java.io.IOException;
Expand All @@ -59,6 +61,8 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -98,6 +102,7 @@ public ExternalTestCluster(
Function<Client, Client> clientWrapper,
String clusterName,
Collection<Class<? extends Plugin>> pluginClasses,
Map<Class<? extends Plugin>, Class<? extends Plugin>> extendedPlugins,
TransportAddress... transportAddresses
) {
super(0);
Expand Down Expand Up @@ -129,7 +134,28 @@ public ExternalTestCluster(
pluginClasses = new ArrayList<>(pluginClasses);
pluginClasses.add(MockHttpTransport.TestPlugin.class);
Settings clientSettings = clientSettingsBuilder.build();
MockNode node = new MockNode(clientSettings, pluginClasses);
MockNode node = new MockNode(
clientSettings,
pluginClasses.stream()
.map(
p -> new PluginInfo(
p.getName(),
"classpath plugin",
"NA",
Version.CURRENT,
"1.8",
p.getName(),
null,
(extendedPlugins != null && extendedPlugins.containsKey(p))
? List.of(extendedPlugins.get(p).getName())
: Collections.emptyList(),
false
)
)
.collect(Collectors.toList()),
null,
true
);
Client client = clientWrapper.apply(node.client());
try {
node.start();
Expand Down
Loading
Loading