diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/LifecycleSettings.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/LifecycleSettings.java index 89677cf6323..0eb1149f54d 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/LifecycleSettings.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/indexlifecycle/LifecycleSettings.java @@ -23,6 +23,7 @@ public class LifecycleSettings { public static final String LIFECYCLE_STEP_TIME = "index.lifecycle.step_time"; public static final String LIFECYCLE_FAILED_STEP = "index.lifecycle.failed_step"; public static final String LIFECYCLE_STEP_INFO = "index.lifecycle.step_info"; + public static final String LIFECYCLE_SKIP = "index.lifecycle.skip"; // NORELEASE: we should probably change the default to something other than three seconds for initial release public static final Setting LIFECYCLE_POLL_INTERVAL_SETTING = Setting.positiveTimeSetting(LIFECYCLE_POLL_INTERVAL, @@ -47,4 +48,6 @@ public class LifecycleSettings { -1L, -1L, Setting.Property.Dynamic, Setting.Property.IndexScope); public static final Setting LIFECYCLE_STEP_INFO_SETTING = Setting.simpleString(LIFECYCLE_STEP_INFO, Setting.Property.Dynamic, Setting.Property.IndexScope, Setting.Property.NotCopyableOnResize); + public static final Setting LIFECYCLE_SKIP_SETTING = Setting.boolSetting(LIFECYCLE_SKIP, false, + Setting.Property.Dynamic, Setting.Property.IndexScope); } diff --git a/x-pack/plugin/index-lifecycle/src/main/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycle.java b/x-pack/plugin/index-lifecycle/src/main/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycle.java index f03eacb11f6..60acd33003a 100644 --- a/x-pack/plugin/index-lifecycle/src/main/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycle.java +++ b/x-pack/plugin/index-lifecycle/src/main/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycle.java @@ -102,6 +102,7 @@ public class IndexLifecycle extends Plugin implements ActionPlugin { LifecycleSettings.LIFECYCLE_STEP_SETTING, LifecycleSettings.LIFECYCLE_STEP_INFO_SETTING, LifecycleSettings.LIFECYCLE_FAILED_STEP_SETTING, + LifecycleSettings.LIFECYCLE_SKIP_SETTING, RolloverAction.LIFECYCLE_ROLLOVER_ALIAS_SETTING); } diff --git a/x-pack/plugin/index-lifecycle/src/main/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleRunner.java b/x-pack/plugin/index-lifecycle/src/main/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleRunner.java index 2adea516ce6..64abadc05c6 100644 --- a/x-pack/plugin/index-lifecycle/src/main/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleRunner.java +++ b/x-pack/plugin/index-lifecycle/src/main/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleRunner.java @@ -48,6 +48,11 @@ public class IndexLifecycleRunner { public void runPolicy(String policy, IndexMetaData indexMetaData, ClusterState currentState, boolean fromClusterStateChange) { Settings indexSettings = indexMetaData.getSettings(); + if (LifecycleSettings.LIFECYCLE_SKIP_SETTING.get(indexSettings)) { + logger.info("skipping policy [" + policy + "] for index [" + indexMetaData.getIndex().getName() + "]." + + LifecycleSettings.LIFECYCLE_SKIP + "== true"); + return; + } Step currentStep = getCurrentStep(stepRegistry, policy, indexSettings); logger.debug("running policy with current-step[" + currentStep.getKey() + "]"); if (currentStep instanceof TerminalPolicyStep) { diff --git a/x-pack/plugin/index-lifecycle/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleRunnerTests.java b/x-pack/plugin/index-lifecycle/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleRunnerTests.java index c95bae981cc..0fa8d1cc487 100644 --- a/x-pack/plugin/index-lifecycle/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleRunnerTests.java +++ b/x-pack/plugin/index-lifecycle/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleRunnerTests.java @@ -44,6 +44,7 @@ import java.util.Objects; import java.util.SortedMap; import static org.hamcrest.Matchers.equalTo; +import static org.mockito.Mockito.mock; public class IndexLifecycleRunnerTests extends ESTestCase { @@ -62,7 +63,7 @@ public class IndexLifecycleRunnerTests extends ESTestCase { String policyName = "async_action_policy"; TerminalPolicyStep step = TerminalPolicyStep.INSTANCE; PolicyStepsRegistry stepRegistry = createOneStepPolicyStepRegistry(policyName, step); - ClusterService clusterService = Mockito.mock(ClusterService.class); + ClusterService clusterService = mock(ClusterService.class); IndexLifecycleRunner runner = new IndexLifecycleRunner(stepRegistry, clusterService, () -> 0L); IndexMetaData indexMetaData = IndexMetaData.builder("my_index").settings(settings(Version.CURRENT)) .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); @@ -77,7 +78,7 @@ public class IndexLifecycleRunnerTests extends ESTestCase { StepKey stepKey = new StepKey("phase", "action", "cluster_state_action_step"); MockClusterStateWaitStep step = new MockClusterStateWaitStep(stepKey, null); PolicyStepsRegistry stepRegistry = createOneStepPolicyStepRegistry(policyName, step); - ClusterService clusterService = Mockito.mock(ClusterService.class); + ClusterService clusterService = mock(ClusterService.class); IndexLifecycleRunner runner = new IndexLifecycleRunner(stepRegistry, clusterService, () -> 0L); IndexMetaData indexMetaData = IndexMetaData.builder("my_index").settings(settings(Version.CURRENT) .put(LifecycleSettings.LIFECYCLE_PHASE, stepKey.getPhase()) @@ -95,7 +96,7 @@ public class IndexLifecycleRunnerTests extends ESTestCase { StepKey stepKey = new StepKey("phase", "action", "cluster_state_action_step"); MockInitializePolicyContextStep step = new MockInitializePolicyContextStep(stepKey, null); PolicyStepsRegistry stepRegistry = createOneStepPolicyStepRegistry(policyName, step); - ClusterService clusterService = Mockito.mock(ClusterService.class); + ClusterService clusterService = mock(ClusterService.class); IndexLifecycleRunner runner = new IndexLifecycleRunner(stepRegistry, clusterService, () -> 0L); IndexMetaData indexMetaData = IndexMetaData.builder("my_index").settings(settings(Version.CURRENT)) .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); @@ -113,7 +114,7 @@ public class IndexLifecycleRunnerTests extends ESTestCase { MockClusterStateWaitStep step = new MockClusterStateWaitStep(stepKey, null); step.setWillComplete(true); PolicyStepsRegistry stepRegistry = createOneStepPolicyStepRegistry(policyName, step); - ClusterService clusterService = Mockito.mock(ClusterService.class); + ClusterService clusterService = mock(ClusterService.class); IndexLifecycleRunner runner = new IndexLifecycleRunner(stepRegistry, clusterService, () -> 0L); IndexMetaData indexMetaData = IndexMetaData.builder("my_index").settings(settings(Version.CURRENT)) .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); @@ -131,7 +132,7 @@ public class IndexLifecycleRunnerTests extends ESTestCase { MockAsyncActionStep step = new MockAsyncActionStep(stepKey, null); step.setWillComplete(true); PolicyStepsRegistry stepRegistry = createOneStepPolicyStepRegistry(policyName, step); - ClusterService clusterService = Mockito.mock(ClusterService.class); + ClusterService clusterService = mock(ClusterService.class); IndexLifecycleRunner runner = new IndexLifecycleRunner(stepRegistry, clusterService, () -> 0L); IndexMetaData indexMetaData = IndexMetaData.builder("my_index").settings(settings(Version.CURRENT)) .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); @@ -151,7 +152,7 @@ public class IndexLifecycleRunnerTests extends ESTestCase { step.setWillComplete(true); step.setIndexSurvives(false); PolicyStepsRegistry stepRegistry = createOneStepPolicyStepRegistry(policyName, step); - ClusterService clusterService = Mockito.mock(ClusterService.class); + ClusterService clusterService = mock(ClusterService.class); IndexLifecycleRunner runner = new IndexLifecycleRunner(stepRegistry, clusterService, () -> 0L); IndexMetaData indexMetaData = IndexMetaData.builder("my_index").settings(settings(Version.CURRENT)) .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); @@ -168,7 +169,7 @@ public class IndexLifecycleRunnerTests extends ESTestCase { MockAsyncActionStep step = new MockAsyncActionStep(stepKey, null); step.setWillComplete(false); PolicyStepsRegistry stepRegistry = createOneStepPolicyStepRegistry(policyName, step); - ClusterService clusterService = Mockito.mock(ClusterService.class); + ClusterService clusterService = mock(ClusterService.class); IndexLifecycleRunner runner = new IndexLifecycleRunner(stepRegistry, clusterService, () -> 0L); IndexMetaData indexMetaData = IndexMetaData.builder("my_index").settings(settings(Version.CURRENT)) .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); @@ -186,7 +187,7 @@ public class IndexLifecycleRunnerTests extends ESTestCase { Exception expectedException = new RuntimeException(); step.setException(expectedException); PolicyStepsRegistry stepRegistry = createOneStepPolicyStepRegistry(policyName, step); - ClusterService clusterService = Mockito.mock(ClusterService.class); + ClusterService clusterService = mock(ClusterService.class); IndexLifecycleRunner runner = new IndexLifecycleRunner(stepRegistry, clusterService, () -> 0L); IndexMetaData indexMetaData = IndexMetaData.builder("my_index").settings(settings(Version.CURRENT)) .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); @@ -206,7 +207,7 @@ public class IndexLifecycleRunnerTests extends ESTestCase { Exception expectedException = new RuntimeException(); step.setException(expectedException); PolicyStepsRegistry stepRegistry = createOneStepPolicyStepRegistry(policyName, step); - ClusterService clusterService = Mockito.mock(ClusterService.class); + ClusterService clusterService = mock(ClusterService.class); IndexLifecycleRunner runner = new IndexLifecycleRunner(stepRegistry, clusterService, () -> 0L); IndexMetaData indexMetaData = IndexMetaData.builder("my_index").settings(settings(Version.CURRENT)) .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); @@ -223,7 +224,7 @@ public class IndexLifecycleRunnerTests extends ESTestCase { MockAsyncWaitStep step = new MockAsyncWaitStep(stepKey, null); step.setWillComplete(true); PolicyStepsRegistry stepRegistry = createOneStepPolicyStepRegistry(policyName, step); - ClusterService clusterService = Mockito.mock(ClusterService.class); + ClusterService clusterService = mock(ClusterService.class); IndexLifecycleRunner runner = new IndexLifecycleRunner(stepRegistry, clusterService, () -> 0L); IndexMetaData indexMetaData = IndexMetaData.builder("my_index").settings(settings(Version.CURRENT)) .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); @@ -244,7 +245,7 @@ public class IndexLifecycleRunnerTests extends ESTestCase { step.expectedInfo(stepInfo); step.setWillComplete(false); PolicyStepsRegistry stepRegistry = createOneStepPolicyStepRegistry(policyName, step); - ClusterService clusterService = Mockito.mock(ClusterService.class); + ClusterService clusterService = mock(ClusterService.class); IndexLifecycleRunner runner = new IndexLifecycleRunner(stepRegistry, clusterService, () -> 0L); IndexMetaData indexMetaData = IndexMetaData.builder("my_index").settings(settings(Version.CURRENT)) .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); @@ -265,7 +266,7 @@ public class IndexLifecycleRunnerTests extends ESTestCase { step.expectedInfo(stepInfo); step.setWillComplete(false); PolicyStepsRegistry stepRegistry = createOneStepPolicyStepRegistry(policyName, step); - ClusterService clusterService = Mockito.mock(ClusterService.class); + ClusterService clusterService = mock(ClusterService.class); IndexLifecycleRunner runner = new IndexLifecycleRunner(stepRegistry, clusterService, () -> 0L); IndexMetaData indexMetaData = IndexMetaData.builder("my_index").settings(settings(Version.CURRENT)) .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); @@ -283,7 +284,7 @@ public class IndexLifecycleRunnerTests extends ESTestCase { Exception expectedException = new RuntimeException(); step.setException(expectedException); PolicyStepsRegistry stepRegistry = createOneStepPolicyStepRegistry(policyName, step); - ClusterService clusterService = Mockito.mock(ClusterService.class); + ClusterService clusterService = mock(ClusterService.class); IndexLifecycleRunner runner = new IndexLifecycleRunner(stepRegistry, clusterService, () -> 0L); IndexMetaData indexMetaData = IndexMetaData.builder("my_index").settings(settings(Version.CURRENT)) .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); @@ -303,7 +304,7 @@ public class IndexLifecycleRunnerTests extends ESTestCase { Exception expectedException = new RuntimeException(); step.setException(expectedException); PolicyStepsRegistry stepRegistry = createOneStepPolicyStepRegistry(policyName, step); - ClusterService clusterService = Mockito.mock(ClusterService.class); + ClusterService clusterService = mock(ClusterService.class); IndexLifecycleRunner runner = new IndexLifecycleRunner(stepRegistry, clusterService, () -> 0L); IndexMetaData indexMetaData = IndexMetaData.builder("my_index").settings(settings(Version.CURRENT)) .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); @@ -319,7 +320,7 @@ public class IndexLifecycleRunnerTests extends ESTestCase { StepKey stepKey = new StepKey("phase", "action", "cluster_state_action_step"); MockStep step = new MockStep(stepKey, null); PolicyStepsRegistry stepRegistry = createOneStepPolicyStepRegistry(policyName, step); - ClusterService clusterService = Mockito.mock(ClusterService.class); + ClusterService clusterService = mock(ClusterService.class); IndexLifecycleRunner runner = new IndexLifecycleRunner(stepRegistry, clusterService, () -> 0L); IndexMetaData indexMetaData = IndexMetaData.builder("my_index").settings(settings(Version.CURRENT)) .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); @@ -678,7 +679,6 @@ public class IndexLifecycleRunnerTests extends ESTestCase { String indexName = "my_index"; StepKey currentStep = new StepKey("current_phase", "current_action", "current_step"); RandomStepInfo stepInfo = new RandomStepInfo(); - ClusterState clusterState = buildClusterState(indexName, Settings.builder().put(LifecycleSettings.LIFECYCLE_PHASE, currentStep.getPhase()) .put(LifecycleSettings.LIFECYCLE_ACTION, currentStep.getAction()) @@ -688,6 +688,25 @@ public class IndexLifecycleRunnerTests extends ESTestCase { assertClusterStateStepInfo(clusterState, index, currentStep, newClusterState, stepInfo); } + @SuppressWarnings("unchecked") + public void testSkipped() { + String policy = randomAlphaOfLength(5); + String index = randomAlphaOfLength(10); + ClusterState clusterState = buildClusterState(index, + Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, policy) + .put(LifecycleSettings.LIFECYCLE_PHASE, randomAlphaOfLength(5)) + .put(LifecycleSettings.LIFECYCLE_ACTION, randomAlphaOfLength(5)) + .put(LifecycleSettings.LIFECYCLE_STEP, randomAlphaOfLength(5)) + .put(LifecycleSettings.LIFECYCLE_SKIP, true)); + Step step = mock(randomFrom(TerminalPolicyStep.class, InitializePolicyContextStep.class, + ClusterStateWaitStep.class, AsyncActionStep.class, AsyncWaitStep.class)); + PolicyStepsRegistry stepRegistry = createOneStepPolicyStepRegistry(policy, step); + ClusterService clusterService = mock(ClusterService.class); + IndexLifecycleRunner runner = new IndexLifecycleRunner(stepRegistry, clusterService, () -> 0L); + runner.runPolicy(policy, clusterState.metaData().index(index), clusterState, randomBoolean()); + Mockito.verifyZeroInteractions(clusterService); + } + private ClusterState buildClusterState(String indexName, Settings.Builder indexSettingsBuilder) { Settings indexSettings = indexSettingsBuilder.put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1) .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0).put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).build();