From 03ce0f8a4db51946cafe1282892c9edb7e2c0ffa Mon Sep 17 00:00:00 2001 From: Lee Hinman Date: Tue, 16 Jun 2020 09:22:10 -0600 Subject: [PATCH] [7.x] Normalized prefix for rollover API (#57271) (69e1c066) (#58171) * Normalized prefix for rollover API (#57271) Co-authored-by: Elastic Machine Co-authored-by: Lee Hinman It fixes the issue #53388 by normalizing prefix at index creation request itself * Fix compilation for backport Co-authored-by: Gaurav Chandani --- .../admin/indices/rollover/RolloverIT.java | 35 +++++++++++++++++++ .../rollover/MetadataRolloverService.java | 1 - .../metadata/MetadataCreateIndexService.java | 14 +++++--- .../MetadataRolloverServiceTests.java | 9 ++--- 4 files changed, 50 insertions(+), 9 deletions(-) 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 8f67d167bd238..598d0cfcf41de 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 @@ -188,6 +188,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()); + index("test_index-2", "_doc", "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 9beab3b6ecf4f..91c78c1acc777 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 @@ -65,7 +65,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 4453e8167fa11..f1585ef59bfda 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataCreateIndexService.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataCreateIndexService.java @@ -276,10 +276,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) { @@ -305,12 +302,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 3361ea1a0ee72..a55b01173a752 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,