Преглед изворни кода

Use WaitForIndexColorStep and drop WaitForYellowStep (#65085)

Andrei Dan пре 5 година
родитељ
комит
05babac79b

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

@@ -6,6 +6,7 @@
 package org.elasticsearch.xpack.core.ilm;
 
 import org.elasticsearch.client.Client;
+import org.elasticsearch.cluster.health.ClusterHealthStatus;
 import org.elasticsearch.common.Strings;
 import org.elasticsearch.common.io.stream.StreamInput;
 import org.elasticsearch.common.io.stream.StreamOutput;
@@ -42,7 +43,7 @@ public final class UnfollowAction implements LifecycleAction {
         StepKey closeFollowerIndex = new StepKey(phase, NAME, CloseFollowerIndexStep.NAME);
         StepKey unfollowFollowerIndex = new StepKey(phase, NAME, UnfollowFollowIndexStep.NAME);
         StepKey openFollowerIndex = new StepKey(phase, NAME, OpenFollowerIndexStep.NAME);
-        StepKey waitForYellowStep = new StepKey(phase, NAME, WaitForYellowStep.NAME);
+        StepKey waitForYellowStep = new StepKey(phase, NAME, WaitForIndexColorStep.NAME);
 
         WaitForIndexingCompleteStep step1 = new WaitForIndexingCompleteStep(indexingComplete, waitForFollowShardTasks);
         WaitForFollowShardTasksStep step2 = new WaitForFollowShardTasksStep(waitForFollowShardTasks, pauseFollowerIndex, client);
@@ -50,7 +51,7 @@ public final class UnfollowAction implements LifecycleAction {
         CloseFollowerIndexStep step4 = new CloseFollowerIndexStep(closeFollowerIndex, unfollowFollowerIndex, client);
         UnfollowFollowIndexStep step5 = new UnfollowFollowIndexStep(unfollowFollowerIndex, openFollowerIndex, client);
         OpenFollowerIndexStep step6 = new OpenFollowerIndexStep(openFollowerIndex, waitForYellowStep, client);
-        WaitForYellowStep step7 = new WaitForYellowStep(waitForYellowStep, nextStepKey);
+        WaitForIndexColorStep step7 = new WaitForIndexColorStep(waitForYellowStep, nextStepKey, ClusterHealthStatus.YELLOW);
         return Arrays.asList(step1, step2, step3, step4, step5, step6, step7);
     }
 

+ 0 - 78
x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForYellowStep.java

@@ -1,78 +0,0 @@
-/*
- * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
- * or more contributor license agreements. Licensed under the Elastic License;
- * you may not use this file except in compliance with the Elastic License.
- */
-package org.elasticsearch.xpack.core.ilm;
-
-import org.elasticsearch.cluster.ClusterState;
-import org.elasticsearch.cluster.routing.IndexRoutingTable;
-import org.elasticsearch.cluster.routing.RoutingTable;
-import org.elasticsearch.common.ParseField;
-import org.elasticsearch.common.xcontent.ToXContentObject;
-import org.elasticsearch.common.xcontent.XContentBuilder;
-import org.elasticsearch.index.Index;
-
-import java.io.IOException;
-import java.util.Objects;
-
-class WaitForYellowStep extends ClusterStateWaitStep {
-
-    static final String NAME = "wait-for-yellow-step";
-
-    WaitForYellowStep(StepKey key, StepKey nextStepKey) {
-        super(key, nextStepKey);
-    }
-
-    @Override
-    public Result isConditionMet(Index index, ClusterState clusterState) {
-        RoutingTable routingTable = clusterState.routingTable();
-        IndexRoutingTable indexShardRoutingTable = routingTable.index(index);
-        if (indexShardRoutingTable == null) {
-            return new Result(false, new Info("index is red; no IndexRoutingTable"));
-        }
-
-        boolean indexIsAtLeastYellow = indexShardRoutingTable.allPrimaryShardsActive();
-        if (indexIsAtLeastYellow) {
-            return new Result(true, null);
-        } else {
-            return new Result(false, new Info("index is red; not all primary 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 (this == o) return true;
-            if (o == null || getClass() != o.getClass()) return false;
-            Info info = (Info) o;
-            return Objects.equals(getMessage(), info.getMessage());
-        }
-
-        @Override
-        public int hashCode() {
-            return Objects.hash(getMessage());
-        }
-    }
-}

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

@@ -5,6 +5,7 @@
  */
 package org.elasticsearch.xpack.core.ilm;
 
+import org.elasticsearch.cluster.health.ClusterHealthStatus;
 import org.elasticsearch.common.io.stream.Writeable.Reader;
 import org.elasticsearch.common.xcontent.XContentParser;
 import org.elasticsearch.xpack.core.ilm.Step.StepKey;
@@ -13,6 +14,7 @@ import java.io.IOException;
 import java.util.List;
 
 import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.is;
 import static org.hamcrest.Matchers.notNullValue;
 
 public class UnfollowActionTests extends AbstractActionTestCase<UnfollowAction> {
@@ -47,7 +49,7 @@ public class UnfollowActionTests extends AbstractActionTestCase<UnfollowAction>
         StepKey expectedFourthStepKey = new StepKey(phase, UnfollowAction.NAME, CloseFollowerIndexStep.NAME);
         StepKey expectedFifthStepKey = new StepKey(phase, UnfollowAction.NAME, UnfollowFollowIndexStep.NAME);
         StepKey expectedSixthStepKey = new StepKey(phase, UnfollowAction.NAME, OpenFollowerIndexStep.NAME);
-        StepKey expectedSeventhStepKey = new StepKey(phase, UnfollowAction.NAME, WaitForYellowStep.NAME);
+        StepKey expectedSeventhStepKey = new StepKey(phase, UnfollowAction.NAME, WaitForIndexColorStep.NAME);
 
         WaitForIndexingCompleteStep firstStep = (WaitForIndexingCompleteStep) steps.get(0);
         assertThat(firstStep.getKey(), equalTo(expectedFirstStepKey));
@@ -73,7 +75,8 @@ public class UnfollowActionTests extends AbstractActionTestCase<UnfollowAction>
         assertThat(sixthStep.getKey(), equalTo(expectedSixthStepKey));
         assertThat(sixthStep.getNextStepKey(), equalTo(expectedSeventhStepKey));
 
-        WaitForYellowStep seventhStep = (WaitForYellowStep) steps.get(6);
+        WaitForIndexColorStep seventhStep = (WaitForIndexColorStep) steps.get(6);
+        assertThat(seventhStep.getColor(), is(ClusterHealthStatus.YELLOW));
         assertThat(seventhStep.getKey(), equalTo(expectedSeventhStepKey));
         assertThat(seventhStep.getNextStepKey(), equalTo(nextStepKey));
     }

+ 0 - 120
x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForYellowStepTests.java

@@ -1,120 +0,0 @@
-/*
- * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
- * or more contributor license agreements. Licensed under the Elastic License;
- * you may not use this file except in compliance with the Elastic License.
- */
-package org.elasticsearch.xpack.core.ilm;
-
-import org.elasticsearch.Version;
-import org.elasticsearch.cluster.ClusterName;
-import org.elasticsearch.cluster.ClusterState;
-import org.elasticsearch.cluster.metadata.IndexMetadata;
-import org.elasticsearch.cluster.metadata.Metadata;
-import org.elasticsearch.cluster.routing.IndexRoutingTable;
-import org.elasticsearch.cluster.routing.RoutingTable;
-import org.elasticsearch.cluster.routing.ShardRouting;
-import org.elasticsearch.cluster.routing.ShardRoutingState;
-import org.elasticsearch.cluster.routing.TestShardRouting;
-import org.elasticsearch.xpack.core.ilm.Step.StepKey;
-
-import static org.hamcrest.Matchers.equalTo;
-import static org.hamcrest.Matchers.is;
-import static org.hamcrest.Matchers.nullValue;
-import static org.hamcrest.core.IsNull.notNullValue;
-
-public class WaitForYellowStepTests extends AbstractStepTestCase<WaitForYellowStep> {
-
-    @Override
-    protected WaitForYellowStep createRandomInstance() {
-        StepKey stepKey = randomStepKey();
-        StepKey nextStepKey = randomStepKey();
-        return new WaitForYellowStep(stepKey, nextStepKey);
-    }
-
-    @Override
-    protected WaitForYellowStep mutateInstance(WaitForYellowStep instance) {
-        StepKey key = instance.getKey();
-        StepKey nextKey = instance.getNextStepKey();
-
-        if (randomBoolean()) {
-            key = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5));
-        } else {
-            nextKey = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5));
-        }
-
-        return new WaitForYellowStep(key, nextKey);
-    }
-
-    @Override
-    protected WaitForYellowStep copyInstance(WaitForYellowStep instance) {
-        return new WaitForYellowStep(instance.getKey(), instance.getNextStepKey());
-    }
-
-    public void testConditionMet() {
-        IndexMetadata indexMetadata = IndexMetadata.builder("former-follower-index")
-            .settings(settings(Version.CURRENT))
-            .numberOfShards(1)
-            .numberOfReplicas(0)
-            .build();
-
-        ShardRouting shardRouting =
-            TestShardRouting.newShardRouting("index2", 0, "1", true, ShardRoutingState.STARTED);
-        IndexRoutingTable indexRoutingTable = IndexRoutingTable.builder(indexMetadata.getIndex())
-            .addShard(shardRouting).build();
-
-        ClusterState clusterState = ClusterState.builder(new ClusterName("_name"))
-            .metadata(Metadata.builder().put(indexMetadata, true).build())
-            .routingTable(RoutingTable.builder().add(indexRoutingTable).build())
-            .build();
-
-        WaitForYellowStep step = new WaitForYellowStep(randomStepKey(), randomStepKey());
-        ClusterStateWaitStep.Result result = step.isConditionMet(indexMetadata.getIndex(), clusterState);
-        assertThat(result.isComplete(), is(true));
-        assertThat(result.getInfomationContext(), nullValue());
-    }
-
-    public void testConditionNotMet() {
-        IndexMetadata indexMetadata = IndexMetadata.builder("former-follower-index")
-            .settings(settings(Version.CURRENT))
-            .numberOfShards(1)
-            .numberOfReplicas(0)
-            .build();
-
-        ShardRouting shardRouting =
-            TestShardRouting.newShardRouting("index2", 0, "1", true, ShardRoutingState.INITIALIZING);
-        IndexRoutingTable indexRoutingTable = IndexRoutingTable.builder(indexMetadata.getIndex())
-            .addShard(shardRouting).build();
-
-        ClusterState clusterState = ClusterState.builder(new ClusterName("_name"))
-            .metadata(Metadata.builder().put(indexMetadata, true).build())
-            .routingTable(RoutingTable.builder().add(indexRoutingTable).build())
-            .build();
-
-        WaitForYellowStep step = new WaitForYellowStep(randomStepKey(), randomStepKey());
-        ClusterStateWaitStep.Result result = step.isConditionMet(indexMetadata.getIndex(), clusterState);
-        assertThat(result.isComplete(), is(false));
-        WaitForYellowStep.Info info = (WaitForYellowStep.Info) result.getInfomationContext();
-        assertThat(info, notNullValue());
-        assertThat(info.getMessage(), equalTo("index is red; not all primary shards are active"));
-    }
-
-    public void testConditionNotMetNoIndexRoutingTable() {
-        IndexMetadata indexMetadata = IndexMetadata.builder("former-follower-index")
-            .settings(settings(Version.CURRENT))
-            .numberOfShards(1)
-            .numberOfReplicas(0)
-            .build();
-
-        ClusterState clusterState = ClusterState.builder(new ClusterName("_name"))
-            .metadata(Metadata.builder().put(indexMetadata, true).build())
-            .routingTable(RoutingTable.builder().build())
-            .build();
-
-        WaitForYellowStep step = new WaitForYellowStep(randomStepKey(), randomStepKey());
-        ClusterStateWaitStep.Result result = step.isConditionMet(indexMetadata.getIndex(), clusterState);
-        assertThat(result.isComplete(), is(false));
-        WaitForYellowStep.Info info = (WaitForYellowStep.Info) result.getInfomationContext();
-        assertThat(info, notNullValue());
-        assertThat(info.getMessage(), equalTo("index is red; no IndexRoutingTable"));
-    }
-}