-
Notifications
You must be signed in to change notification settings - Fork 25.2k
ILM action to wait for SLM policy execution #50454
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
probakowski
merged 28 commits into
elastic:master
from
probakowski:ilm-wait-for-snapshot
Jan 9, 2020
Merged
Changes from 19 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
7f4cc0e
Snapshot action
probakowski b282be6
ILM action to wait for SLM policy execution
probakowski 30a6e8a
merge fix
probakowski 095704e
merge fix
probakowski f4fab83
tests
probakowski 94942b4
tests
probakowski 9ca8d85
spaces fix
probakowski de25513
unused imports removed
probakowski dc33fd1
Review comments
probakowski fa14142
Date foramtting
probakowski 779c28c
Merge remote-tracking branch 'origin/master' into ilm-wait-for-snapshot
probakowski 9ecb8b5
Fixed toLowerCase in test
probakowski 19e5865
Test fix
probakowski 0997fd1
Test fix
probakowski 83feec2
Test fix
probakowski 0f24f07
Test fix
probakowski b8976fc
Test fix
probakowski 5f3a2cb
Test fix
probakowski 94d0cbd
Javadoc
probakowski 0f5d0db
Docs added
probakowski 1033a92
review comments
probakowski 0d7e025
action name changed
probakowski 2c2794c
Revert "action name changed"
probakowski bea77f2
action name changed
probakowski ad5fb88
Merge branch 'master' into ilm-wait-for-snapshot
probakowski 015a940
action name changed
probakowski 704dd62
Action name updated in docs
probakowski 440ba80
test fix
probakowski 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
96 changes: 96 additions & 0 deletions
96
x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForSnapshotAction.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,96 @@ | ||
/* | ||
* 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.ilm; | ||
|
||
import org.elasticsearch.client.Client; | ||
import org.elasticsearch.common.ParseField; | ||
import org.elasticsearch.common.Strings; | ||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.common.xcontent.ConstructingObjectParser; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
import org.elasticsearch.common.xcontent.XContentParser; | ||
import org.elasticsearch.xpack.core.ilm.Step.StepKey; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
/** | ||
* A {@link LifecycleAction} which waits for snapshot to be taken (by configured SLM policy). | ||
*/ | ||
public class WaitForSnapshotAction implements LifecycleAction { | ||
|
||
public static final String NAME = "snapshot"; | ||
public static final ParseField POLICY_FIELD = new ParseField("policy"); | ||
|
||
private static final ConstructingObjectParser<WaitForSnapshotAction, Void> PARSER = new ConstructingObjectParser<>(NAME, | ||
a -> new WaitForSnapshotAction((String) a[0])); | ||
|
||
static { | ||
PARSER.declareString(ConstructingObjectParser.constructorArg(), POLICY_FIELD); | ||
} | ||
|
||
private final String policy; | ||
|
||
public static WaitForSnapshotAction parse(XContentParser parser) { | ||
return PARSER.apply(parser, null); | ||
} | ||
|
||
public WaitForSnapshotAction(String policy) { | ||
if (Strings.hasText(policy) == false) { | ||
throw new IllegalArgumentException("policy name must be specified"); | ||
} | ||
this.policy = policy; | ||
} | ||
|
||
public WaitForSnapshotAction(StreamInput in) throws IOException { | ||
this(in.readString()); | ||
} | ||
|
||
@Override | ||
public List<Step> toSteps(Client client, String phase, StepKey nextStepKey) { | ||
StepKey waitForSnapshotKey = new StepKey(phase, NAME, WaitForSnapshotStep.NAME); | ||
return Collections.singletonList(new WaitForSnapshotStep(waitForSnapshotKey, nextStepKey, policy)); | ||
} | ||
|
||
@Override | ||
public boolean isSafeAction() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public String getWriteableName() { | ||
return NAME; | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
out.writeString(policy); | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject(); | ||
builder.field(POLICY_FIELD.getPreferredName(), policy); | ||
builder.endObject(); | ||
return builder; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
WaitForSnapshotAction that = (WaitForSnapshotAction) o; | ||
return policy.equals(that.policy); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(policy); | ||
} | ||
} |
101 changes: 101 additions & 0 deletions
101
x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForSnapshotStep.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,101 @@ | ||
/* | ||
* 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.ilm; | ||
|
||
import org.elasticsearch.cluster.ClusterState; | ||
import org.elasticsearch.cluster.metadata.IndexMetaData; | ||
import org.elasticsearch.common.xcontent.ToXContentObject; | ||
import org.elasticsearch.index.Index; | ||
import org.elasticsearch.xpack.core.slm.SnapshotLifecycleMetadata; | ||
import org.elasticsearch.xpack.core.slm.SnapshotLifecyclePolicyMetadata; | ||
|
||
import java.util.Date; | ||
import java.util.Locale; | ||
import java.util.Objects; | ||
|
||
/*** | ||
* A step that waits for snapshot to be taken by SLM to ensure we have backup before we delete the index. | ||
* It will signal error if it can't get data needed to do the check (phase time from ILM and SLM metadata) | ||
* and will only return success if execution of SLM policy took place after index entered deleted phase. | ||
*/ | ||
public class WaitForSnapshotStep extends ClusterStateWaitStep { | ||
|
||
static final String NAME = "wait-for-snapshot"; | ||
|
||
private static final String MESSAGE_FIELD = "message"; | ||
private static final String POLICY_NOT_EXECUTED_MESSAGE = "waiting for policy '%s' to be executed since %s"; | ||
private static final String POLICY_NOT_FOUND_MESSAGE = "configured policy '%s' not found"; | ||
private static final String NO_INDEX_METADATA_MESSAGE = "no index metadata found for index '%s'"; | ||
private static final String NO_PHASE_TIME_MESSAGE = "no information about ILM phase start in index metadata for index '%s'"; | ||
|
||
private final String policy; | ||
|
||
WaitForSnapshotStep(StepKey key, StepKey nextStepKey, String policy) { | ||
super(key, nextStepKey); | ||
this.policy = policy; | ||
} | ||
|
||
@Override | ||
public Result isConditionMet(Index index, ClusterState clusterState) { | ||
IndexMetaData indexMetaData = clusterState.metaData().index(index); | ||
if (indexMetaData == null) { | ||
throw error(NO_INDEX_METADATA_MESSAGE, index.getName()); | ||
} | ||
|
||
Long phaseTime = LifecycleExecutionState.fromIndexMetadata(indexMetaData).getPhaseTime(); | ||
|
||
if (phaseTime == null) { | ||
throw error(NO_PHASE_TIME_MESSAGE, index.getName()); | ||
} | ||
|
||
SnapshotLifecycleMetadata snapMeta = clusterState.metaData().custom(SnapshotLifecycleMetadata.TYPE); | ||
if (snapMeta == null || snapMeta.getSnapshotConfigurations().containsKey(policy) == false) { | ||
throw error(POLICY_NOT_FOUND_MESSAGE, policy); | ||
} | ||
SnapshotLifecyclePolicyMetadata snapPolicyMeta = snapMeta.getSnapshotConfigurations().get(policy); | ||
if (snapPolicyMeta.getLastSuccess() == null || snapPolicyMeta.getLastSuccess().getTimestamp() < phaseTime) { | ||
return new Result(false, notExecutedMessage(phaseTime)); | ||
} | ||
|
||
return new Result(true, null); | ||
} | ||
|
||
public String getPolicy() { | ||
return policy; | ||
} | ||
|
||
@Override | ||
public boolean isRetryable() { | ||
return true; | ||
} | ||
|
||
private ToXContentObject notExecutedMessage(long time) { | ||
return (builder, params) -> { | ||
builder.startObject(); | ||
builder.field(MESSAGE_FIELD, String.format(Locale.ROOT, POLICY_NOT_EXECUTED_MESSAGE, policy, new Date(time))); | ||
builder.endObject(); | ||
return builder; | ||
}; | ||
} | ||
|
||
private IllegalStateException error(String message, Object... args) { | ||
return new IllegalStateException(String.format(Locale.ROOT, message, args)); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
if (!super.equals(o)) return false; | ||
WaitForSnapshotStep that = (WaitForSnapshotStep) o; | ||
return policy.equals(that.policy); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(super.hashCode(), policy); | ||
} | ||
} |
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
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
56 changes: 56 additions & 0 deletions
56
...lugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForSnapshotActionTests.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,56 @@ | ||
/* | ||
* 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.ilm; | ||
|
||
import org.elasticsearch.common.io.stream.Writeable; | ||
import org.elasticsearch.common.xcontent.XContentParser; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
public class WaitForSnapshotActionTests extends AbstractActionTestCase<WaitForSnapshotAction> { | ||
|
||
@Override | ||
public void testToSteps() { | ||
WaitForSnapshotAction action = createTestInstance(); | ||
Step.StepKey nextStep = new Step.StepKey("", "", ""); | ||
List<Step> steps = action.toSteps(null, "delete", nextStep); | ||
assertEquals(1, steps.size()); | ||
Step step = steps.get(0); | ||
assertTrue(step instanceof WaitForSnapshotStep); | ||
assertEquals(nextStep, step.getNextStepKey()); | ||
|
||
Step.StepKey key = step.getKey(); | ||
assertEquals("delete", key.getPhase()); | ||
assertEquals(WaitForSnapshotAction.NAME, key.getAction()); | ||
assertEquals(WaitForSnapshotStep.NAME, key.getName()); | ||
} | ||
|
||
@Override | ||
protected WaitForSnapshotAction doParseInstance(XContentParser parser) throws IOException { | ||
return WaitForSnapshotAction.parse(parser); | ||
} | ||
|
||
@Override | ||
protected WaitForSnapshotAction createTestInstance() { | ||
return randomInstance(); | ||
} | ||
|
||
@Override | ||
protected Writeable.Reader<WaitForSnapshotAction> instanceReader() { | ||
return WaitForSnapshotAction::new; | ||
} | ||
|
||
@Override | ||
protected WaitForSnapshotAction mutateInstance(WaitForSnapshotAction instance) throws IOException { | ||
return randomInstance(); | ||
} | ||
|
||
static WaitForSnapshotAction randomInstance() { | ||
return new WaitForSnapshotAction(randomAlphaOfLengthBetween(5, 10)); | ||
} | ||
|
||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.