Skip to content

Commit

Permalink
Make the ILM and SLM history_index_enabled settings dynamic (#86493)
Browse files Browse the repository at this point in the history
  • Loading branch information
joegallo committed May 6, 2022
1 parent 5dae85c commit 6aaf097
Show file tree
Hide file tree
Showing 15 changed files with 159 additions and 100 deletions.
5 changes: 5 additions & 0 deletions docs/changelog/86493.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 86493
summary: Make the ILM and SLM `history_index_enabled` settings dynamic
area: ILM+SLM
type: enhancement
issues: []
2 changes: 1 addition & 1 deletion docs/reference/settings/ilm-settings.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ deprecated:[7.8.0,Basic License features are always enabled] +
This deprecated setting has no effect and will be removed in Elasticsearch 8.0.

`indices.lifecycle.history_index_enabled`::
(<<static-cluster-setting,Static>>, Boolean)
(<<dynamic-cluster-setting,Dynamic>>, Boolean)
Whether ILM's history index is enabled. If enabled, ILM will record the
history of actions taken as part of ILM policies to the `ilm-history-*`
indices. Defaults to `true`.
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/settings/snapshot-settings.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ The following cluster settings configure <<automate-snapshots-slm,{slm}

[[slm-history-index-enabled]]
`slm.history_index_enabled`::
(<<static-cluster-setting,Static>>, Boolean)
(<<dynamic-cluster-setting,Dynamic>>, Boolean)
Controls whether {slm-init} records the history of actions taken as part of {slm-init} policies
to the `slm-history-*` indices. Defaults to `true`.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ public void onFailure(Exception e) {
}

public static ClusterService createClusterService(ThreadPool threadPool) {
return createClusterService(threadPool, new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS));
}

public static ClusterService createClusterService(ThreadPool threadPool, DiscoveryNode localNode) {
return createClusterService(threadPool, localNode, new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS));
}

public static ClusterService createClusterService(ThreadPool threadPool, ClusterSettings clusterSettings) {
DiscoveryNode discoveryNode = new DiscoveryNode(
"node",
"node",
Expand All @@ -108,11 +116,7 @@ public static ClusterService createClusterService(ThreadPool threadPool) {
DiscoveryNodeRole.roles(),
Version.CURRENT
);
return createClusterService(threadPool, discoveryNode);
}

public static ClusterService createClusterService(ThreadPool threadPool, DiscoveryNode localNode) {
return createClusterService(threadPool, localNode, new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS));
return createClusterService(threadPool, discoveryNode, clusterSettings);
}

public static ClusterService createClusterService(ThreadPool threadPool, DiscoveryNode localNode, ClusterSettings clusterSettings) {
Expand Down Expand Up @@ -162,6 +166,12 @@ public static ClusterService createClusterService(ClusterState initialState, Thr
return clusterService;
}

public static ClusterService createClusterService(ClusterState initialState, ThreadPool threadPool, ClusterSettings clusterSettings) {
ClusterService clusterService = createClusterService(threadPool, clusterSettings);
setState(clusterService, initialState);
return clusterService;
}

public static void setState(ClusterService clusterService, ClusterState.Builder clusterStateBuilder) {
setState(clusterService, clusterStateBuilder.build());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public class LifecycleSettings {
public static final Setting<Boolean> LIFECYCLE_HISTORY_INDEX_ENABLED_SETTING = Setting.boolSetting(
LIFECYCLE_HISTORY_INDEX_ENABLED,
true,
Setting.Property.Dynamic,
Setting.Property.NodeScope
);
public static final Setting<TimeValue> LIFECYCLE_STEP_MASTER_TIMEOUT_SETTING = Setting.positiveTimeSetting(
Expand All @@ -91,6 +92,7 @@ public class LifecycleSettings {
public static final Setting<Boolean> SLM_HISTORY_INDEX_ENABLED_SETTING = Setting.boolSetting(
SLM_HISTORY_INDEX_ENABLED,
true,
Setting.Property.Dynamic,
Setting.Property.NodeScope
);
public static final Setting<String> SLM_RETENTION_SCHEDULE_SETTING = Setting.simpleString(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,7 @@ public Collection<Object> createComponents(
xContentRegistry
);
ilmTemplateRegistry.initialize();
ilmHistoryStore.set(
new ILMHistoryStore(settings, new OriginSettingClient(client, INDEX_LIFECYCLE_ORIGIN), clusterService, threadPool)
);
ilmHistoryStore.set(new ILMHistoryStore(new OriginSettingClient(client, INDEX_LIFECYCLE_ORIGIN), clusterService, threadPool));
/*
* Here we use threadPool::absoluteTimeInMillis rather than System::currentTimeInMillis because snapshot start time is set using
* ThreadPool.absoluteTimeInMillis(). ThreadPool.absoluteTimeInMillis() returns a cached time that can be several hundred
Expand Down Expand Up @@ -248,9 +246,7 @@ public Collection<Object> createComponents(
xContentRegistry
);
templateRegistry.initialize();
snapshotHistoryStore.set(
new SnapshotHistoryStore(settings, new OriginSettingClient(client, INDEX_LIFECYCLE_ORIGIN), clusterService)
);
snapshotHistoryStore.set(new SnapshotHistoryStore(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 @@ -21,7 +21,6 @@
import org.elasticsearch.client.internal.Client;
import org.elasticsearch.client.internal.OriginSettingClient;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.threadpool.ThreadPool;
Expand Down Expand Up @@ -59,12 +58,14 @@ public class ILMHistoryStore implements Closeable {
).getBytes()
);

private final boolean ilmHistoryEnabled;
private volatile boolean ilmHistoryEnabled = true;
private final BulkProcessor processor;
private final ThreadPool threadPool;

public ILMHistoryStore(Settings nodeSettings, Client client, ClusterService clusterService, ThreadPool threadPool) {
this.ilmHistoryEnabled = LIFECYCLE_HISTORY_INDEX_ENABLED_SETTING.get(nodeSettings);
public ILMHistoryStore(Client client, ClusterService clusterService, ThreadPool threadPool) {
this.setIlmHistoryEnabled(LIFECYCLE_HISTORY_INDEX_ENABLED_SETTING.get(clusterService.getSettings()));
clusterService.getClusterSettings().addSettingsUpdateConsumer(LIFECYCLE_HISTORY_INDEX_ENABLED_SETTING, this::setIlmHistoryEnabled);

this.threadPool = threadPool;

this.processor = BulkProcessor.builder(new OriginSettingClient(client, INDEX_LIFECYCLE_ORIGIN)::bulk, new BulkProcessor.Listener() {
Expand Down Expand Up @@ -186,4 +187,8 @@ public void close() {
logger.warn("failed to shut down ILM history bulk processor after 10 seconds", e);
}
}

public void setIlmHistoryEnabled(boolean ilmHistoryEnabled) {
this.ilmHistoryEnabled = ilmHistoryEnabled;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.elasticsearch.client.internal.Client;
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.xcontent.ToXContent;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentFactory;
Expand All @@ -38,12 +37,13 @@ public class SnapshotHistoryStore {

private final Client client;
private final ClusterService clusterService;
private final boolean slmHistoryEnabled;
private volatile boolean slmHistoryEnabled = true;

public SnapshotHistoryStore(Settings nodeSettings, Client client, ClusterService clusterService) {
public SnapshotHistoryStore(Client client, ClusterService clusterService) {
this.client = client;
this.clusterService = clusterService;
slmHistoryEnabled = SLM_HISTORY_INDEX_ENABLED_SETTING.get(nodeSettings);
this.setSlmHistoryEnabled(SLM_HISTORY_INDEX_ENABLED_SETTING.get(clusterService.getSettings()));
clusterService.getClusterSettings().addSettingsUpdateConsumer(SLM_HISTORY_INDEX_ENABLED_SETTING, this::setSlmHistoryEnabled);
}

/**
Expand Down Expand Up @@ -100,4 +100,8 @@ public void putAsync(SnapshotHistoryItem item) {
);
}
}

public void setSlmHistoryEnabled(boolean slmHistoryEnabled) {
this.slmHistoryEnabled = slmHistoryEnabled;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.settings.ClusterSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.set.Sets;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.index.Index;
import org.elasticsearch.test.ClusterServiceUtils;
Expand Down Expand Up @@ -74,6 +76,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.concurrent.CopyOnWriteArrayList;
Expand All @@ -86,6 +89,7 @@
import static java.util.stream.Collectors.toList;
import static org.elasticsearch.cluster.metadata.LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.awaitLatch;
import static org.elasticsearch.xpack.core.ilm.LifecycleSettings.LIFECYCLE_HISTORY_INDEX_ENABLED_SETTING;
import static org.elasticsearch.xpack.ilm.LifecyclePolicyTestsUtils.newTestLifecyclePolicy;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
Expand Down Expand Up @@ -114,7 +118,11 @@ public class IndexLifecycleRunnerTests extends ESTestCase {
public void prepare() {
threadPool = new TestThreadPool("test");
noopClient = new NoOpClient(threadPool);
historyStore = new NoOpHistoryStore(noopClient);
ClusterSettings settings = new ClusterSettings(
Settings.EMPTY,
Sets.union(ClusterSettings.BUILT_IN_CLUSTER_SETTINGS, Set.of(LIFECYCLE_HISTORY_INDEX_ENABLED_SETTING))
);
historyStore = new NoOpHistoryStore(noopClient, ClusterServiceUtils.createClusterService(threadPool, settings));
}

@After
Expand Down Expand Up @@ -1259,8 +1267,8 @@ private static class NoOpHistoryStore extends ILMHistoryStore {

private final List<ILMHistoryItem> items = new CopyOnWriteArrayList<>();

NoOpHistoryStore(Client noopClient) {
super(Settings.EMPTY, noopClient, null, null);
NoOpHistoryStore(Client noopClient, ClusterService clusterService) {
super(noopClient, clusterService, null);
}

public List<ILMHistoryItem> getItems() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.TriFunction;
import org.elasticsearch.common.settings.ClusterSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.set.Sets;
import org.elasticsearch.index.shard.ShardId;
import org.elasticsearch.test.ClusterServiceUtils;
import org.elasticsearch.test.ESTestCase;
Expand All @@ -38,6 +40,7 @@
import org.junit.After;
import org.junit.Before;

import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
Expand All @@ -59,7 +62,11 @@ public class ILMHistoryStoreTests extends ESTestCase {
public void setup() {
threadPool = new TestThreadPool(this.getClass().getName());
client = new VerifyingClient(threadPool);
clusterService = ClusterServiceUtils.createClusterService(threadPool);
ClusterSettings settings = new ClusterSettings(
Settings.EMPTY,
Sets.union(ClusterSettings.BUILT_IN_CLUSTER_SETTINGS, Set.of(LIFECYCLE_HISTORY_INDEX_ENABLED_SETTING))
);
clusterService = ClusterServiceUtils.createClusterService(threadPool, settings);
ILMHistoryTemplateRegistry registry = new ILMHistoryTemplateRegistry(
clusterService.getSettings(),
clusterService,
Expand All @@ -74,7 +81,7 @@ public void setup() {
.metadata(Metadata.builder(state.metadata()).indexTemplates(registry.getComposableTemplateConfigs()))
.build()
);
historyStore = new ILMHistoryStore(Settings.EMPTY, client, clusterService, threadPool);
historyStore = new ILMHistoryStore(client, clusterService, threadPool);
}

@After
Expand All @@ -86,21 +93,23 @@ public void setdown() {
}

public void testNoActionIfDisabled() throws Exception {
Settings settings = Settings.builder().put(LIFECYCLE_HISTORY_INDEX_ENABLED_SETTING.getKey(), false).build();
try (ILMHistoryStore disabledHistoryStore = new ILMHistoryStore(settings, client, null, threadPool)) {
String policyId = randomAlphaOfLength(5);
final long timestamp = randomNonNegativeLong();
ILMHistoryItem record = ILMHistoryItem.success("index", policyId, timestamp, null, null);

CountDownLatch latch = new CountDownLatch(1);
client.setVerifier((a, r, l) -> {
fail("the history store is disabled, no action should have been taken");
latch.countDown();
return null;
});
disabledHistoryStore.putAsync(record);
latch.await(10, TimeUnit.SECONDS);
}
ClusterState state = clusterService.state();
Metadata.Builder metadata = Metadata.builder(state.metadata())
.persistentSettings(Settings.builder().put(LIFECYCLE_HISTORY_INDEX_ENABLED_SETTING.getKey(), false).build());
ClusterServiceUtils.setState(clusterService, ClusterState.builder(state).metadata(metadata));

String policyId = randomAlphaOfLength(5);
final long timestamp = randomNonNegativeLong();
ILMHistoryItem record = ILMHistoryItem.success("index", policyId, timestamp, null, null);

CountDownLatch latch = new CountDownLatch(1);
client.setVerifier((a, r, l) -> {
fail("the history store is disabled, no action should have been taken");
latch.countDown();
return null;
});
historyStore.putAsync(record);
latch.await(10, TimeUnit.SECONDS);
}

public void testPut() throws Exception {
Expand Down

0 comments on commit 6aaf097

Please sign in to comment.