Skip to content

Commit dc90751

Browse files
authored
Make ILM BranchingStep predicate project-aware (elastic#127804)
This change only makes the predicate inside `BranchingStep` project-aware, to limit the scope of PRs. A follow-up PR will target `ClusterStateActionStep` and all its descendants (including `BranchingStep`).
1 parent 2e08efe commit dc90751

File tree

8 files changed

+44
-52
lines changed

8 files changed

+44
-52
lines changed

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.apache.lucene.util.SetOnce;
1313
import org.elasticsearch.cluster.ClusterState;
1414
import org.elasticsearch.cluster.metadata.IndexMetadata;
15+
import org.elasticsearch.cluster.metadata.ProjectMetadata;
1516
import org.elasticsearch.index.Index;
1617

1718
import java.util.Objects;
@@ -29,7 +30,7 @@ public class BranchingStep extends ClusterStateActionStep {
2930

3031
private final StepKey nextStepKeyOnFalse;
3132
private final StepKey nextStepKeyOnTrue;
32-
private final BiPredicate<Index, ClusterState> predicate;
33+
private final BiPredicate<Index, ProjectMetadata> predicate;
3334
private final SetOnce<Boolean> predicateValue;
3435

3536
/**
@@ -41,7 +42,12 @@ public class BranchingStep extends ClusterStateActionStep {
4142
* @param nextStepKeyOnTrue the key of the step to run if predicate returns true
4243
* @param predicate the condition to check when deciding which step to run next
4344
*/
44-
public BranchingStep(StepKey key, StepKey nextStepKeyOnFalse, StepKey nextStepKeyOnTrue, BiPredicate<Index, ClusterState> predicate) {
45+
public BranchingStep(
46+
StepKey key,
47+
StepKey nextStepKeyOnFalse,
48+
StepKey nextStepKeyOnTrue,
49+
BiPredicate<Index, ProjectMetadata> predicate
50+
) {
4551
// super.nextStepKey is set to null since it is not used by this step
4652
super(key, null);
4753
this.nextStepKeyOnFalse = nextStepKeyOnFalse;
@@ -63,7 +69,7 @@ public ClusterState performAction(Index index, ClusterState clusterState) {
6369
logger.debug("[{}] lifecycle action for index [{}] executed but index no longer exists", getKey().action(), index.getName());
6470
return clusterState;
6571
}
66-
predicateValue.set(predicate.test(index, clusterState));
72+
predicateValue.set(predicate.test(index, clusterState.metadata().getProject()));
6773
return clusterState;
6874
}
6975

@@ -98,7 +104,7 @@ final StepKey getNextStepKeyOnTrue() {
98104
return nextStepKeyOnTrue;
99105
}
100106

101-
public final BiPredicate<Index, ClusterState> getPredicate() {
107+
public final BiPredicate<Index, ProjectMetadata> getPredicate() {
102108
return predicate;
103109
}
104110

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ public List<Step> toSteps(Client client, String phase, StepKey nextStepKey) {
157157
timeSeriesIndexCheckBranchKey,
158158
nextStepKey,
159159
checkNotWriteIndex,
160-
(index, clusterState) -> {
161-
IndexMetadata indexMetadata = clusterState.metadata().getProject().index(index);
160+
(index, project) -> {
161+
IndexMetadata indexMetadata = project.index(index);
162162
assert indexMetadata != null : "invalid cluster metadata. index [" + index.getName() + "] metadata not found";
163163
if (IndexSettings.MODE.get(indexMetadata.getSettings()) != IndexMode.TIME_SERIES) {
164164
return false;
@@ -258,8 +258,8 @@ public List<Step> toSteps(Client client, String phase, StepKey nextStepKey) {
258258
dataStreamCheckBranchingKey,
259259
swapAliasesKey,
260260
replaceDataStreamIndexKey,
261-
(index, clusterState) -> {
262-
IndexAbstraction indexAbstraction = clusterState.metadata().getProject().getIndicesLookup().get(index.getName());
261+
(index, project) -> {
262+
IndexAbstraction indexAbstraction = project.getIndicesLookup().get(index.getName());
263263
assert indexAbstraction != null : "invalid cluster metadata. index [" + index.getName() + "] was not found";
264264
return indexAbstraction.getParentDataStream() != null;
265265
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ public List<Step> toSteps(Client client, String phase, Step.StepKey nextStepKey)
135135
preForceMergeBranchingKey,
136136
checkNotWriteIndexKey,
137137
nextStepKey,
138-
(index, clusterState) -> {
139-
IndexMetadata indexMetadata = clusterState.metadata().getProject().index(index);
138+
(index, project) -> {
139+
IndexMetadata indexMetadata = project.index(index);
140140
assert indexMetadata != null : "index " + index.getName() + " must exist in the cluster state";
141141
if (indexMetadata.getSettings().get(LifecycleSettings.SNAPSHOT_INDEX_NAME) != null) {
142142
String policyName = indexMetadata.getLifecyclePolicyName();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ public List<Step> toSteps(Client client, String phase, StepKey nextStepKey) {
104104
preMigrateBranchingKey,
105105
migrationKey,
106106
nextStepKey,
107-
(index, clusterState) -> {
108-
IndexMetadata indexMetadata = clusterState.metadata().getProject().index(index);
107+
(index, project) -> {
108+
IndexMetadata indexMetadata = project.index(index);
109109

110110
// partially mounted indices will already have data_frozen, and we don't want to change that if they do
111111
if (indexMetadata.isPartialSearchableSnapshot()) {

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,13 @@ public List<Step> toSteps(Client client, String phase, StepKey nextStepKey, XPac
189189
preActionBranchingKey,
190190
checkNoWriteIndex,
191191
nextStepKey,
192-
(index, clusterState) -> {
192+
(index, project) -> {
193193
if (SEARCHABLE_SNAPSHOT_FEATURE.checkWithoutTracking(licenseState) == false) {
194194
logger.error("[{}] action is not available in the current license", SearchableSnapshotAction.NAME);
195195
throw LicenseUtils.newComplianceException("searchable-snapshots");
196196
}
197197

198-
IndexMetadata indexMetadata = clusterState.getMetadata().getProject().index(index);
198+
IndexMetadata indexMetadata = project.index(index);
199199
assert indexMetadata != null : "index " + index.getName() + " must exist in the cluster state";
200200
String policyName = indexMetadata.getLifecyclePolicyName();
201201
SearchableSnapshotMetadata searchableSnapshotMetadata = extractSearchableSnapshotFromSettings(indexMetadata);
@@ -278,8 +278,8 @@ public List<Step> toSteps(Client client, String phase, StepKey nextStepKey, XPac
278278
skipGeneratingSnapshotKey,
279279
keyForSnapshotGeneration,
280280
waitForDataTierKey,
281-
(index, clusterState) -> {
282-
IndexMetadata indexMetadata = clusterState.getMetadata().getProject().index(index);
281+
(index, project) -> {
282+
IndexMetadata indexMetadata = project.index(index);
283283
String policyName = indexMetadata.getLifecyclePolicyName();
284284
LifecycleExecutionState lifecycleExecutionState = indexMetadata.getLifecycleExecutionState();
285285
SearchableSnapshotMetadata searchableSnapshotMetadata = extractSearchableSnapshotFromSettings(indexMetadata);
@@ -379,8 +379,8 @@ public List<Step> toSteps(Client client, String phase, StepKey nextStepKey, XPac
379379
dataStreamCheckBranchingKey,
380380
swapAliasesKey,
381381
replaceDataStreamIndexKey,
382-
(index, clusterState) -> {
383-
IndexAbstraction indexAbstraction = clusterState.metadata().getProject().getIndicesLookup().get(index.getName());
382+
(index, project) -> {
383+
IndexAbstraction indexAbstraction = project.getIndicesLookup().get(index.getName());
384384
assert indexAbstraction != null : "invalid cluster metadata. index [" + index.getName() + "] was not found";
385385
return indexAbstraction.getParentDataStream() != null;
386386
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,8 @@ public List<Step> toSteps(Client client, String phase, Step.StepKey nextStepKey)
287287
dataStreamCheckBranchingKey,
288288
aliasKey,
289289
replaceDataStreamIndexKey,
290-
(index, clusterState) -> {
291-
IndexAbstraction indexAbstraction = clusterState.metadata().getProject().getIndicesLookup().get(index.getName());
290+
(index, project) -> {
291+
IndexAbstraction indexAbstraction = project.getIndicesLookup().get(index.getName());
292292
assert indexAbstraction != null : "invalid cluster metadata. index [" + index.getName() + "] was not found";
293293
return indexAbstraction.getParentDataStream() != null;
294294
}

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,12 @@ public List<Step> toSteps(Client client, String phase, StepKey nextStepKey) {
5353
StepKey openFollowerIndex = new StepKey(phase, NAME, OPEN_FOLLOWER_INDEX_STEP_NAME);
5454
StepKey waitForYellowStep = new StepKey(phase, NAME, WaitForIndexColorStep.NAME);
5555

56-
BranchingStep conditionalSkipUnfollowStep = new BranchingStep(
57-
preUnfollowKey,
58-
indexingComplete,
59-
nextStepKey,
60-
(index, clusterState) -> {
61-
IndexMetadata followerIndex = clusterState.metadata().getProject().index(index);
62-
Map<String, String> customIndexMetadata = followerIndex.getCustomData(CCR_METADATA_KEY);
63-
// if the index has no CCR metadata we'll skip the unfollow action completely
64-
return customIndexMetadata == null;
65-
}
66-
);
56+
BranchingStep conditionalSkipUnfollowStep = new BranchingStep(preUnfollowKey, indexingComplete, nextStepKey, (index, project) -> {
57+
IndexMetadata followerIndex = project.index(index);
58+
Map<String, String> customIndexMetadata = followerIndex.getCustomData(CCR_METADATA_KEY);
59+
// if the index has no CCR metadata we'll skip the unfollow action completely
60+
return customIndexMetadata == null;
61+
});
6762
WaitForIndexingCompleteStep step1 = new WaitForIndexingCompleteStep(indexingComplete, waitForFollowShardTasks);
6863
WaitForFollowShardTasksStep step2 = new WaitForFollowShardTasksStep(waitForFollowShardTasks, pauseFollowerIndex, client);
6964
PauseFollowerIndexStep step3 = new PauseFollowerIndexStep(pauseFollowerIndex, closeFollowerIndex, client);

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

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,47 +10,39 @@
1010
import org.elasticsearch.cluster.ClusterName;
1111
import org.elasticsearch.cluster.ClusterState;
1212
import org.elasticsearch.cluster.metadata.IndexMetadata;
13-
import org.elasticsearch.cluster.metadata.Metadata;
14-
import org.elasticsearch.index.Index;
13+
import org.elasticsearch.cluster.metadata.ProjectId;
14+
import org.elasticsearch.cluster.metadata.ProjectMetadata;
15+
import org.elasticsearch.core.FixForMultiProject;
1516
import org.elasticsearch.index.IndexVersion;
1617
import org.elasticsearch.xpack.core.ilm.Step.StepKey;
1718

18-
import java.util.function.BiPredicate;
19-
2019
import static org.hamcrest.Matchers.equalTo;
2120

2221
public class BranchingStepTests extends AbstractStepTestCase<BranchingStep> {
2322

2423
public void testPredicateNextStepChange() {
2524
String indexName = randomAlphaOfLength(5);
26-
ClusterState state = ClusterState.builder(ClusterName.DEFAULT)
27-
.metadata(
28-
Metadata.builder()
29-
.put(IndexMetadata.builder(indexName).settings(settings(IndexVersion.current())).numberOfShards(1).numberOfReplicas(0))
30-
)
25+
@FixForMultiProject // Use non-default project ID and pass project directly instead of state
26+
ProjectMetadata project = ProjectMetadata.builder(ProjectId.DEFAULT)
27+
.put(IndexMetadata.builder(indexName).settings(settings(IndexVersion.current())).numberOfShards(1).numberOfReplicas(0))
3128
.build();
29+
ClusterState state = ClusterState.builder(ClusterName.DEFAULT).putProjectMetadata(project).build();
3230
StepKey stepKey = new StepKey(randomAlphaOfLength(5), randomAlphaOfLength(5), BranchingStep.NAME);
3331
StepKey nextStepKey = new StepKey(randomAlphaOfLength(6), randomAlphaOfLength(6), BranchingStep.NAME);
3432
StepKey nextSkipKey = new StepKey(randomAlphaOfLength(7), randomAlphaOfLength(7), BranchingStep.NAME);
3533
{
3634
BranchingStep step = new BranchingStep(stepKey, nextStepKey, nextSkipKey, (i, c) -> true);
3735
expectThrows(IllegalStateException.class, step::getNextStepKey);
38-
step.performAction(state.metadata().getProject().index(indexName).getIndex(), state);
36+
step.performAction(project.index(indexName).getIndex(), state);
3937
assertThat(step.getNextStepKey(), equalTo(step.getNextStepKeyOnTrue()));
40-
expectThrows(
41-
SetOnce.AlreadySetException.class,
42-
() -> step.performAction(state.metadata().getProject().index(indexName).getIndex(), state)
43-
);
38+
expectThrows(SetOnce.AlreadySetException.class, () -> step.performAction(project.index(indexName).getIndex(), state));
4439
}
4540
{
4641
BranchingStep step = new BranchingStep(stepKey, nextStepKey, nextSkipKey, (i, c) -> false);
4742
expectThrows(IllegalStateException.class, step::getNextStepKey);
48-
step.performAction(state.metadata().getProject().index(indexName).getIndex(), state);
43+
step.performAction(project.index(indexName).getIndex(), state);
4944
assertThat(step.getNextStepKey(), equalTo(step.getNextStepKeyOnFalse()));
50-
expectThrows(
51-
SetOnce.AlreadySetException.class,
52-
() -> step.performAction(state.metadata().getProject().index(indexName).getIndex(), state)
53-
);
45+
expectThrows(SetOnce.AlreadySetException.class, () -> step.performAction(project.index(indexName).getIndex(), state));
5446
}
5547
}
5648

@@ -67,7 +59,6 @@ public BranchingStep mutateInstance(BranchingStep instance) {
6759
StepKey key = instance.getKey();
6860
StepKey nextStepKey = instance.getNextStepKeyOnFalse();
6961
StepKey nextSkipStepKey = instance.getNextStepKeyOnTrue();
70-
BiPredicate<Index, ClusterState> predicate = instance.getPredicate();
7162

7263
switch (between(0, 2)) {
7364
case 0 -> key = new StepKey(key.phase(), key.action(), key.name() + randomAlphaOfLength(5));
@@ -80,7 +71,7 @@ public BranchingStep mutateInstance(BranchingStep instance) {
8071
default -> throw new AssertionError("Illegal randomisation branch");
8172
}
8273

83-
return new BranchingStep(key, nextStepKey, nextSkipStepKey, predicate);
74+
return new BranchingStep(key, nextStepKey, nextSkipStepKey, instance.getPredicate());
8475
}
8576

8677
@Override

0 commit comments

Comments
 (0)