Skip to content

Commit b92f94b

Browse files
committed
Use environment settings instead of state settings for Watcher config
Prior to this we used the settings from cluster state to see whether ILM was enabled of disabled, however, these settings don't accurately reflect the `xpack.ilm.enabled` setting in `elasticsearch.yml`. This commit changes to using the `Environment` settings, which correctly reflect the ILM enabled setting. Resolves elastic#41042
1 parent f4c12f0 commit b92f94b

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/Watcher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ public Collection<Object> createComponents(Client client, ClusterService cluster
267267
throw new UncheckedIOException(e);
268268
}
269269

270-
new WatcherIndexTemplateRegistry(clusterService, threadPool, client, xContentRegistry);
270+
new WatcherIndexTemplateRegistry(environment.settings(), clusterService, threadPool, client, xContentRegistry);
271271

272272
// http client
273273
httpClient = new HttpClient(settings, getSslService(), cryptoService, clusterService);

x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/support/WatcherIndexTemplateRegistry.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.elasticsearch.cluster.ClusterStateListener;
1818
import org.elasticsearch.cluster.node.DiscoveryNode;
1919
import org.elasticsearch.cluster.service.ClusterService;
20+
import org.elasticsearch.common.settings.Settings;
2021
import org.elasticsearch.common.unit.TimeValue;
2122
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
2223
import org.elasticsearch.common.xcontent.XContentType;
@@ -63,14 +64,17 @@ public class WatcherIndexTemplateRegistry implements ClusterStateListener {
6364

6465
private static final Logger logger = LogManager.getLogger(WatcherIndexTemplateRegistry.class);
6566

67+
private final Settings settings;
6668
private final Client client;
6769
private final ThreadPool threadPool;
6870
private final NamedXContentRegistry xContentRegistry;
6971
private final ConcurrentMap<String, AtomicBoolean> templateCreationsInProgress = new ConcurrentHashMap<>();
7072
private final AtomicBoolean historyPolicyCreationInProgress = new AtomicBoolean();
7173

72-
public WatcherIndexTemplateRegistry(ClusterService clusterService, ThreadPool threadPool, Client client,
74+
public WatcherIndexTemplateRegistry(Settings clusterSettings, ClusterService clusterService,
75+
ThreadPool threadPool, Client client,
7376
NamedXContentRegistry xContentRegistry) {
77+
this.settings = clusterSettings;
7478
this.client = client;
7579
this.threadPool = threadPool;
7680
this.xContentRegistry = xContentRegistry;
@@ -104,7 +108,7 @@ public void clusterChanged(ClusterChangedEvent event) {
104108
}
105109

106110
private void addTemplatesIfMissing(ClusterState state) {
107-
boolean ilmSupported = XPackSettings.INDEX_LIFECYCLE_ENABLED.get(state.metaData().settings());
111+
boolean ilmSupported = XPackSettings.INDEX_LIFECYCLE_ENABLED.get(this.settings);
108112
final TemplateConfig[] indexTemplates = ilmSupported ? TEMPLATE_CONFIGS : TEMPLATE_CONFIGS_NO_ILM;
109113
for (TemplateConfig template : indexTemplates) {
110114
final String templateName = template.getTemplateName();
@@ -153,7 +157,7 @@ LifecyclePolicy loadWatcherHistoryPolicy() {
153157
}
154158

155159
private void addIndexLifecyclePolicyIfMissing(ClusterState state) {
156-
boolean ilmSupported = XPackSettings.INDEX_LIFECYCLE_ENABLED.get(state.metaData().settings());
160+
boolean ilmSupported = XPackSettings.INDEX_LIFECYCLE_ENABLED.get(this.settings);
157161
if (ilmSupported && historyPolicyCreationInProgress.compareAndSet(false, true)) {
158162
final LifecyclePolicy policyOnDisk = loadWatcherHistoryPolicy();
159163

x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/WatcherIndexTemplateRegistryTests.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,13 @@ public class WatcherIndexTemplateRegistryTests extends ESTestCase {
7373

7474
private WatcherIndexTemplateRegistry registry;
7575
private NamedXContentRegistry xContentRegistry;
76+
private ClusterService clusterService;
77+
private ThreadPool threadPool;
7678
private Client client;
7779

7880
@Before
7981
public void createRegistryAndClient() {
80-
ThreadPool threadPool = mock(ThreadPool.class);
82+
threadPool = mock(ThreadPool.class);
8183
when(threadPool.getThreadContext()).thenReturn(new ThreadContext(Settings.EMPTY));
8284
when(threadPool.generic()).thenReturn(EsExecutors.newDirectExecutorService());
8385

@@ -94,14 +96,14 @@ public void createRegistryAndClient() {
9496
return null;
9597
}).when(indicesAdminClient).putTemplate(any(PutIndexTemplateRequest.class), any(ActionListener.class));
9698

97-
ClusterService clusterService = mock(ClusterService.class);
99+
clusterService = mock(ClusterService.class);
98100
List<NamedXContentRegistry.Entry> entries = new ArrayList<>(ClusterModule.getNamedXWriteables());
99101
entries.addAll(Arrays.asList(
100102
new NamedXContentRegistry.Entry(LifecycleType.class, new ParseField(TimeseriesLifecycleType.TYPE),
101103
(p) -> TimeseriesLifecycleType.INSTANCE),
102104
new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse)));
103105
xContentRegistry = new NamedXContentRegistry(entries);
104-
registry = new WatcherIndexTemplateRegistry(clusterService, threadPool, client, xContentRegistry);
106+
registry = new WatcherIndexTemplateRegistry(Settings.EMPTY, clusterService, threadPool, client, xContentRegistry);
105107
}
106108

107109
public void testThatNonExistingTemplatesAreAddedImmediately() {
@@ -130,9 +132,10 @@ public void testThatNonExistingTemplatesAreAddedEvenWithILMDisabled() {
130132
DiscoveryNode node = new DiscoveryNode("node", ESTestCase.buildNewFakeTransportAddress(), Version.CURRENT);
131133
DiscoveryNodes nodes = DiscoveryNodes.builder().localNodeId("node").masterNodeId("node").add(node).build();
132134

133-
ClusterChangedEvent event = createClusterChangedEvent(Settings.builder()
135+
registry = new WatcherIndexTemplateRegistry(Settings.builder()
134136
.put(XPackSettings.INDEX_LIFECYCLE_ENABLED.getKey(), false).build(),
135-
Collections.emptyList(), Collections.emptyMap(), nodes);
137+
clusterService, threadPool, client, xContentRegistry);
138+
ClusterChangedEvent event = createClusterChangedEvent(Settings.EMPTY, Collections.emptyList(), Collections.emptyMap(), nodes);
136139
registry.clusterChanged(event);
137140
ArgumentCaptor<PutIndexTemplateRequest> argumentCaptor = ArgumentCaptor.forClass(PutIndexTemplateRequest.class);
138141
verify(client.admin().indices(), times(3)).putTemplate(argumentCaptor.capture(), anyObject());
@@ -142,8 +145,9 @@ public void testThatNonExistingTemplatesAreAddedEvenWithILMDisabled() {
142145
WatcherIndexTemplateRegistryField.TRIGGERED_TEMPLATE_NAME), nodes);
143146
registry.clusterChanged(newEvent);
144147
ArgumentCaptor<PutIndexTemplateRequest> captor = ArgumentCaptor.forClass(PutIndexTemplateRequest.class);
145-
verify(client.admin().indices(), times(4)).putTemplate(captor.capture(), anyObject());
148+
verify(client.admin().indices(), times(5)).putTemplate(captor.capture(), anyObject());
146149
captor.getAllValues().forEach(req -> assertNull(req.settings().get("index.lifecycle.name")));
150+
verify(client, times(0)).execute(eq(PutLifecycleAction.INSTANCE), anyObject(), anyObject());
147151
}
148152

149153
public void testThatNonExistingPoliciesAreAddedImmediately() {
@@ -171,9 +175,10 @@ public void testNoPolicyButILMDisabled() {
171175
DiscoveryNode node = new DiscoveryNode("node", ESTestCase.buildNewFakeTransportAddress(), Version.CURRENT);
172176
DiscoveryNodes nodes = DiscoveryNodes.builder().localNodeId("node").masterNodeId("node").add(node).build();
173177

174-
ClusterChangedEvent event = createClusterChangedEvent(Settings.builder()
175-
.put(XPackSettings.INDEX_LIFECYCLE_ENABLED.getKey(), false).build(),
176-
Collections.emptyList(), Collections.emptyMap(), nodes);
178+
registry = new WatcherIndexTemplateRegistry(Settings.builder()
179+
.put(XPackSettings.INDEX_LIFECYCLE_ENABLED.getKey(), false).build(),
180+
clusterService, threadPool, client, xContentRegistry);
181+
ClusterChangedEvent event = createClusterChangedEvent(Settings.EMPTY, Collections.emptyList(), Collections.emptyMap(), nodes);
177182
registry.clusterChanged(event);
178183
verify(client, times(0)).execute(eq(PutLifecycleAction.INSTANCE), anyObject(), anyObject());
179184
}

0 commit comments

Comments
 (0)