Skip to content

Commit 69e1c06

Browse files
authored
Normalized prefix for rollover API (#57271)
Co-authored-by: Elastic Machine <[email protected]> Co-authored-by: Lee Hinman <[email protected]> It fixes the issue #53388 by normalizing prefix at index creation request itself
1 parent 1a21b36 commit 69e1c06

File tree

4 files changed

+50
-9
lines changed

4 files changed

+50
-9
lines changed

server/src/internalClusterTest/java/org/elasticsearch/action/admin/indices/rollover/RolloverIT.java

+35
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,41 @@ public void testRolloverWithIndexSettings() throws Exception {
187187
}
188188
}
189189

190+
public void testRolloverWithIndexSettingsWithoutPrefix() throws Exception {
191+
Alias testAlias = new Alias("test_alias");
192+
boolean explicitWriteIndex = randomBoolean();
193+
if (explicitWriteIndex) {
194+
testAlias.writeIndex(true);
195+
}
196+
assertAcked(prepareCreate("test_index-2").addAlias(testAlias).get());
197+
indexDoc("test_index-2", "1", "field", "value");
198+
flush("test_index-2");
199+
final Settings settings = Settings.builder()
200+
.put("number_of_shards", 1)
201+
.put("number_of_replicas", 0)
202+
.build();
203+
final RolloverResponse response = client().admin().indices().prepareRolloverIndex("test_alias")
204+
.settings(settings).alias(new Alias("extra_alias")).get();
205+
assertThat(response.getOldIndex(), equalTo("test_index-2"));
206+
assertThat(response.getNewIndex(), equalTo("test_index-000003"));
207+
assertThat(response.isDryRun(), equalTo(false));
208+
assertThat(response.isRolledOver(), equalTo(true));
209+
assertThat(response.getConditionStatus().size(), equalTo(0));
210+
final ClusterState state = client().admin().cluster().prepareState().get().getState();
211+
final IndexMetadata oldIndex = state.metadata().index("test_index-2");
212+
final IndexMetadata newIndex = state.metadata().index("test_index-000003");
213+
assertThat(newIndex.getNumberOfShards(), equalTo(1));
214+
assertThat(newIndex.getNumberOfReplicas(), equalTo(0));
215+
assertTrue(newIndex.getAliases().containsKey("test_alias"));
216+
assertTrue(newIndex.getAliases().containsKey("extra_alias"));
217+
if (explicitWriteIndex) {
218+
assertFalse(oldIndex.getAliases().get("test_alias").writeIndex());
219+
assertTrue(newIndex.getAliases().get("test_alias").writeIndex());
220+
} else {
221+
assertFalse(oldIndex.getAliases().containsKey("test_alias"));
222+
}
223+
}
224+
190225
public void testRolloverDryRun() throws Exception {
191226
if (randomBoolean()) {
192227
PutIndexTemplateRequestBuilder putTemplate = client().admin().indices()

server/src/main/java/org/elasticsearch/action/admin/indices/rollover/MetadataRolloverService.java

-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ public class MetadataRolloverService {
6363
private final MetadataCreateIndexService createIndexService;
6464
private final MetadataIndexAliasesService indexAliasesService;
6565
private final IndexNameExpressionResolver indexNameExpressionResolver;
66-
6766
@Inject
6867
public MetadataRolloverService(ThreadPool threadPool,
6968
MetadataCreateIndexService createIndexService, MetadataIndexAliasesService indexAliasesService,

server/src/main/java/org/elasticsearch/cluster/metadata/MetadataCreateIndexService.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,7 @@ public void createIndex(final CreateIndexClusterStateUpdateRequest request,
280280

281281
private void onlyCreateIndex(final CreateIndexClusterStateUpdateRequest request,
282282
final ActionListener<ClusterStateUpdateResponse> listener) {
283-
Settings.Builder updatedSettingsBuilder = Settings.builder();
284-
Settings build = updatedSettingsBuilder.put(request.settings()).normalizePrefix(IndexMetadata.INDEX_SETTING_PREFIX).build();
285-
indexScopedSettings.validate(build, true); // we do validate here - index setting must be consistent
286-
request.settings(build);
283+
normalizeRequestSetting(request);
287284
clusterService.submitStateUpdateTask(
288285
"create-index [" + request.index() + "], cause [" + request.cause() + "]",
289286
new AckedClusterStateUpdateTask<>(Priority.URGENT, request, listener) {
@@ -309,12 +306,21 @@ public void onFailure(String source, Exception e) {
309306
});
310307
}
311308

309+
private void normalizeRequestSetting(CreateIndexClusterStateUpdateRequest createIndexClusterStateRequest) {
310+
Settings.Builder updatedSettingsBuilder = Settings.builder();
311+
Settings build = updatedSettingsBuilder.put(createIndexClusterStateRequest.settings())
312+
.normalizePrefix(IndexMetadata.INDEX_SETTING_PREFIX).build();
313+
indexScopedSettings.validate(build, true);
314+
createIndexClusterStateRequest.settings(build);
315+
}
312316
/**
313317
* Handles the cluster state transition to a version that reflects the {@link CreateIndexClusterStateUpdateRequest}.
314318
* All the requested changes are firstly validated before mutating the {@link ClusterState}.
315319
*/
316320
public ClusterState applyCreateIndexRequest(ClusterState currentState, CreateIndexClusterStateUpdateRequest request, boolean silent,
317321
BiConsumer<Metadata.Builder, IndexMetadata> metadataTransformer) throws Exception {
322+
323+
normalizeRequestSetting(request);
318324
logger.trace("executing IndexCreationTask for [{}] against cluster state version [{}]", request, currentState.version());
319325

320326
validate(request, currentState);

server/src/test/java/org/elasticsearch/action/admin/indices/rollover/MetadataRolloverServiceTests.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.elasticsearch.common.CheckedFunction;
4848
import org.elasticsearch.common.Strings;
4949
import org.elasticsearch.common.UUIDs;
50+
import org.elasticsearch.common.settings.IndexScopedSettings;
5051
import org.elasticsearch.common.compress.CompressedXContent;
5152
import org.elasticsearch.common.settings.Settings;
5253
import org.elasticsearch.common.unit.TimeValue;
@@ -483,8 +484,8 @@ public void testRolloverClusterState() throws Exception {
483484

484485
ShardLimitValidator shardLimitValidator = new ShardLimitValidator(Settings.EMPTY, clusterService);
485486
MetadataCreateIndexService createIndexService = new MetadataCreateIndexService(Settings.EMPTY,
486-
clusterService, indicesService, allocationService, null, shardLimitValidator, env, null,
487-
testThreadPool, null, Collections.emptyList(), false);
487+
clusterService, indicesService, allocationService, null, shardLimitValidator, env,
488+
IndexScopedSettings.DEFAULT_SCOPED_SETTINGS, testThreadPool, null, Collections.emptyList(), false);
488489
MetadataIndexAliasesService indexAliasesService = new MetadataIndexAliasesService(clusterService, indicesService,
489490
new AliasValidator(), null, xContentRegistry());
490491
MetadataRolloverService rolloverService = new MetadataRolloverService(testThreadPool, createIndexService, indexAliasesService,
@@ -560,8 +561,8 @@ public void testRolloverClusterStateForDataStream() throws Exception {
560561

561562
ShardLimitValidator shardLimitValidator = new ShardLimitValidator(Settings.EMPTY, clusterService);
562563
MetadataCreateIndexService createIndexService = new MetadataCreateIndexService(Settings.EMPTY,
563-
clusterService, indicesService, allocationService, null, shardLimitValidator, env, null,
564-
testThreadPool, null, Collections.emptyList(), false);
564+
clusterService, indicesService, allocationService, null, shardLimitValidator, env,
565+
IndexScopedSettings.DEFAULT_SCOPED_SETTINGS, testThreadPool, null, Collections.emptyList(), false);
565566
MetadataIndexAliasesService indexAliasesService = new MetadataIndexAliasesService(clusterService, indicesService,
566567
new AliasValidator(), null, xContentRegistry());
567568
MetadataRolloverService rolloverService = new MetadataRolloverService(testThreadPool, createIndexService, indexAliasesService,

0 commit comments

Comments
 (0)