ソースを参照

Reuse Info in lifecycle step (#89419)

We have a `SingleMessageFiledInfo` defined in
`org/elasticsearch/xpack/core/ilm/step/info` to provide single message
info for `AsyncWaitStep` and `ClusterStateWaitStep`.But there are still
some steps like `CheckNotDataStreamWriteIndexStep` defining their own
single message info. This pr removes the duplicated info defination in
these steps and use `SingleMessageFiledInfo` instand.
mushaoqiong 3 年 前
コミット
22e1150dd6

+ 5 - 0
docs/changelog/89419.yaml

@@ -0,0 +1,5 @@
+pr: 89419
+summary: Reuse Info in lifecycle step
+area: ILM+SLM
+type: enhancement
+issues: []

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

@@ -13,13 +13,9 @@ import org.elasticsearch.cluster.metadata.IndexAbstraction;
 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 class CheckNotDataStreamWriteIndexStep extends ClusterStateWaitStep {
             );
             // 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 class CheckNotDataStreamWriteIndexStep extends ClusterStateWaitStep {
                     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);
-        }
-    }
 }

+ 4 - 39
x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForActiveShardsStep.java

@@ -20,6 +20,7 @@ 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.List;
@@ -61,7 +62,7 @@ public class WaitForActiveShardsStep extends ClusterStateWaitStep {
             );
             // 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 class WaitForActiveShardsStep extends ClusterStateWaitStep {
                 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 @@ public class WaitForActiveShardsStep extends ClusterStateWaitStep {
 
         // 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 class WaitForActiveShardsStep extends ClusterStateWaitStep {
             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);
-        }
-    }
 }

+ 9 - 52
x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForIndexColorStep.java

@@ -17,11 +17,8 @@ import org.elasticsearch.cluster.routing.IndexRoutingTable;
 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 @@ class WaitForIndexColorStep extends ClusterStateWaitStep {
                 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 @@ class WaitForIndexColorStep extends ClusterStateWaitStep {
 
     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"));
     }
 }

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

@@ -17,14 +17,10 @@ import org.elasticsearch.client.internal.Client;
 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 class WaitForNoFollowersStep extends AsyncWaitStep {
                 .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());
-        }
-    }
 }

+ 2 - 1
x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CheckNoDataStreamWriteIndexStepTests.java

@@ -12,6 +12,7 @@ import org.elasticsearch.cluster.metadata.DataStream;
 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 class CheckNoDataStreamWriteIndexStepTests extends AbstractStepTestCase<C
 
         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(

+ 7 - 6
x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForIndexColorStepTests.java

@@ -21,6 +21,7 @@ import org.elasticsearch.cluster.routing.ShardRoutingState;
 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 class WaitForIndexColorStepTests extends AbstractStepTestCase<WaitForInde
         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 class WaitForIndexColorStepTests extends AbstractStepTestCase<WaitForInde
         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 class WaitForIndexColorStepTests extends AbstractStepTestCase<WaitForInde
         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 class WaitForIndexColorStepTests extends AbstractStepTestCase<WaitForInde
         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 class WaitForIndexColorStepTests extends AbstractStepTestCase<WaitForInde
         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 class WaitForIndexColorStepTests extends AbstractStepTestCase<WaitForInde
             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"));
         }