diff --git a/server/src/main/java/org/elasticsearch/cluster/metadata/MetaDataIndexUpgradeService.java b/server/src/main/java/org/elasticsearch/cluster/metadata/MetaDataIndexUpgradeService.java index 72fdea8e609..59cea794f1f 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/MetaDataIndexUpgradeService.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/MetaDataIndexUpgradeService.java @@ -38,11 +38,9 @@ import org.elasticsearch.indices.mapper.MapperRegistry; import org.elasticsearch.script.ScriptService; import java.util.AbstractMap; -import java.util.Collection; import java.util.Collections; import java.util.Map; import java.util.Set; -import java.util.function.UnaryOperator; /** * This service is responsible for upgrading legacy index metadata to the current version @@ -60,22 +58,13 @@ public class MetaDataIndexUpgradeService { private final NamedXContentRegistry xContentRegistry; private final MapperRegistry mapperRegistry; private final IndexScopedSettings indexScopedSettings; - private final UnaryOperator upgraders; public MetaDataIndexUpgradeService(Settings settings, NamedXContentRegistry xContentRegistry, MapperRegistry mapperRegistry, - IndexScopedSettings indexScopedSettings, - Collection> indexMetaDataUpgraders) { + IndexScopedSettings indexScopedSettings) { this.settings = settings; this.xContentRegistry = xContentRegistry; this.mapperRegistry = mapperRegistry; this.indexScopedSettings = indexScopedSettings; - this.upgraders = indexMetaData -> { - IndexMetaData newIndexMetaData = indexMetaData; - for (UnaryOperator upgrader : indexMetaDataUpgraders) { - newIndexMetaData = upgrader.apply(newIndexMetaData); - } - return newIndexMetaData; - }; } /** @@ -95,14 +84,11 @@ public class MetaDataIndexUpgradeService { return archiveBrokenIndexSettings(indexMetaData); } checkSupportedVersion(indexMetaData, minimumIndexCompatibilityVersion); - IndexMetaData newMetaData = indexMetaData; // we have to run this first otherwise in we try to create IndexSettings // with broken settings and fail in checkMappingsCompatibility - newMetaData = archiveBrokenIndexSettings(newMetaData); + final IndexMetaData newMetaData = archiveBrokenIndexSettings(indexMetaData); // only run the check with the upgraded settings!! checkMappingsCompatibility(newMetaData); - // apply plugin checks - newMetaData = upgraders.apply(newMetaData); return markAsUpgraded(newMetaData); } diff --git a/server/src/main/java/org/elasticsearch/gateway/GatewayMetaState.java b/server/src/main/java/org/elasticsearch/gateway/GatewayMetaState.java index 35b22968459..daaaa7684f8 100644 --- a/server/src/main/java/org/elasticsearch/gateway/GatewayMetaState.java +++ b/server/src/main/java/org/elasticsearch/gateway/GatewayMetaState.java @@ -33,6 +33,7 @@ import org.elasticsearch.cluster.ClusterStateApplier; import org.elasticsearch.cluster.coordination.CoordinationState.PersistedState; import org.elasticsearch.cluster.coordination.InMemoryPersistedState; import org.elasticsearch.cluster.metadata.IndexMetaData; +import org.elasticsearch.cluster.metadata.IndexTemplateMetaData; import org.elasticsearch.cluster.metadata.Manifest; import org.elasticsearch.cluster.metadata.MetaData; import org.elasticsearch.cluster.metadata.MetaDataIndexUpgradeService; @@ -213,7 +214,6 @@ public class GatewayMetaState { * Elasticsearch 2.0 removed several deprecated features and as well as support for Lucene 3.x. This method calls * {@link MetaDataIndexUpgradeService} to makes sure that indices are compatible with the current version. The * MetaDataIndexUpgradeService might also update obsolete settings if needed. - * Allows upgrading global custom meta data via {@link MetaDataUpgrader#customMetaDataUpgraders} * * @return input metaData if no upgrade is needed or an upgraded metaData */ @@ -229,11 +229,6 @@ public class GatewayMetaState { changed |= indexMetaData != newMetaData; upgradedMetaData.put(newMetaData, false); } - // upgrade global custom meta data - if (applyPluginUpgraders(metaData.getCustoms(), metaDataUpgrader.customMetaDataUpgraders, - upgradedMetaData::removeCustom, upgradedMetaData::putCustom)) { - changed = true; - } // upgrade current templates if (applyPluginUpgraders(metaData.getTemplates(), metaDataUpgrader.indexTemplateMetaDataUpgraders, upgradedMetaData::removeTemplate, (s, indexTemplateMetaData) -> upgradedMetaData.put(indexTemplateMetaData))) { @@ -242,21 +237,21 @@ public class GatewayMetaState { return changed ? upgradedMetaData.build() : metaData; } - private static boolean applyPluginUpgraders(ImmutableOpenMap existingData, - UnaryOperator> upgrader, - Consumer removeData, - BiConsumer putData) { + private static boolean applyPluginUpgraders(ImmutableOpenMap existingData, + UnaryOperator> upgrader, + Consumer removeData, + BiConsumer putData) { // collect current data - Map existingMap = new HashMap<>(); - for (ObjectObjectCursor customCursor : existingData) { + Map existingMap = new HashMap<>(); + for (ObjectObjectCursor customCursor : existingData) { existingMap.put(customCursor.key, customCursor.value); } // upgrade global custom meta data - Map upgradedCustoms = upgrader.apply(existingMap); + Map upgradedCustoms = upgrader.apply(existingMap); if (upgradedCustoms.equals(existingMap) == false) { // remove all data first so a plugin can remove custom metadata or templates if needed existingMap.keySet().forEach(removeData); - for (Map.Entry upgradedCustomEntry : upgradedCustoms.entrySet()) { + for (Map.Entry upgradedCustomEntry : upgradedCustoms.entrySet()) { putData.accept(upgradedCustomEntry.getKey(), upgradedCustomEntry.getValue()); } return true; diff --git a/server/src/main/java/org/elasticsearch/node/Node.java b/server/src/main/java/org/elasticsearch/node/Node.java index feb35a91283..f30750017fe 100644 --- a/server/src/main/java/org/elasticsearch/node/Node.java +++ b/server/src/main/java/org/elasticsearch/node/Node.java @@ -47,7 +47,6 @@ import org.elasticsearch.cluster.InternalClusterInfoService; import org.elasticsearch.cluster.NodeConnectionsService; import org.elasticsearch.cluster.action.index.MappingUpdatedAction; import org.elasticsearch.cluster.metadata.AliasValidator; -import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.cluster.metadata.IndexTemplateMetaData; import org.elasticsearch.cluster.metadata.MetaData; import org.elasticsearch.cluster.metadata.MetaDataCreateIndexService; @@ -461,19 +460,13 @@ public class Node implements Closeable { final NetworkModule networkModule = new NetworkModule(settings, false, pluginsService.filterPlugins(NetworkPlugin.class), threadPool, bigArrays, pageCacheRecycler, circuitBreakerService, namedWriteableRegistry, xContentRegistry, networkService, restController); - Collection>> customMetaDataUpgraders = - pluginsService.filterPlugins(Plugin.class).stream() - .map(Plugin::getCustomMetaDataUpgrader) - .collect(Collectors.toList()); Collection>> indexTemplateMetaDataUpgraders = pluginsService.filterPlugins(Plugin.class).stream() .map(Plugin::getIndexTemplateMetaDataUpgrader) .collect(Collectors.toList()); - Collection> indexMetaDataUpgraders = pluginsService.filterPlugins(Plugin.class).stream() - .map(Plugin::getIndexMetaDataUpgrader).collect(Collectors.toList()); - final MetaDataUpgrader metaDataUpgrader = new MetaDataUpgrader(customMetaDataUpgraders, indexTemplateMetaDataUpgraders); + final MetaDataUpgrader metaDataUpgrader = new MetaDataUpgrader(indexTemplateMetaDataUpgraders); final MetaDataIndexUpgradeService metaDataIndexUpgradeService = new MetaDataIndexUpgradeService(settings, xContentRegistry, - indicesModule.getMapperRegistry(), settingsModule.getIndexScopedSettings(), indexMetaDataUpgraders); + indicesModule.getMapperRegistry(), settingsModule.getIndexScopedSettings()); new TemplateUpgradeService(client, clusterService, threadPool, indexTemplateMetaDataUpgraders); final Transport transport = networkModule.getTransportSupplier().get(); Set taskHeaders = Stream.concat( diff --git a/server/src/main/java/org/elasticsearch/plugins/MetaDataUpgrader.java b/server/src/main/java/org/elasticsearch/plugins/MetaDataUpgrader.java index 49dd91901d7..5eeca9b45bf 100644 --- a/server/src/main/java/org/elasticsearch/plugins/MetaDataUpgrader.java +++ b/server/src/main/java/org/elasticsearch/plugins/MetaDataUpgrader.java @@ -31,20 +31,9 @@ import java.util.function.UnaryOperator; * Upgrades {@link MetaData} on startup on behalf of installed {@link Plugin}s */ public class MetaDataUpgrader { - public final UnaryOperator> customMetaDataUpgraders; - public final UnaryOperator> indexTemplateMetaDataUpgraders; - public MetaDataUpgrader(Collection>> customMetaDataUpgraders, - Collection>> indexTemplateMetaDataUpgraders) { - this.customMetaDataUpgraders = customs -> { - Map upgradedCustoms = new HashMap<>(customs); - for (UnaryOperator> customMetaDataUpgrader : customMetaDataUpgraders) { - upgradedCustoms = customMetaDataUpgrader.apply(upgradedCustoms); - } - return upgradedCustoms; - }; - + public MetaDataUpgrader(Collection>> indexTemplateMetaDataUpgraders) { this.indexTemplateMetaDataUpgraders = templates -> { Map upgradedTemplates = new HashMap<>(templates); for (UnaryOperator> upgrader : indexTemplateMetaDataUpgraders) { diff --git a/server/src/main/java/org/elasticsearch/plugins/Plugin.java b/server/src/main/java/org/elasticsearch/plugins/Plugin.java index 06ef7d5a679..a1d98365273 100644 --- a/server/src/main/java/org/elasticsearch/plugins/Plugin.java +++ b/server/src/main/java/org/elasticsearch/plugins/Plugin.java @@ -22,7 +22,6 @@ package org.elasticsearch.plugins; import org.elasticsearch.bootstrap.BootstrapCheck; import org.elasticsearch.client.Client; import org.elasticsearch.cluster.ClusterState; -import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.cluster.metadata.IndexTemplateMetaData; import org.elasticsearch.cluster.metadata.MetaData; import org.elasticsearch.cluster.node.DiscoveryNodeRole; @@ -172,22 +171,6 @@ public abstract class Plugin implements Closeable { return Collections.emptyList(); } - /** - * Provides a function to modify global custom meta data on startup. - *

- * Plugins should return the input custom map via {@link UnaryOperator#identity()} if no upgrade is required. - *

- * The order of custom meta data upgraders calls is undefined and can change between runs so, it is expected that - * plugins will modify only data owned by them to avoid conflicts. - *

- * @return Never {@code null}. The same or upgraded {@code MetaData.Custom} map. - * @throws IllegalStateException if the node should not start because at least one {@code MetaData.Custom} - * is unsupported - */ - public UnaryOperator> getCustomMetaDataUpgrader() { - return UnaryOperator.identity(); - } - /** * Provides a function to modify index template meta data on startup. *

@@ -204,21 +187,6 @@ public abstract class Plugin implements Closeable { return UnaryOperator.identity(); } - /** - * Provides a function to modify index meta data when an index is introduced into the cluster state for the first time. - *

- * Plugins should return the input index metadata via {@link UnaryOperator#identity()} if no upgrade is required. - *

- * The order of the index upgrader calls for the same index is undefined and can change between runs so, it is expected that - * plugins will modify only indices owned by them to avoid conflicts. - *

- * @return Never {@code null}. The same or upgraded {@code IndexMetaData}. - * @throws IllegalStateException if the node should not start because the index is unsupported - */ - public UnaryOperator getIndexMetaDataUpgrader() { - return UnaryOperator.identity(); - } - /** * Provides the list of this plugin's custom thread pools, empty if * none. diff --git a/server/src/test/java/org/elasticsearch/cluster/metadata/MetaDataIndexUpgradeServiceTests.java b/server/src/test/java/org/elasticsearch/cluster/metadata/MetaDataIndexUpgradeServiceTests.java index 50166bd42b3..fd5cb54cb34 100644 --- a/server/src/test/java/org/elasticsearch/cluster/metadata/MetaDataIndexUpgradeServiceTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/metadata/MetaDataIndexUpgradeServiceTests.java @@ -116,44 +116,12 @@ public class MetaDataIndexUpgradeServiceTests extends ESTestCase { service.upgradeIndexMetaData(goodMeta, Version.CURRENT.minimumIndexCompatibilityVersion()); } - public void testPluginUpgrade() { - MetaDataIndexUpgradeService service = new MetaDataIndexUpgradeService(Settings.EMPTY, xContentRegistry(), - new MapperRegistry(Collections.emptyMap(), Collections.emptyMap(), MapperPlugin.NOOP_FIELD_FILTER), - IndexScopedSettings.DEFAULT_SCOPED_SETTINGS, Collections.singletonList( - indexMetaData -> IndexMetaData.builder(indexMetaData).settings( - Settings.builder() - .put(indexMetaData.getSettings()) - .put("index.refresh_interval", "10s") - ).build())); - IndexMetaData src = newIndexMeta("foo", Settings.builder().put("index.refresh_interval", "200s").build()); - assertFalse(service.isUpgraded(src)); - src = service.upgradeIndexMetaData(src, Version.CURRENT.minimumIndexCompatibilityVersion()); - assertTrue(service.isUpgraded(src)); - assertEquals("10s", src.getSettings().get("index.refresh_interval")); - assertSame(src, service.upgradeIndexMetaData(src, Version.CURRENT.minimumIndexCompatibilityVersion())); // no double upgrade - } - - public void testPluginUpgradeFailure() { - MetaDataIndexUpgradeService service = new MetaDataIndexUpgradeService(Settings.EMPTY, xContentRegistry(), - new MapperRegistry(Collections.emptyMap(), Collections.emptyMap(), MapperPlugin.NOOP_FIELD_FILTER), - IndexScopedSettings.DEFAULT_SCOPED_SETTINGS, Collections.singletonList( - indexMetaData -> { - throw new IllegalStateException("Cannot upgrade index " + indexMetaData.getIndex().getName()); - } - )); - IndexMetaData src = newIndexMeta("foo", Settings.EMPTY); - String message = expectThrows(IllegalStateException.class, () -> service.upgradeIndexMetaData(src, - Version.CURRENT.minimumIndexCompatibilityVersion())).getMessage(); - assertEquals(message, "Cannot upgrade index foo"); - } - private MetaDataIndexUpgradeService getMetaDataIndexUpgradeService() { return new MetaDataIndexUpgradeService( Settings.EMPTY, xContentRegistry(), new MapperRegistry(Collections.emptyMap(), Collections.emptyMap(), MapperPlugin.NOOP_FIELD_FILTER), - IndexScopedSettings.DEFAULT_SCOPED_SETTINGS, - Collections.emptyList()); + IndexScopedSettings.DEFAULT_SCOPED_SETTINGS); } public static IndexMetaData newIndexMeta(String name, Settings indexSettings) { diff --git a/server/src/test/java/org/elasticsearch/gateway/GatewayMetaStateTests.java b/server/src/test/java/org/elasticsearch/gateway/GatewayMetaStateTests.java index d0101f276d8..dfd2aff4ada 100644 --- a/server/src/test/java/org/elasticsearch/gateway/GatewayMetaStateTests.java +++ b/server/src/test/java/org/elasticsearch/gateway/GatewayMetaStateTests.java @@ -33,96 +33,43 @@ import java.util.Arrays; import java.util.Collections; import java.util.EnumSet; import java.util.HashMap; +import java.util.List; +import java.util.Objects; import static org.hamcrest.Matchers.equalTo; public class GatewayMetaStateTests extends ESTestCase { - public void testAddCustomMetaDataOnUpgrade() throws Exception { + public void testUpdateTemplateMetaDataOnUpgrade() { MetaData metaData = randomMetaData(); - MetaDataUpgrader metaDataUpgrader = new MetaDataUpgrader( - Collections.singletonList(customs -> { - customs.put(CustomMetaData1.TYPE, new CustomMetaData1("modified_data1")); - return customs; - }), - Collections.emptyList() - ); - MetaData upgrade = GatewayMetaState.upgradeMetaData(metaData, new MockMetaDataIndexUpgradeService(false), metaDataUpgrader); - assertTrue(upgrade != metaData); - assertFalse(MetaData.isGlobalStateEquals(upgrade, metaData)); - assertNotNull(upgrade.custom(CustomMetaData1.TYPE)); - assertThat(((TestCustomMetaData) upgrade.custom(CustomMetaData1.TYPE)).getData(), equalTo("modified_data1")); - } - - public void testRemoveCustomMetaDataOnUpgrade() throws Exception { - MetaData metaData = randomMetaData(new CustomMetaData1("data")); - MetaDataUpgrader metaDataUpgrader = new MetaDataUpgrader( - Collections.singletonList(customs -> { - customs.remove(CustomMetaData1.TYPE); - return customs; - }), - Collections.emptyList() - ); - MetaData upgrade = GatewayMetaState.upgradeMetaData(metaData, new MockMetaDataIndexUpgradeService(false), metaDataUpgrader); - assertTrue(upgrade != metaData); - assertFalse(MetaData.isGlobalStateEquals(upgrade, metaData)); - assertNull(upgrade.custom(CustomMetaData1.TYPE)); - } - - public void testUpdateCustomMetaDataOnUpgrade() throws Exception { - MetaData metaData = randomMetaData(new CustomMetaData1("data")); - MetaDataUpgrader metaDataUpgrader = new MetaDataUpgrader( - Collections.singletonList(customs -> { - customs.put(CustomMetaData1.TYPE, new CustomMetaData1("modified_data1")); - return customs; - }), - Collections.emptyList() - ); + MetaDataUpgrader metaDataUpgrader = new MetaDataUpgrader(Collections.singletonList( + templates -> { + templates.put("added_test_template", IndexTemplateMetaData.builder("added_test_template") + .patterns(randomIndexPatterns()).build()); + return templates; + } + )); MetaData upgrade = GatewayMetaState.upgradeMetaData(metaData, new MockMetaDataIndexUpgradeService(false), metaDataUpgrader); - assertTrue(upgrade != metaData); - assertFalse(MetaData.isGlobalStateEquals(upgrade, metaData)); - assertNotNull(upgrade.custom(CustomMetaData1.TYPE)); - assertThat(((TestCustomMetaData) upgrade.custom(CustomMetaData1.TYPE)).getData(), equalTo("modified_data1")); - } - - - public void testUpdateTemplateMetaDataOnUpgrade() throws Exception { - MetaData metaData = randomMetaData(); - MetaDataUpgrader metaDataUpgrader = new MetaDataUpgrader( - Collections.emptyList(), - Collections.singletonList( - templates -> { - templates.put("added_test_template", IndexTemplateMetaData.builder("added_test_template") - .patterns(Arrays.asList(generateRandomStringArray(10, 100, false, false))).build()); - return templates; - } - )); - - MetaData upgrade = GatewayMetaState.upgradeMetaData(metaData, new MockMetaDataIndexUpgradeService(false), metaDataUpgrader); - assertTrue(upgrade != metaData); + assertNotSame(upgrade, metaData); assertFalse(MetaData.isGlobalStateEquals(upgrade, metaData)); assertTrue(upgrade.templates().containsKey("added_test_template")); } - public void testNoMetaDataUpgrade() throws Exception { + public void testNoMetaDataUpgrade() { MetaData metaData = randomMetaData(new CustomMetaData1("data")); - MetaDataUpgrader metaDataUpgrader = new MetaDataUpgrader(Collections.emptyList(), Collections.emptyList()); + MetaDataUpgrader metaDataUpgrader = new MetaDataUpgrader(Collections.emptyList()); MetaData upgrade = GatewayMetaState.upgradeMetaData(metaData, new MockMetaDataIndexUpgradeService(false), metaDataUpgrader); - assertTrue(upgrade == metaData); + assertSame(upgrade, metaData); assertTrue(MetaData.isGlobalStateEquals(upgrade, metaData)); for (IndexMetaData indexMetaData : upgrade) { assertTrue(metaData.hasIndexMetaData(indexMetaData)); } } - public void testCustomMetaDataValidation() throws Exception { + public void testCustomMetaDataValidation() { MetaData metaData = randomMetaData(new CustomMetaData1("data")); - MetaDataUpgrader metaDataUpgrader = new MetaDataUpgrader(Collections.singletonList( - customs -> { - throw new IllegalStateException("custom meta data too old"); - } - ), Collections.emptyList()); + MetaDataUpgrader metaDataUpgrader = new MetaDataUpgrader(Collections.emptyList()); try { GatewayMetaState.upgradeMetaData(metaData, new MockMetaDataIndexUpgradeService(false), metaDataUpgrader); } catch (IllegalStateException e) { @@ -130,82 +77,41 @@ public class GatewayMetaStateTests extends ESTestCase { } } - public void testMultipleCustomMetaDataUpgrade() throws Exception { - final MetaData metaData; - switch (randomIntBetween(0, 2)) { - case 0: - metaData = randomMetaData(new CustomMetaData1("data1"), new CustomMetaData2("data2")); - break; - case 1: - metaData = randomMetaData(randomBoolean() ? new CustomMetaData1("data1") : new CustomMetaData2("data2")); - break; - case 2: - metaData = randomMetaData(); - break; - default: - throw new IllegalStateException("should never happen"); - } - MetaDataUpgrader metaDataUpgrader = new MetaDataUpgrader( - Arrays.asList( - customs -> { - customs.put(CustomMetaData1.TYPE, new CustomMetaData1("modified_data1")); - return customs; - }, - customs -> { - customs.put(CustomMetaData2.TYPE, new CustomMetaData1("modified_data2")); - return customs; - } - ), Collections.emptyList()); - MetaData upgrade = GatewayMetaState.upgradeMetaData(metaData, new MockMetaDataIndexUpgradeService(false), metaDataUpgrader); - assertTrue(upgrade != metaData); - assertFalse(MetaData.isGlobalStateEquals(upgrade, metaData)); - assertNotNull(upgrade.custom(CustomMetaData1.TYPE)); - assertThat(((TestCustomMetaData) upgrade.custom(CustomMetaData1.TYPE)).getData(), equalTo("modified_data1")); - assertNotNull(upgrade.custom(CustomMetaData2.TYPE)); - assertThat(((TestCustomMetaData) upgrade.custom(CustomMetaData2.TYPE)).getData(), equalTo("modified_data2")); - for (IndexMetaData indexMetaData : upgrade) { - assertTrue(metaData.hasIndexMetaData(indexMetaData)); - } - } - - public void testIndexMetaDataUpgrade() throws Exception { + public void testIndexMetaDataUpgrade() { MetaData metaData = randomMetaData(); - MetaDataUpgrader metaDataUpgrader = new MetaDataUpgrader(Collections.emptyList(), Collections.emptyList()); + MetaDataUpgrader metaDataUpgrader = new MetaDataUpgrader(Collections.emptyList()); MetaData upgrade = GatewayMetaState.upgradeMetaData(metaData, new MockMetaDataIndexUpgradeService(true), metaDataUpgrader); - assertTrue(upgrade != metaData); + assertNotSame(upgrade, metaData); assertTrue(MetaData.isGlobalStateEquals(upgrade, metaData)); for (IndexMetaData indexMetaData : upgrade) { assertFalse(metaData.hasIndexMetaData(indexMetaData)); } } - public void testCustomMetaDataNoChange() throws Exception { + public void testCustomMetaDataNoChange() { MetaData metaData = randomMetaData(new CustomMetaData1("data")); - MetaDataUpgrader metaDataUpgrader = new MetaDataUpgrader(Collections.singletonList(HashMap::new), - Collections.singletonList(HashMap::new)); + MetaDataUpgrader metaDataUpgrader = new MetaDataUpgrader(Collections.singletonList(HashMap::new)); MetaData upgrade = GatewayMetaState.upgradeMetaData(metaData, new MockMetaDataIndexUpgradeService(false), metaDataUpgrader); - assertTrue(upgrade == metaData); + assertSame(upgrade, metaData); assertTrue(MetaData.isGlobalStateEquals(upgrade, metaData)); for (IndexMetaData indexMetaData : upgrade) { assertTrue(metaData.hasIndexMetaData(indexMetaData)); } } - public void testIndexTemplateValidation() throws Exception { + public void testIndexTemplateValidation() { MetaData metaData = randomMetaData(); - MetaDataUpgrader metaDataUpgrader = new MetaDataUpgrader( - Collections.emptyList(), - Collections.singletonList( - customs -> { - throw new IllegalStateException("template is incompatible"); - })); + MetaDataUpgrader metaDataUpgrader = new MetaDataUpgrader(Collections.singletonList( + customs -> { + throw new IllegalStateException("template is incompatible"); + })); String message = expectThrows(IllegalStateException.class, () -> GatewayMetaState.upgradeMetaData(metaData, new MockMetaDataIndexUpgradeService(false), metaDataUpgrader)).getMessage(); assertThat(message, equalTo("template is incompatible")); } - public void testMultipleIndexTemplateUpgrade() throws Exception { + public void testMultipleIndexTemplateUpgrade() { final MetaData metaData; switch (randomIntBetween(0, 2)) { case 0: @@ -220,12 +126,10 @@ public class GatewayMetaStateTests extends ESTestCase { default: throw new IllegalStateException("should never happen"); } - MetaDataUpgrader metaDataUpgrader = new MetaDataUpgrader( - Collections.emptyList(), - Arrays.asList( + MetaDataUpgrader metaDataUpgrader = new MetaDataUpgrader(Arrays.asList( indexTemplateMetaDatas -> { indexTemplateMetaDatas.put("template1", IndexTemplateMetaData.builder("template1") - .patterns(Arrays.asList(generateRandomStringArray(10, 100, false, false))) + .patterns(randomIndexPatterns()) .settings(Settings.builder().put(IndexMetaData.INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), 20).build()) .build()); return indexTemplateMetaDatas; @@ -233,14 +137,14 @@ public class GatewayMetaStateTests extends ESTestCase { }, indexTemplateMetaDatas -> { indexTemplateMetaDatas.put("template2", IndexTemplateMetaData.builder("template2") - .patterns(Arrays.asList(generateRandomStringArray(10, 100, false, false))) + .patterns(randomIndexPatterns()) .settings(Settings.builder().put(IndexMetaData.INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), 10).build()).build()); return indexTemplateMetaDatas; } )); MetaData upgrade = GatewayMetaState.upgradeMetaData(metaData, new MockMetaDataIndexUpgradeService(false), metaDataUpgrader); - assertTrue(upgrade != metaData); + assertNotSame(upgrade, metaData); assertFalse(MetaData.isGlobalStateEquals(upgrade, metaData)); assertNotNull(upgrade.templates().get("template1")); assertThat(IndexMetaData.INDEX_NUMBER_OF_SHARDS_SETTING.get(upgrade.templates().get("template1").settings()), equalTo(20)); @@ -255,7 +159,7 @@ public class GatewayMetaStateTests extends ESTestCase { private final boolean upgrade; MockMetaDataIndexUpgradeService(boolean upgrade) { - super(Settings.EMPTY, null, null, null, null); + super(Settings.EMPTY, null, null, null); this.upgrade = upgrade; } @@ -268,30 +172,7 @@ public class GatewayMetaStateTests extends ESTestCase { private static class CustomMetaData1 extends TestCustomMetaData { public static final String TYPE = "custom_md_1"; - protected CustomMetaData1(String data) { - super(data); - } - - @Override - public String getWriteableName() { - return TYPE; - } - - @Override - public Version getMinimalSupportedVersion() { - return Version.CURRENT; - } - - @Override - public EnumSet context() { - return EnumSet.of(MetaData.XContentContext.GATEWAY); - } - } - - private static class CustomMetaData2 extends TestCustomMetaData { - public static final String TYPE = "custom_md_2"; - - protected CustomMetaData2(String data) { + CustomMetaData1(String data) { super(data); } @@ -334,7 +215,7 @@ public class GatewayMetaStateTests extends ESTestCase { .settings(settings(Version.CURRENT) .put(IndexMetaData.INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), randomIntBetween(0, 3)) .put(IndexMetaData.INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), randomIntBetween(1, 5))) - .patterns(Arrays.asList(generateRandomStringArray(10, 100, false, false))) + .patterns(randomIndexPatterns()) .build(); builder.put(templateMetaData); } @@ -348,4 +229,8 @@ public class GatewayMetaStateTests extends ESTestCase { } return builder.build(); } + + private static List randomIndexPatterns() { + return Arrays.asList(Objects.requireNonNull(generateRandomStringArray(10, 100, false, false))); + } } diff --git a/server/src/test/java/org/elasticsearch/indices/cluster/ClusterStateChanges.java b/server/src/test/java/org/elasticsearch/indices/cluster/ClusterStateChanges.java index 5b61573f3b6..7b246b2632f 100644 --- a/server/src/test/java/org/elasticsearch/indices/cluster/ClusterStateChanges.java +++ b/server/src/test/java/org/elasticsearch/indices/cluster/ClusterStateChanges.java @@ -182,8 +182,7 @@ public class ClusterStateChanges { TransportService.NOOP_TRANSPORT_INTERCEPTOR, boundAddress -> DiscoveryNode.createLocal(SETTINGS, boundAddress.publishAddress(), UUIDs.randomBase64UUID()), clusterSettings, Collections.emptySet()); - MetaDataIndexUpgradeService metaDataIndexUpgradeService = new MetaDataIndexUpgradeService(SETTINGS, xContentRegistry, null, null, - null) { + MetaDataIndexUpgradeService metaDataIndexUpgradeService = new MetaDataIndexUpgradeService(SETTINGS, xContentRegistry, null, null) { // metaData upgrader should do nothing @Override public IndexMetaData upgradeIndexMetaData(IndexMetaData indexMetaData, Version minimumIndexCompatibilityVersion) { diff --git a/server/src/test/java/org/elasticsearch/snapshots/SnapshotResiliencyTests.java b/server/src/test/java/org/elasticsearch/snapshots/SnapshotResiliencyTests.java index d8a8be807cf..4e50a8345b8 100644 --- a/server/src/test/java/org/elasticsearch/snapshots/SnapshotResiliencyTests.java +++ b/server/src/test/java/org/elasticsearch/snapshots/SnapshotResiliencyTests.java @@ -1077,9 +1077,7 @@ public class SnapshotResiliencyTests extends ESTestCase { new MetaDataIndexUpgradeService( settings, namedXContentRegistry, mapperRegistry, - indexScopedSettings, - Collections.emptyList() - ), + indexScopedSettings), clusterSettings ); actions.put(PutMappingAction.INSTANCE,