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 2918947fa1fd4..3ad8f099c69f0 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.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 IndexMetaData upgradeIndexMetaData(IndexMetaData indexMetaData, Version m 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 644a1eae644ac..d470661b63a45 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.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; @@ -206,7 +207,6 @@ private static boolean isMasterOrDataNode(Settings settings) { * 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 */ @@ -222,11 +222,6 @@ static MetaData upgradeMetaData(MetaData metaData, 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))) { @@ -235,21 +230,21 @@ static MetaData upgradeMetaData(MetaData metaData, 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 49a6420df787d..b97b12f49ff1a 100644 --- a/server/src/main/java/org/elasticsearch/node/Node.java +++ b/server/src/main/java/org/elasticsearch/node/Node.java @@ -46,7 +46,6 @@ 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; @@ -458,19 +457,13 @@ protected Node( final NetworkModule networkModule = new NetworkModule(settings, 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 49dd91901d756..5eeca9b45bf13 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 @@ * 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 950c08fc32650..759da798c8ca6 100644 --- a/server/src/main/java/org/elasticsearch/plugins/Plugin.java +++ b/server/src/main/java/org/elasticsearch/plugins/Plugin.java @@ -21,9 +21,7 @@ import org.elasticsearch.bootstrap.BootstrapCheck; import org.elasticsearch.client.Client; -import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.cluster.metadata.IndexTemplateMetaData; -import org.elasticsearch.cluster.metadata.MetaData; import org.elasticsearch.cluster.node.DiscoveryNodeRole; import org.elasticsearch.cluster.service.ClusterService; import org.elasticsearch.common.component.LifecycleComponent; @@ -143,22 +141,6 @@ public List> getSettingUpgraders() { 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. *

@@ -175,21 +157,6 @@ public UnaryOperator> getIndexTemplateMetaDat 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 47fb9e3e3bd17..32e7a3bcbd873 100644 --- a/server/src/test/java/org/elasticsearch/cluster/metadata/MetaDataIndexUpgradeServiceTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/metadata/MetaDataIndexUpgradeServiceTests.java @@ -120,44 +120,12 @@ public void testFailUpgrade() { 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 d0101f276d8f6..dfd2aff4ada0d 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.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() - ); - - 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; - } - )); + 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); + 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 void testCustomMetaDataValidation() throws Exception { } } - 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 void testMultipleIndexTemplateUpgrade() throws Exception { 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 void testMultipleIndexTemplateUpgrade() throws Exception { }, 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 @@ private static class MockMetaDataIndexUpgradeService extends MetaDataIndexUpgrad 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 IndexMetaData upgradeIndexMetaData(IndexMetaData indexMetaData, Version m 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 @@ private static MetaData randomMetaDataWithIndexTemplates(String... templates) { .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 @@ private static MetaData randomMetaDataWithIndexTemplates(String... templates) { } 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 21630157105d1..3d3ba3b8cf7b1 100644 --- a/server/src/test/java/org/elasticsearch/indices/cluster/ClusterStateChanges.java +++ b/server/src/test/java/org/elasticsearch/indices/cluster/ClusterStateChanges.java @@ -184,8 +184,7 @@ public ClusterStateChanges(NamedXContentRegistry xContentRegistry, ThreadPool th 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 b6cb04271e7ba..234aaafa5ae38 100644 --- a/server/src/test/java/org/elasticsearch/snapshots/SnapshotResiliencyTests.java +++ b/server/src/test/java/org/elasticsearch/snapshots/SnapshotResiliencyTests.java @@ -1062,9 +1062,7 @@ clusterService, indicesService, threadPool, shardStateAction, mappingUpdatedActi new MetaDataIndexUpgradeService( settings, namedXContentRegistry, mapperRegistry, - indexScopedSettings, - Collections.emptyList() - ), + indexScopedSettings), clusterSettings ); actions.put(PutMappingAction.INSTANCE,