-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Add validation of the number_of_shards parameter in Shrink Action of ILM #74219
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
977ab67
Add validation of the number_of_shards parameter in Shrink Action of ILM
609d402
Merge remote-tracking branch 'origin/master' into enhance_shrink
1f7c94c
Use a single step
e7bc201
Merge remote-tracking branch 'origin/master' into enhance_shrink
4644f45
add more checks and fix test failure
e41b71d
Merge remote-tracking branch 'origin/master' into enhance_shrink
6e810f4
Merge remote-tracking branch 'origin/master' into enhance_shrink
cfa6827
Merge remote-tracking branch 'upstream/enhance_shrink' into enhance_s…
ee9b060
fix test failure
3f50349
Merge remote-tracking branch 'origin/master' into enhance_shrink
4d3400e
fix test failure
c67d7e0
Merge remote-tracking branch 'origin/master' into enhance_shrink
68bec26
Merge remote-tracking branch 'origin/master' into enhance_shrink
a4a5064
add new test
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
...lugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CheckTargetShardsCountStep.java
This file contains hidden or 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,103 @@ | ||
/* | ||
* 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.common.xcontent.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.Locale; | ||
import java.util.Objects; | ||
|
||
/** | ||
* 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); | ||
String indexName = indexMetadata.getIndex().getName(); | ||
if (numberOfShards != null) { | ||
int sourceNumberOfShards = indexMetadata.getNumberOfShards(); | ||
if (sourceNumberOfShards % numberOfShards != 0) { | ||
String errorMessage = String.format(Locale.ROOT, "the target shards count [%d] must be a factor of the source index [%s]" + | ||
gaobinlong marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"'s shards count [%d]", numberOfShards, indexName, sourceNumberOfShards); | ||
logger.debug(errorMessage); | ||
return new Result(false, new Info(errorMessage)); | ||
} | ||
} | ||
|
||
return new Result(true, null); | ||
} | ||
|
||
static final class Info implements ToXContentObject { | ||
gaobinlong marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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); | ||
} | ||
} | ||
} |
This file contains hidden or 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
82 changes: 82 additions & 0 deletions
82
.../core/src/test/java/org/elasticsearch/xpack/core/ilm/CheckTargetShardsCountStepTests.java
This file contains hidden or 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,82 @@ | ||
/* | ||
* 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 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)); | ||
CheckTargetShardsCountStep.Info info = (CheckTargetShardsCountStep.Info) result.getInfomationContext(); | ||
assertThat(info.getMessage(), is("the target shards count [3] must be a factor of the source index [" + indexName + "]" + | ||
"'s shards count [10]")); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.