From 4ae0d4e802d5339bce6073428f04034c61382024 Mon Sep 17 00:00:00 2001 From: Tal Levy Date: Thu, 19 Jul 2018 07:02:37 -0700 Subject: [PATCH] add ILM ShrinkAction IT (#32176) - adds integration test for shrink action - expands phases used by allocate/replicas to include `cold` --- .../TimeSeriesLifecycleActionsIT.java | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/x-pack/plugin/index-lifecycle/src/test/java/org/elasticsearch/xpack/indexlifecycle/TimeSeriesLifecycleActionsIT.java b/x-pack/plugin/index-lifecycle/src/test/java/org/elasticsearch/xpack/indexlifecycle/TimeSeriesLifecycleActionsIT.java index 3d0c328c18c..b4e7917a21b 100644 --- a/x-pack/plugin/index-lifecycle/src/test/java/org/elasticsearch/xpack/indexlifecycle/TimeSeriesLifecycleActionsIT.java +++ b/x-pack/plugin/index-lifecycle/src/test/java/org/elasticsearch/xpack/indexlifecycle/TimeSeriesLifecycleActionsIT.java @@ -23,12 +23,14 @@ import org.elasticsearch.xpack.core.indexlifecycle.LifecycleSettings; import org.elasticsearch.xpack.core.indexlifecycle.Phase; import org.elasticsearch.xpack.core.indexlifecycle.ReadOnlyAction; import org.elasticsearch.xpack.core.indexlifecycle.ReplicasAction; +import org.elasticsearch.xpack.core.indexlifecycle.ShrinkAction; import org.elasticsearch.xpack.core.indexlifecycle.Step.StepKey; import org.elasticsearch.xpack.core.indexlifecycle.TerminalPolicyStep; import org.elasticsearch.xpack.core.indexlifecycle.TimeseriesLifecycleType; import org.junit.Before; import java.io.IOException; +import java.util.Collections; import java.util.List; import java.util.Locale; import java.util.Map; @@ -54,7 +56,7 @@ public class TimeSeriesLifecycleActionsIT extends ESRestTestCase { .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0)); String allocateNodeName = "node-" + randomFrom(0, 1); AllocateAction allocateAction = new AllocateAction(null, null, singletonMap("_name", allocateNodeName)); - createNewSingletonPolicy("warm", allocateAction); + createNewSingletonPolicy(randomFrom("warm", "cold"), allocateAction); updateIndexSettings(index, Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, policy)); assertBusy(() -> { Map settings = getOnlyIndexSettings(index); @@ -123,7 +125,7 @@ public class TimeSeriesLifecycleActionsIT extends ESRestTestCase { int finalNumReplicas = (numReplicas + 1) % 2; createIndexWithSettings(index, Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, numShards) .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, numReplicas)); - createNewSingletonPolicy("warm", new ReplicasAction(finalNumReplicas)); + createNewSingletonPolicy(randomFrom("warm", "cold"), new ReplicasAction(finalNumReplicas)); updateIndexSettings(index, Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, policy)); assertBusy(() -> { @@ -133,6 +135,24 @@ public class TimeSeriesLifecycleActionsIT extends ESRestTestCase { }); } + public void testShrinkAction() throws Exception { + int numShards = 6; + int divisor = randomFrom(2, 3, 6); + int expectedFinalShards = numShards / divisor; + String shrunkenIndex = ShrinkAction.SHRUNKEN_INDEX_PREFIX + index; + createIndexWithSettings(index, Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, numShards) + .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0)); + createNewSingletonPolicy("warm", new ShrinkAction(expectedFinalShards)); + updateIndexSettings(index, Settings.builder().put(LifecycleSettings.LIFECYCLE_NAME, policy)); + assertBusy(() -> { + assertTrue(indexExists(shrunkenIndex)); + assertTrue(aliasExists(shrunkenIndex, index)); + Map settings = getOnlyIndexSettings(shrunkenIndex); + assertThat(getStepKey(settings), equalTo(TerminalPolicyStep.KEY)); + assertThat(settings.get(IndexMetaData.SETTING_NUMBER_OF_SHARDS), equalTo(String.valueOf(expectedFinalShards))); + }); + } + private void createNewSingletonPolicy(String phaseName, LifecycleAction action) throws IOException { Phase phase = new Phase(phaseName, TimeValue.ZERO, singletonMap(action.getWriteableName(), action)); LifecyclePolicy lifecyclePolicy = @@ -156,7 +176,11 @@ public class TimeSeriesLifecycleActionsIT extends ESRestTestCase { @SuppressWarnings("unchecked") private Map getOnlyIndexSettings(String index) throws IOException { - return (Map) ((Map) getIndexSettings(index).get(index)).get("settings"); + Map response = (Map) getIndexSettings(index).get(index); + if (response == null) { + return Collections.emptyMap(); + } + return (Map) response.get("settings"); } private StepKey getStepKey(Map settings) {