Skip to content

Commit 62c31de

Browse files
nielsbaumanelasticsearchmachine
andauthored
[8.19] Make AsyncWaitStep#evaluateCondition take an IndexMetadata instead of Index (#133292) (#133300)
* 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. (cherry picked from commit b0c4ca4) # Conflicts: # x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AsyncWaitStep.java # x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/SegmentCountStep.java # x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForFollowShardTasksStep.java # x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForNoFollowersStep.java # x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForRolloverReadyStep.java # x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForSnapshotStep.java # x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitUntilReplicateForTimePassesStep.java # x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitUntilTimeSeriesEndTimePassesStep.java # x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/SegmentCountStepTests.java # x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForFollowShardTasksStepTests.java # x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForNoFollowersStepTests.java # x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForRolloverReadyStepTests.java # x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForSnapshotStepTests.java # x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitUntilReplicateForTimePassesStepTests.java # x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitUntilTimeSeriesEndTimePassesStepTests.java # x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycleRunner.java # x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/IndexLifecycleRunnerTests.java * [CI] Auto commit changes from spotless --------- Co-authored-by: elasticsearchmachine <[email protected]>
1 parent 9cd4599 commit 62c31de

17 files changed

+67
-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
@@ -7,10 +7,10 @@
77
package org.elasticsearch.xpack.core.ilm;
88

99
import org.elasticsearch.client.internal.Client;
10+
import org.elasticsearch.cluster.metadata.IndexMetadata;
1011
import org.elasticsearch.cluster.metadata.Metadata;
1112
import org.elasticsearch.core.Nullable;
1213
import org.elasticsearch.core.TimeValue;
13-
import org.elasticsearch.index.Index;
1414
import org.elasticsearch.xcontent.ToXContentObject;
1515

1616
/**
@@ -34,7 +34,7 @@ protected Client getClient() {
3434
return client;
3535
}
3636

37-
public abstract void evaluateCondition(Metadata metadata, Index index, Listener listener, TimeValue masterTimeout);
37+
public abstract void evaluateCondition(Metadata metadata, IndexMetadata indexMetadata, Listener listener, TimeValue masterTimeout);
3838

3939
public interface Listener {
4040

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
@@ -14,6 +14,7 @@
1414
import org.elasticsearch.action.admin.indices.segments.ShardSegments;
1515
import org.elasticsearch.action.support.DefaultShardOperationFailedException;
1616
import org.elasticsearch.client.internal.Client;
17+
import org.elasticsearch.cluster.metadata.IndexMetadata;
1718
import org.elasticsearch.cluster.metadata.Metadata;
1819
import org.elasticsearch.cluster.routing.ShardRouting;
1920
import org.elasticsearch.common.Strings;
@@ -56,7 +57,8 @@ public int getMaxNumSegments() {
5657
}
5758

5859
@Override
59-
public void evaluateCondition(Metadata metadata, Index index, Listener listener, TimeValue masterTimeout) {
60+
public void evaluateCondition(Metadata metadata, IndexMetadata indexMetadata, Listener listener, TimeValue masterTimeout) {
61+
Index index = indexMetadata.getIndex();
6062
getClient().admin().indices().segments(new IndicesSegmentsRequest(index.getName()), ActionListener.wrap(response -> {
6163
IndexSegments idxSegments = response.getIndices().get(index.getName());
6264
if (idxSegments == null || (response.getShardFailures() != null && response.getShardFailures().length > 0)) {

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.Metadata;
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(Metadata metadata, Index index, Listener listener, TimeValue masterTimeout) {
43-
IndexMetadata indexMetadata = metadata.index(index);
41+
public void evaluateCondition(Metadata metadata, 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().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
@@ -14,9 +14,9 @@
1414
import org.elasticsearch.action.admin.indices.stats.IndicesStatsRequest;
1515
import org.elasticsearch.action.admin.indices.stats.ShardStats;
1616
import org.elasticsearch.client.internal.Client;
17+
import org.elasticsearch.cluster.metadata.IndexMetadata;
1718
import org.elasticsearch.cluster.metadata.Metadata;
1819
import org.elasticsearch.core.TimeValue;
19-
import org.elasticsearch.index.Index;
2020
import org.elasticsearch.protocol.xpack.XPackInfoRequest;
2121
import org.elasticsearch.protocol.xpack.XPackInfoResponse;
2222
import org.elasticsearch.xpack.core.action.XPackInfoFeatureAction;
@@ -52,7 +52,7 @@ public boolean isRetryable() {
5252
}
5353

5454
@Override
55-
public void evaluateCondition(Metadata metadata, Index index, Listener listener, TimeValue masterTimeout) {
55+
public void evaluateCondition(Metadata metadata, IndexMetadata indexMetadata, Listener listener, TimeValue masterTimeout) {
5656
XPackInfoRequest xPackInfoRequest = new XPackInfoRequest();
5757
xPackInfoRequest.setCategories(EnumSet.of(XPackInfoRequest.Category.FEATURES));
5858
getClient().execute(XPackInfoFeatureAction.CCR, xPackInfoRequest, ActionListener.wrap((xPackInfoResponse) -> {
@@ -61,14 +61,13 @@ public void evaluateCondition(Metadata metadata, Index index, Listener listener,
6161
listener.onResponse(true, null);
6262
return;
6363
}
64-
leaderIndexCheck(metadata, index, listener, masterTimeout);
64+
leaderIndexCheck(indexMetadata.getIndex().getName(), listener);
6565
}, listener::onFailure));
6666
}
6767

68-
private void leaderIndexCheck(Metadata metadata, Index index, Listener listener, TimeValue masterTimeout) {
68+
private void leaderIndexCheck(String indexName, Listener listener) {
6969
IndicesStatsRequest request = new IndicesStatsRequest();
7070
request.clear();
71-
String indexName = index.getName();
7271
request.indices(indexName);
7372

7473
getClient().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(Metadata metadata, Index index, Listener listener, TimeValue masterTimeout) {
83+
public void evaluateCondition(Metadata metadata, IndexMetadata indexMetadata, Listener listener, TimeValue masterTimeout) {
84+
Index index = indexMetadata.getIndex();
8485
IndexAbstraction indexAbstraction = 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(Metadata metadata, Index index, Listener listener,
9596
index.getName(),
9697
targetFailureStore ? "failure store " : "",
9798
dataStream.getName(),
98-
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 = 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.Metadata;
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(Metadata metadata, Index index, Listener listener, TimeValue masterTimeout) {
58-
IndexMetadata indexMetadata = 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(Metadata metadata, 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(Metadata metadata, 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.Metadata;
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(Metadata metadata, Index index, Listener listener, TimeValue masterTimeout) {
70-
IndexMetadata indexMetadata = 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 = metadata.index(index.getName()).getLifecycleExecutionState();
68+
public void evaluateCondition(Metadata metadata, 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(Metadata metadata, 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.Metadata;
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(Metadata metadata, Index index, Listener listener, TimeValue masterTimeout) {
47-
IndexMetadata indexMetadata = 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(Metadata metadata, 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(Metadata metadata, 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
@@ -103,7 +103,7 @@ public void testIsConditionMet() {
103103

104104
SegmentCountStep step = new SegmentCountStep(stepKey, nextStepKey, client, maxNumSegments);
105105
IndexMetadata indexMetadata = makeMeta(index);
106-
step.evaluateCondition(Metadata.builder().put(indexMetadata, true).build(), indexMetadata.getIndex(), new AsyncWaitStep.Listener() {
106+
step.evaluateCondition(Metadata.builder().put(indexMetadata, true).build(), indexMetadata, new AsyncWaitStep.Listener() {
107107
@Override
108108
public void onResponse(boolean conditionMet, ToXContentObject info) {
109109
conditionMetResult.set(conditionMet);
@@ -155,7 +155,7 @@ public void testIsConditionIsTrueEvenWhenMoreSegments() {
155155

156156
SegmentCountStep step = new SegmentCountStep(stepKey, nextStepKey, client, maxNumSegments);
157157
IndexMetadata indexMetadata = makeMeta(index);
158-
step.evaluateCondition(Metadata.builder().put(indexMetadata, true).build(), indexMetadata.getIndex(), new AsyncWaitStep.Listener() {
158+
step.evaluateCondition(Metadata.builder().put(indexMetadata, true).build(), indexMetadata, new AsyncWaitStep.Listener() {
159159
@Override
160160
public void onResponse(boolean conditionMet, ToXContentObject info) {
161161
conditionMetResult.set(conditionMet);
@@ -212,7 +212,7 @@ public void testFailedToRetrieveSomeSegments() {
212212

213213
SegmentCountStep step = new SegmentCountStep(stepKey, nextStepKey, client, maxNumSegments);
214214
IndexMetadata indexMetadata = makeMeta(index);
215-
step.evaluateCondition(Metadata.builder().put(indexMetadata, true).build(), indexMetadata.getIndex(), new AsyncWaitStep.Listener() {
215+
step.evaluateCondition(Metadata.builder().put(indexMetadata, true).build(), indexMetadata, new AsyncWaitStep.Listener() {
216216
@Override
217217
public void onResponse(boolean conditionMet, ToXContentObject info) {
218218
conditionMetResult.set(conditionMet);
@@ -249,7 +249,7 @@ public void testThrowsException() {
249249

250250
SegmentCountStep step = new SegmentCountStep(stepKey, nextStepKey, client, maxNumSegments);
251251
IndexMetadata indexMetadata = makeMeta(index);
252-
step.evaluateCondition(Metadata.builder().put(indexMetadata, true).build(), indexMetadata.getIndex(), new AsyncWaitStep.Listener() {
252+
step.evaluateCondition(Metadata.builder().put(indexMetadata, true).build(), indexMetadata, new AsyncWaitStep.Listener() {
253253
@Override
254254
public void onResponse(boolean conditionMet, ToXContentObject info) {
255255
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
@@ -72,7 +72,7 @@ public void testConditionMet() {
7272
final Exception[] exceptionHolder = new Exception[1];
7373
createRandomInstance().evaluateCondition(
7474
Metadata.builder().put(indexMetadata, true).build(),
75-
indexMetadata.getIndex(),
75+
indexMetadata,
7676
new AsyncWaitStep.Listener() {
7777
@Override
7878
public void onResponse(boolean conditionMet, ToXContentObject informationContext) {
@@ -111,7 +111,7 @@ public void testConditionNotMetShardsNotInSync() {
111111
final Exception[] exceptionHolder = new Exception[1];
112112
createRandomInstance().evaluateCondition(
113113
Metadata.builder().put(indexMetadata, true).build(),
114-
indexMetadata.getIndex(),
114+
indexMetadata,
115115
new AsyncWaitStep.Listener() {
116116
@Override
117117
public void onResponse(boolean conditionMet, ToXContentObject informationContext) {
@@ -149,7 +149,7 @@ public void testConditionNotMetNotAFollowerIndex() {
149149
final Exception[] exceptionHolder = new Exception[1];
150150
createRandomInstance().evaluateCondition(
151151
Metadata.builder().put(indexMetadata, true).build(),
152-
indexMetadata.getIndex(),
152+
indexMetadata,
153153
new AsyncWaitStep.Listener() {
154154
@Override
155155
public void onResponse(boolean conditionMet, ToXContentObject informationContext) {

0 commit comments

Comments
 (0)