(7.x) Use environment settings instead of state settings for Watcher config (#41158)
Backport of (#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 #41042
This commit is contained in:
parent
ef310886a7
commit
7b190609ab
|
@ -21,7 +21,10 @@ public final class WatcherIndexTemplateRegistryField {
|
|||
public static final String TRIGGERED_TEMPLATE_NAME = ".triggered_watches";
|
||||
public static final String WATCHES_TEMPLATE_NAME = ".watches";
|
||||
public static final String[] TEMPLATE_NAMES = new String[] {
|
||||
HISTORY_TEMPLATE_NAME, TRIGGERED_TEMPLATE_NAME, WATCHES_TEMPLATE_NAME
|
||||
HISTORY_TEMPLATE_NAME, TRIGGERED_TEMPLATE_NAME, WATCHES_TEMPLATE_NAME
|
||||
};
|
||||
public static final String[] TEMPLATE_NAMES_NO_ILM = new String[] {
|
||||
HISTORY_TEMPLATE_NAME_NO_ILM, TRIGGERED_TEMPLATE_NAME, WATCHES_TEMPLATE_NAME
|
||||
};
|
||||
|
||||
private WatcherIndexTemplateRegistryField() {}
|
||||
|
|
|
@ -267,7 +267,7 @@ public class Watcher extends Plugin implements ActionPlugin, ScriptPlugin, Reloa
|
|||
throw new UncheckedIOException(e);
|
||||
}
|
||||
|
||||
new WatcherIndexTemplateRegistry(clusterService, threadPool, client, xContentRegistry);
|
||||
new WatcherIndexTemplateRegistry(environment.settings(), clusterService, threadPool, client, xContentRegistry);
|
||||
|
||||
// http client
|
||||
httpClient = new HttpClient(settings, getSslService(), cryptoService, clusterService);
|
||||
|
|
|
@ -17,6 +17,7 @@ import org.elasticsearch.cluster.ClusterState;
|
|||
import org.elasticsearch.cluster.ClusterStateListener;
|
||||
import org.elasticsearch.cluster.node.DiscoveryNode;
|
||||
import org.elasticsearch.cluster.service.ClusterService;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.unit.TimeValue;
|
||||
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
|
||||
import org.elasticsearch.common.xcontent.XContentType;
|
||||
|
@ -63,14 +64,17 @@ public class WatcherIndexTemplateRegistry implements ClusterStateListener {
|
|||
|
||||
private static final Logger logger = LogManager.getLogger(WatcherIndexTemplateRegistry.class);
|
||||
|
||||
private final Settings nodeSettings;
|
||||
private final Client client;
|
||||
private final ThreadPool threadPool;
|
||||
private final NamedXContentRegistry xContentRegistry;
|
||||
private final ConcurrentMap<String, AtomicBoolean> templateCreationsInProgress = new ConcurrentHashMap<>();
|
||||
private final AtomicBoolean historyPolicyCreationInProgress = new AtomicBoolean();
|
||||
|
||||
public WatcherIndexTemplateRegistry(ClusterService clusterService, ThreadPool threadPool, Client client,
|
||||
public WatcherIndexTemplateRegistry(Settings nodeSettings, ClusterService clusterService,
|
||||
ThreadPool threadPool, Client client,
|
||||
NamedXContentRegistry xContentRegistry) {
|
||||
this.nodeSettings = nodeSettings;
|
||||
this.client = client;
|
||||
this.threadPool = threadPool;
|
||||
this.xContentRegistry = xContentRegistry;
|
||||
|
@ -104,7 +108,7 @@ public class WatcherIndexTemplateRegistry implements ClusterStateListener {
|
|||
}
|
||||
|
||||
private void addTemplatesIfMissing(ClusterState state) {
|
||||
boolean ilmSupported = XPackSettings.INDEX_LIFECYCLE_ENABLED.get(state.metaData().settings());
|
||||
boolean ilmSupported = XPackSettings.INDEX_LIFECYCLE_ENABLED.get(this.nodeSettings);
|
||||
final TemplateConfig[] indexTemplates = ilmSupported ? TEMPLATE_CONFIGS : TEMPLATE_CONFIGS_NO_ILM;
|
||||
for (TemplateConfig template : indexTemplates) {
|
||||
final String templateName = template.getTemplateName();
|
||||
|
@ -153,7 +157,7 @@ public class WatcherIndexTemplateRegistry implements ClusterStateListener {
|
|||
}
|
||||
|
||||
private void addIndexLifecyclePolicyIfMissing(ClusterState state) {
|
||||
boolean ilmSupported = XPackSettings.INDEX_LIFECYCLE_ENABLED.get(state.metaData().settings());
|
||||
boolean ilmSupported = XPackSettings.INDEX_LIFECYCLE_ENABLED.get(this.nodeSettings);
|
||||
if (ilmSupported && historyPolicyCreationInProgress.compareAndSet(false, true)) {
|
||||
final LifecyclePolicy policyOnDisk = loadWatcherHistoryPolicy();
|
||||
|
||||
|
|
|
@ -73,11 +73,13 @@ public class WatcherIndexTemplateRegistryTests extends ESTestCase {
|
|||
|
||||
private WatcherIndexTemplateRegistry registry;
|
||||
private NamedXContentRegistry xContentRegistry;
|
||||
private ClusterService clusterService;
|
||||
private ThreadPool threadPool;
|
||||
private Client client;
|
||||
|
||||
@Before
|
||||
public void createRegistryAndClient() {
|
||||
ThreadPool threadPool = mock(ThreadPool.class);
|
||||
threadPool = mock(ThreadPool.class);
|
||||
when(threadPool.getThreadContext()).thenReturn(new ThreadContext(Settings.EMPTY));
|
||||
when(threadPool.generic()).thenReturn(EsExecutors.newDirectExecutorService());
|
||||
|
||||
|
@ -94,14 +96,14 @@ public class WatcherIndexTemplateRegistryTests extends ESTestCase {
|
|||
return null;
|
||||
}).when(indicesAdminClient).putTemplate(any(PutIndexTemplateRequest.class), any(ActionListener.class));
|
||||
|
||||
ClusterService clusterService = mock(ClusterService.class);
|
||||
clusterService = mock(ClusterService.class);
|
||||
List<NamedXContentRegistry.Entry> entries = new ArrayList<>(ClusterModule.getNamedXWriteables());
|
||||
entries.addAll(Arrays.asList(
|
||||
new NamedXContentRegistry.Entry(LifecycleType.class, new ParseField(TimeseriesLifecycleType.TYPE),
|
||||
(p) -> TimeseriesLifecycleType.INSTANCE),
|
||||
new NamedXContentRegistry.Entry(LifecycleAction.class, new ParseField(DeleteAction.NAME), DeleteAction::parse)));
|
||||
xContentRegistry = new NamedXContentRegistry(entries);
|
||||
registry = new WatcherIndexTemplateRegistry(clusterService, threadPool, client, xContentRegistry);
|
||||
registry = new WatcherIndexTemplateRegistry(Settings.EMPTY, clusterService, threadPool, client, xContentRegistry);
|
||||
}
|
||||
|
||||
public void testThatNonExistingTemplatesAreAddedImmediately() {
|
||||
|
@ -130,9 +132,10 @@ public class WatcherIndexTemplateRegistryTests extends ESTestCase {
|
|||
DiscoveryNode node = new DiscoveryNode("node", ESTestCase.buildNewFakeTransportAddress(), Version.CURRENT);
|
||||
DiscoveryNodes nodes = DiscoveryNodes.builder().localNodeId("node").masterNodeId("node").add(node).build();
|
||||
|
||||
ClusterChangedEvent event = createClusterChangedEvent(Settings.builder()
|
||||
registry = new WatcherIndexTemplateRegistry(Settings.builder()
|
||||
.put(XPackSettings.INDEX_LIFECYCLE_ENABLED.getKey(), false).build(),
|
||||
Collections.emptyList(), Collections.emptyMap(), nodes);
|
||||
clusterService, threadPool, client, xContentRegistry);
|
||||
ClusterChangedEvent event = createClusterChangedEvent(Settings.EMPTY, Collections.emptyList(), Collections.emptyMap(), nodes);
|
||||
registry.clusterChanged(event);
|
||||
ArgumentCaptor<PutIndexTemplateRequest> argumentCaptor = ArgumentCaptor.forClass(PutIndexTemplateRequest.class);
|
||||
verify(client.admin().indices(), times(3)).putTemplate(argumentCaptor.capture(), anyObject());
|
||||
|
@ -142,8 +145,9 @@ public class WatcherIndexTemplateRegistryTests extends ESTestCase {
|
|||
WatcherIndexTemplateRegistryField.TRIGGERED_TEMPLATE_NAME), nodes);
|
||||
registry.clusterChanged(newEvent);
|
||||
ArgumentCaptor<PutIndexTemplateRequest> captor = ArgumentCaptor.forClass(PutIndexTemplateRequest.class);
|
||||
verify(client.admin().indices(), times(4)).putTemplate(captor.capture(), anyObject());
|
||||
verify(client.admin().indices(), times(5)).putTemplate(captor.capture(), anyObject());
|
||||
captor.getAllValues().forEach(req -> assertNull(req.settings().get("index.lifecycle.name")));
|
||||
verify(client, times(0)).execute(eq(PutLifecycleAction.INSTANCE), anyObject(), anyObject());
|
||||
}
|
||||
|
||||
public void testThatNonExistingPoliciesAreAddedImmediately() {
|
||||
|
@ -171,9 +175,10 @@ public class WatcherIndexTemplateRegistryTests extends ESTestCase {
|
|||
DiscoveryNode node = new DiscoveryNode("node", ESTestCase.buildNewFakeTransportAddress(), Version.CURRENT);
|
||||
DiscoveryNodes nodes = DiscoveryNodes.builder().localNodeId("node").masterNodeId("node").add(node).build();
|
||||
|
||||
ClusterChangedEvent event = createClusterChangedEvent(Settings.builder()
|
||||
.put(XPackSettings.INDEX_LIFECYCLE_ENABLED.getKey(), false).build(),
|
||||
Collections.emptyList(), Collections.emptyMap(), nodes);
|
||||
registry = new WatcherIndexTemplateRegistry(Settings.builder()
|
||||
.put(XPackSettings.INDEX_LIFECYCLE_ENABLED.getKey(), false).build(),
|
||||
clusterService, threadPool, client, xContentRegistry);
|
||||
ClusterChangedEvent event = createClusterChangedEvent(Settings.EMPTY, Collections.emptyList(), Collections.emptyMap(), nodes);
|
||||
registry.clusterChanged(event);
|
||||
verify(client, times(0)).execute(eq(PutLifecycleAction.INSTANCE), anyObject(), anyObject());
|
||||
}
|
||||
|
|
|
@ -76,7 +76,7 @@ public class SmokeTestWatcherWithSecurityClientYamlTestSuiteIT extends ESClientY
|
|||
});
|
||||
|
||||
assertBusy(() -> {
|
||||
for (String template : WatcherIndexTemplateRegistryField.TEMPLATE_NAMES) {
|
||||
for (String template : WatcherIndexTemplateRegistryField.TEMPLATE_NAMES_NO_ILM) {
|
||||
ClientYamlTestResponse templateExistsResponse = getAdminExecutionContext().callApi("indices.exists_template",
|
||||
singletonMap("name", template), emptyList(), emptyMap());
|
||||
assertThat(templateExistsResponse.getStatusCode(), is(200));
|
||||
|
|
|
@ -63,7 +63,7 @@ public class SmokeTestWatcherTestSuiteIT extends ESRestTestCase {
|
|||
});
|
||||
|
||||
assertBusy(() -> {
|
||||
for (String template : WatcherIndexTemplateRegistryField.TEMPLATE_NAMES) {
|
||||
for (String template : WatcherIndexTemplateRegistryField.TEMPLATE_NAMES_NO_ILM) {
|
||||
Response templateExistsResponse = adminClient().performRequest(new Request("HEAD", "/_template/" + template));
|
||||
assertThat(templateExistsResponse.getStatusLine().getStatusCode(), is(200));
|
||||
}
|
||||
|
|
|
@ -58,7 +58,7 @@ public class WatcherRestIT extends ESClientYamlSuiteTestCase {
|
|||
});
|
||||
|
||||
assertBusy(() -> {
|
||||
for (String template : WatcherIndexTemplateRegistryField.TEMPLATE_NAMES) {
|
||||
for (String template : WatcherIndexTemplateRegistryField.TEMPLATE_NAMES_NO_ILM) {
|
||||
ClientYamlTestResponse templateExistsResponse = getAdminExecutionContext().callApi("indices.exists_template",
|
||||
singletonMap("name", template), emptyList(), emptyMap());
|
||||
assertThat(templateExistsResponse.getStatusCode(), is(200));
|
||||
|
|
|
@ -37,7 +37,7 @@ public class WatcherJiraYamlTestSuiteIT extends ESClientYamlSuiteTestCase {
|
|||
|
||||
@Before
|
||||
public void startWatcher() throws Exception {
|
||||
final List<String> watcherTemplates = Arrays.asList(WatcherIndexTemplateRegistryField.TEMPLATE_NAMES);
|
||||
final List<String> watcherTemplates = Arrays.asList(WatcherIndexTemplateRegistryField.TEMPLATE_NAMES_NO_ILM);
|
||||
assertBusy(() -> {
|
||||
try {
|
||||
getAdminExecutionContext().callApi("watcher.start", emptyMap(), emptyList(), emptyMap());
|
||||
|
|
|
@ -37,7 +37,7 @@ public class WatcherPagerDutyYamlTestSuiteIT extends ESClientYamlSuiteTestCase {
|
|||
|
||||
@Before
|
||||
public void startWatcher() throws Exception {
|
||||
final List<String> watcherTemplates = Arrays.asList(WatcherIndexTemplateRegistryField.TEMPLATE_NAMES);
|
||||
final List<String> watcherTemplates = Arrays.asList(WatcherIndexTemplateRegistryField.TEMPLATE_NAMES_NO_ILM);
|
||||
assertBusy(() -> {
|
||||
try {
|
||||
getAdminExecutionContext().callApi("watcher.start", emptyMap(), emptyList(), emptyMap());
|
||||
|
|
|
@ -37,7 +37,7 @@ public class WatcherSlackYamlTestSuiteIT extends ESClientYamlSuiteTestCase {
|
|||
|
||||
@Before
|
||||
public void startWatcher() throws Exception {
|
||||
final List<String> watcherTemplates = Arrays.asList(WatcherIndexTemplateRegistryField.TEMPLATE_NAMES);
|
||||
final List<String> watcherTemplates = Arrays.asList(WatcherIndexTemplateRegistryField.TEMPLATE_NAMES_NO_ILM);
|
||||
assertBusy(() -> {
|
||||
try {
|
||||
getAdminExecutionContext().callApi("watcher.start", emptyMap(), emptyList(), emptyMap());
|
||||
|
|
Loading…
Reference in New Issue