-
Notifications
You must be signed in to change notification settings - Fork 25.3k
Ensure ILM policies run safely on leader indices #38140
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
gwbrown
merged 6 commits into
elastic:master
from
gwbrown:ilm/check-shard-history-leases-step
Feb 2, 2019
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
962d5ba
Ensure ILM policies run safely on leader indices
gwbrown fa2e3a9
Fix test
gwbrown 2c175ab
Fix integration test
gwbrown 1de1466
Clean up unused functions
gwbrown 906da63
Review feedback
gwbrown 99a2acf
Merge remote-tracking branch 'elastic/master' into pr/38140
jasontedor 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 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
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
108 changes: 108 additions & 0 deletions
108
...ore/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/WaitForNoFollowersStep.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,108 @@ | ||
/* | ||
* 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.ActionListener; | ||
import org.elasticsearch.action.admin.indices.stats.IndexStats; | ||
import org.elasticsearch.action.admin.indices.stats.IndicesStatsRequest; | ||
import org.elasticsearch.action.admin.indices.stats.ShardStats; | ||
import org.elasticsearch.client.Client; | ||
import org.elasticsearch.cluster.metadata.IndexMetaData; | ||
import org.elasticsearch.common.ParseField; | ||
import org.elasticsearch.common.xcontent.ToXContentObject; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
|
||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.Objects; | ||
|
||
/** | ||
* A step that waits until the index it's used on is no longer a leader index. | ||
* This is necessary as there are some actions which are not safe to perform on | ||
* a leader index, such as those which delete the index, including Shrink and | ||
* Delete. | ||
*/ | ||
public class WaitForNoFollowersStep extends AsyncWaitStep { | ||
|
||
private static final Logger logger = LogManager.getLogger(WaitForNoFollowersStep.class); | ||
|
||
static final String NAME = "wait-for-shard-history-leases"; | ||
static final String CCR_LEASE_KEY = "ccr"; | ||
|
||
WaitForNoFollowersStep(StepKey key, StepKey nextStepKey, Client client) { | ||
super(key, nextStepKey, client); | ||
} | ||
|
||
@Override | ||
public void evaluateCondition(IndexMetaData indexMetaData, Listener listener) { | ||
IndicesStatsRequest request = new IndicesStatsRequest(); | ||
request.clear(); | ||
String indexName = indexMetaData.getIndex().getName(); | ||
request.indices(indexName); | ||
getClient().admin().indices().stats(request, ActionListener.wrap((response) -> { | ||
IndexStats indexStats = response.getIndex(indexName); | ||
if (indexStats == null) { | ||
// Index was probably deleted | ||
logger.debug("got null shard stats for index {}, proceeding on the assumption it has been deleted", | ||
indexMetaData.getIndex()); | ||
listener.onResponse(true, null); | ||
return; | ||
} | ||
|
||
boolean isCurrentlyLeaderIndex = Arrays.stream(indexStats.getShards()) | ||
.map(ShardStats::getRetentionLeaseStats) | ||
.flatMap(retentionLeaseStats -> retentionLeaseStats.retentionLeases().leases().stream()) | ||
.anyMatch(lease -> CCR_LEASE_KEY.equals(lease.source())); | ||
|
||
if (isCurrentlyLeaderIndex) { | ||
listener.onResponse(false, new Info()); | ||
} else { | ||
listener.onResponse(true, null); | ||
} | ||
}, listener::onFailure)); | ||
} | ||
|
||
static final class Info implements ToXContentObject { | ||
|
||
static final ParseField MESSAGE_FIELD = new ParseField("message"); | ||
|
||
private static final String message = "this index is a leader index; waiting for all following indices to cease " + | ||
"following before proceeding"; | ||
|
||
Info() { } | ||
|
||
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; | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(getMessage()); | ||
} | ||
} | ||
} |
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
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
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.
Can you add javadoc please?