diff --git a/server/src/internalClusterTest/java/org/elasticsearch/action/admin/indices/rollover/RolloverIT.java b/server/src/internalClusterTest/java/org/elasticsearch/action/admin/indices/rollover/RolloverIT.java index 46300feaf56dd..acae80f5c93b3 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/action/admin/indices/rollover/RolloverIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/action/admin/indices/rollover/RolloverIT.java @@ -187,6 +187,41 @@ public void testRolloverWithIndexSettings() throws Exception { } } + public void testRolloverWithIndexSettingsWithoutPrefix() throws Exception { + Alias testAlias = new Alias("test_alias"); + boolean explicitWriteIndex = randomBoolean(); + if (explicitWriteIndex) { + testAlias.writeIndex(true); + } + assertAcked(prepareCreate("test_index-2").addAlias(testAlias).get()); + indexDoc("test_index-2", "1", "field", "value"); + flush("test_index-2"); + final Settings settings = Settings.builder() + .put("number_of_shards", 1) + .put("number_of_replicas", 0) + .build(); + final RolloverResponse response = client().admin().indices().prepareRolloverIndex("test_alias") + .settings(settings).alias(new Alias("extra_alias")).get(); + assertThat(response.getOldIndex(), equalTo("test_index-2")); + assertThat(response.getNewIndex(), equalTo("test_index-000003")); + assertThat(response.isDryRun(), equalTo(false)); + assertThat(response.isRolledOver(), equalTo(true)); + assertThat(response.getConditionStatus().size(), equalTo(0)); + final ClusterState state = client().admin().cluster().prepareState().get().getState(); + final IndexMetadata oldIndex = state.metadata().index("test_index-2"); + final IndexMetadata newIndex = state.metadata().index("test_index-000003"); + assertThat(newIndex.getNumberOfShards(), equalTo(1)); + assertThat(newIndex.getNumberOfReplicas(), equalTo(0)); + assertTrue(newIndex.getAliases().containsKey("test_alias")); + assertTrue(newIndex.getAliases().containsKey("extra_alias")); + if (explicitWriteIndex) { + assertFalse(oldIndex.getAliases().get("test_alias").writeIndex()); + assertTrue(newIndex.getAliases().get("test_alias").writeIndex()); + } else { + assertFalse(oldIndex.getAliases().containsKey("test_alias")); + } + } + public void testRolloverDryRun() throws Exception { if (randomBoolean()) { PutIndexTemplateRequestBuilder putTemplate = client().admin().indices() diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/rollover/MetadataRolloverService.java b/server/src/main/java/org/elasticsearch/action/admin/indices/rollover/MetadataRolloverService.java index f480dd166b4ee..8d4c65447649c 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/rollover/MetadataRolloverService.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/rollover/MetadataRolloverService.java @@ -63,7 +63,6 @@ public class MetadataRolloverService { private final MetadataCreateIndexService createIndexService; private final MetadataIndexAliasesService indexAliasesService; private final IndexNameExpressionResolver indexNameExpressionResolver; - @Inject public MetadataRolloverService(ThreadPool threadPool, MetadataCreateIndexService createIndexService, MetadataIndexAliasesService indexAliasesService, diff --git a/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataCreateIndexService.java b/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataCreateIndexService.java index 29aeed331ee37..134d3c7a7de69 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataCreateIndexService.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataCreateIndexService.java @@ -280,10 +280,7 @@ public void createIndex(final CreateIndexClusterStateUpdateRequest request, private void onlyCreateIndex(final CreateIndexClusterStateUpdateRequest request, final ActionListener listener) { - Settings.Builder updatedSettingsBuilder = Settings.builder(); - Settings build = updatedSettingsBuilder.put(request.settings()).normalizePrefix(IndexMetadata.INDEX_SETTING_PREFIX).build(); - indexScopedSettings.validate(build, true); // we do validate here - index setting must be consistent - request.settings(build); + normalizeRequestSetting(request); clusterService.submitStateUpdateTask( "create-index [" + request.index() + "], cause [" + request.cause() + "]", new AckedClusterStateUpdateTask<>(Priority.URGENT, request, listener) { @@ -309,12 +306,21 @@ public void onFailure(String source, Exception e) { }); } + private void normalizeRequestSetting(CreateIndexClusterStateUpdateRequest createIndexClusterStateRequest) { + Settings.Builder updatedSettingsBuilder = Settings.builder(); + Settings build = updatedSettingsBuilder.put(createIndexClusterStateRequest.settings()) + .normalizePrefix(IndexMetadata.INDEX_SETTING_PREFIX).build(); + indexScopedSettings.validate(build, true); + createIndexClusterStateRequest.settings(build); + } /** * Handles the cluster state transition to a version that reflects the {@link CreateIndexClusterStateUpdateRequest}. * All the requested changes are firstly validated before mutating the {@link ClusterState}. */ public ClusterState applyCreateIndexRequest(ClusterState currentState, CreateIndexClusterStateUpdateRequest request, boolean silent, BiConsumer metadataTransformer) throws Exception { + + normalizeRequestSetting(request); logger.trace("executing IndexCreationTask for [{}] against cluster state version [{}]", request, currentState.version()); validate(request, currentState); diff --git a/server/src/test/java/org/elasticsearch/action/admin/indices/rollover/MetadataRolloverServiceTests.java b/server/src/test/java/org/elasticsearch/action/admin/indices/rollover/MetadataRolloverServiceTests.java index 0ee6fc074980e..86ac6ecaa7ce4 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/indices/rollover/MetadataRolloverServiceTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/indices/rollover/MetadataRolloverServiceTests.java @@ -47,6 +47,7 @@ import org.elasticsearch.common.CheckedFunction; import org.elasticsearch.common.Strings; import org.elasticsearch.common.UUIDs; +import org.elasticsearch.common.settings.IndexScopedSettings; import org.elasticsearch.common.compress.CompressedXContent; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; @@ -483,8 +484,8 @@ public void testRolloverClusterState() throws Exception { ShardLimitValidator shardLimitValidator = new ShardLimitValidator(Settings.EMPTY, clusterService); MetadataCreateIndexService createIndexService = new MetadataCreateIndexService(Settings.EMPTY, - clusterService, indicesService, allocationService, null, shardLimitValidator, env, null, - testThreadPool, null, Collections.emptyList(), false); + clusterService, indicesService, allocationService, null, shardLimitValidator, env, + IndexScopedSettings.DEFAULT_SCOPED_SETTINGS, testThreadPool, null, Collections.emptyList(), false); MetadataIndexAliasesService indexAliasesService = new MetadataIndexAliasesService(clusterService, indicesService, new AliasValidator(), null, xContentRegistry()); MetadataRolloverService rolloverService = new MetadataRolloverService(testThreadPool, createIndexService, indexAliasesService, @@ -560,8 +561,8 @@ public void testRolloverClusterStateForDataStream() throws Exception { ShardLimitValidator shardLimitValidator = new ShardLimitValidator(Settings.EMPTY, clusterService); MetadataCreateIndexService createIndexService = new MetadataCreateIndexService(Settings.EMPTY, - clusterService, indicesService, allocationService, null, shardLimitValidator, env, null, - testThreadPool, null, Collections.emptyList(), false); + clusterService, indicesService, allocationService, null, shardLimitValidator, env, + IndexScopedSettings.DEFAULT_SCOPED_SETTINGS, testThreadPool, null, Collections.emptyList(), false); MetadataIndexAliasesService indexAliasesService = new MetadataIndexAliasesService(clusterService, indicesService, new AliasValidator(), null, xContentRegistry()); MetadataRolloverService rolloverService = new MetadataRolloverService(testThreadPool, createIndexService, indexAliasesService,