Skip to content

Commit b0c4ca4

Browse files
authored
Make AsyncWaitStep#evaluateCondition take an IndexMetadata instead of Index (#133292)
This avoids subclasses from having to look up the index metadata themselves, which in turn saves some null-checks.
1 parent e091235 commit b0c4ca4

17 files changed

+66
-82
lines changed

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

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

99
import org.elasticsearch.client.internal.Client;
1010
import org.elasticsearch.cluster.ProjectState;
11+
import org.elasticsearch.cluster.metadata.IndexMetadata;
1112
import org.elasticsearch.cluster.metadata.ProjectId;
1213
import org.elasticsearch.core.Nullable;
1314
import org.elasticsearch.core.TimeValue;
14-
import org.elasticsearch.index.Index;
1515
import org.elasticsearch.xcontent.ToXContentObject;
1616

1717
/**
@@ -40,7 +40,7 @@ protected Client getClient(ProjectId projectId) {
4040
return client.projectClient(projectId);
4141
}
4242

43-
public abstract void evaluateCondition(ProjectState state, Index index, Listener listener, TimeValue masterTimeout);
43+
public abstract void evaluateCondition(ProjectState state, IndexMetadata indexMetadata, Listener listener, TimeValue masterTimeout);
4444

4545
public interface Listener {
4646

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.elasticsearch.action.support.DefaultShardOperationFailedException;
1616
import org.elasticsearch.client.internal.Client;
1717
import org.elasticsearch.cluster.ProjectState;
18+
import org.elasticsearch.cluster.metadata.IndexMetadata;
1819
import org.elasticsearch.cluster.routing.ShardRouting;
1920
import org.elasticsearch.common.Strings;
2021
import org.elasticsearch.core.TimeValue;
@@ -56,7 +57,8 @@ public int getMaxNumSegments() {
5657
}
5758

5859
@Override
59-
public void evaluateCondition(ProjectState state, Index index, Listener listener, TimeValue masterTimeout) {
60+
public void evaluateCondition(ProjectState state, IndexMetadata indexMetadata, Listener listener, TimeValue masterTimeout) {
61+
Index index = indexMetadata.getIndex();
6062
getClient(state.projectId()).admin()
6163
.indices()
6264
.segments(new IndicesSegmentsRequest(index.getName()), ActionListener.wrap(response -> {

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import org.elasticsearch.cluster.metadata.IndexMetadata;
1313
import org.elasticsearch.common.Strings;
1414
import org.elasticsearch.core.TimeValue;
15-
import org.elasticsearch.index.Index;
1615
import org.elasticsearch.xcontent.ParseField;
1716
import org.elasticsearch.xcontent.ToXContentObject;
1817
import org.elasticsearch.xcontent.XContentBuilder;
@@ -39,16 +38,15 @@ public boolean isRetryable() {
3938
}
4039

4140
@Override
42-
public void evaluateCondition(ProjectState state, Index index, Listener listener, TimeValue masterTimeout) {
43-
IndexMetadata indexMetadata = state.metadata().index(index);
41+
public void evaluateCondition(ProjectState state, IndexMetadata indexMetadata, Listener listener, TimeValue masterTimeout) {
4442
Map<String, String> customIndexMetadata = indexMetadata.getCustomData(CCR_METADATA_KEY);
4543
if (customIndexMetadata == null) {
4644
listener.onResponse(true, null);
4745
return;
4846
}
4947

5048
FollowStatsAction.StatsRequest request = new FollowStatsAction.StatsRequest();
51-
request.setIndices(new String[] { index.getName() });
49+
request.setIndices(new String[] { indexMetadata.getIndex().getName() });
5250
getClient(state.projectId()).execute(
5351
FollowStatsAction.INSTANCE,
5452
request,

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
import org.elasticsearch.action.admin.indices.stats.ShardStats;
1616
import org.elasticsearch.client.internal.Client;
1717
import org.elasticsearch.cluster.ProjectState;
18+
import org.elasticsearch.cluster.metadata.IndexMetadata;
1819
import org.elasticsearch.cluster.metadata.ProjectId;
1920
import org.elasticsearch.core.TimeValue;
20-
import org.elasticsearch.index.Index;
2121
import org.elasticsearch.protocol.xpack.XPackInfoRequest;
2222
import org.elasticsearch.protocol.xpack.XPackInfoResponse;
2323
import org.elasticsearch.xpack.core.action.XPackInfoFeatureAction;
@@ -53,7 +53,7 @@ public boolean isRetryable() {
5353
}
5454

5555
@Override
56-
public void evaluateCondition(ProjectState state, Index index, Listener listener, TimeValue masterTimeout) {
56+
public void evaluateCondition(ProjectState state, IndexMetadata indexMetadata, Listener listener, TimeValue masterTimeout) {
5757
XPackInfoRequest xPackInfoRequest = new XPackInfoRequest();
5858
xPackInfoRequest.setCategories(EnumSet.of(XPackInfoRequest.Category.FEATURES));
5959
getClient(state.projectId()).execute(XPackInfoFeatureAction.CCR, xPackInfoRequest, ActionListener.wrap((xPackInfoResponse) -> {
@@ -62,14 +62,13 @@ public void evaluateCondition(ProjectState state, Index index, Listener listener
6262
listener.onResponse(true, null);
6363
return;
6464
}
65-
leaderIndexCheck(state.projectId(), index, listener);
65+
leaderIndexCheck(state.projectId(), indexMetadata.getIndex().getName(), listener);
6666
}, listener::onFailure));
6767
}
6868

69-
private void leaderIndexCheck(ProjectId projectId, Index index, Listener listener) {
69+
private void leaderIndexCheck(ProjectId projectId, String indexName, Listener listener) {
7070
IndicesStatsRequest request = new IndicesStatsRequest();
7171
request.clear();
72-
String indexName = index.getName();
7372
request.indices(indexName);
7473

7574
getClient(projectId).admin().indices().stats(request, ActionListener.wrap((response) -> {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ public boolean isRetryable() {
8080
}
8181

8282
@Override
83-
public void evaluateCondition(ProjectState state, Index index, Listener listener, TimeValue masterTimeout) {
83+
public void evaluateCondition(ProjectState state, IndexMetadata indexMetadata, Listener listener, TimeValue masterTimeout) {
84+
Index index = indexMetadata.getIndex();
8485
IndexAbstraction indexAbstraction = state.metadata().getIndicesLookup().get(index.getName());
8586
assert indexAbstraction != null : "invalid cluster metadata. index [" + index.getName() + "] was not found";
8687
final String rolloverTarget;
@@ -95,14 +96,13 @@ public void evaluateCondition(ProjectState state, Index index, Listener listener
9596
index.getName(),
9697
targetFailureStore ? "failure store " : "",
9798
dataStream.getName(),
98-
state.metadata().index(index).getLifecyclePolicyName()
99+
indexMetadata.getLifecyclePolicyName()
99100
);
100101
listener.onResponse(true, EmptyInfo.INSTANCE);
101102
return;
102103
}
103104
rolloverTarget = dataStream.getName();
104105
} else {
105-
IndexMetadata indexMetadata = state.metadata().index(index);
106106
String rolloverAlias = RolloverAction.LIFECYCLE_ROLLOVER_ALIAS_SETTING.get(indexMetadata.getSettings());
107107

108108
if (Strings.isNullOrEmpty(rolloverAlias)) {

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

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import org.elasticsearch.cluster.metadata.IndexMetadata;
1616
import org.elasticsearch.common.Strings;
1717
import org.elasticsearch.core.TimeValue;
18-
import org.elasticsearch.index.Index;
1918
import org.elasticsearch.xcontent.ToXContentObject;
2019
import org.elasticsearch.xpack.core.ilm.step.info.EmptyInfo;
2120
import org.elasticsearch.xpack.core.slm.SnapshotLifecycleMetadata;
@@ -43,7 +42,6 @@ public class WaitForSnapshotStep extends AsyncWaitStep {
4342

4443
private static final String UNEXPECTED_SNAPSHOT_STATE_MESSAGE =
4544
"unexpected number of snapshots retrieved for repository '%s' and snapshot '%s' (expected 1, found %d)";
46-
private static final String NO_INDEX_METADATA_MESSAGE = "no index metadata found for index '%s'";
4745
private static final String NO_ACTION_TIME_MESSAGE = "no information about ILM action start in index metadata for index '%s'";
4846

4947
private final String policy;
@@ -54,17 +52,12 @@ public class WaitForSnapshotStep extends AsyncWaitStep {
5452
}
5553

5654
@Override
57-
public void evaluateCondition(ProjectState state, Index index, Listener listener, TimeValue masterTimeout) {
58-
IndexMetadata indexMetadata = state.metadata().index(index);
59-
if (indexMetadata == null) {
60-
listener.onFailure(error(NO_INDEX_METADATA_MESSAGE, index.getName()));
61-
return;
62-
}
63-
55+
public void evaluateCondition(ProjectState state, IndexMetadata indexMetadata, Listener listener, TimeValue masterTimeout) {
56+
String indexName = indexMetadata.getIndex().getName();
6457
Long actionTime = indexMetadata.getLifecycleExecutionState().actionTime();
6558

6659
if (actionTime == null) {
67-
listener.onFailure(error(NO_ACTION_TIME_MESSAGE, index.getName()));
60+
listener.onFailure(error(NO_ACTION_TIME_MESSAGE, indexName));
6861
return;
6962
}
7063

@@ -112,10 +105,10 @@ public void evaluateCondition(ProjectState state, Index index, Listener listener
112105
if (response.getSnapshots().size() != 1) {
113106
listener.onFailure(error(UNEXPECTED_SNAPSHOT_STATE_MESSAGE, repositoryName, snapshotName, response.getSnapshots().size()));
114107
} else {
115-
if (response.getSnapshots().get(0).indices().contains(index.getName())) {
108+
if (response.getSnapshots().get(0).indices().contains(indexName)) {
116109
listener.onResponse(true, EmptyInfo.INSTANCE);
117110
} else {
118-
listener.onFailure(error(INDEX_NOT_INCLUDED_IN_SNAPSHOT_MESSAGE, policy, index.getName()));
111+
listener.onFailure(error(INDEX_NOT_INCLUDED_IN_SNAPSHOT_MESSAGE, policy, indexName));
119112
}
120113
}
121114
}, listener::onFailure));

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import org.elasticsearch.cluster.metadata.LifecycleExecutionState;
1313
import org.elasticsearch.common.Strings;
1414
import org.elasticsearch.core.TimeValue;
15-
import org.elasticsearch.index.Index;
1615
import org.elasticsearch.xpack.core.ilm.step.info.EmptyInfo;
1716
import org.elasticsearch.xpack.core.ilm.step.info.SingleMessageFieldInfo;
1817

@@ -66,18 +65,15 @@ public boolean equals(Object obj) {
6665
}
6766

6867
@Override
69-
public void evaluateCondition(ProjectState state, Index index, Listener listener, TimeValue masterTimeout) {
70-
IndexMetadata indexMetadata = state.metadata().index(index);
71-
assert indexMetadata != null
72-
: "the index metadata for index [" + index.getName() + "] must exist in the cluster state for step [" + NAME + "]";
73-
74-
final LifecycleExecutionState executionState = state.metadata().index(index.getName()).getLifecycleExecutionState();
68+
public void evaluateCondition(ProjectState state, IndexMetadata indexMetadata, Listener listener, TimeValue masterTimeout) {
69+
String indexName = indexMetadata.getIndex().getName();
70+
final LifecycleExecutionState executionState = indexMetadata.getLifecycleExecutionState();
7571
assert executionState != null
76-
: "the lifecycle execution state for index [" + index.getName() + "] must exist in the cluster state for step [" + NAME + "]";
72+
: "the lifecycle execution state for index [" + indexName + "] must exist in the cluster state for step [" + NAME + "]";
7773

7874
if (replicateFor == null) {
7975
// assert at dev-time, but treat this as a no-op at runtime if somehow this should happen (which it shouldn't)
80-
assert false : "the replicate_for time value for index [" + index.getName() + "] must not be null for step [" + NAME + "]";
76+
assert false : "the replicate_for time value for index [" + indexName + "] must not be null for step [" + NAME + "]";
8177
listener.onResponse(true, EmptyInfo.INSTANCE);
8278
return;
8379
}
@@ -97,7 +93,7 @@ public void evaluateCondition(ProjectState state, Index index, Listener listener
9793
// balance between precision and efficiency.
9894
approximateTimeRemaining(remaining),
9995
this.replicateFor,
100-
index.getName()
96+
indexName
10197
)
10298
)
10399
);

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import org.elasticsearch.cluster.metadata.IndexMetadata;
1111
import org.elasticsearch.common.Strings;
1212
import org.elasticsearch.core.TimeValue;
13-
import org.elasticsearch.index.Index;
1413
import org.elasticsearch.index.IndexMode;
1514
import org.elasticsearch.index.IndexSettings;
1615
import org.elasticsearch.xpack.core.ilm.step.info.EmptyInfo;
@@ -43,18 +42,15 @@ public boolean isRetryable() {
4342
}
4443

4544
@Override
46-
public void evaluateCondition(ProjectState state, Index index, Listener listener, TimeValue masterTimeout) {
47-
IndexMetadata indexMetadata = state.metadata().index(index);
48-
assert indexMetadata != null
49-
: "the index metadata for index [" + index.getName() + "] must exist in the cluster state for step [" + NAME + "]";
50-
45+
public void evaluateCondition(ProjectState state, IndexMetadata indexMetadata, Listener listener, TimeValue masterTimeout) {
46+
String indexName = indexMetadata.getIndex().getName();
5147
if (IndexSettings.MODE.get(indexMetadata.getSettings()) != IndexMode.TIME_SERIES) {
5248
// this index is not a time series index so no need to wait
5349
listener.onResponse(true, EmptyInfo.INSTANCE);
5450
return;
5551
}
5652
Instant configuredEndTime = IndexSettings.TIME_SERIES_END_TIME.get(indexMetadata.getSettings());
57-
assert configuredEndTime != null : "a time series index must have an end time configured but [" + index.getName() + "] does not";
53+
assert configuredEndTime != null : "a time series index must have an end time configured but [" + indexName + "] does not";
5854
if (nowSupplier.get().isBefore(configuredEndTime)) {
5955
listener.onResponse(
6056
false,
@@ -63,7 +59,7 @@ public void evaluateCondition(ProjectState state, Index index, Listener listener
6359
"The [%s] setting for index [%s] is [%s]. Waiting until the index's time series end time lapses before"
6460
+ " proceeding with action [%s] as the index can still accept writes.",
6561
IndexSettings.TIME_SERIES_END_TIME.getKey(),
66-
index.getName(),
62+
indexName,
6763
configuredEndTime.toEpochMilli(),
6864
getKey().action()
6965
)

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public void testIsConditionMet() {
104104
SegmentCountStep step = new SegmentCountStep(stepKey, nextStepKey, client, maxNumSegments);
105105
IndexMetadata indexMetadata = makeMeta(index);
106106
final var state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, true));
107-
step.evaluateCondition(state, indexMetadata.getIndex(), new AsyncWaitStep.Listener() {
107+
step.evaluateCondition(state, indexMetadata, new AsyncWaitStep.Listener() {
108108
@Override
109109
public void onResponse(boolean conditionMet, ToXContentObject info) {
110110
conditionMetResult.set(conditionMet);
@@ -157,7 +157,7 @@ public void testIsConditionIsTrueEvenWhenMoreSegments() {
157157
SegmentCountStep step = new SegmentCountStep(stepKey, nextStepKey, client, maxNumSegments);
158158
IndexMetadata indexMetadata = makeMeta(index);
159159
final var state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, true));
160-
step.evaluateCondition(state, indexMetadata.getIndex(), new AsyncWaitStep.Listener() {
160+
step.evaluateCondition(state, indexMetadata, new AsyncWaitStep.Listener() {
161161
@Override
162162
public void onResponse(boolean conditionMet, ToXContentObject info) {
163163
conditionMetResult.set(conditionMet);
@@ -215,7 +215,7 @@ public void testFailedToRetrieveSomeSegments() {
215215
SegmentCountStep step = new SegmentCountStep(stepKey, nextStepKey, client, maxNumSegments);
216216
IndexMetadata indexMetadata = makeMeta(index);
217217
final var state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, true));
218-
step.evaluateCondition(state, indexMetadata.getIndex(), new AsyncWaitStep.Listener() {
218+
step.evaluateCondition(state, indexMetadata, new AsyncWaitStep.Listener() {
219219
@Override
220220
public void onResponse(boolean conditionMet, ToXContentObject info) {
221221
conditionMetResult.set(conditionMet);
@@ -253,7 +253,7 @@ public void testThrowsException() {
253253
SegmentCountStep step = new SegmentCountStep(stepKey, nextStepKey, client, maxNumSegments);
254254
IndexMetadata indexMetadata = makeMeta(index);
255255
final var state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, true));
256-
step.evaluateCondition(state, indexMetadata.getIndex(), new AsyncWaitStep.Listener() {
256+
step.evaluateCondition(state, indexMetadata, new AsyncWaitStep.Listener() {
257257
@Override
258258
public void onResponse(boolean conditionMet, ToXContentObject info) {
259259
throw new AssertionError("unexpected method call");

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public void testConditionMet() {
7171
final ToXContentObject[] informationContextHolder = new ToXContentObject[1];
7272
final Exception[] exceptionHolder = new Exception[1];
7373
final var state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, true));
74-
createRandomInstance().evaluateCondition(state, indexMetadata.getIndex(), new AsyncWaitStep.Listener() {
74+
createRandomInstance().evaluateCondition(state, indexMetadata, new AsyncWaitStep.Listener() {
7575
@Override
7676
public void onResponse(boolean conditionMet, ToXContentObject informationContext) {
7777
conditionMetHolder[0] = conditionMet;
@@ -106,7 +106,7 @@ public void testConditionNotMetShardsNotInSync() {
106106
final ToXContentObject[] informationContextHolder = new ToXContentObject[1];
107107
final Exception[] exceptionHolder = new Exception[1];
108108
final var state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, true));
109-
createRandomInstance().evaluateCondition(state, indexMetadata.getIndex(), new AsyncWaitStep.Listener() {
109+
createRandomInstance().evaluateCondition(state, indexMetadata, new AsyncWaitStep.Listener() {
110110
@Override
111111
public void onResponse(boolean conditionMet, ToXContentObject informationContext) {
112112
conditionMetHolder[0] = conditionMet;
@@ -140,7 +140,7 @@ public void testConditionNotMetNotAFollowerIndex() {
140140
final ToXContentObject[] informationContextHolder = new ToXContentObject[1];
141141
final Exception[] exceptionHolder = new Exception[1];
142142
final var state = projectStateFromProject(ProjectMetadata.builder(randomProjectIdOrDefault()).put(indexMetadata, true));
143-
createRandomInstance().evaluateCondition(state, indexMetadata.getIndex(), new AsyncWaitStep.Listener() {
143+
createRandomInstance().evaluateCondition(state, indexMetadata, new AsyncWaitStep.Listener() {
144144
@Override
145145
public void onResponse(boolean conditionMet, ToXContentObject informationContext) {
146146
conditionMetHolder[0] = conditionMet;

0 commit comments

Comments
 (0)