Skip to content

Commit

Permalink
[7.11] [ILM] check if delete phase has actions (#66664) (#67076)
Browse files Browse the repository at this point in the history
* [ILM] check if delete phase has actions (#65847) (#66664)

Validate the delete phase to define actions.

(cherry picked from commit 7f39514)
Signed-off-by: Andrei Dan <andrei.dan@elastic.co>

* Test: fix testValidatePhases for delete phase (#67082)

The delete phase can no longer be defined as empty, it requires at least
one action to be defined. This fixes the test that used an empty collection
of actions for the delete phase to include the delete action.

(cherry picked from commit 8b158ad)
Signed-off-by: Andrei Dan <andrei.dan@elastic.co>
  • Loading branch information
andreidan committed Jan 6, 2021
1 parent 688d6a5 commit a02050b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ public LifecyclePolicy(String name, Map<String, Phase> phases) {
if (ALLOWED_ACTIONS.containsKey(phase.getName()) == false) {
throw new IllegalArgumentException("Lifecycle does not support phase [" + phase.getName() + "]");
}
phase.getActions().forEach((actionName, action) -> {
if (phase.getName().equals("delete") && phase.getActions().size() == 0) {
throw new IllegalArgumentException("phase [" + phase.getName() + "] must define actions");
}
phase.getActions().forEach((actionName, action) -> {
if (ALLOWED_ACTIONS.get(phase.getName()).contains(actionName) == false) {
throw new IllegalArgumentException("invalid action [" + actionName + "] " +
"defined in phase [" + phase.getName() +"]");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ public void testValidatePhases() {
phaseName += randomAlphaOfLength(5);
}
Map<String, Phase> phases = Collections.singletonMap(phaseName,
new Phase(phaseName, TimeValue.ZERO, Collections.emptyMap()));
new Phase(phaseName, TimeValue.ZERO, phaseName.equals("delete") ? Collections.singletonMap(DeleteAction.NAME,
new DeleteAction()) : Collections.emptyMap()));
if (invalid) {
Exception e = expectThrows(IllegalArgumentException.class, () -> new LifecyclePolicy(lifecycleName, phases));
assertThat(e.getMessage(), equalTo("Lifecycle does not support phase [" + phaseName + "]"));
Expand Down Expand Up @@ -192,6 +193,17 @@ public void testValidateDeletePhase() {
}
}

public void testValidateEmptyDeletePhase() {
Map<String, LifecycleAction> actions = new HashMap<>();

Phase delete = new Phase("delete", TimeValue.ZERO, actions);
Map<String, Phase> phases = Collections.singletonMap("delete", delete);

Exception e = expectThrows(IllegalArgumentException.class,
() -> new LifecyclePolicy(lifecycleName, phases));
assertThat(e.getMessage(), equalTo("phase [" + delete.getName() + "] must define actions"));
}

public static LifecyclePolicy createRandomPolicy(String lifecycleName) {
List<String> phaseNames = randomSubsetOf(Arrays.asList("hot", "warm", "cold", "delete"));
Map<String, Phase> phases = new HashMap<>(phaseNames.size());
Expand All @@ -208,6 +220,17 @@ public static LifecyclePolicy createRandomPolicy(String lifecycleName) {
default:
throw new IllegalArgumentException("invalid phase [" + phase + "]");
}};
Function<String, Boolean> allowEmptyActions = (phase) -> {
switch (phase) {
case "hot":
case "warm":
case "cold":
return true;
case "delete":
return false;
default:
throw new IllegalArgumentException("invalid phase [" + phase + "]");
}};
Function<String, LifecycleAction> randomAction = (action) -> {
switch (action) {
case AllocateAction.NAME:
Expand Down Expand Up @@ -238,7 +261,12 @@ public static LifecyclePolicy createRandomPolicy(String lifecycleName) {
for (String phase : phaseNames) {
TimeValue after = TimeValue.parseTimeValue(randomTimeValue(0, 1000000000, "s", "m", "h", "d"), "test_after");
Map<String, LifecycleAction> actions = new HashMap<>();
List<String> actionNames = randomSubsetOf(validActions.apply(phase));
List<String> actionNames;
if (allowEmptyActions.apply(phase)) {
actionNames = randomSubsetOf(validActions.apply(phase));
} else {
actionNames = randomSubsetOf(randomIntBetween(1, validActions.apply(phase).size()), validActions.apply(phase));
}
for (String action : actionNames) {
actions.put(action, randomAction.apply(action));
}
Expand Down

0 comments on commit a02050b

Please sign in to comment.