|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | +package org.elasticsearch.xpack.core.ilm; |
| 8 | + |
| 9 | +import org.elasticsearch.Version; |
| 10 | +import org.elasticsearch.cluster.ClusterState; |
| 11 | +import org.elasticsearch.cluster.metadata.IndexMetadata; |
| 12 | +import org.elasticsearch.cluster.metadata.Metadata; |
| 13 | +import org.elasticsearch.xpack.core.ilm.Step.StepKey; |
| 14 | +import org.elasticsearch.xpack.core.ilm.step.info.SingleMessageFieldInfo; |
| 15 | + |
| 16 | +import static org.hamcrest.Matchers.is; |
| 17 | + |
| 18 | +public class CheckTargetShardsCountStepTests extends AbstractStepTestCase<CheckTargetShardsCountStep> { |
| 19 | + |
| 20 | + @Override |
| 21 | + protected CheckTargetShardsCountStep createRandomInstance() { |
| 22 | + return new CheckTargetShardsCountStep(randomStepKey(), randomStepKey(), null); |
| 23 | + } |
| 24 | + |
| 25 | + @Override |
| 26 | + protected CheckTargetShardsCountStep mutateInstance(CheckTargetShardsCountStep instance) { |
| 27 | + StepKey key = instance.getKey(); |
| 28 | + StepKey nextKey = instance.getNextStepKey(); |
| 29 | + |
| 30 | + switch (between(0, 1)) { |
| 31 | + case 0: |
| 32 | + key = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); |
| 33 | + break; |
| 34 | + case 1: |
| 35 | + nextKey = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); |
| 36 | + break; |
| 37 | + default: |
| 38 | + throw new AssertionError("Illegal randomisation branch"); |
| 39 | + } |
| 40 | + |
| 41 | + return new CheckTargetShardsCountStep(key, nextKey, null); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + protected CheckTargetShardsCountStep copyInstance(CheckTargetShardsCountStep instance) { |
| 46 | + return new CheckTargetShardsCountStep(instance.getKey(), instance.getNextStepKey(), instance.getNumberOfShards()); |
| 47 | + } |
| 48 | + |
| 49 | + public void testStepCompleteIfTargetShardsCountIsValid() { |
| 50 | + String policyName = "test-ilm-policy"; |
| 51 | + IndexMetadata indexMetadata = |
| 52 | + IndexMetadata.builder(randomAlphaOfLength(10)).settings(settings(Version.CURRENT) |
| 53 | + .put(LifecycleSettings.LIFECYCLE_NAME, policyName)) |
| 54 | + .numberOfShards(10).numberOfReplicas(randomIntBetween(0, 5)).build(); |
| 55 | + |
| 56 | + ClusterState clusterState = ClusterState.builder(emptyClusterState()).metadata( |
| 57 | + Metadata.builder().put(indexMetadata, true).build()).build(); |
| 58 | + |
| 59 | + CheckTargetShardsCountStep checkTargetShardsCountStep = new CheckTargetShardsCountStep(randomStepKey(), randomStepKey(), 2); |
| 60 | + |
| 61 | + ClusterStateWaitStep.Result result = checkTargetShardsCountStep.isConditionMet(indexMetadata.getIndex(), clusterState); |
| 62 | + assertThat(result.isComplete(), is(true)); |
| 63 | + } |
| 64 | + |
| 65 | + public void testStepIncompleteIfTargetShardsCountNotValid() { |
| 66 | + String indexName = randomAlphaOfLength(10); |
| 67 | + String policyName = "test-ilm-policy"; |
| 68 | + IndexMetadata indexMetadata = |
| 69 | + IndexMetadata.builder(indexName).settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) |
| 70 | + .numberOfShards(10).numberOfReplicas(randomIntBetween(0, 5)).build(); |
| 71 | + |
| 72 | + ClusterState clusterState = ClusterState.builder(emptyClusterState()).metadata( |
| 73 | + Metadata.builder().put(indexMetadata, true).build()).build(); |
| 74 | + |
| 75 | + CheckTargetShardsCountStep checkTargetShardsCountStep = new CheckTargetShardsCountStep(randomStepKey(), randomStepKey(), 3); |
| 76 | + |
| 77 | + ClusterStateWaitStep.Result result = checkTargetShardsCountStep.isConditionMet(indexMetadata.getIndex(), clusterState); |
| 78 | + assertThat(result.isComplete(), is(false)); |
| 79 | + SingleMessageFieldInfo info = (SingleMessageFieldInfo) result.getInfomationContext(); |
| 80 | + assertThat(info.getMessage(), is("lifecycle action of policy [" + policyName + "] for index [" + indexName + |
| 81 | + "] cannot make progress because the target shards count [3] must be a factor of the source index's shards count [10]")); |
| 82 | + } |
| 83 | +} |
0 commit comments