-
Notifications
You must be signed in to change notification settings - Fork 25k
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
Add dedicated step for checking shrink allocation status #35161
Merged
dakrone
merged 6 commits into
elastic:master
from
dakrone:ilm-fix-shrink-allocation-check
Nov 5, 2018
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a162d59
Add dedicated step for checking shrink allocation status
dakrone 86733dc
Merge remote-tracking branch 'origin/index-lifecycle' into ilm-fix-sh…
dakrone e1ecdf2
Add a test that includes relocating shards
dakrone 00b8479
Merge remote-tracking branch 'origin/index-lifecycle' into ilm-fix-sh…
dakrone c98c11d
Update after moving from _name to _id
dakrone 86e380d
Merge remote-tracking branch 'origin/master' into ilm-fix-shrink-allo…
dakrone 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
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
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
175 changes: 175 additions & 0 deletions
175
.../core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/CheckShrinkReadyStep.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,175 @@ | ||
/* | ||
* 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.indexlifecycle; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.elasticsearch.action.support.ActiveShardCount; | ||
import org.elasticsearch.cluster.ClusterState; | ||
import org.elasticsearch.cluster.metadata.IndexMetaData; | ||
import org.elasticsearch.cluster.routing.IndexRoutingTable; | ||
import org.elasticsearch.cluster.routing.ShardRouting; | ||
import org.elasticsearch.cluster.routing.ShardRoutingState; | ||
import org.elasticsearch.common.ParseField; | ||
import org.elasticsearch.common.Strings; | ||
import org.elasticsearch.common.xcontent.ConstructingObjectParser; | ||
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 is used prior to running a shrink step in order to ensure that the index being shrunk | ||
* has a copy of each shard allocated on one particular node (the node used by the require | ||
* parameter) and that the shards are not relocating. | ||
*/ | ||
public class CheckShrinkReadyStep extends ClusterStateWaitStep { | ||
public static final String NAME = "check-shrink-allocation"; | ||
|
||
private static final Logger logger = LogManager.getLogger(CheckShrinkReadyStep.class); | ||
|
||
CheckShrinkReadyStep(StepKey key, StepKey nextStepKey) { | ||
super(key, nextStepKey); | ||
} | ||
|
||
@Override | ||
public Result isConditionMet(Index index, ClusterState clusterState) { | ||
IndexMetaData idxMeta = clusterState.metaData().index(index); | ||
|
||
if (idxMeta == 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); | ||
} | ||
|
||
// How many shards the node should have | ||
int expectedShardCount = idxMeta.getNumberOfShards(); | ||
|
||
if (ActiveShardCount.ALL.enoughShardsActive(clusterState, index.getName()) == false) { | ||
logger.debug("[{}] shrink action for [{}] cannot make progress because not all shards are active", | ||
getKey().getAction(), index.getName()); | ||
return new Result(false, new CheckShrinkReadyStep.Info("", expectedShardCount, -1)); | ||
} | ||
|
||
// The id of the node the shards should be on | ||
final String idShardsShouldBeOn = idxMeta.getSettings().get(IndexMetaData.INDEX_ROUTING_REQUIRE_GROUP_PREFIX + "._id"); | ||
if (idShardsShouldBeOn == null) { | ||
throw new IllegalStateException("Cannot check shrink allocation as there are no allocation rules by _id"); | ||
} | ||
|
||
final IndexRoutingTable routingTable = clusterState.getRoutingTable().index(index); | ||
int foundShards = 0; | ||
for (ShardRouting shard : routingTable.shardsWithState(ShardRoutingState.STARTED)) { | ||
final String currentNodeId = shard.currentNodeId(); | ||
if (idShardsShouldBeOn.equals(currentNodeId) && shard.relocating() == false) { | ||
foundShards++; | ||
} | ||
} | ||
|
||
logger.trace("{} checking for shrink readiness on [{}], found {} shards and need {}", | ||
index, idShardsShouldBeOn, foundShards, expectedShardCount); | ||
|
||
if (foundShards == expectedShardCount) { | ||
logger.trace("{} successfully found {} allocated shards for shrink readiness on node [{}] ({})", | ||
index, expectedShardCount, idShardsShouldBeOn, getKey().getAction()); | ||
return new Result(true, null); | ||
} else { | ||
logger.trace("{} failed to find {} allocated shards (found {}) on node [{}] for shrink readiness ({})", | ||
index, expectedShardCount, foundShards, idShardsShouldBeOn, getKey().getAction()); | ||
return new Result(false, new CheckShrinkReadyStep.Info(idShardsShouldBeOn, expectedShardCount, | ||
expectedShardCount - foundShards)); | ||
} | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return 612; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (obj == null) { | ||
return false; | ||
} | ||
if (getClass() != obj.getClass()) { | ||
return false; | ||
} | ||
return super.equals(obj); | ||
} | ||
|
||
public static final class Info implements ToXContentObject { | ||
|
||
private final String nodeId; | ||
private final long actualReplicas; | ||
private final long numberShardsLeftToAllocate; | ||
private final String message; | ||
|
||
static final ParseField NODE_ID = new ParseField("node_id"); | ||
static final ParseField EXPECTED_SHARDS = new ParseField("expected_shards"); | ||
static final ParseField SHARDS_TO_ALLOCATE = new ParseField("shards_left_to_allocate"); | ||
static final ParseField MESSAGE = new ParseField("message"); | ||
static final ConstructingObjectParser<CheckShrinkReadyStep.Info, Void> PARSER = new ConstructingObjectParser<>( | ||
"check_shrink_ready_step_info", a -> new CheckShrinkReadyStep.Info((String) a[0], (long) a[1], (long) a[2])); | ||
static { | ||
PARSER.declareString(ConstructingObjectParser.constructorArg(), NODE_ID); | ||
PARSER.declareLong(ConstructingObjectParser.constructorArg(), EXPECTED_SHARDS); | ||
PARSER.declareLong(ConstructingObjectParser.constructorArg(), SHARDS_TO_ALLOCATE); | ||
PARSER.declareString((i, s) -> {}, MESSAGE); | ||
} | ||
|
||
public Info(String nodeId, long expectedShards, long numberShardsLeftToAllocate) { | ||
this.nodeId = nodeId; | ||
this.actualReplicas = expectedShards; | ||
this.numberShardsLeftToAllocate = numberShardsLeftToAllocate; | ||
if (numberShardsLeftToAllocate < 0) { | ||
this.message = "Waiting for all shards to become active"; | ||
} else { | ||
this.message = String.format(Locale.ROOT, "Waiting for node [%s] to contain [%d] shards, found [%d], remaining [%d]", | ||
nodeId, expectedShards, expectedShards - numberShardsLeftToAllocate, numberShardsLeftToAllocate); | ||
} | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject(); | ||
builder.field(MESSAGE.getPreferredName(), message); | ||
builder.field(NODE_ID.getPreferredName(), nodeId); | ||
builder.field(SHARDS_TO_ALLOCATE.getPreferredName(), numberShardsLeftToAllocate); | ||
builder.field(EXPECTED_SHARDS.getPreferredName(), actualReplicas); | ||
builder.endObject(); | ||
return builder; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(nodeId, actualReplicas, numberShardsLeftToAllocate); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (obj == null) { | ||
return false; | ||
} | ||
if (getClass() != obj.getClass()) { | ||
return false; | ||
} | ||
CheckShrinkReadyStep.Info other = (CheckShrinkReadyStep.Info) obj; | ||
return Objects.equals(actualReplicas, other.actualReplicas) && | ||
Objects.equals(numberShardsLeftToAllocate, other.numberShardsLeftToAllocate) && | ||
Objects.equals(nodeId, other.nodeId); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return Strings.toString(this); | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to add a check for the shard being started here too otherwise we will end up with the same issue on the allocate action but it will be harder to diagnose
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to address that in a separate PR, since I think it warrants more testing for the full ramifications, would that be okay with you?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, could you create an issue so we can track the extra work and it doesn't get missed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Certainly, opened #35258