forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ILM action to wait for SLM policy execution (elastic#50454)
This change add new ILM action to wait for SLM policy execution to ensure that index has snapshot before deletion. Closes elastic#45067
- Loading branch information
1 parent
86fb06a
commit 7744c7e
Showing
14 changed files
with
540 additions
and
2 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
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
96 changes: 96 additions & 0 deletions
96
x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForSnapshotAction.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,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 = "wait_for_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 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 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
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
56 changes: 56 additions & 0 deletions
56
...lugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForSnapshotActionTests.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,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.