Skip to content

Commit 03ce0f8

Browse files
dakroneGaurav614
andauthored
[7.x] Normalized prefix for rollover API (#57271) (69e1c06) (#58171)
* 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 * Fix compilation for backport Co-authored-by: Gaurav Chandani <[email protected]>
1 parent 7079a3b commit 03ce0f8

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,41 @@ public void testRolloverWithIndexSettings() throws Exception {
188188
}
189189
}
190190

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

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ public class MetadataRolloverService {
6565
private final MetadataCreateIndexService createIndexService;
6666
private final MetadataIndexAliasesService indexAliasesService;
6767
private final IndexNameExpressionResolver indexNameExpressionResolver;
68-
6968
@Inject
7069
public MetadataRolloverService(ThreadPool threadPool,
7170
MetadataCreateIndexService createIndexService, MetadataIndexAliasesService indexAliasesService,

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,7 @@ public void createIndex(final CreateIndexClusterStateUpdateRequest request,
276276

277277
private void onlyCreateIndex(final CreateIndexClusterStateUpdateRequest request,
278278
final ActionListener<ClusterStateUpdateResponse> listener) {
279-
Settings.Builder updatedSettingsBuilder = Settings.builder();
280-
Settings build = updatedSettingsBuilder.put(request.settings()).normalizePrefix(IndexMetadata.INDEX_SETTING_PREFIX).build();
281-
indexScopedSettings.validate(build, true); // we do validate here - index setting must be consistent
282-
request.settings(build);
279+
normalizeRequestSetting(request);
283280
clusterService.submitStateUpdateTask(
284281
"create-index [" + request.index() + "], cause [" + request.cause() + "]",
285282
new AckedClusterStateUpdateTask<ClusterStateUpdateResponse>(Priority.URGENT, request, listener) {
@@ -305,12 +302,21 @@ public void onFailure(String source, Exception e) {
305302
});
306303
}
307304

305+
private void normalizeRequestSetting(CreateIndexClusterStateUpdateRequest createIndexClusterStateRequest) {
306+
Settings.Builder updatedSettingsBuilder = Settings.builder();
307+
Settings build = updatedSettingsBuilder.put(createIndexClusterStateRequest.settings())
308+
.normalizePrefix(IndexMetadata.INDEX_SETTING_PREFIX).build();
309+
indexScopedSettings.validate(build, true);
310+
createIndexClusterStateRequest.settings(build);
311+
}
308312
/**
309313
* Handles the cluster state transition to a version that reflects the {@link CreateIndexClusterStateUpdateRequest}.
310314
* All the requested changes are firstly validated before mutating the {@link ClusterState}.
311315
*/
312316
public ClusterState applyCreateIndexRequest(ClusterState currentState, CreateIndexClusterStateUpdateRequest request, boolean silent,
313317
BiConsumer<Metadata.Builder, IndexMetadata> metadataTransformer) throws Exception {
318+
319+
normalizeRequestSetting(request);
314320
logger.trace("executing IndexCreationTask for [{}] against cluster state version [{}]", request, currentState.version());
315321

316322
validate(request, currentState);

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

Lines changed: 5 additions & 4 deletions
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)