From b12f1a95268c77635a7951eec8e9e56822dc99c2 Mon Sep 17 00:00:00 2001 From: Colin Goodheart-Smithe Date: Fri, 15 Dec 2017 11:20:59 +0000 Subject: [PATCH] More phase tests This time for when setting the new action fails --- .../xpack/indexlifecycle/MockAction.java | 6 +- .../xpack/indexlifecycle/PhaseTests.java | 243 ++++++++++++++++++ 2 files changed, 248 insertions(+), 1 deletion(-) diff --git a/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/MockAction.java b/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/MockAction.java index 0573fe84817..81aafecdf42 100644 --- a/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/MockAction.java +++ b/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/MockAction.java @@ -26,7 +26,7 @@ public class MockAction implements LifecycleAction { public static final ParseField EXECUTED_COUNT_FIELD = new ParseField("executed_count"); public static final ParseField NAME_FIELD = new ParseField("name"); public static final String NAME = "TEST_ACTION"; - private final SetOnce completed = new SetOnce<>(); + private SetOnce completed = new SetOnce<>(); private final AtomicLong executedCount; private Exception exceptionToThrow = null; private boolean completeOnExecute = false; @@ -89,6 +89,10 @@ public class MockAction implements LifecycleAction { return completed.get() != null && completed.get(); } + public void resetCompleted() { + completed = new SetOnce<>(); + } + public long getExecutedCount() { return executedCount.get(); } diff --git a/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/PhaseTests.java b/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/PhaseTests.java index c4fc74d1f46..28081d53bfc 100644 --- a/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/PhaseTests.java +++ b/x-pack/plugin/src/test/java/org/elasticsearch/xpack/indexlifecycle/PhaseTests.java @@ -783,4 +783,247 @@ public class PhaseTests extends AbstractSerializingTestCase { assertEquals(1L, thirdAction.getExecutedCount()); } + public void testExecuteActionFailedSetAction() throws Exception { + String indexName = randomAlphaOfLengthBetween(1, 20); + String phaseName = randomAlphaOfLengthBetween(1, 20); + TimeValue after = TimeValue.timeValueSeconds(randomIntBetween(10, 100)); + Map actions = new HashMap<>(); + MockAction firstAction = new MockAction() { + @Override + public String getWriteableName() { + return "first_action"; + } + }; + firstAction.setCompleteOnExecute(false); + actions.put(firstAction.getWriteableName(), firstAction); + MockAction secondAction = new MockAction() { + @Override + public String getWriteableName() { + return "second_action"; + } + }; + secondAction.setCompleteOnExecute(true); + actions.put(secondAction.getWriteableName(), secondAction); + MockAction thirdAction = new MockAction() { + @Override + public String getWriteableName() { + return "third_action"; + } + }; + thirdAction.setCompleteOnExecute(false); + actions.put(thirdAction.getWriteableName(), thirdAction); + Phase phase = new Phase(phaseName, after, actions); + + MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, phaseName, secondAction.getWriteableName(), 0) { + + @Override + public boolean canExecute(Phase phase) { + throw new AssertionError("canExecute should not have been called"); + } + }; + + // First check that if setting the new action fails we don't execute the + // next phase + Exception exception = new RuntimeException(); + context.failOnSetters(exception); + + phase.execute(context, current -> { + if (current == null) { + return firstAction; + } else if ("first_action".equals(current.getWriteableName())) { + return secondAction; + } else if ("second_action".equals(current.getWriteableName())) { + return thirdAction; + } + return null; + }); + + assertEquals(indexName, context.getLifecycleTarget()); + assertEquals(phaseName, context.getPhase()); + assertEquals(secondAction.getWriteableName(), context.getAction()); + + assertFalse(firstAction.wasCompleted()); + assertEquals(0L, firstAction.getExecutedCount()); + assertTrue(secondAction.wasCompleted()); + assertEquals(1L, secondAction.getExecutedCount()); + assertFalse(thirdAction.wasCompleted()); + assertEquals(0L, thirdAction.getExecutedCount()); + + // Now check that if we execute the phase again the current action is + // re-executed and if setting the new action fails again we still don't + // execute the next action + secondAction.setCompleteOnExecute(true); + secondAction.resetCompleted(); + + phase.execute(context, current -> { + if (current == null) { + return firstAction; + } else if ("first_action".equals(current.getWriteableName())) { + return secondAction; + } else if ("second_action".equals(current.getWriteableName())) { + return thirdAction; + } + return null; + }); + + assertEquals(indexName, context.getLifecycleTarget()); + assertEquals(phaseName, context.getPhase()); + assertEquals(secondAction.getWriteableName(), context.getAction()); + + assertFalse(firstAction.wasCompleted()); + assertEquals(0L, firstAction.getExecutedCount()); + assertTrue(secondAction.wasCompleted()); + assertEquals(2L, secondAction.getExecutedCount()); + assertFalse(thirdAction.wasCompleted()); + assertEquals(0L, thirdAction.getExecutedCount()); + + // Now check that if we execute the phase again the current action is + // re-executed and if setting the new action suceeds now the next action + // is executed + secondAction.setCompleteOnExecute(true); + context.failOnSetters(null); + secondAction.resetCompleted(); + + phase.execute(context, current -> { + if (current == null) { + return firstAction; + } else if ("first_action".equals(current.getWriteableName())) { + return secondAction; + } else if ("second_action".equals(current.getWriteableName())) { + return thirdAction; + } + return null; + }); + + assertEquals(indexName, context.getLifecycleTarget()); + assertEquals(phaseName, context.getPhase()); + assertEquals(thirdAction.getWriteableName(), context.getAction()); + + assertFalse(firstAction.wasCompleted()); + assertEquals(0L, firstAction.getExecutedCount()); + assertTrue(secondAction.wasCompleted()); + assertEquals(3L, secondAction.getExecutedCount()); + assertFalse(thirdAction.wasCompleted()); + assertEquals(1L, thirdAction.getExecutedCount()); + } + + public void testExecuteLastActionFailedSetAction() throws Exception { + String indexName = randomAlphaOfLengthBetween(1, 20); + String phaseName = randomAlphaOfLengthBetween(1, 20); + TimeValue after = TimeValue.timeValueSeconds(randomIntBetween(10, 100)); + Map actions = new HashMap<>(); + MockAction firstAction = new MockAction() { + @Override + public String getWriteableName() { + return "first_action"; + } + }; + firstAction.setCompleteOnExecute(false); + actions.put(firstAction.getWriteableName(), firstAction); + MockAction secondAction = new MockAction() { + @Override + public String getWriteableName() { + return "second_action"; + } + }; + secondAction.setCompleteOnExecute(false); + actions.put(secondAction.getWriteableName(), secondAction); + MockAction thirdAction = new MockAction() { + @Override + public String getWriteableName() { + return "third_action"; + } + }; + thirdAction.setCompleteOnExecute(true); + actions.put(thirdAction.getWriteableName(), thirdAction); + Phase phase = new Phase(phaseName, after, actions); + + MockIndexLifecycleContext context = new MockIndexLifecycleContext(indexName, phaseName, thirdAction.getWriteableName(), 0) { + + @Override + public boolean canExecute(Phase phase) { + throw new AssertionError("canExecute should not have been called"); + } + }; + + // First check setting the ACTION_COMPLETED fails + Exception exception = new RuntimeException(); + context.failOnSetters(exception); + + phase.execute(context, current -> { + if (current == null) { + return firstAction; + } else if ("first_action".equals(current.getWriteableName())) { + return secondAction; + } else if ("second_action".equals(current.getWriteableName())) { + return thirdAction; + } + return null; + }); + + assertEquals(indexName, context.getLifecycleTarget()); + assertEquals(phaseName, context.getPhase()); + assertEquals(thirdAction.getWriteableName(), context.getAction()); + + assertFalse(firstAction.wasCompleted()); + assertEquals(0L, firstAction.getExecutedCount()); + assertFalse(secondAction.wasCompleted()); + assertEquals(0L, secondAction.getExecutedCount()); + assertTrue(thirdAction.wasCompleted()); + assertEquals(1L, thirdAction.getExecutedCount()); + + // Now check the same happens if it fails again + thirdAction.setCompleteOnExecute(true); + thirdAction.resetCompleted(); + + phase.execute(context, current -> { + if (current == null) { + return firstAction; + } else if ("first_action".equals(current.getWriteableName())) { + return secondAction; + } else if ("second_action".equals(current.getWriteableName())) { + return thirdAction; + } + return null; + }); + + assertEquals(indexName, context.getLifecycleTarget()); + assertEquals(phaseName, context.getPhase()); + assertEquals(thirdAction.getWriteableName(), context.getAction()); + + assertFalse(firstAction.wasCompleted()); + assertEquals(0L, firstAction.getExecutedCount()); + assertFalse(secondAction.wasCompleted()); + assertEquals(0L, secondAction.getExecutedCount()); + assertTrue(thirdAction.wasCompleted()); + assertEquals(2L, thirdAction.getExecutedCount()); + + // Now check the action updates if we try again and setting the + // PHASE_COMPLETED succeeds + context.failOnSetters(null); + thirdAction.resetCompleted(); + + phase.execute(context, current -> { + if (current == null) { + return firstAction; + } else if ("first_action".equals(current.getWriteableName())) { + return secondAction; + } else if ("second_action".equals(current.getWriteableName())) { + return thirdAction; + } + return null; + }); + + assertEquals(indexName, context.getLifecycleTarget()); + assertEquals(phaseName, context.getPhase()); + assertEquals(Phase.PHASE_COMPLETED, context.getAction()); + + assertFalse(firstAction.wasCompleted()); + assertEquals(0L, firstAction.getExecutedCount()); + assertFalse(secondAction.wasCompleted()); + assertEquals(0L, secondAction.getExecutedCount()); + assertTrue(thirdAction.wasCompleted()); + assertEquals(3L, thirdAction.getExecutedCount()); + } + }