From 2c7fe6ac258bd5cdcf5332cd2869b4464c0ca2bc Mon Sep 17 00:00:00 2001 From: liketic Date: Sun, 4 Mar 2018 16:38:11 +0800 Subject: [PATCH] Reject setting index.optimize_auto_generated_id after version 7.0.0 (#27583) --- .../index/engine/EngineConfig.java | 6 ++ .../index/engine/EngineConfigTests.java | 69 +++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 server/src/test/java/org/elasticsearch/index/engine/EngineConfigTests.java diff --git a/server/src/main/java/org/elasticsearch/index/engine/EngineConfig.java b/server/src/main/java/org/elasticsearch/index/engine/EngineConfig.java index 30743c18cfe10..5bec1c113ebb5 100644 --- a/server/src/main/java/org/elasticsearch/index/engine/EngineConfig.java +++ b/server/src/main/java/org/elasticsearch/index/engine/EngineConfig.java @@ -26,6 +26,7 @@ import org.apache.lucene.search.ReferenceManager; import org.apache.lucene.search.Sort; import org.apache.lucene.search.similarities.Similarity; +import org.elasticsearch.Version; import org.elasticsearch.common.Nullable; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Setting.Property; @@ -131,6 +132,11 @@ public EngineConfig(OpenMode openMode, ShardId shardId, String allocationId, Thr if (openMode == null) { throw new IllegalArgumentException("openMode must not be null"); } + if (indexSettings.getIndexVersionCreated().onOrAfter(Version.V_7_0_0_alpha1) + && INDEX_OPTIMIZE_AUTO_GENERATED_IDS.exists(indexSettings.getSettings())) { + throw new IllegalArgumentException( + "Setting [" + INDEX_OPTIMIZE_AUTO_GENERATED_IDS.getKey() + "] was removed in version 7.0.0"); + } this.shardId = shardId; this.allocationId = allocationId; this.indexSettings = indexSettings; diff --git a/server/src/test/java/org/elasticsearch/index/engine/EngineConfigTests.java b/server/src/test/java/org/elasticsearch/index/engine/EngineConfigTests.java new file mode 100644 index 0000000000000..caea9ae7e2688 --- /dev/null +++ b/server/src/test/java/org/elasticsearch/index/engine/EngineConfigTests.java @@ -0,0 +1,69 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.index.engine; + +import org.elasticsearch.Version; +import org.elasticsearch.cluster.metadata.IndexMetaData; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.index.IndexSettings; +import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.IndexSettingsModule; + + +public class EngineConfigTests extends ESTestCase { + + + private EngineConfig createEngineConfigWithSettings(IndexSettings indexSettings) { + return new EngineConfig(EngineConfig.OpenMode.OPEN_INDEX_AND_TRANSLOG, null, null, null, + indexSettings, null, null, null, null, + null, null, null, null, null, + true, null, null, null, null, null, + null, null, null); + } + + public void testOptimizeAutoGeneratedIdsSettingRemoval() throws Exception { + Version version = Version.V_7_0_0_alpha1; + boolean optimizeAutoGeneratedIds = randomBoolean(); + Settings.Builder builder = Settings.builder() + .put(IndexMetaData.SETTING_VERSION_CREATED, version) + .put(EngineConfig.INDEX_OPTIMIZE_AUTO_GENERATED_IDS.getKey(), optimizeAutoGeneratedIds); + IndexSettings indexSettings = IndexSettingsModule.newIndexSettings("index1", builder.build()); + + IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, + () -> createEngineConfigWithSettings(indexSettings)); + assertEquals("Setting [" + EngineConfig.INDEX_OPTIMIZE_AUTO_GENERATED_IDS.getKey() + "] was removed in version 7.0.0", + exception.getMessage()); + + version = randomFrom(Version.V_5_0_0, Version.V_5_0_0_alpha1, Version.V_5_6_9, Version.V_6_0_0_rc1, + Version.V_6_0_0, Version.V_6_2_0, Version.V_6_3_0); + optimizeAutoGeneratedIds = randomBoolean(); + builder = Settings.builder() + .put(IndexMetaData.SETTING_VERSION_CREATED, version) + .put(EngineConfig.INDEX_OPTIMIZE_AUTO_GENERATED_IDS.getKey(), optimizeAutoGeneratedIds); + IndexSettings indexSettings2 = IndexSettingsModule.newIndexSettings("index2", builder.build()); + EngineConfig config = createEngineConfigWithSettings(indexSettings2); + assertEquals(optimizeAutoGeneratedIds, config.isAutoGeneratedIDsOptimizationEnabled()); + + version = Version.V_7_0_0_alpha1; + builder = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, version); + indexSettings2 = IndexSettingsModule.newIndexSettings("index3", builder.build()); + config = createEngineConfigWithSettings(indexSettings2); + assertTrue(config.isAutoGeneratedIDsOptimizationEnabled()); + } +}