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

Make stack.templates.enabled a dynamic setting #63764

Merged
merged 4 commits into from
Oct 16, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public abstract class IndexTemplateRegistry implements ClusterStateListener {
protected final Client client;
protected final ThreadPool threadPool;
protected final NamedXContentRegistry xContentRegistry;
protected final ClusterService clusterService;
protected final ConcurrentMap<String, AtomicBoolean> templateCreationsInProgress = new ConcurrentHashMap<>();
protected final ConcurrentMap<String, AtomicBoolean> policyCreationsInProgress = new ConcurrentHashMap<>();

Expand All @@ -69,6 +70,13 @@ public IndexTemplateRegistry(Settings nodeSettings, ClusterService clusterServic
this.client = client;
this.threadPool = threadPool;
this.xContentRegistry = xContentRegistry;
this.clusterService = clusterService;
}

/**
* Initialize the template registry, adding it as a listener so templates will be installed as necessary
*/
public void initialize() {
clusterService.addListener(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ public Collection<Object> createComponents(Client client, ClusterService cluster
@SuppressWarnings("unused")
dakrone marked this conversation as resolved.
Show resolved Hide resolved
ILMHistoryTemplateRegistry ilmTemplateRegistry =
new ILMHistoryTemplateRegistry(settings, clusterService, threadPool, client, xContentRegistry);
ilmTemplateRegistry.initialize();
ilmHistoryStore.set(new ILMHistoryStore(settings, new OriginSettingClient(client, INDEX_LIFECYCLE_ORIGIN),
clusterService, threadPool));
indexLifecycleInitialisationService.set(new IndexLifecycleService(settings, client, clusterService, threadPool,
Expand All @@ -190,6 +191,7 @@ public Collection<Object> createComponents(Client client, ClusterService cluster
@SuppressWarnings("unused")
dakrone marked this conversation as resolved.
Show resolved Hide resolved
SnapshotLifecycleTemplateRegistry templateRegistry = new SnapshotLifecycleTemplateRegistry(settings, clusterService, threadPool,
client, xContentRegistry);
templateRegistry.initialize();
snapshotHistoryStore.set(new SnapshotHistoryStore(settings, new OriginSettingClient(client, INDEX_LIFECYCLE_ORIGIN),
clusterService));
snapshotLifecycleService.set(new SnapshotLifecycleService(settings,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,8 @@ public Collection<Object> createComponents(Client client, ClusterService cluster

this.mlUpgradeModeActionFilter.set(new MlUpgradeModeActionFilter(clusterService));

new MlIndexTemplateRegistry(settings, clusterService, threadPool, client, xContentRegistry);
MlIndexTemplateRegistry registry = new MlIndexTemplateRegistry(settings, clusterService, threadPool, client, xContentRegistry);
registry.initialize();

AnomalyDetectionAuditor anomalyDetectionAuditor = new AnomalyDetectionAuditor(client, clusterService);
DataFrameAnalyticsAuditor dataFrameAnalyticsAuditor = new DataFrameAnalyticsAuditor(client, clusterService);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.watcher.ResourceWatcherService;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
Expand All @@ -30,19 +29,13 @@
public class StackPlugin extends Plugin implements ActionPlugin {
private final Settings settings;

public static final Setting<Boolean> STACK_TEMPLATES_ENABLED = Setting.boolSetting(
"stack.templates.enabled",
true,
Setting.Property.NodeScope
);

public StackPlugin(Settings settings) {
this.settings = settings;
}

@Override
public List<Setting<?>> getSettings() {
return Collections.singletonList(STACK_TEMPLATES_ENABLED);
return Collections.singletonList(StackTemplateRegistry.STACK_TEMPLATES_ENABLED);
}

@Override
Expand All @@ -59,8 +52,8 @@ public Collection<Object> createComponents(
IndexNameExpressionResolver indexNameExpressionResolver,
Supplier<RepositoriesService> repositoriesServiceSupplier
) {
final List<Object> components = new ArrayList<>();
StackTemplateRegistry templateRegistry = new StackTemplateRegistry(settings, clusterService, threadPool, client, xContentRegistry);
return components;
templateRegistry.initialize();
return Collections.singleton(templateRegistry);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@

package org.elasticsearch.xpack.stack;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.threadpool.ThreadPool;
Expand All @@ -19,17 +22,27 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class StackTemplateRegistry extends IndexTemplateRegistry {
private static final Logger logger = LogManager.getLogger(StackTemplateRegistry.class);

// The stack template registry should remain at version 0. This is because templates and
// policies will be changed by the ingest manager once they exist, and ES should only ever put
// the template in place if it does not exist. If this were incremented we could accidentally
// overwrite a template or policy changed by the ingest manager.
public static final int REGISTRY_VERSION = 0;

public static final String TEMPLATE_VERSION_VARIABLE = "xpack.stack.template.version";
public static final Setting<Boolean> STACK_TEMPLATES_ENABLED = Setting.boolSetting(
"stack.templates.enabled",
true,
Setting.Property.NodeScope,
Setting.Property.Dynamic
);

private final boolean stackTemplateEnabled;
private final ClusterService clusterService;
private volatile boolean stackTemplateEnabled;

//////////////////////////////////////////////////////////
// Logs components (for matching logs-*-* indices)
Expand Down Expand Up @@ -129,7 +142,27 @@ public StackTemplateRegistry(
NamedXContentRegistry xContentRegistry
) {
super(nodeSettings, clusterService, threadPool, client, xContentRegistry);
this.stackTemplateEnabled = StackPlugin.STACK_TEMPLATES_ENABLED.get(nodeSettings);
this.clusterService = clusterService;
this.stackTemplateEnabled = STACK_TEMPLATES_ENABLED.get(nodeSettings);
}

@Override
public void initialize() {
super.initialize();
clusterService.getClusterSettings().addSettingsUpdateConsumer(STACK_TEMPLATES_ENABLED, this::updateEnabledSetting);
}

private void updateEnabledSetting(boolean newValue) {
if (newValue) {
this.stackTemplateEnabled = true;
} else {
logger.info(
"stack composable templates [{}] and component templates [{}] will not be installed or reinstalled",
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is a bit confusing as to why this happened. Should we also mention that the setting was updated and as a result, the templates will not be installed/reinstalled anymore?

Copy link
Member Author

Choose a reason for hiding this comment

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

the settings infrastructure already logs a message to that effect, so this looks like:

[2020-10-16T09:16:27,285][INFO ][o.e.c.s.ClusterSettings  ] [runTask-0] updating [stack.templates.enabled] from [true] to [false]
[2020-10-16T09:16:27,286][INFO ][o.e.x.s.StackTemplateRegistry] [runTask-0] stack composable templates [logs,metrics,synthetics] and component templates [logs-mappings,logs-settings,metrics-mappings,metrics-settings,synthetics-mappings,synthetics-settings] will not be installed or reinstalled

That's why I left out any mention that the setting was updated (it happens regardless)

getComposableTemplateConfigs().stream().map(IndexTemplateConfig::getTemplateName).collect(Collectors.joining(",")),
getComponentTemplateConfigs().stream().map(IndexTemplateConfig::getTemplateName).collect(Collectors.joining(","))
);
this.stackTemplateEnabled = false;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void tearDown() throws Exception {
}

public void testDisabledDoesNotAddTemplates() {
Settings settings = Settings.builder().put(StackPlugin.STACK_TEMPLATES_ENABLED.getKey(), false).build();
Settings settings = Settings.builder().put(StackTemplateRegistry.STACK_TEMPLATES_ENABLED.getKey(), false).build();
StackTemplateRegistry disabledRegistry = new StackTemplateRegistry(settings, clusterService, threadPool, client, xContentRegistry);
assertThat(disabledRegistry.getComponentTemplateConfigs(), hasSize(0));
assertThat(disabledRegistry.getComposableTemplateConfigs(), hasSize(0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,9 @@ public Collection<Object> createComponents(Client client, ClusterService cluster
throw new UncheckedIOException(e);
}

new WatcherIndexTemplateRegistry(environment.settings(), clusterService, threadPool, client, xContentRegistry);
WatcherIndexTemplateRegistry templateRegistry = new WatcherIndexTemplateRegistry(environment.settings(),
clusterService, threadPool, client, xContentRegistry);
templateRegistry.initialize();

final SSLService sslService = getSslService();
// http client
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"Stack templates can be disabled":
- do:
cluster.put_settings:
body:
transient:
stack.templates.enabled: false

- do:
indices.get_index_template:
name: logs

- do:
indices.delete_index_template:
name: logs

- do:
catch: missing
indices.get_index_template:
name: logs