Skip to content

Commit 67663df

Browse files
[7.x] Copy all execution state in CopyExecutionStateStep (#73792) (#73848)
Co-authored-by: Elastic Machine <[email protected]>
1 parent 545de3a commit 67663df

File tree

8 files changed

+117
-57
lines changed

8 files changed

+117
-57
lines changed

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,11 @@ public ClusterState performAction(Index index, ClusterState clusterState) {
8080
String step = targetNextStepKey.getName();
8181
long lifecycleDate = lifecycleState.getLifecycleDate();
8282

83-
LifecycleExecutionState.Builder relevantTargetCustomData = LifecycleExecutionState.builder();
84-
relevantTargetCustomData.setIndexCreationDate(lifecycleDate);
85-
relevantTargetCustomData.setAction(action);
83+
LifecycleExecutionState.Builder relevantTargetCustomData = LifecycleExecutionState.builder(lifecycleState);
84+
// Override the phase, action, and step for the target next StepKey
8685
relevantTargetCustomData.setPhase(phase);
86+
relevantTargetCustomData.setAction(action);
8787
relevantTargetCustomData.setStep(step);
88-
relevantTargetCustomData.setSnapshotRepository(lifecycleState.getSnapshotRepository());
89-
relevantTargetCustomData.setSnapshotName(lifecycleState.getSnapshotName());
90-
relevantTargetCustomData.setSnapshotIndexName(lifecycleState.getSnapshotIndexName());
91-
relevantTargetCustomData.setShrinkIndexName(lifecycleState.getShrinkIndexName());
9288

9389
Metadata.Builder newMetadata = Metadata.builder(clusterState.getMetadata())
9490
.put(IndexMetadata.builder(targetIndexMetadata)

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,14 +371,16 @@ public boolean equals(Object o) {
371371
Objects.equals(getSnapshotRepository(), that.getSnapshotRepository()) &&
372372
Objects.equals(getSnapshotName(), that.getSnapshotName()) &&
373373
Objects.equals(getSnapshotIndexName(), that.getSnapshotIndexName()) &&
374+
Objects.equals(getShrinkIndexName(), that.getShrinkIndexName()) &&
375+
Objects.equals(getRollupIndexName(), that.getRollupIndexName()) &&
374376
Objects.equals(getPhaseDefinition(), that.getPhaseDefinition());
375377
}
376378

377379
@Override
378380
public int hashCode() {
379381
return Objects.hash(getPhase(), getAction(), getStep(), getFailedStep(), isAutoRetryableError(), getFailedStepRetryCount(),
380382
getStepInfo(), getPhaseDefinition(), getLifecycleDate(), getPhaseTime(), getActionTime(), getStepTime(),
381-
getSnapshotRepository(), getSnapshotName(), getSnapshotIndexName());
383+
getSnapshotRepository(), getSnapshotName(), getSnapshotIndexName(), getShrinkIndexName(), getRollupIndexName());
382384
}
383385

384386
@Override

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ public LifecyclePolicy(LifecycleType type, String name, Map<String, Phase> phase
122122
this.phases = phases;
123123
this.type = type;
124124
this.metadata = metadata;
125+
}
126+
127+
public void validate() {
125128
this.type.validate(phases.values());
126129
}
127130

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ public static LifecyclePolicy loadPolicy(String name, String resource, NamedXCon
3939

4040
try (XContentParser parser = XContentType.JSON.xContent()
4141
.createParser(xContentRegistry, LoggingDeprecationHandler.THROW_UNSUPPORTED_OPERATION, source.utf8ToString())) {
42-
return LifecyclePolicy.parse(parser, name);
42+
LifecyclePolicy policy = LifecyclePolicy.parse(parser, name);
43+
policy.validate();
44+
return policy;
4345
}
4446
} catch (Exception e) {
4547
throw new IllegalArgumentException("unable to load policy [" + name + "] from [" + resource + "]", e);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public LifecyclePolicy getPolicy() {
6161

6262
@Override
6363
public ActionRequestValidationException validate() {
64+
this.policy.validate();
6465
ActionRequestValidationException err = null;
6566
String phaseTimingErr = TimeseriesLifecycleType.validateMonotonicallyIncreasingPhaseTimings(this.policy.getPhases().values());
6667
if (Strings.hasText(phaseTimingErr)) {

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.elasticsearch.cluster.metadata.Metadata;
1515
import org.elasticsearch.xpack.core.ilm.Step.StepKey;
1616

17+
import java.util.HashMap;
1718
import java.util.Map;
1819
import java.util.function.BiFunction;
1920

@@ -102,6 +103,43 @@ public void testPerformAction() {
102103
assertEquals(newIndexData.getSnapshotName(), oldIndexData.getSnapshotName());
103104
}
104105

106+
public void testAllStateCopied() {
107+
CopyExecutionStateStep step = createRandomInstance();
108+
String indexName = randomAlphaOfLengthBetween(5, 20);
109+
110+
IndexMetadata originalIndexMetadata = IndexMetadata.builder(indexName)
111+
.settings(settings(Version.CURRENT)).numberOfShards(randomIntBetween(1,5))
112+
.numberOfReplicas(randomIntBetween(1,5))
113+
.putCustom(ILM_CUSTOM_METADATA_KEY, createCustomMetadata())
114+
.build();
115+
IndexMetadata shrunkIndexMetadata =
116+
IndexMetadata.builder(step.getTargetIndexNameSupplier().apply(indexName, LifecycleExecutionState.builder().build()))
117+
.settings(settings(Version.CURRENT)).numberOfShards(randomIntBetween(1,5))
118+
.numberOfReplicas(randomIntBetween(1,5))
119+
.build();
120+
121+
ClusterState originalClusterState = ClusterState.builder(ClusterName.DEFAULT)
122+
.metadata(Metadata.builder()
123+
.put(originalIndexMetadata, false)
124+
.put(shrunkIndexMetadata, false))
125+
.build();
126+
127+
ClusterState newClusterState = step.performAction(originalIndexMetadata.getIndex(), originalClusterState);
128+
129+
LifecycleExecutionState oldIndexData = LifecycleExecutionState.fromIndexMetadata(originalIndexMetadata);
130+
LifecycleExecutionState newIndexData = LifecycleExecutionState
131+
.fromIndexMetadata(newClusterState.metadata().index(step.getTargetIndexNameSupplier().apply(indexName,
132+
LifecycleExecutionState.builder().build())));
133+
134+
Map<String, String> beforeMap = new HashMap<>(oldIndexData.asMap());
135+
// The target step key's StepKey is used in the new metadata, so update the "before" map with the new info so it can be compared
136+
beforeMap.put("phase", step.getTargetNextStepKey().getPhase());
137+
beforeMap.put("action", step.getTargetNextStepKey().getAction());
138+
beforeMap.put("step", step.getTargetNextStepKey().getName());
139+
Map<String, String> newMap = newIndexData.asMap();
140+
assertThat(beforeMap, equalTo(newMap));
141+
}
142+
105143
public void testPerformActionWithNoTarget() {
106144
CopyExecutionStateStep step = createRandomInstance();
107145
String indexName = randomAlphaOfLengthBetween(5, 20);

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

Lines changed: 62 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -137,52 +137,65 @@ public void testGetCurrentStepKey() {
137137

138138
private static LifecycleExecutionState mutate(LifecycleExecutionState toMutate) {
139139
LifecycleExecutionState.Builder newState = LifecycleExecutionState.builder(toMutate);
140-
boolean changed = false;
141-
if (randomBoolean()) {
142-
newState.setPhase(randomValueOtherThan(toMutate.getPhase(), () -> randomAlphaOfLengthBetween(5, 20)));
143-
changed = true;
140+
switch (randomIntBetween(0, 17)) {
141+
case 0:
142+
newState.setPhase(randomValueOtherThan(toMutate.getPhase(), () -> randomAlphaOfLengthBetween(5, 20)));
143+
break;
144+
case 1:
145+
newState.setAction(randomValueOtherThan(toMutate.getAction(), () -> randomAlphaOfLengthBetween(5, 20)));
146+
break;
147+
case 2:
148+
newState.setStep(randomValueOtherThan(toMutate.getStep(), () -> randomAlphaOfLengthBetween(5, 20)));
149+
break;
150+
case 3:
151+
newState.setPhaseDefinition(randomValueOtherThan(toMutate.getPhaseDefinition(), () -> randomAlphaOfLengthBetween(5, 20)));
152+
break;
153+
case 4:
154+
newState.setFailedStep(randomValueOtherThan(toMutate.getFailedStep(), () -> randomAlphaOfLengthBetween(5, 20)));
155+
break;
156+
case 5:
157+
newState.setStepInfo(randomValueOtherThan(toMutate.getStepInfo(), () -> randomAlphaOfLengthBetween(5, 20)));
158+
break;
159+
case 6:
160+
newState.setPhaseTime(randomValueOtherThan(toMutate.getPhaseTime(), ESTestCase::randomLong));
161+
break;
162+
case 7:
163+
newState.setActionTime(randomValueOtherThan(toMutate.getActionTime(), ESTestCase::randomLong));
164+
break;
165+
case 8:
166+
newState.setStepTime(randomValueOtherThan(toMutate.getStepTime(), ESTestCase::randomLong));
167+
break;
168+
case 9:
169+
newState.setIndexCreationDate(randomValueOtherThan(toMutate.getLifecycleDate(), ESTestCase::randomLong));
170+
break;
171+
case 10:
172+
newState.setShrinkIndexName(randomValueOtherThan(toMutate.getShrinkIndexName(), () -> randomAlphaOfLengthBetween(5, 20)));
173+
break;
174+
case 11:
175+
newState.setSnapshotRepository(randomValueOtherThan(toMutate.getSnapshotRepository(),
176+
() -> randomAlphaOfLengthBetween(5, 20)));
177+
break;
178+
case 12:
179+
newState.setSnapshotIndexName(randomValueOtherThan(toMutate.getSnapshotIndexName(),
180+
() -> randomAlphaOfLengthBetween(5, 20)));
181+
break;
182+
case 13:
183+
newState.setSnapshotName(randomValueOtherThan(toMutate.getSnapshotName(), () -> randomAlphaOfLengthBetween(5, 20)));
184+
break;
185+
case 14:
186+
newState.setRollupIndexName(randomValueOtherThan(toMutate.getRollupIndexName(), () -> randomAlphaOfLengthBetween(5, 20)));
187+
break;
188+
case 15:
189+
newState.setIsAutoRetryableError(randomValueOtherThan(toMutate.isAutoRetryableError(), ESTestCase::randomBoolean));
190+
break;
191+
case 16:
192+
newState.setFailedStepRetryCount(randomValueOtherThan(toMutate.getFailedStepRetryCount(), ESTestCase::randomInt));
193+
break;
194+
case 17:
195+
return LifecycleExecutionState.builder().build();
196+
default:
197+
throw new IllegalStateException("unknown randomization branch");
144198
}
145-
if (randomBoolean()) {
146-
newState.setAction(randomValueOtherThan(toMutate.getAction(), () -> randomAlphaOfLengthBetween(5, 20)));
147-
changed = true;
148-
}
149-
if (randomBoolean()) {
150-
newState.setStep(randomValueOtherThan(toMutate.getStep(), () -> randomAlphaOfLengthBetween(5, 20)));
151-
changed = true;
152-
}
153-
if (randomBoolean()) {
154-
newState.setPhaseDefinition(randomValueOtherThan(toMutate.getPhaseDefinition(), () -> randomAlphaOfLengthBetween(5, 20)));
155-
changed = true;
156-
}
157-
if (randomBoolean()) {
158-
newState.setFailedStep(randomValueOtherThan(toMutate.getFailedStep(), () -> randomAlphaOfLengthBetween(5, 20)));
159-
changed = true;
160-
}
161-
if (randomBoolean()) {
162-
newState.setStepInfo(randomValueOtherThan(toMutate.getStepInfo(), () -> randomAlphaOfLengthBetween(5, 20)));
163-
changed = true;
164-
}
165-
if (randomBoolean()) {
166-
newState.setPhaseTime(randomValueOtherThan(toMutate.getPhaseTime(), ESTestCase::randomLong));
167-
changed = true;
168-
}
169-
if (randomBoolean()) {
170-
newState.setActionTime(randomValueOtherThan(toMutate.getActionTime(), ESTestCase::randomLong));
171-
changed = true;
172-
}
173-
if (randomBoolean()) {
174-
newState.setStepTime(randomValueOtherThan(toMutate.getStepTime(), ESTestCase::randomLong));
175-
changed = true;
176-
}
177-
if (randomBoolean()) {
178-
newState.setIndexCreationDate(randomValueOtherThan(toMutate.getLifecycleDate(), ESTestCase::randomLong));
179-
changed = true;
180-
}
181-
182-
if (changed == false) {
183-
return LifecycleExecutionState.builder().build();
184-
}
185-
186199
return newState.build();
187200
}
188201

@@ -215,6 +228,10 @@ static Map<String, String> createCustomMetadata() {
215228
customMetadata.put("snapshot_repository", repositoryName);
216229
customMetadata.put("snapshot_name", snapshotName);
217230
customMetadata.put("snapshot_index_name", snapshotIndexName);
231+
customMetadata.put("shrink_index_name", randomAlphaOfLengthBetween(5, 20));
232+
customMetadata.put("rollup_index_name", randomAlphaOfLengthBetween(5, 20));
233+
customMetadata.put("is_auto_retryable_error", String.valueOf(randomBoolean()));
234+
customMetadata.put("failed_step_retry_count", String.valueOf(randomInt()));
218235
return customMetadata;
219236
}
220237
}

x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/actions/SearchableSnapshotActionIT.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.apache.http.entity.StringEntity;
1212
import org.elasticsearch.client.Request;
1313
import org.elasticsearch.client.Response;
14+
import org.elasticsearch.client.ResponseException;
1415
import org.elasticsearch.cluster.metadata.DataStream;
1516
import org.elasticsearch.cluster.metadata.IndexMetadata;
1617
import org.elasticsearch.cluster.metadata.Template;
@@ -207,7 +208,7 @@ public void testDeleteActionDeletesSearchableSnapshot() throws Exception {
207208
}
208209

209210
public void testCreateInvalidPolicy() {
210-
IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> createPolicy(client(), policy,
211+
ResponseException exception = expectThrows(ResponseException.class, () -> createPolicy(client(), policy,
211212
new Phase("hot", TimeValue.ZERO, org.elasticsearch.common.collect.Map.of(RolloverAction.NAME,
212213
new RolloverAction(null, null, null, 1L), SearchableSnapshotAction.NAME,
213214
new SearchableSnapshotAction(randomAlphaOfLengthBetween(4, 10)))),
@@ -218,7 +219,7 @@ public void testCreateInvalidPolicy() {
218219
)
219220
);
220221

221-
assertThat(exception.getMessage(), is("phases [warm,cold] define one or more of [forcemerge, freeze, shrink, rollup]" +
222+
assertThat(exception.getMessage(), containsString("phases [warm,cold] define one or more of [forcemerge, freeze, shrink, rollup]" +
222223
" actions which are not allowed after a managed index is mounted as a searchable snapshot"));
223224
}
224225

@@ -458,7 +459,7 @@ public void testSecondSearchableSnapshotUsingDifferentRepoThrows() throws Except
458459
String secondRepo = randomAlphaOfLengthBetween(10, 20);
459460
createSnapshotRepo(client(), snapshotRepo, randomBoolean());
460461
createSnapshotRepo(client(), secondRepo, randomBoolean());
461-
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () ->
462+
ResponseException e = expectThrows(ResponseException.class, () ->
462463
createPolicy(client(), policy, null, null,
463464
new Phase("cold", TimeValue.ZERO,
464465
singletonMap(SearchableSnapshotAction.NAME, new SearchableSnapshotAction(snapshotRepo, randomBoolean()))),

0 commit comments

Comments
 (0)