diff --git a/plugin/src/main/java/org/elasticsearch/xpack/XPackPlugin.java b/plugin/src/main/java/org/elasticsearch/xpack/XPackPlugin.java index 18b70d6e3b1..ab8ede57532 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/XPackPlugin.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/XPackPlugin.java @@ -14,6 +14,7 @@ import org.elasticsearch.bootstrap.BootstrapCheck; import org.elasticsearch.client.Client; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; +import org.elasticsearch.cluster.metadata.IndexTemplateMetaData; import org.elasticsearch.cluster.node.DiscoveryNodes; import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.inject.Binder; @@ -466,7 +467,11 @@ public class XPackPlugin extends Plugin implements ScriptPlugin, ActionPlugin, I entries.addAll(machineLearning.getNamedXContent()); entries.addAll(licensing.getNamedXContent()); return entries; + } + @Override + public UnaryOperator> getIndexTemplateMetaDataUpgrader() { + return watcher.getIndexTemplateMetaDataUpgrader(); } public void onIndexModule(IndexModule module) { diff --git a/plugin/src/main/java/org/elasticsearch/xpack/watcher/Watcher.java b/plugin/src/main/java/org/elasticsearch/xpack/watcher/Watcher.java index 6f442b585ca..279b6c3f708 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/watcher/Watcher.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/watcher/Watcher.java @@ -10,6 +10,7 @@ import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionResponse; import org.elasticsearch.cluster.NamedDiff; import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; +import org.elasticsearch.cluster.metadata.IndexTemplateMetaData; import org.elasticsearch.cluster.metadata.MetaData; import org.elasticsearch.cluster.node.DiscoveryNodes; import org.elasticsearch.cluster.service.ClusterService; @@ -165,6 +166,7 @@ import java.util.Set; import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Supplier; +import java.util.function.UnaryOperator; import java.util.stream.Collectors; import static java.util.Collections.emptyList; @@ -505,4 +507,14 @@ public class Watcher implements ActionPlugin, ScriptPlugin { " that any future history indices after 6 months with the pattern " + "[.watcher-history-YYYY.MM.dd] are allowed to be created", value); } + + // These are all old templates from pre 6.0 era, that need to be deleted + public UnaryOperator> getIndexTemplateMetaDataUpgrader() { + return map -> { + map.keySet().removeIf(name -> "watches".equals(name) || "triggered_watches".equals(name) + || name.startsWith("watch_history_")); + + return map; + }; + } } diff --git a/plugin/src/main/java/org/elasticsearch/xpack/watcher/support/WatcherIndexTemplateRegistry.java b/plugin/src/main/java/org/elasticsearch/xpack/watcher/support/WatcherIndexTemplateRegistry.java index 1cc9929e841..2ef828d341f 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/watcher/support/WatcherIndexTemplateRegistry.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/watcher/support/WatcherIndexTemplateRegistry.java @@ -98,22 +98,6 @@ public class WatcherIndexTemplateRegistry extends AbstractComponent implements C } addTemplatesIfMissing(state); - deleteDeprecatedTemplates(state); - } - - /** - * This methods deletes the 5.x watcher index templates - * @param state The current cluster state - */ - private void deleteDeprecatedTemplates(ClusterState state) { - for (ObjectCursor cursor : state.metaData().getTemplates().keys()) { - String name = cursor.value; - if ("watches".equals(name) || "triggered_watches".equals(name) || name.startsWith("watcher_history_")) { - client.deleteTemplate(new DeleteIndexTemplateRequest(name), ActionListener.wrap( - r -> logger.debug("Deleted old index template [{}]", name), - e -> logger.debug("Could not delete watcher template [{}]: [{}]", name, e.getMessage()))); - } - } } void addTemplatesIfMissing(ClusterState state) { diff --git a/plugin/src/test/java/org/elasticsearch/xpack/watcher/OldWatcherIndicesBackwardsCompatibilityTests.java b/plugin/src/test/java/org/elasticsearch/xpack/watcher/OldWatcherIndicesBackwardsCompatibilityTests.java index 7691ce5cf93..512d6931b1a 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/watcher/OldWatcherIndicesBackwardsCompatibilityTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/watcher/OldWatcherIndicesBackwardsCompatibilityTests.java @@ -8,7 +8,9 @@ package org.elasticsearch.xpack.watcher; import org.apache.lucene.util.LuceneTestCase.AwaitsFix; import org.elasticsearch.AbstractOldXPackIndicesBackwardsCompatibilityTestCase; import org.elasticsearch.Version; +import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse; import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.cluster.metadata.IndexTemplateMetaData; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.xpack.XPackSettings; import org.elasticsearch.xpack.common.text.TextTemplate; @@ -31,9 +33,11 @@ import static org.elasticsearch.common.unit.TimeValue.timeValueSeconds; import static org.hamcrest.Matchers.everyItem; import static org.hamcrest.Matchers.greaterThanOrEqualTo; import static org.hamcrest.Matchers.hasEntry; +import static org.hamcrest.Matchers.hasItems; import static org.hamcrest.Matchers.hasKey; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.startsWith; /** * Tests for watcher indexes created before 5.0. @@ -62,6 +66,7 @@ public class OldWatcherIndicesBackwardsCompatibilityTests extends AbstractOldXPa }); try { + assertOldTemplatesAreDeleted(); assertWatchIndexContentsWork(version); assertBasicWatchInteractions(); } finally { @@ -73,6 +78,12 @@ public class OldWatcherIndicesBackwardsCompatibilityTests extends AbstractOldXPa } } + private void assertOldTemplatesAreDeleted() { + GetIndexTemplatesResponse response = client().admin().indices().prepareGetTemplates().get(); + List templateNames = response.getIndexTemplates().stream().map(IndexTemplateMetaData::getName).collect(Collectors.toList()); + assertThat(templateNames, not(hasItems(is("watches"), startsWith("watch-history"), is("triggered_watches")))); + } + void assertWatchIndexContentsWork(Version version) throws Exception { WatcherClient watcherClient = new WatcherClient(client()); diff --git a/plugin/src/test/java/org/elasticsearch/xpack/watcher/support/WatcherIndexTemplateRegistryTests.java b/plugin/src/test/java/org/elasticsearch/xpack/watcher/support/WatcherIndexTemplateRegistryTests.java index 39d0a9c7df5..3594378323e 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/watcher/support/WatcherIndexTemplateRegistryTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/watcher/support/WatcherIndexTemplateRegistryTests.java @@ -5,7 +5,6 @@ */ package org.elasticsearch.xpack.watcher.support; -import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest; import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest; import org.elasticsearch.client.AdminClient; import org.elasticsearch.client.Client; @@ -29,19 +28,14 @@ import org.elasticsearch.xpack.security.InternalClient; import org.junit.Before; import org.mockito.ArgumentCaptor; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; -import java.util.stream.Collectors; import static org.elasticsearch.mock.orig.Mockito.verify; import static org.elasticsearch.mock.orig.Mockito.when; -import static org.hamcrest.Matchers.hasItem; -import static org.hamcrest.Matchers.hasSize; -import static org.hamcrest.Matchers.startsWith; import static org.mockito.Matchers.anyObject; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; @@ -96,52 +90,6 @@ public class WatcherIndexTemplateRegistryTests extends ESTestCase { return event; } - public void testThatDeprecatedTemplatesAreRemovedOnce() throws Exception { - List templateNames = new ArrayList<>(); - - // old index templates to be deleted - boolean containsWatchesTemplate = randomBoolean(); - if (containsWatchesTemplate) { - templateNames.add("watches"); - } - boolean containsTriggeredWatchesTemplates = randomBoolean(); - if (containsTriggeredWatchesTemplates) { - templateNames.add("triggered_watches"); - } - boolean containsVersionedWatchHistoryTemplate = randomBoolean(); - if (containsVersionedWatchHistoryTemplate) { - templateNames.add("watcher_history_" + randomIntBetween(0, 100)); - } - - List templatesInClusterState = new ArrayList<>(); - templatesInClusterState.addAll(Arrays.asList(WatcherIndexTemplateRegistry.HISTORY_TEMPLATE_NAME, - WatcherIndexTemplateRegistry.TRIGGERED_TEMPLATE_NAME, WatcherIndexTemplateRegistry.WATCHES_TEMPLATE_NAME)); - templatesInClusterState.addAll(templateNames); - ClusterChangedEvent event = createClusterChangedEvent(templatesInClusterState); - registry.clusterChanged(event); - - ArgumentCaptor requestArgumentCaptor = ArgumentCaptor.forClass(DeleteIndexTemplateRequest.class); - verify(client, times(templateNames.size())).execute(anyObject(), requestArgumentCaptor.capture(), anyObject()); - assertThat(requestArgumentCaptor.getAllValues(), hasSize(templateNames.size())); - List deletedTemplateNames = requestArgumentCaptor.getAllValues().stream().map(DeleteIndexTemplateRequest::name) - .collect(Collectors.toList()); - if (containsWatchesTemplate) { - assertThat(deletedTemplateNames, hasItem("watches")); - } - if (containsTriggeredWatchesTemplates) { - assertThat(deletedTemplateNames, hasItem("triggered_watches")); - } - if (containsVersionedWatchHistoryTemplate) { - assertThat(deletedTemplateNames, hasItem(startsWith("watcher_history_"))); - } - - // a second event with removed templates should not trigger any further requests, so the invocation count stays the same - ClusterChangedEvent newEvent = createClusterChangedEvent(Arrays.asList(WatcherIndexTemplateRegistry.HISTORY_TEMPLATE_NAME, - WatcherIndexTemplateRegistry.TRIGGERED_TEMPLATE_NAME, WatcherIndexTemplateRegistry.WATCHES_TEMPLATE_NAME)); - registry.clusterChanged(newEvent); - verify(client, times(templateNames.size())).execute(anyObject(), anyObject(), anyObject()); - } - public void testThatNonExistingTemplatesAreAddedImmediately() { ClusterChangedEvent event = createClusterChangedEvent(Collections.emptyList()); registry.clusterChanged(event);