From 233c64e94237b7139ba5f37a990a5c8caf189645 Mon Sep 17 00:00:00 2001 From: Tanguy Leroux Date: Tue, 26 Apr 2016 13:47:59 +0200 Subject: [PATCH] Monitoring: Fix cleaner tests Some changes in elastic/x-pack@d13557c517d8bc35fad6917268ff747f9a70c5ac change the testIgnoreTimestampedIndicesInOtherVersions method and it now sometime fails. This commit revert the previous behavior of the test and ensures that at least 1 index is cleaned up/deleted in each test. Original commit: elastic/x-pack-elasticsearch@3c6acb4ff81a53e6431587dedfa5e587b28b28a6 --- .../resolver/MonitoringIndexNameResolver.java | 14 ++++- .../AbstractExporterTemplateTestCase.java | 10 +-- .../resolver/cluster/ClusterStateTests.java | 6 +- .../AbstractIndicesCleanerTestCase.java | 63 ++++++++++++------- .../marvel/test/MarvelIntegTestCase.java | 12 ++-- 5 files changed, 70 insertions(+), 35 deletions(-) diff --git a/elasticsearch/x-pack/marvel/src/main/java/org/elasticsearch/marvel/agent/resolver/MonitoringIndexNameResolver.java b/elasticsearch/x-pack/marvel/src/main/java/org/elasticsearch/marvel/agent/resolver/MonitoringIndexNameResolver.java index 6bfaf6e8dfd..6b62666a408 100644 --- a/elasticsearch/x-pack/marvel/src/main/java/org/elasticsearch/marvel/agent/resolver/MonitoringIndexNameResolver.java +++ b/elasticsearch/x-pack/marvel/src/main/java/org/elasticsearch/marvel/agent/resolver/MonitoringIndexNameResolver.java @@ -133,7 +133,12 @@ public abstract class MonitoringIndexNameResolver { private final String index; public Data() { - this.index = String.join(DELIMITER, PREFIX, DATA, String.valueOf(MarvelTemplateUtils.TEMPLATE_VERSION)); + this(MarvelTemplateUtils.TEMPLATE_VERSION); + } + + // Used in tests + protected Data(Integer version) { + this.index = String.join(DELIMITER, PREFIX, DATA, String.valueOf(version)); } @Override @@ -171,8 +176,13 @@ public abstract class MonitoringIndexNameResolver { private final String index; public Timestamped(MonitoredSystem system, Settings settings) { + this(system, settings, MarvelTemplateUtils.TEMPLATE_VERSION); + } + + // Used in tests + protected Timestamped(MonitoredSystem system, Settings settings, Integer version) { this.system = system; - this.index = String.join(DELIMITER, PREFIX, system.getSystem(), String.valueOf(MarvelTemplateUtils.TEMPLATE_VERSION)); + this.index = String.join(DELIMITER, PREFIX, system.getSystem(), String.valueOf(version)); String format = INDEX_NAME_TIME_FORMAT_SETTING.get(settings); try { this.formatter = DateTimeFormat.forPattern(format).withZoneUTC(); diff --git a/elasticsearch/x-pack/marvel/src/test/java/org/elasticsearch/marvel/agent/exporter/AbstractExporterTemplateTestCase.java b/elasticsearch/x-pack/marvel/src/test/java/org/elasticsearch/marvel/agent/exporter/AbstractExporterTemplateTestCase.java index 34f49a11013..2986a0f28e3 100644 --- a/elasticsearch/x-pack/marvel/src/test/java/org/elasticsearch/marvel/agent/exporter/AbstractExporterTemplateTestCase.java +++ b/elasticsearch/x-pack/marvel/src/test/java/org/elasticsearch/marvel/agent/exporter/AbstractExporterTemplateTestCase.java @@ -126,17 +126,18 @@ public abstract class AbstractExporterTemplateTestCase extends MarvelIntegTestCa } private String dataTemplateName() { - MockDataIndexNameResolver resolver = new MockDataIndexNameResolver(); + MockDataIndexNameResolver resolver = new MockDataIndexNameResolver(MarvelTemplateUtils.TEMPLATE_VERSION); return resolver.templateName(); } private String indexTemplateName() { - MockTimestampedIndexNameResolver resolver = new MockTimestampedIndexNameResolver(MonitoredSystem.ES, exporterSettings()); + MockTimestampedIndexNameResolver resolver = + new MockTimestampedIndexNameResolver(MonitoredSystem.ES, exporterSettings(), MarvelTemplateUtils.TEMPLATE_VERSION); return resolver.templateName(); } private String currentDataIndexName() { - MockDataIndexNameResolver resolver = new MockDataIndexNameResolver(); + MockDataIndexNameResolver resolver = new MockDataIndexNameResolver(MarvelTemplateUtils.TEMPLATE_VERSION); return resolver.index(null); } @@ -144,7 +145,8 @@ public abstract class AbstractExporterTemplateTestCase extends MarvelIntegTestCa MonitoringDoc doc = new MonitoringDoc(MonitoredSystem.ES.getSystem(), Version.CURRENT.toString()); doc.setTimestamp(System.currentTimeMillis()); - MockTimestampedIndexNameResolver resolver = new MockTimestampedIndexNameResolver(MonitoredSystem.ES, exporterSettings()); + MockTimestampedIndexNameResolver resolver = + new MockTimestampedIndexNameResolver(MonitoredSystem.ES, exporterSettings(), MarvelTemplateUtils.TEMPLATE_VERSION); return resolver.index(doc); } diff --git a/elasticsearch/x-pack/marvel/src/test/java/org/elasticsearch/marvel/agent/resolver/cluster/ClusterStateTests.java b/elasticsearch/x-pack/marvel/src/test/java/org/elasticsearch/marvel/agent/resolver/cluster/ClusterStateTests.java index 9b172ec25e7..037423ed882 100644 --- a/elasticsearch/x-pack/marvel/src/test/java/org/elasticsearch/marvel/agent/resolver/cluster/ClusterStateTests.java +++ b/elasticsearch/x-pack/marvel/src/test/java/org/elasticsearch/marvel/agent/resolver/cluster/ClusterStateTests.java @@ -12,6 +12,7 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.marvel.MonitoringSettings; import org.elasticsearch.marvel.agent.collector.cluster.ClusterStateCollector; +import org.elasticsearch.marvel.agent.exporter.MarvelTemplateUtils; import org.elasticsearch.marvel.agent.resolver.MonitoringIndexNameResolver; import org.elasticsearch.marvel.test.MarvelIntegTestCase; import org.elasticsearch.search.SearchHit; @@ -107,7 +108,8 @@ public class ClusterStateTests extends MarvelIntegTestCase { public void testClusterStateNodes() throws Exception { final long nbNodes = internalCluster().size(); - MonitoringIndexNameResolver.Timestamped timestampedResolver = new MockTimestampedIndexNameResolver(ES); + MonitoringIndexNameResolver.Timestamped timestampedResolver = + new MockTimestampedIndexNameResolver(ES, Settings.EMPTY, MarvelTemplateUtils.TEMPLATE_VERSION); assertNotNull(timestampedResolver); String timestampedIndex = timestampedResolver.indexPattern(); @@ -152,7 +154,7 @@ public class ClusterStateTests extends MarvelIntegTestCase { public void testDiscoveryNodes() throws Exception { final long nbNodes = internalCluster().size(); - MonitoringIndexNameResolver.Data dataResolver = new MockDataIndexNameResolver(); + MonitoringIndexNameResolver.Data dataResolver = new MockDataIndexNameResolver(MarvelTemplateUtils.TEMPLATE_VERSION); assertNotNull(dataResolver); String dataIndex = dataResolver.indexPattern(); diff --git a/elasticsearch/x-pack/marvel/src/test/java/org/elasticsearch/marvel/cleaner/AbstractIndicesCleanerTestCase.java b/elasticsearch/x-pack/marvel/src/test/java/org/elasticsearch/marvel/cleaner/AbstractIndicesCleanerTestCase.java index 90846a2fc53..31c25bc8635 100644 --- a/elasticsearch/x-pack/marvel/src/test/java/org/elasticsearch/marvel/cleaner/AbstractIndicesCleanerTestCase.java +++ b/elasticsearch/x-pack/marvel/src/test/java/org/elasticsearch/marvel/cleaner/AbstractIndicesCleanerTestCase.java @@ -19,8 +19,6 @@ import org.elasticsearch.test.ESIntegTestCase.ClusterScope; import org.elasticsearch.test.VersionUtils; import org.joda.time.DateTime; import org.joda.time.DateTimeZone; -import org.joda.time.format.DateTimeFormat; -import org.joda.time.format.DateTimeFormatter; import java.util.Locale; @@ -59,8 +57,12 @@ public abstract class AbstractIndicesCleanerTestCase extends MarvelIntegTestCase public void testIgnoreCurrentDataIndex() throws Exception { internalCluster().startNode(); + // Will be deleted + createTimestampedIndex(now().minusDays(10)); + + // Won't be deleted createDataIndex(now().minusDays(10)); - assertIndicesCount(1); + assertIndicesCount(2); CleanerService.Listener listener = getListener(); listener.onCleanUpIndices(days(0)); @@ -70,13 +72,14 @@ public abstract class AbstractIndicesCleanerTestCase extends MarvelIntegTestCase public void testIgnoreDataIndicesInOtherVersions() throws Exception { internalCluster().startNode(); - createIndex(MonitoringSettings.LEGACY_DATA_INDEX_NAME, now().minusYears(1)); - createDataIndex(now().minusDays(10)); - String olderIndex = String.join(MonitoringIndexNameResolver.DELIMITER, MonitoringIndexNameResolver.PREFIX, - MonitoringIndexNameResolver.Data.DATA, "1"); - createIndex(olderIndex, now().minusDays(20)); + // Will be deleted + createTimestampedIndex(now().minusDays(10)); - assertIndicesCount(3); + // Won't be deleted + createIndex(MonitoringSettings.LEGACY_DATA_INDEX_NAME, now().minusYears(1)); + createDataIndex(now().minusDays(10), 0); + createDataIndex(now().minusDays(10), 1); + assertIndicesCount(4); CleanerService.Listener listener = getListener(); listener.onCleanUpIndices(days(0)); @@ -86,7 +89,10 @@ public abstract class AbstractIndicesCleanerTestCase extends MarvelIntegTestCase public void testIgnoreCurrentTimestampedIndex() throws Exception { internalCluster().startNode(); + // Will be deleted createTimestampedIndex(now().minusDays(10)); + + // Won't be deleted createTimestampedIndex(now()); assertIndicesCount(2); @@ -98,18 +104,18 @@ public abstract class AbstractIndicesCleanerTestCase extends MarvelIntegTestCase public void testIgnoreTimestampedIndicesInOtherVersions() throws Exception { internalCluster().startNode(); + // Will be deleted createTimestampedIndex(now().minusDays(10)); - // create an older index based on an older template - DateTimeFormatter formatter = DateTimeFormat.forPattern("YYYY.MM.dd").withZoneUTC(); - String index = String.join(MonitoringIndexNameResolver.DELIMITER, MonitoringIndexNameResolver.PREFIX, - MonitoredSystem.ES.getSystem(), "1"); - String timestampedIndex = String.join("-", index, formatter.print(now().minusDays(10))); - createIndex(timestampedIndex, now().minusDays(10)); - assertIndicesCount(2); + + // Won't be deleted + createTimestampedIndex(now().minusDays(10), 0); + createTimestampedIndex(now().minusDays(10), 1); + createTimestampedIndex(now().minusDays(10), Integer.MAX_VALUE); + assertIndicesCount(4); CleanerService.Listener listener = getListener(); listener.onCleanUpIndices(days(0)); - assertIndicesCount(2); + assertIndicesCount(3); } public void testDeleteIndices() throws Exception { @@ -169,7 +175,7 @@ public abstract class AbstractIndicesCleanerTestCase extends MarvelIntegTestCase } protected CleanerService.Listener getListener() { - Exporters exporters = internalCluster().getInstance(Exporters.class); + Exporters exporters = internalCluster().getInstance(Exporters.class, internalCluster().getMasterName()); for (Exporter exporter : exporters) { if (exporter instanceof CleanerService.Listener) { return (CleanerService.Listener) exporter; @@ -186,17 +192,32 @@ public abstract class AbstractIndicesCleanerTestCase extends MarvelIntegTestCase * Creates a monitoring data index in a given version. */ protected void createDataIndex(DateTime creationDate) { - createIndex(new MockDataIndexNameResolver().index(randomMonitoringDoc()), creationDate); + createDataIndex(creationDate, MarvelTemplateUtils.TEMPLATE_VERSION); } /** - * Creates a monitoring timestamped index in a given version. + * Creates a monitoring data index in a given version. + */ + protected void createDataIndex(DateTime creationDate, int version) { + createIndex(new MockDataIndexNameResolver(version).index(randomMonitoringDoc()), creationDate); + } + + /** + * Creates a monitoring timestamped index using the current template version. */ protected void createTimestampedIndex(DateTime creationDate) { + createTimestampedIndex(creationDate, MarvelTemplateUtils.TEMPLATE_VERSION); + } + + /** + * Creates a monitoring timestamped index using a given template version. + */ + protected void createTimestampedIndex(DateTime creationDate, int version) { MonitoringDoc monitoringDoc = randomMonitoringDoc(); monitoringDoc.setTimestamp(creationDate.getMillis()); - MonitoringIndexNameResolver.Timestamped resolver = new MockTimestampedIndexNameResolver(MonitoredSystem.ES); + MonitoringIndexNameResolver.Timestamped resolver = + new MockTimestampedIndexNameResolver(MonitoredSystem.ES, Settings.EMPTY, version); createIndex(resolver.index(monitoringDoc), creationDate); } diff --git a/elasticsearch/x-pack/marvel/src/test/java/org/elasticsearch/marvel/test/MarvelIntegTestCase.java b/elasticsearch/x-pack/marvel/src/test/java/org/elasticsearch/marvel/test/MarvelIntegTestCase.java index c8dd4ec0070..649f26962f3 100644 --- a/elasticsearch/x-pack/marvel/src/test/java/org/elasticsearch/marvel/test/MarvelIntegTestCase.java +++ b/elasticsearch/x-pack/marvel/src/test/java/org/elasticsearch/marvel/test/MarvelIntegTestCase.java @@ -440,6 +440,10 @@ public abstract class MarvelIntegTestCase extends ESIntegTestCase { public class MockDataIndexNameResolver extends MonitoringIndexNameResolver.Data { + public MockDataIndexNameResolver(Integer version) { + super(version); + } + @Override public String type(MonitoringDoc document) { throw new UnsupportedOperationException("MockDataIndexNameResolver does not support resolving type"); @@ -458,12 +462,8 @@ public abstract class MarvelIntegTestCase extends ESIntegTestCase { protected class MockTimestampedIndexNameResolver extends MonitoringIndexNameResolver.Timestamped { - public MockTimestampedIndexNameResolver(MonitoredSystem id, Settings settings) { - super(id, settings); - } - - public MockTimestampedIndexNameResolver(MonitoredSystem id) { - this(id, Settings.EMPTY); + public MockTimestampedIndexNameResolver(MonitoredSystem system, Settings settings, Integer version) { + super(system, settings, version); } @Override