瀏覽代碼

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

Simon Cooper 1 年之前
父節點
當前提交
1fe9aa9467

+ 36 - 18
server/src/main/java/org/elasticsearch/node/NodeConstruction.java

@@ -40,6 +40,7 @@ import org.elasticsearch.cluster.coordination.Reconfigurator;
 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;
@@ -52,6 +53,7 @@ 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.AllocationService;
 import org.elasticsearch.cluster.routing.allocation.DiskThresholdMonitor;
 import org.elasticsearch.cluster.routing.allocation.ShardsAvailabilityHealthIndicatorService;
 import org.elasticsearch.cluster.routing.allocation.WriteLoadForecaster;
@@ -739,24 +741,40 @@ class NodeConstruction {
             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<>();
 

+ 113 - 4
server/src/main/java/org/elasticsearch/plugins/Plugin.java

@@ -62,6 +62,83 @@ import java.util.function.UnaryOperator;
  */
 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>
@@ -69,22 +146,54 @@ public abstract class Plugin implements Closeable {
      * 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
     public Collection<Object> createComponents(
         Client client,
         ClusterService clusterService,