Skip to content

Commit fc1eeaa

Browse files
nielsbaumanelasticsearchmachine
andauthored
[8.19] Use ILM skip setting in shrink action (elastic#129455) (elastic#129544)
* Use ILM skip setting in shrink action (elastic#129455) 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 elastic#109206 (cherry picked from commit 217275c) # Conflicts: # x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CopyExecutionStateStep.java * [CI] Auto commit changes from spotless * Fix compilation --------- Co-authored-by: elasticsearchmachine <[email protected]>
1 parent f33ac4b commit fc1eeaa

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

docs/changelog/129455.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 129455
2+
summary: Prevent ILM from processing shrunken index before its execution state is copied over
3+
area: ILM+SLM
4+
type: bug
5+
issues:
6+
- 109206

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@
1313
import org.elasticsearch.cluster.ClusterState;
1414
import org.elasticsearch.cluster.metadata.IndexMetadata;
1515
import org.elasticsearch.cluster.metadata.LifecycleExecutionState;
16+
import org.elasticsearch.cluster.metadata.Metadata;
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,11 +105,21 @@ public ClusterState performAction(Index index, ClusterState clusterState) {
101105
newLifecycleState.setAction(action);
102106
newLifecycleState.setStep(step);
103107

104-
return LifecycleExecutionStateUtils.newClusterStateWithLifecycleState(
105-
clusterState,
106-
targetIndexMetadata.getIndex(),
107-
newLifecycleState.build()
108-
);
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+
120+
return ClusterState.builder(clusterState)
121+
.metadata(Metadata.builder(clusterState.metadata()).put(indexMetadataBuilder.build(), false))
122+
.build();
109123
}
110124

111125
@Override

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)