Skip to content

Commit 10af017

Browse files
authored
Make ILM ClusterStateWaitStep project-aware (#129042)
This is part of an iterative process to make ILM project-aware.
1 parent 10f355d commit 10af017

30 files changed

+365
-406
lines changed

test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2829,4 +2829,11 @@ public static IndexSearcher newSearcher(IndexReader r, boolean maybeWrap, boolea
28292829
public static ProjectState projectStateFromProject(ProjectMetadata.Builder project) {
28302830
return ClusterState.builder(ClusterName.DEFAULT).putProjectMetadata(project).build().projectState(project.getId());
28312831
}
2832+
2833+
/**
2834+
* Constructs an empty {@link ProjectState} with one (empty) project.
2835+
*/
2836+
public static ProjectState projectStateWithEmptyProject() {
2837+
return projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()));
2838+
}
28322839
}

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

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import org.apache.logging.log4j.LogManager;
1010
import org.apache.logging.log4j.Logger;
1111
import org.elasticsearch.action.support.ActiveShardCount;
12-
import org.elasticsearch.cluster.ClusterState;
12+
import org.elasticsearch.cluster.ProjectState;
1313
import org.elasticsearch.cluster.metadata.IndexMetadata;
1414
import org.elasticsearch.cluster.routing.IndexRoutingTable;
1515
import org.elasticsearch.cluster.routing.IndexShardRoutingTable;
@@ -45,18 +45,14 @@ public boolean isRetryable() {
4545
}
4646

4747
@Override
48-
public Result isConditionMet(Index index, ClusterState clusterState) {
49-
IndexMetadata idxMeta = clusterState.metadata().getProject().index(index);
48+
public Result isConditionMet(Index index, ProjectState currentState) {
49+
IndexMetadata idxMeta = currentState.metadata().index(index);
5050
if (idxMeta == null) {
5151
// Index must have been since deleted, ignore it
5252
logger.debug("[{}] lifecycle action for index [{}] executed but index no longer exists", getKey().action(), index.getName());
5353
return new Result(false, null);
5454
}
55-
if (ActiveShardCount.ALL.enoughShardsActive(
56-
clusterState.metadata().getProject(),
57-
clusterState.routingTable(),
58-
index.getName()
59-
) == false) {
55+
if (ActiveShardCount.ALL.enoughShardsActive(currentState.metadata(), currentState.routingTable(), index.getName()) == false) {
6056
logger.debug(
6157
"[{}] lifecycle action for index [{}] cannot make progress because not all shards are active",
6258
getKey().action(),
@@ -68,12 +64,12 @@ public Result isConditionMet(Index index, ClusterState clusterState) {
6864
AllocationDeciders allocationDeciders = new AllocationDeciders(
6965
List.of(
7066
new FilterAllocationDecider(
71-
clusterState.getMetadata().settings(),
67+
currentState.cluster().metadata().settings(),
7268
new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS)
7369
)
7470
)
7571
);
76-
int allocationPendingAllShards = getPendingAllocations(index, allocationDeciders, clusterState);
72+
int allocationPendingAllShards = getPendingAllocations(index, allocationDeciders, currentState);
7773

7874
if (allocationPendingAllShards > 0) {
7975
logger.debug(
@@ -89,22 +85,22 @@ public Result isConditionMet(Index index, ClusterState clusterState) {
8985
}
9086
}
9187

92-
static int getPendingAllocations(Index index, AllocationDeciders allocationDeciders, ClusterState clusterState) {
88+
static int getPendingAllocations(Index index, AllocationDeciders allocationDeciders, ProjectState currentState) {
9389
// All the allocation attributes are already set so just need to check
9490
// if the allocation has happened
95-
RoutingAllocation allocation = new RoutingAllocation(allocationDeciders, clusterState, null, null, System.nanoTime());
91+
RoutingAllocation allocation = new RoutingAllocation(allocationDeciders, currentState.cluster(), null, null, System.nanoTime());
9692

9793
int allocationPendingAllShards = 0;
9894

99-
final IndexRoutingTable indexRoutingTable = clusterState.getRoutingTable().index(index);
95+
final IndexRoutingTable indexRoutingTable = currentState.routingTable().index(index);
10096
for (int shardId = 0; shardId < indexRoutingTable.size(); shardId++) {
10197
final IndexShardRoutingTable indexShardRoutingTable = indexRoutingTable.shard(shardId);
10298
for (int copy = 0; copy < indexShardRoutingTable.size(); copy++) {
10399
ShardRouting shardRouting = indexShardRoutingTable.shard(copy);
104100
String currentNodeId = shardRouting.currentNodeId();
105101
boolean canRemainOnCurrentNode = allocationDeciders.canRemain(
106102
shardRouting,
107-
clusterState.getRoutingNodes().node(currentNodeId),
103+
currentState.cluster().getRoutingNodes().node(currentNodeId),
108104
allocation
109105
).type() == Decision.Type.YES;
110106
if (canRemainOnCurrentNode == false || shardRouting.started() == false) {

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88

99
import org.apache.logging.log4j.LogManager;
1010
import org.apache.logging.log4j.Logger;
11-
import org.elasticsearch.cluster.ClusterState;
11+
import org.elasticsearch.cluster.ProjectState;
1212
import org.elasticsearch.cluster.metadata.DataStream;
1313
import org.elasticsearch.cluster.metadata.IndexAbstraction;
1414
import org.elasticsearch.cluster.metadata.IndexMetadata;
15-
import org.elasticsearch.cluster.metadata.Metadata;
1615
import org.elasticsearch.common.Strings;
1716
import org.elasticsearch.index.Index;
1817
import org.elasticsearch.xpack.core.ilm.step.info.SingleMessageFieldInfo;
@@ -39,9 +38,8 @@ public boolean isRetryable() {
3938
}
4039

4140
@Override
42-
public Result isConditionMet(Index index, ClusterState clusterState) {
43-
Metadata metadata = clusterState.metadata();
44-
IndexMetadata indexMetadata = metadata.getProject().index(index);
41+
public Result isConditionMet(Index index, ProjectState currentState) {
42+
IndexMetadata indexMetadata = currentState.metadata().index(index);
4543
String indexName = index.getName();
4644

4745
if (indexMetadata == null) {
@@ -56,7 +54,7 @@ public Result isConditionMet(Index index, ClusterState clusterState) {
5654
}
5755

5856
String policyName = indexMetadata.getLifecyclePolicyName();
59-
IndexAbstraction indexAbstraction = clusterState.metadata().getProject().getIndicesLookup().get(indexName);
57+
IndexAbstraction indexAbstraction = currentState.metadata().getIndicesLookup().get(indexName);
6058
assert indexAbstraction != null : "invalid cluster metadata. index [" + indexName + "] was not found";
6159
DataStream dataStream = indexAbstraction.getParentDataStream();
6260
if (dataStream != null) {

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import org.apache.logging.log4j.LogManager;
1111
import org.apache.logging.log4j.Logger;
12-
import org.elasticsearch.cluster.ClusterState;
12+
import org.elasticsearch.cluster.ProjectState;
1313
import org.elasticsearch.cluster.metadata.IndexMetadata;
1414
import org.elasticsearch.cluster.metadata.SingleNodeShutdownMetadata;
1515
import org.elasticsearch.cluster.routing.IndexRoutingTable;
@@ -51,8 +51,8 @@ public boolean isCompletable() {
5151
}
5252

5353
@Override
54-
public Result isConditionMet(Index index, ClusterState clusterState) {
55-
IndexMetadata idxMeta = clusterState.metadata().getProject().index(index);
54+
public Result isConditionMet(Index index, ProjectState currentState) {
55+
IndexMetadata idxMeta = currentState.metadata().index(index);
5656

5757
if (idxMeta == null) {
5858
// Index must have been since deleted, ignore it
@@ -69,10 +69,10 @@ public Result isConditionMet(Index index, ClusterState clusterState) {
6969
throw new IllegalStateException("Cannot check shrink allocation as there are no allocation rules by _id");
7070
}
7171

72-
var shutdown = clusterState.metadata().nodeShutdowns().get(idShardsShouldBeOn);
72+
var shutdown = currentState.cluster().metadata().nodeShutdowns().get(idShardsShouldBeOn);
7373
boolean nodeBeingRemoved = shutdown != null && shutdown.getType() != SingleNodeShutdownMetadata.Type.RESTART;
7474

75-
final IndexRoutingTable routingTable = clusterState.getRoutingTable().index(index);
75+
final IndexRoutingTable routingTable = currentState.routingTable().index(index);
7676
int foundShards = 0;
7777
for (ShardRouting shard : routingTable.shardsWithState(ShardRoutingState.STARTED)) {
7878
final String currentNodeId = shard.currentNodeId();

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import org.apache.logging.log4j.LogManager;
1010
import org.apache.logging.log4j.Logger;
11-
import org.elasticsearch.cluster.ClusterState;
11+
import org.elasticsearch.cluster.ProjectState;
1212
import org.elasticsearch.cluster.metadata.IndexMetadata;
1313
import org.elasticsearch.common.Strings;
1414
import org.elasticsearch.index.Index;
@@ -40,8 +40,8 @@ public Integer getNumberOfShards() {
4040
}
4141

4242
@Override
43-
public Result isConditionMet(Index index, ClusterState clusterState) {
44-
IndexMetadata indexMetadata = clusterState.metadata().getProject().index(index);
43+
public Result isConditionMet(Index index, ProjectState currentState) {
44+
IndexMetadata indexMetadata = currentState.metadata().index(index);
4545
if (indexMetadata == null) {
4646
// Index must have been since deleted, ignore it
4747
logger.debug("[{}] lifecycle action for index [{}] executed but index no longer exists", getKey().action(), index.getName());

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77
package org.elasticsearch.xpack.core.ilm;
88

9-
import org.elasticsearch.cluster.ClusterState;
9+
import org.elasticsearch.cluster.ProjectState;
1010
import org.elasticsearch.index.Index;
1111
import org.elasticsearch.xcontent.ToXContentObject;
1212

@@ -21,7 +21,7 @@ public ClusterStateWaitStep(StepKey key, StepKey nextStepKey) {
2121
super(key, nextStepKey);
2222
}
2323

24-
public abstract Result isConditionMet(Index index, ClusterState clusterState);
24+
public abstract Result isConditionMet(Index index, ProjectState currentState);
2525

2626
/**
2727
* Whether the step can be completed at all. This only affects the

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import org.apache.logging.log4j.LogManager;
1111
import org.apache.logging.log4j.Logger;
12-
import org.elasticsearch.cluster.ClusterState;
12+
import org.elasticsearch.cluster.ProjectState;
1313
import org.elasticsearch.cluster.metadata.IndexMetadata;
1414
import org.elasticsearch.cluster.metadata.LifecycleExecutionState;
1515
import org.elasticsearch.common.Strings;
@@ -52,15 +52,15 @@ public boolean isRetryable() {
5252
}
5353

5454
@Override
55-
public Result isConditionMet(Index index, ClusterState clusterState) {
56-
IndexMetadata idxMeta = clusterState.metadata().getProject().index(index);
55+
public Result isConditionMet(Index index, ProjectState currentState) {
56+
IndexMetadata idxMeta = currentState.metadata().index(index);
5757
if (idxMeta == null) {
5858
// Index must have been since deleted, ignore it
5959
logger.debug("[{}] lifecycle action for index [{}] executed but index no longer exists", getKey().action(), index.getName());
6060
return new Result(false, null);
6161
}
6262

63-
Result stepResult = stepToExecute.isConditionMet(index, clusterState);
63+
Result stepResult = stepToExecute.isConditionMet(index, currentState);
6464

6565
if (stepResult.complete() == false) {
6666
// checking the threshold after we execute the step to make sure we execute the wrapped step at least once (because time is a

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import org.apache.logging.log4j.LogManager;
1010
import org.apache.logging.log4j.Logger;
1111
import org.elasticsearch.action.support.ActiveShardCount;
12-
import org.elasticsearch.cluster.ClusterState;
12+
import org.elasticsearch.cluster.ProjectState;
1313
import org.elasticsearch.cluster.metadata.DesiredNodes;
1414
import org.elasticsearch.cluster.metadata.IndexMetadata;
1515
import org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders;
@@ -45,8 +45,8 @@ public boolean isRetryable() {
4545
}
4646

4747
@Override
48-
public Result isConditionMet(Index index, ClusterState clusterState) {
49-
IndexMetadata idxMeta = clusterState.metadata().getProject().index(index);
48+
public Result isConditionMet(Index index, ProjectState currentState) {
49+
IndexMetadata idxMeta = currentState.metadata().index(index);
5050
if (idxMeta == null) {
5151
// Index must have been since deleted, ignore it
5252
logger.debug("[{}] lifecycle action for index [{}] executed but index no longer exists", getKey().action(), index.getName());
@@ -55,16 +55,12 @@ public Result isConditionMet(Index index, ClusterState clusterState) {
5555
List<String> preferredTierConfiguration = idxMeta.getTierPreference();
5656
Optional<String> availableDestinationTier = DataTierAllocationDecider.preferredAvailableTier(
5757
preferredTierConfiguration,
58-
clusterState.getNodes(),
59-
DesiredNodes.latestFromClusterState(clusterState),
60-
clusterState.metadata().nodeShutdowns()
58+
currentState.cluster().getNodes(),
59+
DesiredNodes.latestFromClusterState(currentState.cluster()),
60+
currentState.cluster().metadata().nodeShutdowns()
6161
);
6262

63-
if (ActiveShardCount.ALL.enoughShardsActive(
64-
clusterState.metadata().getProject(),
65-
clusterState.routingTable(),
66-
index.getName()
67-
) == false) {
63+
if (ActiveShardCount.ALL.enoughShardsActive(currentState.metadata(), currentState.routingTable(), index.getName()) == false) {
6864
if (preferredTierConfiguration.isEmpty()) {
6965
logger.debug(
7066
"[{}] lifecycle action for index [{}] cannot make progress because not all shards are active",
@@ -103,7 +99,7 @@ public Result isConditionMet(Index index, ClusterState clusterState) {
10399
return new Result(true, null);
104100
}
105101

106-
int allocationPendingAllShards = getPendingAllocations(index, DECIDERS, clusterState);
102+
int allocationPendingAllShards = getPendingAllocations(index, DECIDERS, currentState);
107103

108104
if (allocationPendingAllShards > 0) {
109105
String statusMessage = availableDestinationTier.map(

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77
package org.elasticsearch.xpack.core.ilm;
88

9-
import org.elasticsearch.cluster.ClusterState;
9+
import org.elasticsearch.cluster.ProjectState;
1010
import org.elasticsearch.index.Index;
1111

1212
/**
@@ -27,7 +27,7 @@ public boolean isRetryable() {
2727
}
2828

2929
@Override
30-
public Result isConditionMet(Index index, ClusterState clusterState) {
30+
public Result isConditionMet(Index index, ProjectState currentState) {
3131
// We always want to move forward with this step so this should always be true
3232
return new Result(true, null);
3333
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import org.apache.logging.log4j.LogManager;
1010
import org.apache.logging.log4j.Logger;
1111
import org.elasticsearch.action.support.ActiveShardCount;
12-
import org.elasticsearch.cluster.ClusterState;
12+
import org.elasticsearch.cluster.ProjectState;
1313
import org.elasticsearch.cluster.metadata.IndexMetadata;
1414
import org.elasticsearch.cluster.metadata.LifecycleExecutionState;
1515
import org.elasticsearch.common.Strings;
@@ -42,8 +42,8 @@ public boolean isRetryable() {
4242
}
4343

4444
@Override
45-
public Result isConditionMet(Index index, ClusterState clusterState) {
46-
IndexMetadata indexMetadata = clusterState.metadata().getProject().index(index);
45+
public Result isConditionMet(Index index, ProjectState currentState) {
46+
IndexMetadata indexMetadata = currentState.metadata().index(index);
4747
if (indexMetadata == null) {
4848
// Index must have been since deleted, ignore it
4949
logger.debug("[{}] lifecycle action for index [{}] executed but index no longer exists", getKey().action(), index.getName());
@@ -55,16 +55,16 @@ public Result isConditionMet(Index index, ClusterState clusterState) {
5555

5656
// We only want to make progress if all shards of the shrunk index are
5757
// active
58-
boolean indexExists = clusterState.metadata().getProject().index(shrunkenIndexName) != null;
58+
boolean indexExists = currentState.metadata().index(shrunkenIndexName) != null;
5959
if (indexExists == false) {
6060
return new Result(false, new Info(false, -1, false));
6161
}
6262
boolean allShardsActive = ActiveShardCount.ALL.enoughShardsActive(
63-
clusterState.metadata().getProject(),
64-
clusterState.routingTable(),
63+
currentState.metadata(),
64+
currentState.routingTable(),
6565
shrunkenIndexName
6666
);
67-
int numShrunkIndexShards = clusterState.metadata().getProject().index(shrunkenIndexName).getNumberOfShards();
67+
int numShrunkIndexShards = currentState.metadata().index(shrunkenIndexName).getNumberOfShards();
6868
if (allShardsActive) {
6969
return new Result(true, null);
7070
} else {

0 commit comments

Comments
 (0)