Skip to content

Commit

Permalink
Use environment settings instead of state settings for Watcher config (
Browse files Browse the repository at this point in the history
…#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

* Rename settings object to nodeSettings

* Use correct template list in WatcherRestIT

* Use correct template list in other tests
  • Loading branch information
dakrone authored Apr 12, 2019
1 parent eb81855 commit 2106c8a
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ public Collection<Object> createComponents(Client client, ClusterService cluster
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -104,7 +108,7 @@ public void clusterChanged(ClusterChangedEvent event) {
}

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();
Expand Down Expand Up @@ -153,7 +157,7 @@ LifecyclePolicy loadWatcherHistoryPolicy() {
}

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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand All @@ -94,14 +96,14 @@ public void createRegistryAndClient() {
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() {
Expand Down Expand Up @@ -130,9 +132,10 @@ public void testThatNonExistingTemplatesAreAddedEvenWithILMDisabled() {
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());
Expand All @@ -142,8 +145,9 @@ public void testThatNonExistingTemplatesAreAddedEvenWithILMDisabled() {
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() {
Expand Down Expand Up @@ -171,9 +175,10 @@ public void testNoPolicyButILMDisabled() {
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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void startWatcher() throws Exception {
});

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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void startWatcher() throws Exception {
});

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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void startWatcher() throws Exception {
});

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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static Iterable<Object[]> parameters() throws Exception {

@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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static Iterable<Object[]> parameters() throws Exception {

@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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static Iterable<Object[]> parameters() throws Exception {

@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());
Expand Down

0 comments on commit 2106c8a

Please sign in to comment.