diff --git a/docs/changelog/89419.yaml b/docs/changelog/89419.yaml new file mode 100644 index 0000000000000..95cb2be603b44 --- /dev/null +++ b/docs/changelog/89419.yaml @@ -0,0 +1,5 @@ +pr: 89419 +summary: Reuse Info in lifecycle step +area: ILM+SLM +type: enhancement +issues: [] diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CheckNotDataStreamWriteIndexStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CheckNotDataStreamWriteIndexStep.java index e790aff389510..c8e90427e6feb 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CheckNotDataStreamWriteIndexStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CheckNotDataStreamWriteIndexStep.java @@ -13,13 +13,9 @@ import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.index.Index; -import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.xcontent.ToXContentObject; -import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xpack.core.ilm.step.info.SingleMessageFieldInfo; -import java.io.IOException; import java.util.Locale; -import java.util.Objects; /** * Some actions cannot be executed on a data stream's write index (eg. `searchable-snapshot`). This step checks if the managed index is @@ -57,7 +53,7 @@ public Result isConditionMet(Index index, ClusterState clusterState) { ); // Index must have been since deleted logger.debug(errorMessage); - return new Result(false, new Info(errorMessage)); + return new Result(false, new SingleMessageFieldInfo(errorMessage)); } String policyName = indexMetadata.getLifecyclePolicyName(); @@ -77,50 +73,10 @@ public Result isConditionMet(Index index, ClusterState clusterState) { policyName ); logger.debug(errorMessage); - return new Result(false, new Info(errorMessage)); + return new Result(false, new SingleMessageFieldInfo(errorMessage)); } } return new Result(true, null); } - - static final class Info implements ToXContentObject { - - private final String message; - - static final ParseField MESSAGE = new ParseField("message"); - - Info(String message) { - this.message = message; - } - - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.startObject(); - builder.field(MESSAGE.getPreferredName(), message); - builder.endObject(); - return builder; - } - - public String getMessage() { - return message; - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - Info info = (Info) o; - return Objects.equals(message, info.message); - } - - @Override - public int hashCode() { - return Objects.hash(message); - } - } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForActiveShardsStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForActiveShardsStep.java index b4a2af3bda603..8af173eda920b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForActiveShardsStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForActiveShardsStep.java @@ -20,6 +20,7 @@ import org.elasticsearch.xcontent.ParseField; import org.elasticsearch.xcontent.ToXContentObject; import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xpack.core.ilm.step.info.SingleMessageFieldInfo; import java.io.IOException; import java.util.List; @@ -61,7 +62,7 @@ public Result isConditionMet(Index index, ClusterState clusterState) { ); // Index must have been since deleted logger.debug(errorMessage); - return new Result(false, new Info(errorMessage)); + return new Result(false, new SingleMessageFieldInfo(errorMessage)); } boolean indexingComplete = LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE_SETTING.get(originalIndexMeta.getSettings()); @@ -73,7 +74,7 @@ public Result isConditionMet(Index index, ClusterState clusterState) { WaitForActiveShardsStep.NAME ); logger.trace(message); - return new Result(true, new Info(message)); + return new Result(true, new SingleMessageFieldInfo(message)); } IndexAbstraction indexAbstraction = metadata.getIndicesLookup().get(index.getName()); @@ -149,7 +150,7 @@ private static Result getErrorResultOnNullMetadata(StepKey key, Index originalIn // Index must have been since deleted logger.debug(errorMessage); - return new Result(false, new Info(errorMessage)); + return new Result(false, new SingleMessageFieldInfo(errorMessage)); } static final class ActiveShardsInfo implements ToXContentObject { @@ -211,40 +212,4 @@ public int hashCode() { return Objects.hash(currentActiveShardsCount, targetActiveShardsCount, enoughShardsActive, message); } } - - static final class Info implements ToXContentObject { - - private final String message; - - static final ParseField MESSAGE = new ParseField("message"); - - Info(String message) { - this.message = message; - } - - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.startObject(); - builder.field(MESSAGE.getPreferredName(), message); - builder.endObject(); - return builder; - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - Info info = (Info) o; - return Objects.equals(message, info.message); - } - - @Override - public int hashCode() { - return Objects.hash(message); - } - } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForIndexColorStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForIndexColorStep.java index 59982b4d7931d..7a464390784d9 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForIndexColorStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForIndexColorStep.java @@ -17,11 +17,8 @@ import org.elasticsearch.cluster.routing.ShardRouting; import org.elasticsearch.core.Nullable; import org.elasticsearch.index.Index; -import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.xcontent.ToXContentObject; -import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xpack.core.ilm.step.info.SingleMessageFieldInfo; -import java.io.IOException; import java.util.Locale; import java.util.Objects; import java.util.function.BiFunction; @@ -100,7 +97,7 @@ public Result isConditionMet(Index index, ClusterState clusterState) { indexName ); logger.debug(errorMessage); - return new Result(false, new Info(errorMessage)); + return new Result(false, new SingleMessageFieldInfo(errorMessage)); } IndexRoutingTable indexRoutingTable = clusterState.routingTable().index(indexMetadata.getIndex()); @@ -119,79 +116,39 @@ public boolean isRetryable() { private static Result waitForRed(IndexRoutingTable indexRoutingTable) { if (indexRoutingTable == null) { - return new Result(true, new Info("index is red")); + return new Result(true, new SingleMessageFieldInfo("index is red")); } - return new Result(false, new Info("index is not red")); + return new Result(false, new SingleMessageFieldInfo("index is not red")); } private static Result waitForYellow(IndexRoutingTable indexRoutingTable) { if (indexRoutingTable == null) { - return new Result(false, new Info("index is red; no indexRoutingTable")); + return new Result(false, new SingleMessageFieldInfo("index is red; no indexRoutingTable")); } boolean indexIsAtLeastYellow = indexRoutingTable.allPrimaryShardsActive(); if (indexIsAtLeastYellow) { return new Result(true, null); } else { - return new Result(false, new Info("index is red; not all primary shards are active")); + return new Result(false, new SingleMessageFieldInfo("index is red; not all primary shards are active")); } } private static Result waitForGreen(IndexRoutingTable indexRoutingTable) { if (indexRoutingTable == null) { - return new Result(false, new Info("index is red; no indexRoutingTable")); + return new Result(false, new SingleMessageFieldInfo("index is red; no indexRoutingTable")); } if (indexRoutingTable.allPrimaryShardsActive()) { for (int i = 0; i < indexRoutingTable.size(); i++) { boolean replicaIndexIsGreen = indexRoutingTable.shard(i).replicaShards().stream().allMatch(ShardRouting::active); if (replicaIndexIsGreen == false) { - return new Result(false, new Info("index is yellow; not all replica shards are active")); + return new Result(false, new SingleMessageFieldInfo("index is yellow; not all replica shards are active")); } } return new Result(true, null); } - return new Result(false, new Info("index is not green; not all shards are active")); - } - - static final class Info implements ToXContentObject { - - static final ParseField MESSAGE_FIELD = new ParseField("message"); - - private final String message; - - Info(String message) { - this.message = message; - } - - String getMessage() { - return message; - } - - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.startObject(); - builder.field(MESSAGE_FIELD.getPreferredName(), message); - builder.endObject(); - return builder; - } - - @Override - public boolean equals(Object o) { - if (o == null) { - return false; - } - if (getClass() != o.getClass()) { - return false; - } - Info info = (Info) o; - return Objects.equals(getMessage(), info.getMessage()); - } - - @Override - public int hashCode() { - return Objects.hash(getMessage()); - } + return new Result(false, new SingleMessageFieldInfo("index is not green; not all shards are active")); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForNoFollowersStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForNoFollowersStep.java index dc0eab829add2..79d55243b58a4 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForNoFollowersStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForNoFollowersStep.java @@ -17,14 +17,10 @@ import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.Index; -import org.elasticsearch.xcontent.ParseField; -import org.elasticsearch.xcontent.ToXContentObject; -import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xpack.core.ilm.step.info.SingleMessageFieldInfo; -import java.io.IOException; import java.util.Arrays; import java.util.Collection; -import java.util.Objects; import java.util.Optional; /** @@ -39,6 +35,8 @@ public class WaitForNoFollowersStep extends AsyncWaitStep { static final String NAME = "wait-for-shard-history-leases"; static final String CCR_LEASE_KEY = "ccr"; + private static final String WAIT_MESSAGE = "this index is a leader index; waiting for all following indices to cease " + + "following before proceeding"; WaitForNoFollowersStep(StepKey key, StepKey nextStepKey, Client client) { super(key, nextStepKey, client); @@ -73,48 +71,10 @@ public void evaluateCondition(Metadata metadata, Index index, Listener listener, .anyMatch(lease -> lease.isPresent() && lease.get().anyMatch(l -> CCR_LEASE_KEY.equals(l.source()))); if (isCurrentlyLeaderIndex) { - listener.onResponse(false, new Info()); + listener.onResponse(false, new SingleMessageFieldInfo(WAIT_MESSAGE)); } else { listener.onResponse(true, null); } }, listener::onFailure)); } - - static final class Info implements ToXContentObject { - - static final ParseField MESSAGE_FIELD = new ParseField("message"); - - private static final String message = "this index is a leader index; waiting for all following indices to cease " - + "following before proceeding"; - - Info() {} - - static String getMessage() { - return message; - } - - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.startObject(); - builder.field(MESSAGE_FIELD.getPreferredName(), message); - builder.endObject(); - return builder; - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - return true; - } - - @Override - public int hashCode() { - return Objects.hash(getMessage()); - } - } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CheckNoDataStreamWriteIndexStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CheckNoDataStreamWriteIndexStepTests.java index e7fb3b40f30e3..07a9d975a1a98 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CheckNoDataStreamWriteIndexStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CheckNoDataStreamWriteIndexStepTests.java @@ -12,6 +12,7 @@ import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.index.Index; +import org.elasticsearch.xpack.core.ilm.step.info.SingleMessageFieldInfo; import java.util.List; @@ -80,7 +81,7 @@ public void testStepIncompleteIfIndexIsTheDataStreamWriteIndex() { ClusterStateWaitStep.Result result = createRandomInstance().isConditionMet(indexMetadata.getIndex(), clusterState); assertThat(result.isComplete(), is(false)); - CheckNotDataStreamWriteIndexStep.Info info = (CheckNotDataStreamWriteIndexStep.Info) result.getInfomationContext(); + SingleMessageFieldInfo info = (SingleMessageFieldInfo) result.getInfomationContext(); assertThat( info.getMessage(), is( diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForIndexColorStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForIndexColorStepTests.java index 9259c63f243e9..89d2381ca1d74 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForIndexColorStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForIndexColorStepTests.java @@ -21,6 +21,7 @@ import org.elasticsearch.cluster.routing.TestShardRouting; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.xpack.core.ilm.Step.StepKey; +import org.elasticsearch.xpack.core.ilm.step.info.SingleMessageFieldInfo; import java.util.function.BiFunction; @@ -125,7 +126,7 @@ public void testConditionNotMetForGreen() { WaitForIndexColorStep step = new WaitForIndexColorStep(randomStepKey(), randomStepKey(), ClusterHealthStatus.GREEN); ClusterStateWaitStep.Result result = step.isConditionMet(indexMetadata.getIndex(), clusterState); assertThat(result.isComplete(), is(false)); - WaitForIndexColorStep.Info info = (WaitForIndexColorStep.Info) result.getInfomationContext(); + SingleMessageFieldInfo info = (SingleMessageFieldInfo) result.getInfomationContext(); assertThat(info, notNullValue()); assertThat(info.getMessage(), equalTo("index is not green; not all shards are active")); } @@ -145,7 +146,7 @@ public void testConditionNotMetNoIndexRoutingTable() { WaitForIndexColorStep step = new WaitForIndexColorStep(randomStepKey(), randomStepKey(), ClusterHealthStatus.YELLOW); ClusterStateWaitStep.Result result = step.isConditionMet(indexMetadata.getIndex(), clusterState); assertThat(result.isComplete(), is(false)); - WaitForIndexColorStep.Info info = (WaitForIndexColorStep.Info) result.getInfomationContext(); + SingleMessageFieldInfo info = (SingleMessageFieldInfo) result.getInfomationContext(); assertThat(info, notNullValue()); assertThat(info.getMessage(), equalTo("index is red; no indexRoutingTable")); } @@ -199,7 +200,7 @@ public void testConditionNotMetForYellow() { WaitForIndexColorStep step = new WaitForIndexColorStep(randomStepKey(), randomStepKey(), ClusterHealthStatus.YELLOW); ClusterStateWaitStep.Result result = step.isConditionMet(indexMetadata.getIndex(), clusterState); assertThat(result.isComplete(), is(false)); - WaitForIndexColorStep.Info info = (WaitForIndexColorStep.Info) result.getInfomationContext(); + SingleMessageFieldInfo info = (SingleMessageFieldInfo) result.getInfomationContext(); assertThat(info, notNullValue()); assertThat(info.getMessage(), equalTo("index is red; not all primary shards are active")); } @@ -219,7 +220,7 @@ public void testConditionNotMetNoIndexRoutingTableForYellow() { WaitForIndexColorStep step = new WaitForIndexColorStep(randomStepKey(), randomStepKey(), ClusterHealthStatus.YELLOW); ClusterStateWaitStep.Result result = step.isConditionMet(indexMetadata.getIndex(), clusterState); assertThat(result.isComplete(), is(false)); - WaitForIndexColorStep.Info info = (WaitForIndexColorStep.Info) result.getInfomationContext(); + SingleMessageFieldInfo info = (SingleMessageFieldInfo) result.getInfomationContext(); assertThat(info, notNullValue()); assertThat(info.getMessage(), equalTo("index is red; no indexRoutingTable")); } @@ -249,7 +250,7 @@ public void testStepReturnsFalseIfTargetIndexIsMissing() { WaitForIndexColorStep step = new WaitForIndexColorStep(randomStepKey(), randomStepKey(), ClusterHealthStatus.GREEN, indexPrefix); ClusterStateWaitStep.Result result = step.isConditionMet(originalIndex.getIndex(), clusterState); assertThat(result.isComplete(), is(false)); - WaitForIndexColorStep.Info info = (WaitForIndexColorStep.Info) result.getInfomationContext(); + SingleMessageFieldInfo info = (SingleMessageFieldInfo) result.getInfomationContext(); String targetIndex = indexPrefix + originalIndex.getIndex().getName(); assertThat( info.getMessage(), @@ -309,7 +310,7 @@ public void testStepWaitsForTargetIndexHealthWhenPrefixConfigured() { WaitForIndexColorStep step = new WaitForIndexColorStep(randomStepKey(), randomStepKey(), ClusterHealthStatus.GREEN); ClusterStateWaitStep.Result result = step.isConditionMet(originalIndex.getIndex(), clusterTargetInitializing); assertThat(result.isComplete(), is(false)); - WaitForIndexColorStep.Info info = (WaitForIndexColorStep.Info) result.getInfomationContext(); + SingleMessageFieldInfo info = (SingleMessageFieldInfo) result.getInfomationContext(); assertThat(info.getMessage(), is("index is not green; not all shards are active")); }