Skip to content

Commit c471f54

Browse files
committed
Use ILM skip setting in shrink action
We add the skip setting to prevent ILM from processing the shrunken index before the execution state has been copied - which could happen if the shards of the shrunken index take a long time to allocate. Resolves #109206
1 parent 19bc926 commit c471f54

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CopyExecutionStateStep.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@
1313
import org.elasticsearch.cluster.ProjectState;
1414
import org.elasticsearch.cluster.metadata.IndexMetadata;
1515
import org.elasticsearch.cluster.metadata.LifecycleExecutionState;
16+
import org.elasticsearch.cluster.metadata.ProjectMetadata;
17+
import org.elasticsearch.common.settings.Settings;
1618
import org.elasticsearch.core.Tuple;
1719
import org.elasticsearch.index.Index;
1820

1921
import java.util.Objects;
2022
import java.util.function.BiFunction;
2123

24+
import static org.elasticsearch.cluster.metadata.LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY;
25+
2226
/**
2327
* Copies the execution state data from one index to another, typically after a
2428
* new index has been created. As part of the execution state copy it will set the target index
@@ -101,8 +105,20 @@ public ProjectState performAction(Index index, ProjectState projectState) {
101105
newLifecycleState.setAction(action);
102106
newLifecycleState.setStep(step);
103107

108+
// Build a new index metadata with the version incremented and the new lifecycle state.
109+
IndexMetadata.Builder indexMetadataBuilder = IndexMetadata.builder(targetIndexMetadata)
110+
.version(targetIndexMetadata.getVersion() + 1)
111+
.putCustom(ILM_CUSTOM_METADATA_KEY, newLifecycleState.build().asMap());
112+
113+
// Remove the skip setting if it's present.
114+
if (targetIndexMetadata.getSettings().hasValue(LifecycleSettings.LIFECYCLE_SKIP)) {
115+
final var newSettings = Settings.builder().put(targetIndexMetadata.getSettings());
116+
newSettings.remove(LifecycleSettings.LIFECYCLE_SKIP);
117+
indexMetadataBuilder.settingsVersion(targetIndexMetadata.getSettingsVersion() + 1).settings(newSettings);
118+
}
119+
104120
return projectState.updateProject(
105-
projectState.metadata().withLifecycleState(targetIndexMetadata.getIndex(), newLifecycleState.build())
121+
ProjectMetadata.builder(projectState.metadata()).put(indexMetadataBuilder.build(), false).build()
106122
);
107123
}
108124

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrinkStep.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ public void performAction(
8383
// need to remove the single shard, allocation so replicas can be allocated
8484
builder.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, indexMetadata.getNumberOfReplicas())
8585
.put(LifecycleSettings.LIFECYCLE_NAME, policyName)
86+
// We add the skip setting to prevent ILM from processing the shrunken index before the execution state has been copied - which
87+
// could happen if the shards of the shrunken index take a long time to allocate.
88+
.put(LifecycleSettings.LIFECYCLE_SKIP, true)
8689
.put(IndexMetadata.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getKey() + "_id", (String) null);
8790
if (numberOfShards != null) {
8891
builder.put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, numberOfShards);

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrinkStepTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ public void testPerformAction() throws Exception {
106106
Settings.Builder builder = Settings.builder();
107107
builder.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, sourceIndexMetadata.getNumberOfReplicas())
108108
.put(LifecycleSettings.LIFECYCLE_NAME, lifecycleName)
109+
.put(LifecycleSettings.LIFECYCLE_SKIP, true)
109110
.put(IndexMetadata.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getKey() + "_id", (String) null);
110111
if (step.getNumberOfShards() != null) {
111112
builder.put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, step.getNumberOfShards());

0 commit comments

Comments
 (0)