diff --git a/server/src/main/java/org/opensearch/cluster/service/ClusterManagerService.java b/server/src/main/java/org/opensearch/cluster/service/ClusterManagerService.java index 4f3f5ecb86d1f..fa8c965b4d538 100644 --- a/server/src/main/java/org/opensearch/cluster/service/ClusterManagerService.java +++ b/server/src/main/java/org/opensearch/cluster/service/ClusterManagerService.java @@ -9,14 +9,11 @@ package org.opensearch.cluster.service; import org.opensearch.cluster.ClusterManagerMetrics; -import org.opensearch.cluster.service.applicationtemplates.SystemTemplatesPlugin; import org.opensearch.common.annotation.PublicApi; import org.opensearch.common.settings.ClusterSettings; import org.opensearch.common.settings.Settings; import org.opensearch.threadpool.ThreadPool; -import java.util.List; - /** * Main Cluster Manager Node Service * @@ -33,9 +30,8 @@ public ClusterManagerService( Settings settings, ClusterSettings clusterSettings, ThreadPool threadPool, - ClusterManagerMetrics clusterManagerMetrics, - List systemTemplatesPlugins + ClusterManagerMetrics clusterManagerMetrics ) { - super(settings, clusterSettings, threadPool, clusterManagerMetrics, systemTemplatesPlugins); + super(settings, clusterSettings, threadPool, clusterManagerMetrics); } } diff --git a/server/src/main/java/org/opensearch/cluster/service/ClusterService.java b/server/src/main/java/org/opensearch/cluster/service/ClusterService.java index d3282bb158774..c3c48dd8b87ef 100644 --- a/server/src/main/java/org/opensearch/cluster/service/ClusterService.java +++ b/server/src/main/java/org/opensearch/cluster/service/ClusterService.java @@ -46,7 +46,6 @@ import org.opensearch.cluster.node.DiscoveryNode; import org.opensearch.cluster.routing.OperationRouting; import org.opensearch.cluster.routing.RerouteService; -import org.opensearch.cluster.service.applicationtemplates.SystemTemplatesPlugin; import org.opensearch.common.annotation.PublicApi; import org.opensearch.common.lifecycle.AbstractLifecycleComponent; import org.opensearch.common.settings.ClusterSettings; @@ -59,7 +58,6 @@ import org.opensearch.threadpool.ThreadPool; import java.util.Collections; -import java.util.List; import java.util.Map; /** @@ -96,20 +94,19 @@ public class ClusterService extends AbstractLifecycleComponent { private IndexingPressureService indexingPressureService; public ClusterService(Settings settings, ClusterSettings clusterSettings, ThreadPool threadPool) { - this(settings, clusterSettings, threadPool, new ClusterManagerMetrics(NoopMetricsRegistry.INSTANCE), null); + this(settings, clusterSettings, threadPool, new ClusterManagerMetrics(NoopMetricsRegistry.INSTANCE)); } public ClusterService( Settings settings, ClusterSettings clusterSettings, ThreadPool threadPool, - ClusterManagerMetrics clusterManagerMetrics, - List systemTemplatesPlugins + ClusterManagerMetrics clusterManagerMetrics ) { this( settings, clusterSettings, - new ClusterManagerService(settings, clusterSettings, threadPool, clusterManagerMetrics, systemTemplatesPlugins), + new ClusterManagerService(settings, clusterSettings, threadPool, clusterManagerMetrics), new ClusterApplierService(Node.NODE_NAME_SETTING.get(settings), settings, clusterSettings, threadPool, clusterManagerMetrics) ); } diff --git a/server/src/main/java/org/opensearch/cluster/service/MasterService.java b/server/src/main/java/org/opensearch/cluster/service/MasterService.java index de420b420dbad..686e9793a8fd3 100644 --- a/server/src/main/java/org/opensearch/cluster/service/MasterService.java +++ b/server/src/main/java/org/opensearch/cluster/service/MasterService.java @@ -53,8 +53,6 @@ import org.opensearch.cluster.node.DiscoveryNode; import org.opensearch.cluster.node.DiscoveryNodes; import org.opensearch.cluster.routing.RoutingTable; -import org.opensearch.cluster.service.applicationtemplates.SystemTemplatesPlugin; -import org.opensearch.cluster.service.applicationtemplates.SystemTemplatesService; import org.opensearch.common.Nullable; import org.opensearch.common.Priority; import org.opensearch.common.annotation.DeprecatedApi; @@ -142,18 +140,16 @@ public class MasterService extends AbstractLifecycleComponent { private final ClusterManagerThrottlingStats throttlingStats; private final ClusterStateStats stateStats; private final ClusterManagerMetrics clusterManagerMetrics; - private final SystemTemplatesService systemTemplatesService; public MasterService(Settings settings, ClusterSettings clusterSettings, ThreadPool threadPool) { - this(settings, clusterSettings, threadPool, new ClusterManagerMetrics(NoopMetricsRegistry.INSTANCE), null); + this(settings, clusterSettings, threadPool, new ClusterManagerMetrics(NoopMetricsRegistry.INSTANCE)); } public MasterService( Settings settings, ClusterSettings clusterSettings, ThreadPool threadPool, - ClusterManagerMetrics clusterManagerMetrics, - List systemTemplatesPlugins + ClusterManagerMetrics clusterManagerMetrics ) { this.nodeName = Objects.requireNonNull(Node.NODE_NAME_SETTING.get(settings)); @@ -173,7 +169,6 @@ public MasterService( this.stateStats = new ClusterStateStats(); this.threadPool = threadPool; this.clusterManagerMetrics = clusterManagerMetrics; - this.systemTemplatesService = new SystemTemplatesService(systemTemplatesPlugins, clusterSettings, settings); } private void setSlowTaskLoggingThreshold(TimeValue slowTaskLoggingThreshold) { @@ -400,7 +395,6 @@ void onPublicationSuccess(ClusterChangedEvent clusterChangedEvent, TaskOutputs t try { taskOutputs.clusterStatePublished(clusterChangedEvent); - threadPool.executor(ThreadPool.Names.GENERIC).submit(() -> systemTemplatesService.refreshTemplates()); } catch (Exception e) { logger.error( () -> new ParameterizedMessage( diff --git a/server/src/main/java/org/opensearch/cluster/service/applicationtemplates/SystemTemplatesService.java b/server/src/main/java/org/opensearch/cluster/service/applicationtemplates/SystemTemplatesService.java index c9f1c4de477e0..c665fe7421c6c 100644 --- a/server/src/main/java/org/opensearch/cluster/service/applicationtemplates/SystemTemplatesService.java +++ b/server/src/main/java/org/opensearch/cluster/service/applicationtemplates/SystemTemplatesService.java @@ -10,6 +10,9 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.opensearch.cluster.ClusterChangedEvent; +import org.opensearch.cluster.ClusterStateListener; +import org.opensearch.cluster.LocalNodeClusterManagerListener; import org.opensearch.common.settings.ClusterSettings; import org.opensearch.common.settings.Setting; import org.opensearch.common.settings.Settings; @@ -21,7 +24,7 @@ /** * Service class to orchestrate execution around available templates' management. */ -public class SystemTemplatesService { +public class SystemTemplatesService implements LocalNodeClusterManagerListener { public static final int APPLICATION_BASED_CONFIGURATION_TEMPLATES_LOAD_DEFAULT_COUNT = 50; @@ -52,10 +55,20 @@ public SystemTemplatesService(List systemTemplatesPluginL this.settings = settings; } - public void refreshTemplates() { + @Override + public void onClusterManager() { + refreshTemplates(); + } + + @Override + public void offClusterManager() { + // do nothing + } + + private void refreshTemplates() { if (loaded.compareAndSet(false, true)) { if (SETTING_APPLICATION_BASED_CONFIGURATION_TEMPLATES_LOAD.get(settings) > 0) { - int countOfTemplatesToLoad = SETTING_APPLICATION_BASED_CONFIGURATION_TEMPLATES_LOAD.get(settings); + int countOfTemplatesToLoad = templatesToLoad; int templatesLoaded = 0; int failedLoadingTemplates = 0; int failedLoadingRepositories = 0; diff --git a/server/src/main/java/org/opensearch/common/settings/ClusterSettings.java b/server/src/main/java/org/opensearch/common/settings/ClusterSettings.java index 5dcf23ae52294..8f613b40f2a7b 100644 --- a/server/src/main/java/org/opensearch/common/settings/ClusterSettings.java +++ b/server/src/main/java/org/opensearch/common/settings/ClusterSettings.java @@ -82,6 +82,7 @@ import org.opensearch.cluster.service.ClusterManagerService; import org.opensearch.cluster.service.ClusterManagerTaskThrottler; import org.opensearch.cluster.service.ClusterService; +import org.opensearch.cluster.service.applicationtemplates.SystemTemplatesService; import org.opensearch.common.annotation.PublicApi; import org.opensearch.common.cache.CacheType; import org.opensearch.common.cache.settings.CacheSettings; @@ -758,7 +759,9 @@ public void apply(Settings value, Settings current, Settings previous) { SearchService.CLUSTER_ALLOW_DERIVED_FIELD_SETTING, // Composite index settings - CompositeIndexSettings.STAR_TREE_INDEX_ENABLED_SETTING + CompositeIndexSettings.STAR_TREE_INDEX_ENABLED_SETTING, + + SystemTemplatesService.SETTING_APPLICATION_BASED_CONFIGURATION_TEMPLATES_LOAD ) ) ); diff --git a/server/src/main/java/org/opensearch/node/Node.java b/server/src/main/java/org/opensearch/node/Node.java index 95dcebfc463f7..950558159acec 100644 --- a/server/src/main/java/org/opensearch/node/Node.java +++ b/server/src/main/java/org/opensearch/node/Node.java @@ -85,6 +85,7 @@ import org.opensearch.cluster.routing.allocation.DiskThresholdMonitor; import org.opensearch.cluster.service.ClusterService; import org.opensearch.cluster.service.applicationtemplates.SystemTemplatesPlugin; +import org.opensearch.cluster.service.applicationtemplates.SystemTemplatesService; import org.opensearch.common.SetOnce; import org.opensearch.common.StopWatch; import org.opensearch.common.cache.module.CacheModule; @@ -664,18 +665,20 @@ protected Node( settings, settingsModule.getClusterSettings(), threadPool, - clusterManagerMetrics, - pluginsService.filterPlugins(SystemTemplatesPlugin.class) + clusterManagerMetrics ); clusterService.addStateApplier(scriptService); resourcesToClose.add(clusterService); final Set> consistentSettings = settingsModule.getConsistentSettings(); if (consistentSettings.isEmpty() == false) { - clusterService.addLocalNodeMasterListener( + clusterService.addLocalNodeClusterManagerListener( new ConsistentSettingsService(settings, clusterService, consistentSettings).newHashPublisher() ); } + clusterService.addLocalNodeClusterManagerListener(new SystemTemplatesService(pluginsService.filterPlugins(SystemTemplatesPlugin.class), + clusterService.getClusterSettings(), settings)); + final ClusterInfoService clusterInfoService = newClusterInfoService(settings, clusterService, threadPool, client); final UsageService usageService = new UsageService();