|
@@ -0,0 +1,499 @@
|
|
|
+/*
|
|
|
+ * 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.ilm.action;
|
|
|
+
|
|
|
+import org.elasticsearch.Version;
|
|
|
+import org.elasticsearch.client.Client;
|
|
|
+import org.elasticsearch.cluster.ClusterState;
|
|
|
+import org.elasticsearch.cluster.metadata.IndexMetaData;
|
|
|
+import org.elasticsearch.cluster.metadata.MetaData;
|
|
|
+import org.elasticsearch.common.Strings;
|
|
|
+import org.elasticsearch.common.settings.Settings;
|
|
|
+import org.elasticsearch.common.unit.TimeValue;
|
|
|
+import org.elasticsearch.common.xcontent.NamedXContentRegistry;
|
|
|
+import org.elasticsearch.test.ESTestCase;
|
|
|
+import org.elasticsearch.xpack.core.ilm.AllocateAction;
|
|
|
+import org.elasticsearch.xpack.core.ilm.AllocationRoutedStep;
|
|
|
+import org.elasticsearch.xpack.core.ilm.ErrorStep;
|
|
|
+import org.elasticsearch.xpack.core.ilm.ForceMergeAction;
|
|
|
+import org.elasticsearch.xpack.core.ilm.FreezeAction;
|
|
|
+import org.elasticsearch.xpack.core.ilm.LifecycleAction;
|
|
|
+import org.elasticsearch.xpack.core.ilm.LifecycleExecutionState;
|
|
|
+import org.elasticsearch.xpack.core.ilm.LifecyclePolicy;
|
|
|
+import org.elasticsearch.xpack.core.ilm.LifecyclePolicyMetadata;
|
|
|
+import org.elasticsearch.xpack.core.ilm.LifecycleSettings;
|
|
|
+import org.elasticsearch.xpack.core.ilm.Phase;
|
|
|
+import org.elasticsearch.xpack.core.ilm.PhaseExecutionInfo;
|
|
|
+import org.elasticsearch.xpack.core.ilm.ReadOnlyAction;
|
|
|
+import org.elasticsearch.xpack.core.ilm.RolloverAction;
|
|
|
+import org.elasticsearch.xpack.core.ilm.RolloverStep;
|
|
|
+import org.elasticsearch.xpack.core.ilm.SegmentCountStep;
|
|
|
+import org.elasticsearch.xpack.core.ilm.SetPriorityAction;
|
|
|
+import org.elasticsearch.xpack.core.ilm.Step;
|
|
|
+import org.elasticsearch.xpack.core.ilm.UpdateRolloverLifecycleDateStep;
|
|
|
+import org.elasticsearch.xpack.core.ilm.WaitForRolloverReadyStep;
|
|
|
+import org.elasticsearch.xpack.ilm.IndexLifecycle;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import static org.elasticsearch.xpack.core.ilm.LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY;
|
|
|
+import static org.hamcrest.Matchers.contains;
|
|
|
+import static org.hamcrest.Matchers.equalTo;
|
|
|
+import static org.mockito.Mockito.mock;
|
|
|
+
|
|
|
+public class TransportPutLifecycleActionTests extends ESTestCase {
|
|
|
+ private static final NamedXContentRegistry REGISTRY;
|
|
|
+ private static final Client client = mock(Client.class);
|
|
|
+ private static final String index = "eggplant";
|
|
|
+
|
|
|
+ static {
|
|
|
+ try (IndexLifecycle indexLifecycle = new IndexLifecycle(Settings.EMPTY)) {
|
|
|
+ List<NamedXContentRegistry.Entry> entries = new ArrayList<>(indexLifecycle.getNamedXContent());
|
|
|
+ REGISTRY = new NamedXContentRegistry(entries);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testEligibleForRefresh() {
|
|
|
+ IndexMetaData meta = mkMeta().build();
|
|
|
+ assertFalse(TransportPutLifecycleAction.eligibleToCheckForRefresh(meta));
|
|
|
+
|
|
|
+ LifecycleExecutionState state = LifecycleExecutionState.builder().build();
|
|
|
+ meta = mkMeta().putCustom(ILM_CUSTOM_METADATA_KEY, state.asMap()).build();
|
|
|
+ assertFalse(TransportPutLifecycleAction.eligibleToCheckForRefresh(meta));
|
|
|
+
|
|
|
+ state = LifecycleExecutionState.builder()
|
|
|
+ .setPhase("phase")
|
|
|
+ .setAction("action")
|
|
|
+ .setStep("step")
|
|
|
+ .build();
|
|
|
+ meta = mkMeta().putCustom(ILM_CUSTOM_METADATA_KEY, state.asMap()).build();
|
|
|
+ assertFalse(TransportPutLifecycleAction.eligibleToCheckForRefresh(meta));
|
|
|
+
|
|
|
+ state = LifecycleExecutionState.builder()
|
|
|
+ .setPhaseDefinition("{}")
|
|
|
+ .build();
|
|
|
+ meta = mkMeta().putCustom(ILM_CUSTOM_METADATA_KEY, state.asMap()).build();
|
|
|
+ assertFalse(TransportPutLifecycleAction.eligibleToCheckForRefresh(meta));
|
|
|
+
|
|
|
+ state = LifecycleExecutionState.builder()
|
|
|
+ .setPhase("phase")
|
|
|
+ .setAction("action")
|
|
|
+ .setStep(ErrorStep.NAME)
|
|
|
+ .setPhaseDefinition("{}")
|
|
|
+ .build();
|
|
|
+ meta = mkMeta().putCustom(ILM_CUSTOM_METADATA_KEY, state.asMap()).build();
|
|
|
+ assertFalse(TransportPutLifecycleAction.eligibleToCheckForRefresh(meta));
|
|
|
+
|
|
|
+ state = LifecycleExecutionState.builder()
|
|
|
+ .setPhase("phase")
|
|
|
+ .setAction("action")
|
|
|
+ .setStep("step")
|
|
|
+ .setPhaseDefinition("{}")
|
|
|
+ .build();
|
|
|
+ meta = mkMeta().putCustom(ILM_CUSTOM_METADATA_KEY, state.asMap()).build();
|
|
|
+ assertTrue(TransportPutLifecycleAction.eligibleToCheckForRefresh(meta));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testReadStepKeys() {
|
|
|
+ assertNull(TransportPutLifecycleAction.readStepKeys(REGISTRY, client, "{}", "phase"));
|
|
|
+ assertNull(TransportPutLifecycleAction.readStepKeys(REGISTRY, client, "aoeu", "phase"));
|
|
|
+ assertNull(TransportPutLifecycleAction.readStepKeys(REGISTRY, client, "", "phase"));
|
|
|
+
|
|
|
+ assertThat(TransportPutLifecycleAction.readStepKeys(REGISTRY, client, "{\n" +
|
|
|
+ " \"policy\": \"my_lifecycle3\",\n" +
|
|
|
+ " \"phase_definition\": { \n" +
|
|
|
+ " \"min_age\": \"0ms\",\n" +
|
|
|
+ " \"actions\": {\n" +
|
|
|
+ " \"rollover\": {\n" +
|
|
|
+ " \"max_age\": \"30s\"\n" +
|
|
|
+ " }\n" +
|
|
|
+ " }\n" +
|
|
|
+ " },\n" +
|
|
|
+ " \"version\": 3, \n" +
|
|
|
+ " \"modified_date_in_millis\": 1539609701576 \n" +
|
|
|
+ " }", "phase"),
|
|
|
+ contains(new Step.StepKey("phase", "rollover", WaitForRolloverReadyStep.NAME),
|
|
|
+ new Step.StepKey("phase", "rollover", RolloverStep.NAME),
|
|
|
+ new Step.StepKey("phase", "rollover", UpdateRolloverLifecycleDateStep.NAME),
|
|
|
+ new Step.StepKey("phase", "rollover", RolloverAction.INDEXING_COMPLETE_STEP_NAME)));
|
|
|
+
|
|
|
+ assertThat(TransportPutLifecycleAction.readStepKeys(REGISTRY, client, "{\n" +
|
|
|
+ " \"policy\" : \"my_lifecycle3\",\n" +
|
|
|
+ " \"phase_definition\" : {\n" +
|
|
|
+ " \"min_age\" : \"20m\",\n" +
|
|
|
+ " \"actions\" : {\n" +
|
|
|
+ " \"rollover\" : {\n" +
|
|
|
+ " \"max_age\" : \"5s\"\n" +
|
|
|
+ " },\n" +
|
|
|
+ " \"set_priority\" : {\n" +
|
|
|
+ " \"priority\" : 150\n" +
|
|
|
+ " }\n" +
|
|
|
+ " }\n" +
|
|
|
+ " },\n" +
|
|
|
+ " \"version\" : 1,\n" +
|
|
|
+ " \"modified_date_in_millis\" : 1578521007076\n" +
|
|
|
+ " }", "phase"),
|
|
|
+ contains(new Step.StepKey("phase", "rollover", WaitForRolloverReadyStep.NAME),
|
|
|
+ new Step.StepKey("phase", "rollover", RolloverStep.NAME),
|
|
|
+ new Step.StepKey("phase", "rollover", UpdateRolloverLifecycleDateStep.NAME),
|
|
|
+ new Step.StepKey("phase", "rollover", RolloverAction.INDEXING_COMPLETE_STEP_NAME),
|
|
|
+ new Step.StepKey("phase", "set_priority", SetPriorityAction.NAME)));
|
|
|
+
|
|
|
+ Map<String, LifecycleAction> actions = new HashMap<>();
|
|
|
+ actions.put("forcemerge", new ForceMergeAction(5));
|
|
|
+ actions.put("freeze", new FreezeAction());
|
|
|
+ actions.put("allocate", new AllocateAction(1, null, null, null));
|
|
|
+ PhaseExecutionInfo pei = new PhaseExecutionInfo("policy", new Phase("wonky", TimeValue.ZERO, actions), 1, 1);
|
|
|
+ String phaseDef = Strings.toString(pei);
|
|
|
+ logger.info("--> phaseDef: {}", phaseDef);
|
|
|
+
|
|
|
+ assertThat(TransportPutLifecycleAction.readStepKeys(REGISTRY, client, phaseDef, "phase"),
|
|
|
+ contains(new Step.StepKey("phase", "freeze", FreezeAction.NAME),
|
|
|
+ new Step.StepKey("phase", "allocate", AllocateAction.NAME),
|
|
|
+ new Step.StepKey("phase", "allocate", AllocationRoutedStep.NAME),
|
|
|
+ new Step.StepKey("phase", "forcemerge", ReadOnlyAction.NAME),
|
|
|
+ new Step.StepKey("phase", "forcemerge", ForceMergeAction.NAME),
|
|
|
+ new Step.StepKey("phase", "forcemerge", SegmentCountStep.NAME)));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testIndexCanBeSafelyUpdated() {
|
|
|
+
|
|
|
+ // Success case, it can be updated even though the configuration for the
|
|
|
+ // rollover and set_priority actions has changed
|
|
|
+ {
|
|
|
+ LifecycleExecutionState exState = LifecycleExecutionState.builder()
|
|
|
+ .setPhase("hot")
|
|
|
+ .setAction("rollover")
|
|
|
+ .setStep("check-rollover-ready")
|
|
|
+ .setPhaseDefinition("{\n" +
|
|
|
+ " \"policy\" : \"my-policy\",\n" +
|
|
|
+ " \"phase_definition\" : {\n" +
|
|
|
+ " \"min_age\" : \"20m\",\n" +
|
|
|
+ " \"actions\" : {\n" +
|
|
|
+ " \"rollover\" : {\n" +
|
|
|
+ " \"max_age\" : \"5s\"\n" +
|
|
|
+ " },\n" +
|
|
|
+ " \"set_priority\" : {\n" +
|
|
|
+ " \"priority\" : 150\n" +
|
|
|
+ " }\n" +
|
|
|
+ " }\n" +
|
|
|
+ " },\n" +
|
|
|
+ " \"version\" : 1,\n" +
|
|
|
+ " \"modified_date_in_millis\" : 1578521007076\n" +
|
|
|
+ " }")
|
|
|
+ .build();
|
|
|
+
|
|
|
+ IndexMetaData meta = mkMeta()
|
|
|
+ .putCustom(ILM_CUSTOM_METADATA_KEY, exState.asMap())
|
|
|
+ .build();
|
|
|
+
|
|
|
+ Map<String, LifecycleAction> actions = new HashMap<>();
|
|
|
+ actions.put("rollover", new RolloverAction(null, null, 1L));
|
|
|
+ actions.put("set_priority", new SetPriorityAction(100));
|
|
|
+ Phase hotPhase = new Phase("hot", TimeValue.ZERO, actions);
|
|
|
+ Map<String, Phase> phases = Collections.singletonMap("hot", hotPhase);
|
|
|
+ LifecyclePolicy newPolicy = new LifecyclePolicy("my-policy", phases);
|
|
|
+
|
|
|
+ assertTrue(TransportPutLifecycleAction.isIndexPhaseDefinitionUpdatable(REGISTRY, client, meta, newPolicy));
|
|
|
+ }
|
|
|
+
|
|
|
+ // Failure case, can't update because the step we're currently on has been removed in the new policy
|
|
|
+ {
|
|
|
+ LifecycleExecutionState exState = LifecycleExecutionState.builder()
|
|
|
+ .setPhase("hot")
|
|
|
+ .setAction("rollover")
|
|
|
+ .setStep("check-rollover-ready")
|
|
|
+ .setPhaseDefinition("{\n" +
|
|
|
+ " \"policy\" : \"my-policy\",\n" +
|
|
|
+ " \"phase_definition\" : {\n" +
|
|
|
+ " \"min_age\" : \"20m\",\n" +
|
|
|
+ " \"actions\" : {\n" +
|
|
|
+ " \"rollover\" : {\n" +
|
|
|
+ " \"max_age\" : \"5s\"\n" +
|
|
|
+ " },\n" +
|
|
|
+ " \"set_priority\" : {\n" +
|
|
|
+ " \"priority\" : 150\n" +
|
|
|
+ " }\n" +
|
|
|
+ " }\n" +
|
|
|
+ " },\n" +
|
|
|
+ " \"version\" : 1,\n" +
|
|
|
+ " \"modified_date_in_millis\" : 1578521007076\n" +
|
|
|
+ " }")
|
|
|
+ .build();
|
|
|
+
|
|
|
+ IndexMetaData meta = mkMeta()
|
|
|
+ .putCustom(ILM_CUSTOM_METADATA_KEY, exState.asMap())
|
|
|
+ .build();
|
|
|
+
|
|
|
+ Map<String, LifecycleAction> actions = new HashMap<>();
|
|
|
+ actions.put("set_priority", new SetPriorityAction(150));
|
|
|
+ Phase hotPhase = new Phase("hot", TimeValue.ZERO, actions);
|
|
|
+ Map<String, Phase> phases = Collections.singletonMap("hot", hotPhase);
|
|
|
+ LifecyclePolicy newPolicy = new LifecyclePolicy("my-policy", phases);
|
|
|
+
|
|
|
+ assertFalse(TransportPutLifecycleAction.isIndexPhaseDefinitionUpdatable(REGISTRY, client, meta, newPolicy));
|
|
|
+ }
|
|
|
+
|
|
|
+ // Failure case, can't update because the future step has been deleted
|
|
|
+ {
|
|
|
+ LifecycleExecutionState exState = LifecycleExecutionState.builder()
|
|
|
+ .setPhase("hot")
|
|
|
+ .setAction("rollover")
|
|
|
+ .setStep("check-rollover-ready")
|
|
|
+ .setPhaseDefinition("{\n" +
|
|
|
+ " \"policy\" : \"my-policy\",\n" +
|
|
|
+ " \"phase_definition\" : {\n" +
|
|
|
+ " \"min_age\" : \"20m\",\n" +
|
|
|
+ " \"actions\" : {\n" +
|
|
|
+ " \"rollover\" : {\n" +
|
|
|
+ " \"max_age\" : \"5s\"\n" +
|
|
|
+ " },\n" +
|
|
|
+ " \"set_priority\" : {\n" +
|
|
|
+ " \"priority\" : 150\n" +
|
|
|
+ " }\n" +
|
|
|
+ " }\n" +
|
|
|
+ " },\n" +
|
|
|
+ " \"version\" : 1,\n" +
|
|
|
+ " \"modified_date_in_millis\" : 1578521007076\n" +
|
|
|
+ " }")
|
|
|
+ .build();
|
|
|
+
|
|
|
+ IndexMetaData meta = mkMeta()
|
|
|
+ .putCustom(ILM_CUSTOM_METADATA_KEY, exState.asMap())
|
|
|
+ .build();
|
|
|
+
|
|
|
+ Map<String, LifecycleAction> actions = new HashMap<>();
|
|
|
+ actions.put("rollover", new RolloverAction(null, TimeValue.timeValueSeconds(5), null));
|
|
|
+ Phase hotPhase = new Phase("hot", TimeValue.ZERO, actions);
|
|
|
+ Map<String, Phase> phases = Collections.singletonMap("hot", hotPhase);
|
|
|
+ LifecyclePolicy newPolicy = new LifecyclePolicy("my-policy", phases);
|
|
|
+
|
|
|
+ assertFalse(TransportPutLifecycleAction.isIndexPhaseDefinitionUpdatable(REGISTRY, client, meta, newPolicy));
|
|
|
+ }
|
|
|
+
|
|
|
+ // Failure case, index doesn't have enough info to check
|
|
|
+ {
|
|
|
+ LifecycleExecutionState exState = LifecycleExecutionState.builder()
|
|
|
+ .setPhaseDefinition("{\n" +
|
|
|
+ " \"policy\" : \"my-policy\",\n" +
|
|
|
+ " \"phase_definition\" : {\n" +
|
|
|
+ " \"min_age\" : \"20m\",\n" +
|
|
|
+ " \"actions\" : {\n" +
|
|
|
+ " \"rollover\" : {\n" +
|
|
|
+ " \"max_age\" : \"5s\"\n" +
|
|
|
+ " },\n" +
|
|
|
+ " \"set_priority\" : {\n" +
|
|
|
+ " \"priority\" : 150\n" +
|
|
|
+ " }\n" +
|
|
|
+ " }\n" +
|
|
|
+ " },\n" +
|
|
|
+ " \"version\" : 1,\n" +
|
|
|
+ " \"modified_date_in_millis\" : 1578521007076\n" +
|
|
|
+ " }")
|
|
|
+ .build();
|
|
|
+
|
|
|
+ IndexMetaData meta = mkMeta()
|
|
|
+ .putCustom(ILM_CUSTOM_METADATA_KEY, exState.asMap())
|
|
|
+ .build();
|
|
|
+
|
|
|
+ Map<String, LifecycleAction> actions = new HashMap<>();
|
|
|
+ actions.put("rollover", new RolloverAction(null, null, 1L));
|
|
|
+ actions.put("set_priority", new SetPriorityAction(100));
|
|
|
+ Phase hotPhase = new Phase("hot", TimeValue.ZERO, actions);
|
|
|
+ Map<String, Phase> phases = Collections.singletonMap("hot", hotPhase);
|
|
|
+ LifecyclePolicy newPolicy = new LifecyclePolicy("my-policy", phases);
|
|
|
+
|
|
|
+ assertFalse(TransportPutLifecycleAction.isIndexPhaseDefinitionUpdatable(REGISTRY, client, meta, newPolicy));
|
|
|
+ }
|
|
|
+
|
|
|
+ // Failure case, the phase JSON is unparseable
|
|
|
+ {
|
|
|
+ LifecycleExecutionState exState = LifecycleExecutionState.builder()
|
|
|
+ .setPhase("hot")
|
|
|
+ .setAction("rollover")
|
|
|
+ .setStep("check-rollover-ready")
|
|
|
+ .setPhaseDefinition("potato")
|
|
|
+ .build();
|
|
|
+
|
|
|
+ IndexMetaData meta = mkMeta()
|
|
|
+ .putCustom(ILM_CUSTOM_METADATA_KEY, exState.asMap())
|
|
|
+ .build();
|
|
|
+
|
|
|
+ Map<String, LifecycleAction> actions = new HashMap<>();
|
|
|
+ actions.put("rollover", new RolloverAction(null, null, 1L));
|
|
|
+ actions.put("set_priority", new SetPriorityAction(100));
|
|
|
+ Phase hotPhase = new Phase("hot", TimeValue.ZERO, actions);
|
|
|
+ Map<String, Phase> phases = Collections.singletonMap("hot", hotPhase);
|
|
|
+ LifecyclePolicy newPolicy = new LifecyclePolicy("my-policy", phases);
|
|
|
+
|
|
|
+ assertFalse(TransportPutLifecycleAction.isIndexPhaseDefinitionUpdatable(REGISTRY, client, meta, newPolicy));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testRefreshPhaseJson() {
|
|
|
+ LifecycleExecutionState exState = LifecycleExecutionState.builder()
|
|
|
+ .setPhase("hot")
|
|
|
+ .setAction("rollover")
|
|
|
+ .setStep("check-rollover-ready")
|
|
|
+ .setPhaseDefinition("{\n" +
|
|
|
+ " \"policy\" : \"my-policy\",\n" +
|
|
|
+ " \"phase_definition\" : {\n" +
|
|
|
+ " \"min_age\" : \"20m\",\n" +
|
|
|
+ " \"actions\" : {\n" +
|
|
|
+ " \"rollover\" : {\n" +
|
|
|
+ " \"max_age\" : \"5s\"\n" +
|
|
|
+ " },\n" +
|
|
|
+ " \"set_priority\" : {\n" +
|
|
|
+ " \"priority\" : 150\n" +
|
|
|
+ " }\n" +
|
|
|
+ " }\n" +
|
|
|
+ " },\n" +
|
|
|
+ " \"version\" : 1,\n" +
|
|
|
+ " \"modified_date_in_millis\" : 1578521007076\n" +
|
|
|
+ " }")
|
|
|
+ .build();
|
|
|
+
|
|
|
+ IndexMetaData meta = mkMeta()
|
|
|
+ .putCustom(ILM_CUSTOM_METADATA_KEY, exState.asMap())
|
|
|
+ .build();
|
|
|
+
|
|
|
+ Map<String, LifecycleAction> actions = new HashMap<>();
|
|
|
+ actions.put("rollover", new RolloverAction(null, null, 1L));
|
|
|
+ actions.put("set_priority", new SetPriorityAction(100));
|
|
|
+ Phase hotPhase = new Phase("hot", TimeValue.ZERO, actions);
|
|
|
+ Map<String, Phase> phases = Collections.singletonMap("hot", hotPhase);
|
|
|
+ LifecyclePolicy newPolicy = new LifecyclePolicy("my-policy", phases);
|
|
|
+ LifecyclePolicyMetadata policyMetadata = new LifecyclePolicyMetadata(newPolicy, Collections.emptyMap(), 2L, 2L);
|
|
|
+
|
|
|
+ ClusterState existingState = ClusterState.builder(ClusterState.EMPTY_STATE)
|
|
|
+ .metaData(MetaData.builder(MetaData.EMPTY_META_DATA)
|
|
|
+ .put(meta, false)
|
|
|
+ .build())
|
|
|
+ .build();
|
|
|
+
|
|
|
+ ClusterState changedState = TransportPutLifecycleAction.refreshPhaseDefinition(existingState, index, policyMetadata);
|
|
|
+
|
|
|
+ IndexMetaData newIdxMeta = changedState.metaData().index(index);
|
|
|
+ LifecycleExecutionState afterExState = LifecycleExecutionState.fromIndexMetadata(newIdxMeta);
|
|
|
+ Map<String, String> beforeState = new HashMap<>(exState.asMap());
|
|
|
+ beforeState.remove("phase_definition");
|
|
|
+ Map<String, String> afterState = new HashMap<>(afterExState.asMap());
|
|
|
+ afterState.remove("phase_definition");
|
|
|
+ // Check that no other execution state changes have been made
|
|
|
+ assertThat(beforeState, equalTo(afterState));
|
|
|
+
|
|
|
+ // Check that the phase definition has been refreshed
|
|
|
+ assertThat(afterExState.getPhaseDefinition(),
|
|
|
+ equalTo("{\"policy\":\"my-policy\",\"phase_definition\":{\"min_age\":\"0ms\",\"actions\":{\"rollover\":{\"max_docs\":1}," +
|
|
|
+ "\"set_priority\":{\"priority\":100}}},\"version\":2,\"modified_date_in_millis\":2}"));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testUpdateIndicesForPolicy() {
|
|
|
+ LifecycleExecutionState exState = LifecycleExecutionState.builder()
|
|
|
+ .setPhase("hot")
|
|
|
+ .setAction("rollover")
|
|
|
+ .setStep("check-rollover-ready")
|
|
|
+ .setPhaseDefinition("{\"policy\":\"my-policy\",\"phase_definition\":{\"min_age\":\"0ms\",\"actions\":{\"rollover\":" +
|
|
|
+ "{\"max_docs\":1},\"set_priority\":{\"priority\":100}}},\"version\":1,\"modified_date_in_millis\":1578521007076}")
|
|
|
+ .build();
|
|
|
+
|
|
|
+ IndexMetaData meta = mkMeta()
|
|
|
+ .putCustom(ILM_CUSTOM_METADATA_KEY, exState.asMap())
|
|
|
+ .build();
|
|
|
+
|
|
|
+ assertTrue(TransportPutLifecycleAction.eligibleToCheckForRefresh(meta));
|
|
|
+
|
|
|
+ Map<String, LifecycleAction> oldActions = new HashMap<>();
|
|
|
+ oldActions.put("rollover", new RolloverAction(null, null, 1L));
|
|
|
+ oldActions.put("set_priority", new SetPriorityAction(100));
|
|
|
+ Phase oldHotPhase = new Phase("hot", TimeValue.ZERO, oldActions);
|
|
|
+ Map<String, Phase> oldPhases = Collections.singletonMap("hot", oldHotPhase);
|
|
|
+ LifecyclePolicy oldPolicy = new LifecyclePolicy("my-policy", oldPhases);
|
|
|
+
|
|
|
+ Map<String, LifecycleAction> actions = new HashMap<>();
|
|
|
+ actions.put("rollover", new RolloverAction(null, null, 1L));
|
|
|
+ actions.put("set_priority", new SetPriorityAction(100));
|
|
|
+ Phase hotPhase = new Phase("hot", TimeValue.ZERO, actions);
|
|
|
+ Map<String, Phase> phases = Collections.singletonMap("hot", hotPhase);
|
|
|
+ LifecyclePolicy newPolicy = new LifecyclePolicy("my-policy", phases);
|
|
|
+ LifecyclePolicyMetadata policyMetadata = new LifecyclePolicyMetadata(newPolicy, Collections.emptyMap(), 2L, 2L);
|
|
|
+
|
|
|
+ assertTrue(TransportPutLifecycleAction.isIndexPhaseDefinitionUpdatable(REGISTRY, client, meta, newPolicy));
|
|
|
+
|
|
|
+ ClusterState existingState = ClusterState.builder(ClusterState.EMPTY_STATE)
|
|
|
+ .metaData(MetaData.builder(MetaData.EMPTY_META_DATA)
|
|
|
+ .put(meta, false)
|
|
|
+ .build())
|
|
|
+ .build();
|
|
|
+
|
|
|
+ logger.info("--> update for unchanged policy");
|
|
|
+ ClusterState updatedState = TransportPutLifecycleAction.updateIndicesForPolicy(existingState, REGISTRY,
|
|
|
+ client, oldPolicy, policyMetadata);
|
|
|
+
|
|
|
+ // No change, because the policies were identical
|
|
|
+ assertThat(updatedState, equalTo(existingState));
|
|
|
+
|
|
|
+ actions = new HashMap<>();
|
|
|
+ actions.put("rollover", new RolloverAction(null, null, 2L));
|
|
|
+ actions.put("set_priority", new SetPriorityAction(150));
|
|
|
+ hotPhase = new Phase("hot", TimeValue.ZERO, actions);
|
|
|
+ phases = Collections.singletonMap("hot", hotPhase);
|
|
|
+ newPolicy = new LifecyclePolicy("my-policy", phases);
|
|
|
+ policyMetadata = new LifecyclePolicyMetadata(newPolicy, Collections.emptyMap(), 2L, 2L);
|
|
|
+
|
|
|
+ logger.info("--> update with changed policy, but not configured in settings");
|
|
|
+ updatedState = TransportPutLifecycleAction.updateIndicesForPolicy(existingState, REGISTRY, client, oldPolicy, policyMetadata);
|
|
|
+
|
|
|
+ // No change, because the index doesn't have a lifecycle.name setting for this policy
|
|
|
+ assertThat(updatedState, equalTo(existingState));
|
|
|
+
|
|
|
+ meta = IndexMetaData.builder(index)
|
|
|
+ .settings(Settings.builder()
|
|
|
+ .put(LifecycleSettings.LIFECYCLE_NAME, "my-policy")
|
|
|
+ .put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 10))
|
|
|
+ .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, randomIntBetween(0, 5))
|
|
|
+ .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT)
|
|
|
+ .put(IndexMetaData.SETTING_INDEX_UUID, randomAlphaOfLength(5)))
|
|
|
+ .putCustom(ILM_CUSTOM_METADATA_KEY, exState.asMap())
|
|
|
+ .build();
|
|
|
+ existingState = ClusterState.builder(ClusterState.EMPTY_STATE)
|
|
|
+ .metaData(MetaData.builder(MetaData.EMPTY_META_DATA)
|
|
|
+ .put(meta, false)
|
|
|
+ .build())
|
|
|
+ .build();
|
|
|
+
|
|
|
+ logger.info("--> update with changed policy and this index has the policy");
|
|
|
+ updatedState = TransportPutLifecycleAction.updateIndicesForPolicy(existingState, REGISTRY, client, oldPolicy, policyMetadata);
|
|
|
+
|
|
|
+ IndexMetaData newIdxMeta = updatedState.metaData().index(index);
|
|
|
+ LifecycleExecutionState afterExState = LifecycleExecutionState.fromIndexMetadata(newIdxMeta);
|
|
|
+ Map<String, String> beforeState = new HashMap<>(exState.asMap());
|
|
|
+ beforeState.remove("phase_definition");
|
|
|
+ Map<String, String> afterState = new HashMap<>(afterExState.asMap());
|
|
|
+ afterState.remove("phase_definition");
|
|
|
+ // Check that no other execution state changes have been made
|
|
|
+ assertThat(beforeState, equalTo(afterState));
|
|
|
+
|
|
|
+ // Check that the phase definition has been refreshed
|
|
|
+ assertThat(afterExState.getPhaseDefinition(),
|
|
|
+ equalTo("{\"policy\":\"my-policy\",\"phase_definition\":{\"min_age\":\"0ms\",\"actions\":{\"rollover\":{\"max_docs\":2}," +
|
|
|
+ "\"set_priority\":{\"priority\":150}}},\"version\":2,\"modified_date_in_millis\":2}"));
|
|
|
+ }
|
|
|
+
|
|
|
+ private static IndexMetaData.Builder mkMeta() {
|
|
|
+ return IndexMetaData.builder(index)
|
|
|
+ .settings(Settings.builder()
|
|
|
+ .put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 10))
|
|
|
+ .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, randomIntBetween(0, 5))
|
|
|
+ .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT)
|
|
|
+ .put(IndexMetaData.SETTING_INDEX_UUID, randomAlphaOfLength(5)));
|
|
|
+ }
|
|
|
+}
|