forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ILM: Add validation of the number_of_shards parameter in Shrink Actio…
…n of ILM (elastic#74219) Add validation of the number_of_shards parameter in Shrink Action of ILM
- Loading branch information
1 parent
5c53a66
commit 58feb4e
Showing
7 changed files
with
255 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...lugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CheckTargetShardsCountStep.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
package org.elasticsearch.xpack.core.ilm; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.elasticsearch.cluster.ClusterState; | ||
import org.elasticsearch.cluster.metadata.IndexMetadata; | ||
import org.elasticsearch.index.Index; | ||
import org.elasticsearch.xpack.core.ilm.step.info.SingleMessageFieldInfo; | ||
|
||
import java.util.Locale; | ||
|
||
/** | ||
* This step checks whether the new shrunken index's shards count is a factor of the source index's shards count. | ||
*/ | ||
public class CheckTargetShardsCountStep extends ClusterStateWaitStep { | ||
|
||
public static final String NAME = "check-target-shards-count"; | ||
|
||
private final Integer numberOfShards; | ||
|
||
private static final Logger logger = LogManager.getLogger(CheckTargetShardsCountStep.class); | ||
|
||
CheckTargetShardsCountStep(StepKey key, StepKey nextStepKey, Integer numberOfShards) { | ||
super(key, nextStepKey); | ||
this.numberOfShards = numberOfShards; | ||
} | ||
|
||
@Override | ||
public boolean isRetryable() { | ||
return true; | ||
} | ||
|
||
public Integer getNumberOfShards() { | ||
return numberOfShards; | ||
} | ||
|
||
@Override | ||
public Result isConditionMet(Index index, ClusterState clusterState) { | ||
IndexMetadata indexMetadata = clusterState.metadata().index(index); | ||
if (indexMetadata == null) { | ||
// Index must have been since deleted, ignore it | ||
logger.debug("[{}] lifecycle action for index [{}] executed but index no longer exists", | ||
getKey().getAction(), index.getName()); | ||
return new Result(false, null); | ||
} | ||
String indexName = indexMetadata.getIndex().getName(); | ||
if (numberOfShards != null) { | ||
int sourceNumberOfShards = indexMetadata.getNumberOfShards(); | ||
if (sourceNumberOfShards % numberOfShards != 0) { | ||
String policyName = indexMetadata.getSettings().get(LifecycleSettings.LIFECYCLE_NAME); | ||
String errorMessage = String.format(Locale.ROOT, "lifecycle action of policy [%s] for index [%s] cannot make progress " + | ||
"because the target shards count [%d] must be a factor of the source index's shards count [%d]", | ||
policyName, indexName, numberOfShards, sourceNumberOfShards); | ||
logger.debug(errorMessage); | ||
return new Result(false, new SingleMessageFieldInfo(errorMessage)); | ||
} | ||
} | ||
|
||
return new Result(true, null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
.../core/src/test/java/org/elasticsearch/xpack/core/ilm/CheckTargetShardsCountStepTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
package org.elasticsearch.xpack.core.ilm; | ||
|
||
import org.elasticsearch.Version; | ||
import org.elasticsearch.cluster.ClusterState; | ||
import org.elasticsearch.cluster.metadata.IndexMetadata; | ||
import org.elasticsearch.cluster.metadata.Metadata; | ||
import org.elasticsearch.xpack.core.ilm.Step.StepKey; | ||
import org.elasticsearch.xpack.core.ilm.step.info.SingleMessageFieldInfo; | ||
|
||
import static org.hamcrest.Matchers.is; | ||
|
||
public class CheckTargetShardsCountStepTests extends AbstractStepTestCase<CheckTargetShardsCountStep> { | ||
|
||
@Override | ||
protected CheckTargetShardsCountStep createRandomInstance() { | ||
return new CheckTargetShardsCountStep(randomStepKey(), randomStepKey(), null); | ||
} | ||
|
||
@Override | ||
protected CheckTargetShardsCountStep mutateInstance(CheckTargetShardsCountStep instance) { | ||
StepKey key = instance.getKey(); | ||
StepKey nextKey = instance.getNextStepKey(); | ||
|
||
switch (between(0, 1)) { | ||
case 0: | ||
key = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); | ||
break; | ||
case 1: | ||
nextKey = new StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5)); | ||
break; | ||
default: | ||
throw new AssertionError("Illegal randomisation branch"); | ||
} | ||
|
||
return new CheckTargetShardsCountStep(key, nextKey, null); | ||
} | ||
|
||
@Override | ||
protected CheckTargetShardsCountStep copyInstance(CheckTargetShardsCountStep instance) { | ||
return new CheckTargetShardsCountStep(instance.getKey(), instance.getNextStepKey(), instance.getNumberOfShards()); | ||
} | ||
|
||
public void testStepCompleteIfTargetShardsCountIsValid() { | ||
String policyName = "test-ilm-policy"; | ||
IndexMetadata indexMetadata = | ||
IndexMetadata.builder(randomAlphaOfLength(10)).settings(settings(Version.CURRENT) | ||
.put(LifecycleSettings.LIFECYCLE_NAME, policyName)) | ||
.numberOfShards(10).numberOfReplicas(randomIntBetween(0, 5)).build(); | ||
|
||
ClusterState clusterState = ClusterState.builder(emptyClusterState()).metadata( | ||
Metadata.builder().put(indexMetadata, true).build()).build(); | ||
|
||
CheckTargetShardsCountStep checkTargetShardsCountStep = new CheckTargetShardsCountStep(randomStepKey(), randomStepKey(), 2); | ||
|
||
ClusterStateWaitStep.Result result = checkTargetShardsCountStep.isConditionMet(indexMetadata.getIndex(), clusterState); | ||
assertThat(result.isComplete(), is(true)); | ||
} | ||
|
||
public void testStepIncompleteIfTargetShardsCountNotValid() { | ||
String indexName = randomAlphaOfLength(10); | ||
String policyName = "test-ilm-policy"; | ||
IndexMetadata indexMetadata = | ||
IndexMetadata.builder(indexName).settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) | ||
.numberOfShards(10).numberOfReplicas(randomIntBetween(0, 5)).build(); | ||
|
||
ClusterState clusterState = ClusterState.builder(emptyClusterState()).metadata( | ||
Metadata.builder().put(indexMetadata, true).build()).build(); | ||
|
||
CheckTargetShardsCountStep checkTargetShardsCountStep = new CheckTargetShardsCountStep(randomStepKey(), randomStepKey(), 3); | ||
|
||
ClusterStateWaitStep.Result result = checkTargetShardsCountStep.isConditionMet(indexMetadata.getIndex(), clusterState); | ||
assertThat(result.isComplete(), is(false)); | ||
SingleMessageFieldInfo info = (SingleMessageFieldInfo) result.getInfomationContext(); | ||
assertThat(info.getMessage(), is("lifecycle action of policy [" + policyName + "] for index [" + indexName + | ||
"] cannot make progress because the target shards count [3] must be a factor of the source index's shards count [10]")); | ||
} | ||
} |
Oops, something went wrong.