Skip to content

Commit 87ddefd

Browse files
dakroneGurkan Kaymak
authored and
Gurkan Kaymak
committed
Use environment settings instead of state settings for Watcher config (elastic#41087)
* 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 * Rename settings object to nodeSettings * Use correct template list in WatcherRestIT * Use correct template list in other tests
1 parent e66cbe2 commit 87ddefd

File tree

10 files changed

+32
-20
lines changed

10 files changed

+32
-20
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ public final class WatcherIndexTemplateRegistryField {
2121
public static final String TRIGGERED_TEMPLATE_NAME = ".triggered_watches";
2222
public static final String WATCHES_TEMPLATE_NAME = ".watches";
2323
public static final String[] TEMPLATE_NAMES = new String[] {
24-
HISTORY_TEMPLATE_NAME, TRIGGERED_TEMPLATE_NAME, WATCHES_TEMPLATE_NAME
24+
HISTORY_TEMPLATE_NAME, TRIGGERED_TEMPLATE_NAME, WATCHES_TEMPLATE_NAME
25+
};
26+
public static final String[] TEMPLATE_NAMES_NO_ILM = new String[] {
27+
HISTORY_TEMPLATE_NAME_NO_ILM, TRIGGERED_TEMPLATE_NAME, WATCHES_TEMPLATE_NAME
2528
};
2629

2730
private WatcherIndexTemplateRegistryField() {}

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 nodeSettings;
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 nodeSettings, ClusterService clusterService,
75+
ThreadPool threadPool, Client client,
7376
NamedXContentRegistry xContentRegistry) {
77+
this.nodeSettings = nodeSettings;
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.nodeSettings);
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.nodeSettings);
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
}

x-pack/qa/smoke-test-watcher-with-security/src/test/java/org/elasticsearch/smoketest/SmokeTestWatcherWithSecurityClientYamlTestSuiteIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void startWatcher() throws Exception {
7676
});
7777

7878
assertBusy(() -> {
79-
for (String template : WatcherIndexTemplateRegistryField.TEMPLATE_NAMES) {
79+
for (String template : WatcherIndexTemplateRegistryField.TEMPLATE_NAMES_NO_ILM) {
8080
ClientYamlTestResponse templateExistsResponse = getAdminExecutionContext().callApi("indices.exists_template",
8181
singletonMap("name", template), emptyList(), emptyMap());
8282
assertThat(templateExistsResponse.getStatusCode(), is(200));

x-pack/qa/smoke-test-watcher/src/test/java/org/elasticsearch/smoketest/SmokeTestWatcherTestSuiteIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void startWatcher() throws Exception {
6464
});
6565

6666
assertBusy(() -> {
67-
for (String template : WatcherIndexTemplateRegistryField.TEMPLATE_NAMES) {
67+
for (String template : WatcherIndexTemplateRegistryField.TEMPLATE_NAMES_NO_ILM) {
6868
Response templateExistsResponse = adminClient().performRequest(new Request("HEAD", "/_template/" + template));
6969
assertThat(templateExistsResponse.getStatusLine().getStatusCode(), is(200));
7070
}

x-pack/qa/smoke-test-watcher/src/test/java/org/elasticsearch/smoketest/WatcherRestIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void startWatcher() throws Exception {
5858
});
5959

6060
assertBusy(() -> {
61-
for (String template : WatcherIndexTemplateRegistryField.TEMPLATE_NAMES) {
61+
for (String template : WatcherIndexTemplateRegistryField.TEMPLATE_NAMES_NO_ILM) {
6262
ClientYamlTestResponse templateExistsResponse = getAdminExecutionContext().callApi("indices.exists_template",
6363
singletonMap("name", template), emptyList(), emptyMap());
6464
assertThat(templateExistsResponse.getStatusCode(), is(200));

x-pack/qa/third-party/jira/src/test/java/org/elasticsearch/smoketest/WatcherJiraYamlTestSuiteIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static Iterable<Object[]> parameters() throws Exception {
3737

3838
@Before
3939
public void startWatcher() throws Exception {
40-
final List<String> watcherTemplates = Arrays.asList(WatcherIndexTemplateRegistryField.TEMPLATE_NAMES);
40+
final List<String> watcherTemplates = Arrays.asList(WatcherIndexTemplateRegistryField.TEMPLATE_NAMES_NO_ILM);
4141
assertBusy(() -> {
4242
try {
4343
getAdminExecutionContext().callApi("watcher.start", emptyMap(), emptyList(), emptyMap());

x-pack/qa/third-party/pagerduty/src/test/java/org/elasticsearch/smoketest/WatcherPagerDutyYamlTestSuiteIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static Iterable<Object[]> parameters() throws Exception {
3737

3838
@Before
3939
public void startWatcher() throws Exception {
40-
final List<String> watcherTemplates = Arrays.asList(WatcherIndexTemplateRegistryField.TEMPLATE_NAMES);
40+
final List<String> watcherTemplates = Arrays.asList(WatcherIndexTemplateRegistryField.TEMPLATE_NAMES_NO_ILM);
4141
assertBusy(() -> {
4242
try {
4343
getAdminExecutionContext().callApi("watcher.start", emptyMap(), emptyList(), emptyMap());

x-pack/qa/third-party/slack/src/test/java/org/elasticsearch/smoketest/WatcherSlackYamlTestSuiteIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static Iterable<Object[]> parameters() throws Exception {
3737

3838
@Before
3939
public void startWatcher() throws Exception {
40-
final List<String> watcherTemplates = Arrays.asList(WatcherIndexTemplateRegistryField.TEMPLATE_NAMES);
40+
final List<String> watcherTemplates = Arrays.asList(WatcherIndexTemplateRegistryField.TEMPLATE_NAMES_NO_ILM);
4141
assertBusy(() -> {
4242
try {
4343
getAdminExecutionContext().callApi("watcher.start", emptyMap(), emptyList(), emptyMap());

0 commit comments

Comments
 (0)