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

Consolidate all Plugin.createComponents arguments into a single accessor class #101305

Merged
merged 3 commits into from
Oct 26, 2023
Merged
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
54 changes: 36 additions & 18 deletions server/src/main/java/org/elasticsearch/node/NodeConstruction.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.elasticsearch.cluster.coordination.StableMasterHealthIndicatorService;
import org.elasticsearch.cluster.desirednodes.DesiredNodesSettingsValidator;
import org.elasticsearch.cluster.metadata.IndexMetadataVerifier;
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
import org.elasticsearch.cluster.metadata.IndexTemplateMetadata;
import org.elasticsearch.cluster.metadata.MetadataCreateDataStreamService;
import org.elasticsearch.cluster.metadata.MetadataCreateIndexService;
Expand All @@ -52,6 +53,7 @@
import org.elasticsearch.cluster.node.DiscoveryNodeRole;
import org.elasticsearch.cluster.routing.BatchedRerouteService;
import org.elasticsearch.cluster.routing.RerouteService;
import org.elasticsearch.cluster.routing.allocation.AllocationService;
import org.elasticsearch.cluster.routing.allocation.DiskThresholdMonitor;
import org.elasticsearch.cluster.routing.allocation.ShardsAvailabilityHealthIndicatorService;
import org.elasticsearch.cluster.routing.allocation.WriteLoadForecaster;
Expand Down Expand Up @@ -739,24 +741,40 @@ private void construct(Environment initialEnvironment, NodeServiceProvider servi
threadPool
);

Collection<Object> pluginComponents = pluginsService.flatMap(
p -> p.createComponents(
client,
clusterService,
threadPool,
resourceWatcherService,
scriptService,
xContentRegistry,
environment,
nodeEnvironment,
namedWriteableRegistry,
clusterModule.getIndexNameExpressionResolver(),
repositoriesServiceReference::get,
telemetryProvider,
clusterModule.getAllocationService(),
indicesService
)
).toList();
record PluginServiceInstances(
Client client,
ClusterService clusterService,
ThreadPool threadPool,
ResourceWatcherService resourceWatcherService,
ScriptService scriptService,
NamedXContentRegistry xContentRegistry,
Environment environment,
NodeEnvironment nodeEnvironment,
NamedWriteableRegistry namedWriteableRegistry,
IndexNameExpressionResolver indexNameExpressionResolver,
Supplier<RepositoriesService> repositoriesServiceSupplier,
TelemetryProvider telemetryProvider,
AllocationService allocationService,
IndicesService indicesService
) implements Plugin.PluginServices {}
PluginServiceInstances pluginServices = new PluginServiceInstances(
client,
clusterService,
threadPool,
resourceWatcherService,
scriptService,
xContentRegistry,
environment,
nodeEnvironment,
namedWriteableRegistry,
clusterModule.getIndexNameExpressionResolver(),
repositoriesServiceReference::get,
telemetryProvider,
clusterModule.getAllocationService(),
indicesService
);

Collection<?> pluginComponents = pluginsService.flatMap(p -> p.createComponents(pluginServices)).toList();

List<ReservedClusterStateHandler<?>> reservedStateHandlers = new ArrayList<>();

Expand Down
117 changes: 113 additions & 4 deletions server/src/main/java/org/elasticsearch/plugins/Plugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,29 +62,138 @@
*/
public abstract class Plugin implements Closeable {

/**
* Provides access to various Elasticsearch services.
*/
public interface PluginServices {
/**
* A client to make requests to the system
*/
Client client();

/**
* A service to allow watching and updating cluster state
*/
ClusterService clusterService();

/**
* A service to allow retrieving an executor to run an async action
*/
ThreadPool threadPool();

/**
* A service to watch for changes to node local files
*/
ResourceWatcherService resourceWatcherService();

/**
* A service to allow running scripts on the local node
*/
ScriptService scriptService();

/**
* The registry for extensible xContent parsing
*/
NamedXContentRegistry xContentRegistry();

/**
* The environment for path and setting configurations
*/
Environment environment();

/**
* The node environment used coordinate access to the data paths
*/
NodeEnvironment nodeEnvironment();

/**
* The registry for {@link NamedWriteable} object parsing
*/
NamedWriteableRegistry namedWriteableRegistry();

/**
* A service that resolves expression to index and alias names
*/
IndexNameExpressionResolver indexNameExpressionResolver();

/**
* A supplier for the service that manages snapshot repositories.
* This will return null when {@link #createComponents(PluginServices)} is called,
* but will return the repositories service once the node is initialized.
*/
Supplier<RepositoriesService> repositoriesServiceSupplier();

/**
* An interface for distributed tracing
*/
TelemetryProvider telemetryProvider();

/**
* A service to manage shard allocation in the cluster
*/
AllocationService allocationService();

/**
* A service to manage indices in the cluster
*/
IndicesService indicesService();
}

/**
* Returns components added by this plugin.
* <p>
* Any components returned that implement {@link LifecycleComponent} will have their lifecycle managed.
* Note: To aid in the migration away from guice, all objects returned as components will be bound in guice
* to themselves.
*
* @param services Provides access to various Elasticsearch services
*/
public Collection<?> createComponents(PluginServices services) {
return createComponents(
services.client(),
services.clusterService(),
services.threadPool(),
services.resourceWatcherService(),
services.scriptService(),
services.xContentRegistry(),
services.environment(),
services.nodeEnvironment(),
services.namedWriteableRegistry(),
services.indexNameExpressionResolver(),
services.repositoriesServiceSupplier(),
services.telemetryProvider(),
services.allocationService(),
services.indicesService()
);
}

/**
* Returns components added by this plugin. Either this method or {@link #createComponents(PluginServices)}
* should be implemented.
* <p>
* Any components returned that implement {@link LifecycleComponent} will have their lifecycle managed.
* Note: To aid in the migration away from guice, all objects returned as components will be bound in guice
* to themselves.
*
* @param client A client to make requests to the system
* @param clusterService A service to allow watching and updating cluster state
* @param threadPool A service to allow retrieving an executor to run an async action
* @param resourceWatcherService A service to watch for changes to node local files
* @param scriptService A service to allow running scripts on the local node
* @param xContentRegistry the registry for extensible xContent parsing
* @param environment the environment for path and setting configurations
* @param nodeEnvironment the node environment used coordinate access to the data paths
* @param namedWriteableRegistry the registry for {@link NamedWriteable} object parsing
* @param xContentRegistry The registry for extensible xContent parsing
* @param environment The environment for path and setting configurations
* @param nodeEnvironment The node environment used coordinate access to the data paths
* @param namedWriteableRegistry The registry for {@link NamedWriteable} object parsing
* @param indexNameExpressionResolver A service that resolves expression to index and alias names
* @param repositoriesServiceSupplier A supplier for the service that manages snapshot repositories; will return null when this method
* is called, but will return the repositories service once the node is initialized.
* @param telemetryProvider An interface for distributed tracing
* @param allocationService A service to manage shard allocation in the cluster
* @param indicesService A service to manage indices in the cluster
*
* @deprecated New services will only be added to {@link PluginServices}; this method is maintained for compatibility.
*/
@Deprecated
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can just be left around for compatibility, but any new services would be added to PluginServices only

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to do this, or at least we can remove this once serverless is updated. There are no compatibility guarantees for non-stable plugins.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll migrate all the existing implementations as a separate PR

public Collection<Object> createComponents(
Client client,
ClusterService clusterService,
Expand Down