Watcher: Use existing plugin hook to delete all old templates (elastic/x-pack-elasticsearch#1425)
This commit ensures the old 5.x index templates are removed using the existing plugin hook, instead of the self written part. Original commit: elastic/x-pack-elasticsearch@6faf08d98d
This commit is contained in:
parent
55359433ae
commit
3f68b4facd
|
@ -14,6 +14,7 @@ import org.elasticsearch.bootstrap.BootstrapCheck;
|
||||||
import org.elasticsearch.client.Client;
|
import org.elasticsearch.client.Client;
|
||||||
import org.elasticsearch.client.transport.TransportClient;
|
import org.elasticsearch.client.transport.TransportClient;
|
||||||
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
|
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
|
||||||
|
import org.elasticsearch.cluster.metadata.IndexTemplateMetaData;
|
||||||
import org.elasticsearch.cluster.node.DiscoveryNodes;
|
import org.elasticsearch.cluster.node.DiscoveryNodes;
|
||||||
import org.elasticsearch.cluster.service.ClusterService;
|
import org.elasticsearch.cluster.service.ClusterService;
|
||||||
import org.elasticsearch.common.inject.Binder;
|
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(machineLearning.getNamedXContent());
|
||||||
entries.addAll(licensing.getNamedXContent());
|
entries.addAll(licensing.getNamedXContent());
|
||||||
return entries;
|
return entries;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public UnaryOperator<Map<String, IndexTemplateMetaData>> getIndexTemplateMetaDataUpgrader() {
|
||||||
|
return watcher.getIndexTemplateMetaDataUpgrader();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onIndexModule(IndexModule module) {
|
public void onIndexModule(IndexModule module) {
|
||||||
|
|
|
@ -10,6 +10,7 @@ import org.elasticsearch.action.ActionRequest;
|
||||||
import org.elasticsearch.action.ActionResponse;
|
import org.elasticsearch.action.ActionResponse;
|
||||||
import org.elasticsearch.cluster.NamedDiff;
|
import org.elasticsearch.cluster.NamedDiff;
|
||||||
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
|
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
|
||||||
|
import org.elasticsearch.cluster.metadata.IndexTemplateMetaData;
|
||||||
import org.elasticsearch.cluster.metadata.MetaData;
|
import org.elasticsearch.cluster.metadata.MetaData;
|
||||||
import org.elasticsearch.cluster.node.DiscoveryNodes;
|
import org.elasticsearch.cluster.node.DiscoveryNodes;
|
||||||
import org.elasticsearch.cluster.service.ClusterService;
|
import org.elasticsearch.cluster.service.ClusterService;
|
||||||
|
@ -165,6 +166,7 @@ import java.util.Set;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
import java.util.function.UnaryOperator;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import static java.util.Collections.emptyList;
|
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 " +
|
" that any future history indices after 6 months with the pattern " +
|
||||||
"[.watcher-history-YYYY.MM.dd] are allowed to be created", value);
|
"[.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<Map<String, IndexTemplateMetaData>> getIndexTemplateMetaDataUpgrader() {
|
||||||
|
return map -> {
|
||||||
|
map.keySet().removeIf(name -> "watches".equals(name) || "triggered_watches".equals(name)
|
||||||
|
|| name.startsWith("watch_history_"));
|
||||||
|
|
||||||
|
return map;
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -98,22 +98,6 @@ public class WatcherIndexTemplateRegistry extends AbstractComponent implements C
|
||||||
}
|
}
|
||||||
|
|
||||||
addTemplatesIfMissing(state);
|
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<String> 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) {
|
void addTemplatesIfMissing(ClusterState state) {
|
||||||
|
|
|
@ -8,7 +8,9 @@ package org.elasticsearch.xpack.watcher;
|
||||||
import org.apache.lucene.util.LuceneTestCase.AwaitsFix;
|
import org.apache.lucene.util.LuceneTestCase.AwaitsFix;
|
||||||
import org.elasticsearch.AbstractOldXPackIndicesBackwardsCompatibilityTestCase;
|
import org.elasticsearch.AbstractOldXPackIndicesBackwardsCompatibilityTestCase;
|
||||||
import org.elasticsearch.Version;
|
import org.elasticsearch.Version;
|
||||||
|
import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse;
|
||||||
import org.elasticsearch.action.search.SearchResponse;
|
import org.elasticsearch.action.search.SearchResponse;
|
||||||
|
import org.elasticsearch.cluster.metadata.IndexTemplateMetaData;
|
||||||
import org.elasticsearch.common.settings.Settings;
|
import org.elasticsearch.common.settings.Settings;
|
||||||
import org.elasticsearch.xpack.XPackSettings;
|
import org.elasticsearch.xpack.XPackSettings;
|
||||||
import org.elasticsearch.xpack.common.text.TextTemplate;
|
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.everyItem;
|
||||||
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
|
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
|
||||||
import static org.hamcrest.Matchers.hasEntry;
|
import static org.hamcrest.Matchers.hasEntry;
|
||||||
|
import static org.hamcrest.Matchers.hasItems;
|
||||||
import static org.hamcrest.Matchers.hasKey;
|
import static org.hamcrest.Matchers.hasKey;
|
||||||
import static org.hamcrest.Matchers.is;
|
import static org.hamcrest.Matchers.is;
|
||||||
import static org.hamcrest.Matchers.not;
|
import static org.hamcrest.Matchers.not;
|
||||||
|
import static org.hamcrest.Matchers.startsWith;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for watcher indexes created before 5.0.
|
* Tests for watcher indexes created before 5.0.
|
||||||
|
@ -62,6 +66,7 @@ public class OldWatcherIndicesBackwardsCompatibilityTests extends AbstractOldXPa
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
assertOldTemplatesAreDeleted();
|
||||||
assertWatchIndexContentsWork(version);
|
assertWatchIndexContentsWork(version);
|
||||||
assertBasicWatchInteractions();
|
assertBasicWatchInteractions();
|
||||||
} finally {
|
} finally {
|
||||||
|
@ -73,6 +78,12 @@ public class OldWatcherIndicesBackwardsCompatibilityTests extends AbstractOldXPa
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void assertOldTemplatesAreDeleted() {
|
||||||
|
GetIndexTemplatesResponse response = client().admin().indices().prepareGetTemplates().get();
|
||||||
|
List<String> 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 {
|
void assertWatchIndexContentsWork(Version version) throws Exception {
|
||||||
WatcherClient watcherClient = new WatcherClient(client());
|
WatcherClient watcherClient = new WatcherClient(client());
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,6 @@
|
||||||
*/
|
*/
|
||||||
package org.elasticsearch.xpack.watcher.support;
|
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.action.admin.indices.template.put.PutIndexTemplateRequest;
|
||||||
import org.elasticsearch.client.AdminClient;
|
import org.elasticsearch.client.AdminClient;
|
||||||
import org.elasticsearch.client.Client;
|
import org.elasticsearch.client.Client;
|
||||||
|
@ -29,19 +28,14 @@ import org.elasticsearch.xpack.security.InternalClient;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.mockito.ArgumentCaptor;
|
import org.mockito.ArgumentCaptor;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
import static org.elasticsearch.mock.orig.Mockito.verify;
|
import static org.elasticsearch.mock.orig.Mockito.verify;
|
||||||
import static org.elasticsearch.mock.orig.Mockito.when;
|
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.anyObject;
|
||||||
import static org.mockito.Matchers.eq;
|
import static org.mockito.Matchers.eq;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
|
@ -96,52 +90,6 @@ public class WatcherIndexTemplateRegistryTests extends ESTestCase {
|
||||||
return event;
|
return event;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testThatDeprecatedTemplatesAreRemovedOnce() throws Exception {
|
|
||||||
List<String> 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<String> 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<DeleteIndexTemplateRequest> requestArgumentCaptor = ArgumentCaptor.forClass(DeleteIndexTemplateRequest.class);
|
|
||||||
verify(client, times(templateNames.size())).execute(anyObject(), requestArgumentCaptor.capture(), anyObject());
|
|
||||||
assertThat(requestArgumentCaptor.getAllValues(), hasSize(templateNames.size()));
|
|
||||||
List<String> 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() {
|
public void testThatNonExistingTemplatesAreAddedImmediately() {
|
||||||
ClusterChangedEvent event = createClusterChangedEvent(Collections.emptyList());
|
ClusterChangedEvent event = createClusterChangedEvent(Collections.emptyList());
|
||||||
registry.clusterChanged(event);
|
registry.clusterChanged(event);
|
||||||
|
|
Loading…
Reference in New Issue