diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AsyncActionStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AsyncActionStep.java index e2f5387071076..712caab508236 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AsyncActionStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AsyncActionStep.java @@ -9,6 +9,9 @@ import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.ClusterStateObserver; import org.elasticsearch.cluster.metadata.IndexMetaData; +import org.elasticsearch.common.unit.TimeValue; + +import java.util.Objects; /** * Performs an action which must be performed asynchronously because it may take time to complete. @@ -26,6 +29,16 @@ protected Client getClient() { return client; } + //visible for testing + void setClient(Client client){ + this.client = client; + } + + public static TimeValue getMasterTimeout(ClusterState clusterState){ + Objects.requireNonNull(clusterState, "cannot determine master timeout when cluster state is null"); + return LifecycleSettings.LIFECYCLE_STEP_MASTER_TIMEOUT_SETTING.get(clusterState.metaData().settings()); + } + public boolean indexSurvives() { return true; } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AsyncWaitStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AsyncWaitStep.java index 5c2071f8f76a1..304b6a87adaab 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AsyncWaitStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AsyncWaitStep.java @@ -7,6 +7,7 @@ import org.elasticsearch.client.Client; import org.elasticsearch.cluster.metadata.IndexMetaData; +import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.ToXContentObject; /** @@ -28,11 +29,11 @@ protected Client getClient() { return client; } - public abstract void evaluateCondition(IndexMetaData indexMetaData, Listener listener); + public abstract void evaluateCondition(IndexMetaData indexMetaData, Listener listener, TimeValue masterTimeout); public interface Listener { - void onResponse(boolean conditionMet, ToXContentObject infomationContext); + void onResponse(boolean conditionMet, ToXContentObject informationContext); void onFailure(Exception e); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CloseFollowerIndexStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CloseFollowerIndexStep.java index dbd7ba54d2212..8a3e9503f64fa 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CloseFollowerIndexStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/CloseFollowerIndexStep.java @@ -33,7 +33,8 @@ void performDuringNoSnapshot(IndexMetaData indexMetaData, ClusterState currentCl } if (indexMetaData.getState() == IndexMetaData.State.OPEN) { - CloseIndexRequest closeIndexRequest = new CloseIndexRequest(followerIndex); + CloseIndexRequest closeIndexRequest = new CloseIndexRequest(followerIndex) + .masterNodeTimeout(getMasterTimeout(currentClusterState)); getClient().admin().indices().close(closeIndexRequest, ActionListener.wrap( r -> { assert r.isAcknowledged() : "close index response is not acknowledged"; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/DeleteStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/DeleteStep.java index c6a242a7410dc..c19f8f3ef7fc5 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/DeleteStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/DeleteStep.java @@ -24,7 +24,7 @@ public DeleteStep(StepKey key, StepKey nextStepKey, Client client) { @Override public void performDuringNoSnapshot(IndexMetaData indexMetaData, ClusterState currentState, Listener listener) { getClient().admin().indices() - .delete(new DeleteIndexRequest(indexMetaData.getIndex().getName()), + .delete(new DeleteIndexRequest(indexMetaData.getIndex().getName()).masterNodeTimeout(getMasterTimeout(currentState)), ActionListener.wrap(response -> listener.onResponse(true), listener::onFailure)); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/FreezeStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/FreezeStep.java index 32f4fc94ce97e..4b9c9a24d4caa 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/FreezeStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/FreezeStep.java @@ -25,7 +25,7 @@ public FreezeStep(StepKey key, StepKey nextStepKey, Client client) { @Override public void performDuringNoSnapshot(IndexMetaData indexMetaData, ClusterState currentState, Listener listener) { getClient().admin().indices().execute(FreezeIndexAction.INSTANCE, - new FreezeRequest(indexMetaData.getIndex().getName()), + new FreezeRequest(indexMetaData.getIndex().getName()).masterNodeTimeout(getMasterTimeout(currentState)), ActionListener.wrap(response -> listener.onResponse(true), listener::onFailure)); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/LifecycleSettings.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/LifecycleSettings.java index 3d186892ec983..f42d38436661d 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/LifecycleSettings.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/LifecycleSettings.java @@ -20,6 +20,7 @@ public class LifecycleSettings { public static final String LIFECYCLE_ORIGINATION_DATE = "index.lifecycle.origination_date"; public static final String LIFECYCLE_PARSE_ORIGINATION_DATE = "index.lifecycle.parse_origination_date"; public static final String LIFECYCLE_HISTORY_INDEX_ENABLED = "index.lifecycle.history_index_enabled"; + public static final String LIFECYCLE_STEP_MASTER_TIMEOUT = "index.lifecycle.step.master_timeout"; public static final String SLM_HISTORY_INDEX_ENABLED = "slm.history_index_enabled"; public static final String SLM_RETENTION_SCHEDULE = "slm.retention_schedule"; @@ -38,7 +39,9 @@ public class LifecycleSettings { false, Setting.Property.Dynamic, Setting.Property.IndexScope); public static final Setting LIFECYCLE_HISTORY_INDEX_ENABLED_SETTING = Setting.boolSetting(LIFECYCLE_HISTORY_INDEX_ENABLED, true, Setting.Property.NodeScope); - + public static final Setting LIFECYCLE_STEP_MASTER_TIMEOUT_SETTING = + Setting.positiveTimeSetting(LIFECYCLE_STEP_MASTER_TIMEOUT, TimeValue.timeValueSeconds(30), Setting.Property.Dynamic, + Setting.Property.NodeScope); public static final Setting SLM_HISTORY_INDEX_ENABLED_SETTING = Setting.boolSetting(SLM_HISTORY_INDEX_ENABLED, true, Setting.Property.NodeScope); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/OpenFollowerIndexStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/OpenFollowerIndexStep.java index 9883f83d02057..ea19d139407e4 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/OpenFollowerIndexStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/OpenFollowerIndexStep.java @@ -24,7 +24,8 @@ final class OpenFollowerIndexStep extends AsyncActionStep { public void performAction(IndexMetaData indexMetaData, ClusterState currentClusterState, ClusterStateObserver observer, Listener listener) { if (indexMetaData.getState() == IndexMetaData.State.CLOSE) { - OpenIndexRequest request = new OpenIndexRequest(indexMetaData.getIndex().getName()); + OpenIndexRequest request = new OpenIndexRequest(indexMetaData.getIndex().getName()) + .masterNodeTimeout(getMasterTimeout(currentClusterState)); getClient().admin().indices().open(request, ActionListener.wrap( r -> { assert r.isAcknowledged() : "open index response is not acknowledged"; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/RolloverStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/RolloverStep.java index d8c11088f5eb8..6db7fd41939e0 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/RolloverStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/RolloverStep.java @@ -70,7 +70,8 @@ public void performAction(IndexMetaData indexMetaData, ClusterState currentClust } // Calling rollover with no conditions will always roll over the index - RolloverRequest rolloverRequest = new RolloverRequest(rolloverAlias, null); + RolloverRequest rolloverRequest = new RolloverRequest(rolloverAlias, null) + .masterNodeTimeout(getMasterTimeout(currentClusterState)); // We don't wait for active shards when we perform the rollover because the // {@link org.elasticsearch.xpack.core.ilm.WaitForActiveShardsStep} step will do so rolloverRequest.setWaitForActiveShards(ActiveShardCount.NONE); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/SegmentCountStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/SegmentCountStep.java index ab5438396917e..980c2d0b4a335 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/SegmentCountStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/SegmentCountStep.java @@ -17,6 +17,7 @@ import org.elasticsearch.cluster.routing.ShardRouting; import org.elasticsearch.common.ParseField; import org.elasticsearch.common.Strings; +import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.ConstructingObjectParser; import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -48,7 +49,7 @@ public int getMaxNumSegments() { } @Override - public void evaluateCondition(IndexMetaData indexMetaData, Listener listener) { + public void evaluateCondition(IndexMetaData indexMetaData, Listener listener, TimeValue masterTimeout) { getClient().admin().indices().segments(new IndicesSegmentsRequest(indexMetaData.getIndex().getName()), ActionListener.wrap(response -> { IndexSegments idxSegments = response.getIndices().get(indexMetaData.getIndex().getName()); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/SetSingleNodeAllocateStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/SetSingleNodeAllocateStep.java index 39fb801e09b5d..ac125f7d50c9c 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/SetSingleNodeAllocateStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/SetSingleNodeAllocateStep.java @@ -84,6 +84,7 @@ public void performAction(IndexMetaData indexMetaData, ClusterState clusterState Settings settings = Settings.builder() .put(IndexMetaData.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getKey() + "_id", nodeId.get()).build(); UpdateSettingsRequest updateSettingsRequest = new UpdateSettingsRequest(indexMetaData.getIndex().getName()) + .masterNodeTimeout(getMasterTimeout(clusterState)) .settings(settings); getClient().admin().indices().updateSettings(updateSettingsRequest, ActionListener.wrap(response -> listener.onResponse(true), listener::onFailure)); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrinkSetAliasStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrinkSetAliasStep.java index 78426c5b336f1..b9fc51f89cff9 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrinkSetAliasStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrinkSetAliasStep.java @@ -38,6 +38,7 @@ public void performDuringNoSnapshot(IndexMetaData indexMetaData, ClusterState cu // get target shrink index String targetIndexName = shrunkIndexPrefix + index; IndicesAliasesRequest aliasesRequest = new IndicesAliasesRequest() + .masterNodeTimeout(getMasterTimeout(currentState)) .addAliasAction(IndicesAliasesRequest.AliasActions.removeIndex().index(index)) .addAliasAction(IndicesAliasesRequest.AliasActions.add().index(targetIndexName).alias(index)); // copy over other aliases from original index diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrinkStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrinkStep.java index 2bddfe6771317..fb15ea6d9adff 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrinkStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrinkStep.java @@ -57,7 +57,8 @@ public void performAction(IndexMetaData indexMetaData, ClusterState currentState .build(); String shrunkenIndexName = shrunkIndexPrefix + indexMetaData.getIndex().getName(); - ResizeRequest resizeRequest = new ResizeRequest(shrunkenIndexName, indexMetaData.getIndex().getName()); + ResizeRequest resizeRequest = new ResizeRequest(shrunkenIndexName, indexMetaData.getIndex().getName()) + .masterNodeTimeout(getMasterTimeout(currentState)); resizeRequest.getTargetIndexRequest().settings(relevantTargetSettings); getClient().admin().indices().resizeIndex(resizeRequest, ActionListener.wrap(response -> { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/UpdateSettingsStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/UpdateSettingsStep.java index 40383069423d4..1872105ac47a0 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/UpdateSettingsStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/UpdateSettingsStep.java @@ -35,7 +35,9 @@ public boolean isRetryable() { @Override public void performAction(IndexMetaData indexMetaData, ClusterState currentState, ClusterStateObserver observer, Listener listener) { - UpdateSettingsRequest updateSettingsRequest = new UpdateSettingsRequest(indexMetaData.getIndex().getName()).settings(settings); + UpdateSettingsRequest updateSettingsRequest = new UpdateSettingsRequest(indexMetaData.getIndex().getName()) + .masterNodeTimeout(getMasterTimeout(currentState)) + .settings(settings); getClient().admin().indices().updateSettings(updateSettingsRequest, ActionListener.wrap(response -> listener.onResponse(true), listener::onFailure)); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForFollowShardTasksStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForFollowShardTasksStep.java index bc44870f974d9..a4027f55b9cdf 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForFollowShardTasksStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForFollowShardTasksStep.java @@ -10,6 +10,7 @@ import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.common.ParseField; import org.elasticsearch.common.Strings; +import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.xpack.core.ccr.ShardFollowNodeTaskStatus; @@ -32,7 +33,7 @@ final class WaitForFollowShardTasksStep extends AsyncWaitStep { } @Override - public void evaluateCondition(IndexMetaData indexMetaData, Listener listener) { + public void evaluateCondition(IndexMetaData indexMetaData, Listener listener, TimeValue masterTimeout) { Map customIndexMetadata = indexMetaData.getCustomData(CCR_METADATA_KEY); if (customIndexMetadata == null) { listener.onResponse(true, null); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForNoFollowersStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForNoFollowersStep.java index f07c4095fc47b..a4313028aa48d 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForNoFollowersStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForNoFollowersStep.java @@ -15,6 +15,7 @@ import org.elasticsearch.client.Client; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.common.ParseField; +import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -42,7 +43,7 @@ public class WaitForNoFollowersStep extends AsyncWaitStep { } @Override - public void evaluateCondition(IndexMetaData indexMetaData, Listener listener) { + public void evaluateCondition(IndexMetaData indexMetaData, Listener listener, TimeValue masterTimeout) { IndicesStatsRequest request = new IndicesStatsRequest(); request.clear(); String indexName = indexMetaData.getIndex().getName(); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForRolloverReadyStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForRolloverReadyStep.java index 8cbf64d1d392f..62072cf52a413 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForRolloverReadyStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForRolloverReadyStep.java @@ -48,7 +48,7 @@ public boolean isRetryable() { } @Override - public void evaluateCondition(IndexMetaData indexMetaData, Listener listener) { + public void evaluateCondition(IndexMetaData indexMetaData, Listener listener, TimeValue masterTimeout) { String rolloverAlias = RolloverAction.LIFECYCLE_ROLLOVER_ALIAS_SETTING.get(indexMetaData.getSettings()); if (Strings.isNullOrEmpty(rolloverAlias)) { @@ -113,7 +113,7 @@ public void evaluateCondition(IndexMetaData indexMetaData, Listener listener) { "index [%s] is not the write index for alias [%s]", indexMetaData.getIndex().getName(), rolloverAlias))); } - RolloverRequest rolloverRequest = new RolloverRequest(rolloverAlias, null); + RolloverRequest rolloverRequest = new RolloverRequest(rolloverAlias, null).masterNodeTimeout(masterTimeout); rolloverRequest.dryRun(true); if (maxAge != null) { rolloverRequest.addMaxIndexAgeCondition(maxAge); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/AbstractStepMasterTimeoutTestCase.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/AbstractStepMasterTimeoutTestCase.java new file mode 100644 index 0000000000000..f93d07d580bd3 --- /dev/null +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/AbstractStepMasterTimeoutTestCase.java @@ -0,0 +1,83 @@ +/* + * 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.core.ilm; + +import org.elasticsearch.action.ActionListener; +import org.elasticsearch.action.ActionRequest; +import org.elasticsearch.action.ActionResponse; +import org.elasticsearch.action.ActionType; +import org.elasticsearch.action.support.master.MasterNodeRequest; +import org.elasticsearch.cluster.ClusterName; +import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.metadata.IndexMetaData; +import org.elasticsearch.cluster.metadata.MetaData; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.test.client.NoOpClient; +import org.elasticsearch.threadpool.TestThreadPool; +import org.elasticsearch.threadpool.ThreadPool; +import org.junit.After; +import org.junit.Before; + +import static org.elasticsearch.xpack.core.ilm.LifecycleSettings.LIFECYCLE_STEP_MASTER_TIMEOUT; +import static org.hamcrest.Matchers.equalTo; + +public abstract class AbstractStepMasterTimeoutTestCase extends AbstractStepTestCase { + + protected ThreadPool pool; + + @Before + public void setupThreadPool() { + pool = new TestThreadPool("timeoutTestPool"); + } + + @After + public void shutdownThreadPool() { + pool.shutdownNow(); + } + + public void testMasterTimeout() { + checkMasterTimeout(TimeValue.timeValueSeconds(30), + ClusterState.builder(ClusterName.DEFAULT).metaData(MetaData.builder().build()).build()); + checkMasterTimeout(TimeValue.timeValueSeconds(10), + ClusterState.builder(ClusterName.DEFAULT) + .metaData(MetaData.builder() + .persistentSettings(Settings.builder().put(LIFECYCLE_STEP_MASTER_TIMEOUT, "10s").build()) + .build()) + .build()); + } + + private void checkMasterTimeout(TimeValue timeValue, ClusterState currentClusterState) { + T instance = createRandomInstance(); + instance.setClient(new NoOpClient(pool) { + @Override + protected void doExecute(ActionType action, + Request request, + ActionListener listener) { + if (request instanceof MasterNodeRequest) { + assertThat(((MasterNodeRequest) request).masterNodeTimeout(), equalTo(timeValue)); + } + } + }); + instance.performAction(getIndexMetaData(), currentClusterState, null, new AsyncActionStep.Listener() { + @Override + public void onResponse(boolean complete) { + + } + + @Override + public void onFailure(Exception e) { + + } + }); + } + + protected abstract IndexMetaData getIndexMetaData(); + + public static ClusterState emptyClusterState() { + return ClusterState.builder(ClusterName.DEFAULT).build(); + } +} diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/AbstractStepTestCase.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/AbstractStepTestCase.java index 63ba0dba02633..ea6738e6e42a6 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/AbstractStepTestCase.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/AbstractStepTestCase.java @@ -5,6 +5,7 @@ */ package org.elasticsearch.xpack.core.ilm; +import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.EqualsHashCodeTestUtils; import org.elasticsearch.xpack.core.ilm.Step.StepKey; @@ -12,6 +13,7 @@ public abstract class AbstractStepTestCase extends ESTestCase { protected static final int NUMBER_OF_TEST_RUNS = 20; + protected static final TimeValue MASTER_TIMEOUT = TimeValue.timeValueSeconds(30); protected abstract T createRandomInstance(); protected abstract T mutateInstance(T instance); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CloseFollowerIndexStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CloseFollowerIndexStepTests.java index d096ff0009c6b..364833fa4cc73 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CloseFollowerIndexStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CloseFollowerIndexStepTests.java @@ -23,15 +23,20 @@ import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.Matchers.sameInstance; -public class CloseFollowerIndexStepTests extends AbstractStepTestCase { +public class CloseFollowerIndexStepTests extends AbstractStepMasterTimeoutTestCase { - public void testCloseFollowingIndex() { - IndexMetaData indexMetadata = IndexMetaData.builder("follower-index") + @Override + protected IndexMetaData getIndexMetaData() { + return IndexMetaData.builder("follower-index") .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE, "true")) .putCustom(CCR_METADATA_KEY, Collections.emptyMap()) .numberOfShards(1) .numberOfReplicas(0) .build(); + } + + public void testCloseFollowingIndex() { + IndexMetaData indexMetadata = getIndexMetaData(); Client client = Mockito.mock(Client.class); AdminClient adminClient = Mockito.mock(AdminClient.class); @@ -51,7 +56,7 @@ public void testCloseFollowingIndex() { Boolean[] completed = new Boolean[1]; Exception[] failure = new Exception[1]; CloseFollowerIndexStep step = new CloseFollowerIndexStep(randomStepKey(), randomStepKey(), client); - step.performAction(indexMetadata, null, null, new AsyncActionStep.Listener() { + step.performAction(indexMetadata, emptyClusterState(), null, new AsyncActionStep.Listener() { @Override public void onResponse(boolean complete) { completed[0] = complete; @@ -67,12 +72,7 @@ public void onFailure(Exception e) { } public void testCloseFollowingIndexFailed() { - IndexMetaData indexMetadata = IndexMetaData.builder("follower-index") - .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE, "true")) - .putCustom(CCR_METADATA_KEY, Collections.emptyMap()) - .numberOfShards(1) - .numberOfReplicas(0) - .build(); + IndexMetaData indexMetadata = getIndexMetaData(); // Mock pause follow api call: Client client = Mockito.mock(Client.class); @@ -93,7 +93,7 @@ public void testCloseFollowingIndexFailed() { Boolean[] completed = new Boolean[1]; Exception[] failure = new Exception[1]; CloseFollowerIndexStep step = new CloseFollowerIndexStep(randomStepKey(), randomStepKey(), client); - step.performAction(indexMetadata, null, null, new AsyncActionStep.Listener() { + step.performAction(indexMetadata, emptyClusterState(), null, new AsyncActionStep.Listener() { @Override public void onResponse(boolean complete) { completed[0] = complete; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/DeleteStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/DeleteStepTests.java index f72e2ce456bc7..379c42469d708 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/DeleteStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/DeleteStepTests.java @@ -23,7 +23,7 @@ import static org.hamcrest.Matchers.equalTo; -public class DeleteStepTests extends AbstractStepTestCase { +public class DeleteStepTests extends AbstractStepMasterTimeoutTestCase { private Client client; @@ -64,13 +64,18 @@ public DeleteStep copyInstance(DeleteStep instance) { return new DeleteStep(instance.getKey(), instance.getNextStepKey(), instance.getClient()); } + @Override + protected IndexMetaData getIndexMetaData() { + return IndexMetaData.builder(randomAlphaOfLength(10)).settings(settings(Version.CURRENT)) + .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + } + public void testIndexSurvives() { assertFalse(createRandomInstance().indexSurvives()); } public void testDeleted() { - IndexMetaData indexMetaData = IndexMetaData.builder(randomAlphaOfLength(10)).settings(settings(Version.CURRENT)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + IndexMetaData indexMetaData = getIndexMetaData(); AdminClient adminClient = Mockito.mock(AdminClient.class); IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class); @@ -91,7 +96,7 @@ public void testDeleted() { SetOnce actionCompleted = new SetOnce<>(); DeleteStep step = createRandomInstance(); - step.performAction(indexMetaData, null, null, new AsyncActionStep.Listener() { + step.performAction(indexMetaData, emptyClusterState(), null, new AsyncActionStep.Listener() { @Override public void onResponse(boolean complete) { actionCompleted.set(complete); @@ -111,8 +116,7 @@ public void onFailure(Exception e) { } public void testExceptionThrown() { - IndexMetaData indexMetaData = IndexMetaData.builder(randomAlphaOfLength(10)).settings(settings(Version.CURRENT)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + IndexMetaData indexMetaData = getIndexMetaData(); Exception exception = new RuntimeException(); AdminClient adminClient = Mockito.mock(AdminClient.class); @@ -138,7 +142,7 @@ public Void answer(InvocationOnMock invocation) throws Throwable { SetOnce exceptionThrown = new SetOnce<>(); DeleteStep step = createRandomInstance(); - step.performAction(indexMetaData, null, null, new AsyncActionStep.Listener() { + step.performAction(indexMetaData, emptyClusterState(), null, new AsyncActionStep.Listener() { @Override public void onResponse(boolean complete) { throw new AssertionError("Unexpected method call"); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/FreezeStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/FreezeStepTests.java index fcd22d369533a..c9755edbe3c97 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/FreezeStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/FreezeStepTests.java @@ -19,12 +19,11 @@ import org.elasticsearch.xpack.core.ilm.Step.StepKey; import org.junit.Before; import org.mockito.Mockito; -import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; import static org.hamcrest.Matchers.equalTo; -public class FreezeStepTests extends AbstractStepTestCase { +public class FreezeStepTests extends AbstractStepMasterTimeoutTestCase { private Client client; @@ -65,13 +64,18 @@ public FreezeStep copyInstance(FreezeStep instance) { return new FreezeStep(instance.getKey(), instance.getNextStepKey(), instance.getClient()); } + @Override + protected IndexMetaData getIndexMetaData() { + return IndexMetaData.builder(randomAlphaOfLength(10)).settings(settings(Version.CURRENT)) + .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + } + public void testIndexSurvives() { assertTrue(createRandomInstance().indexSurvives()); } public void testFreeze() { - IndexMetaData indexMetaData = IndexMetaData.builder(randomAlphaOfLength(10)).settings(settings(Version.CURRENT)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + IndexMetaData indexMetaData = getIndexMetaData(); AdminClient adminClient = Mockito.mock(AdminClient.class); IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class); @@ -93,7 +97,7 @@ public void testFreeze() { SetOnce actionCompleted = new SetOnce<>(); FreezeStep step = createRandomInstance(); - step.performAction(indexMetaData, null, null, new AsyncActionStep.Listener() { + step.performAction(indexMetaData, emptyClusterState(), null, new AsyncActionStep.Listener() { @Override public void onResponse(boolean complete) { actionCompleted.set(complete); @@ -113,8 +117,7 @@ public void onFailure(Exception e) { } public void testExceptionThrown() { - IndexMetaData indexMetaData = IndexMetaData.builder(randomAlphaOfLength(10)).settings(settings(Version.CURRENT)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + IndexMetaData indexMetaData = getIndexMetaData(); Exception exception = new RuntimeException(); AdminClient adminClient = Mockito.mock(AdminClient.class); @@ -122,21 +125,16 @@ public void testExceptionThrown() { Mockito.when(client.admin()).thenReturn(adminClient); Mockito.when(adminClient.indices()).thenReturn(indicesClient); - Mockito.doAnswer(new Answer() { - - @Override - public Void answer(InvocationOnMock invocation) throws Throwable { - @SuppressWarnings("unchecked") - ActionListener listener = (ActionListener) invocation.getArguments()[2]; - listener.onFailure(exception); - return null; - } - + Mockito.doAnswer((Answer) invocation -> { + @SuppressWarnings("unchecked") + ActionListener listener = (ActionListener) invocation.getArguments()[2]; + listener.onFailure(exception); + return null; }).when(indicesClient).execute(Mockito.any(), Mockito.any(), Mockito.any()); SetOnce exceptionThrown = new SetOnce<>(); FreezeStep step = createRandomInstance(); - step.performAction(indexMetaData, null, null, new AsyncActionStep.Listener() { + step.performAction(indexMetaData, emptyClusterState(), null, new AsyncActionStep.Listener() { @Override public void onResponse(boolean complete) { throw new AssertionError("Unexpected method call"); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/OpenFollowerIndexStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/OpenFollowerIndexStepTests.java index 577d66d906aee..b2cf4d542e9f4 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/OpenFollowerIndexStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/OpenFollowerIndexStepTests.java @@ -23,7 +23,7 @@ import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.Matchers.sameInstance; -public class OpenFollowerIndexStepTests extends AbstractStepTestCase { +public class OpenFollowerIndexStepTests extends AbstractStepMasterTimeoutTestCase { @Override protected OpenFollowerIndexStep createRandomInstance() { @@ -51,14 +51,19 @@ protected OpenFollowerIndexStep copyInstance(OpenFollowerIndexStep instance) { return new OpenFollowerIndexStep(instance.getKey(), instance.getNextStepKey(), instance.getClient()); } - public void testOpenFollowerIndexIsNoopForAlreadyOpenIndex() { - IndexMetaData indexMetadata = IndexMetaData.builder("follower-index") + @Override + protected IndexMetaData getIndexMetaData() { + return IndexMetaData.builder("follower-index") .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE, "true")) .putCustom(CCR_METADATA_KEY, Collections.emptyMap()) .state(IndexMetaData.State.OPEN) .numberOfShards(1) .numberOfReplicas(0) .build(); + } + + public void testOpenFollowerIndexIsNoopForAlreadyOpenIndex() { + IndexMetaData indexMetadata = getIndexMetaData(); Client client = Mockito.mock(Client.class); OpenFollowerIndexStep step = new OpenFollowerIndexStep(randomStepKey(), randomStepKey(), client); step.performAction(indexMetadata, null, null, new AsyncActionStep.Listener() { @@ -102,7 +107,7 @@ public void testOpenFollowingIndex() { Boolean[] completed = new Boolean[1]; Exception[] failure = new Exception[1]; OpenFollowerIndexStep step = new OpenFollowerIndexStep(randomStepKey(), randomStepKey(), client); - step.performAction(indexMetadata, null, null, new AsyncActionStep.Listener() { + step.performAction(indexMetadata, emptyClusterState(), null, new AsyncActionStep.Listener() { @Override public void onResponse(boolean complete) { completed[0] = complete; @@ -144,7 +149,7 @@ public void testOpenFollowingIndexFailed() { Boolean[] completed = new Boolean[1]; Exception[] failure = new Exception[1]; OpenFollowerIndexStep step = new OpenFollowerIndexStep(randomStepKey(), randomStepKey(), client); - step.performAction(indexMetadata, null, null, new AsyncActionStep.Listener() { + step.performAction(indexMetadata, emptyClusterState(), null, new AsyncActionStep.Listener() { @Override public void onResponse(boolean complete) { completed[0] = complete; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/RolloverStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/RolloverStepTests.java index cb8e09ab462f4..a83117e2c4d66 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/RolloverStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/RolloverStepTests.java @@ -30,7 +30,7 @@ import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.core.Is.is; -public class RolloverStepTests extends AbstractStepTestCase { +public class RolloverStepTests extends AbstractStepMasterTimeoutTestCase { private Client client; @@ -72,6 +72,18 @@ public RolloverStep copyInstance(RolloverStep instance) { return new RolloverStep(instance.getKey(), instance.getNextStepKey(), instance.getClient()); } + private IndexMetaData getIndexMetaData(String alias) { + return IndexMetaData.builder(randomAlphaOfLength(10)) + .putAlias(AliasMetaData.builder(alias)) + .settings(settings(Version.CURRENT).put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias)) + .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + } + + @Override + protected IndexMetaData getIndexMetaData() { + return getIndexMetaData(randomAlphaOfLength(5)); + } + private static void assertRolloverIndexRequest(RolloverRequest request, String alias) { assertNotNull(request); assertEquals(1, request.indices().length); @@ -83,10 +95,7 @@ private static void assertRolloverIndexRequest(RolloverRequest request, String a public void testPerformAction() { String alias = randomAlphaOfLength(5); - IndexMetaData indexMetaData = IndexMetaData.builder(randomAlphaOfLength(10)) - .putAlias(AliasMetaData.builder(alias)) - .settings(settings(Version.CURRENT).put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + IndexMetaData indexMetaData = getIndexMetaData(alias); RolloverStep step = createRandomInstance(); @@ -110,7 +119,7 @@ public Void answer(InvocationOnMock invocation) throws Throwable { }).when(indicesClient).rolloverIndex(Mockito.any(), Mockito.any()); SetOnce actionCompleted = new SetOnce<>(); - step.performAction(indexMetaData, null, null, new AsyncActionStep.Listener() { + step.performAction(indexMetaData, emptyClusterState(), null, new AsyncActionStep.Listener() { @Override public void onResponse(boolean complete) { @@ -193,10 +202,7 @@ public void onFailure(Exception e) { public void testPerformActionFailure() { String alias = randomAlphaOfLength(5); - IndexMetaData indexMetaData = IndexMetaData.builder(randomAlphaOfLength(10)) - .putAlias(AliasMetaData.builder(alias)) - .settings(settings(Version.CURRENT).put(RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + IndexMetaData indexMetaData = getIndexMetaData(alias); Exception exception = new RuntimeException(); RolloverStep step = createRandomInstance(); @@ -220,7 +226,7 @@ public Void answer(InvocationOnMock invocation) throws Throwable { }).when(indicesClient).rolloverIndex(Mockito.any(), Mockito.any()); SetOnce exceptionThrown = new SetOnce<>(); - step.performAction(indexMetaData, null, null, new AsyncActionStep.Listener() { + step.performAction(indexMetaData, emptyClusterState(), null, new AsyncActionStep.Listener() { @Override public void onResponse(boolean complete) { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/SegmentCountStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/SegmentCountStepTests.java index b15ba2669ec05..c4ddd9baae5e3 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/SegmentCountStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/SegmentCountStepTests.java @@ -134,7 +134,7 @@ public void onFailure(Exception e) { logger.warn("unexpected onFailure call", e); throw new AssertionError("unexpected method call"); } - }); + }, MASTER_TIMEOUT); assertTrue(conditionMetResult.get()); assertEquals(new SegmentCountStep.Info(0L), conditionInfo.get()); @@ -192,7 +192,7 @@ public void onFailure(Exception e) { logger.warn("unexpected onFailure call", e); throw new AssertionError("unexpected method call"); } - }); + }, MASTER_TIMEOUT); assertTrue(conditionMetResult.get()); assertEquals(new SegmentCountStep.Info(0L), conditionInfo.get()); @@ -253,7 +253,7 @@ public void onFailure(Exception e) { logger.warn("unexpected onFailure call", e); throw new AssertionError("unexpected method call: " + e); } - }); + }, MASTER_TIMEOUT); assertTrue(conditionMetResult.get()); assertEquals(new SegmentCountStep.Info(-1L), conditionInfo.get()); @@ -293,7 +293,7 @@ public void onFailure(Exception e) { assertThat(e, equalTo(exception)); exceptionThrown.set(true); } - }); + }, MASTER_TIMEOUT); assertTrue(exceptionThrown.get()); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrinkSetAliasStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrinkSetAliasStepTests.java index 09d1fc2a7a035..58d1659535a49 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrinkSetAliasStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrinkSetAliasStepTests.java @@ -20,12 +20,12 @@ import org.elasticsearch.xpack.core.ilm.Step.StepKey; import org.junit.Before; import org.mockito.Mockito; -import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; import java.util.Arrays; import java.util.List; +import static org.elasticsearch.xpack.core.ilm.AbstractStepMasterTimeoutTestCase.emptyClusterState; import static org.hamcrest.Matchers.equalTo; public class ShrinkSetAliasStepTests extends AbstractStepTestCase { @@ -104,22 +104,17 @@ public void testPerformAction() { Mockito.when(client.admin()).thenReturn(adminClient); Mockito.when(adminClient.indices()).thenReturn(indicesClient); - Mockito.doAnswer(new Answer() { - - @Override - public Void answer(InvocationOnMock invocation) throws Throwable { - IndicesAliasesRequest request = (IndicesAliasesRequest) invocation.getArguments()[0]; - assertThat(request.getAliasActions(), equalTo(expectedAliasActions)); - @SuppressWarnings("unchecked") - ActionListener listener = (ActionListener) invocation.getArguments()[1]; - listener.onResponse(new AcknowledgedResponse(true)); - return null; - } - + Mockito.doAnswer( invocation -> { + IndicesAliasesRequest request = (IndicesAliasesRequest) invocation.getArguments()[0]; + assertThat(request.getAliasActions(), equalTo(expectedAliasActions)); + @SuppressWarnings("unchecked") + ActionListener listener = (ActionListener) invocation.getArguments()[1]; + listener.onResponse(new AcknowledgedResponse(true)); + return null; }).when(indicesClient).aliases(Mockito.any(), Mockito.any()); SetOnce actionCompleted = new SetOnce<>(); - step.performAction(indexMetaData, null, null, new Listener() { + step.performAction(indexMetaData, emptyClusterState(), null, new Listener() { @Override public void onResponse(boolean complete) { @@ -150,20 +145,15 @@ public void testPerformActionFailure() { Mockito.when(client.admin()).thenReturn(adminClient); Mockito.when(adminClient.indices()).thenReturn(indicesClient); - Mockito.doAnswer(new Answer() { - - @Override - public Void answer(InvocationOnMock invocation) throws Throwable { - @SuppressWarnings("unchecked") - ActionListener listener = (ActionListener) invocation.getArguments()[1]; - listener.onFailure(exception); - return null; - } - + Mockito.doAnswer((Answer) invocation -> { + @SuppressWarnings("unchecked") + ActionListener listener = (ActionListener) invocation.getArguments()[1]; + listener.onFailure(exception); + return null; }).when(indicesClient).aliases(Mockito.any(), Mockito.any()); SetOnce exceptionThrown = new SetOnce<>(); - step.performAction(indexMetaData, null, null, new Listener() { + step.performAction(indexMetaData, emptyClusterState(), null, new Listener() { @Override public void onResponse(boolean complete) { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrinkStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrinkStepTests.java index b09871489315c..4021112533f4c 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrinkStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ShrinkStepTests.java @@ -26,6 +26,7 @@ import java.util.Collections; +import static org.elasticsearch.xpack.core.ilm.AbstractStepMasterTimeoutTestCase.emptyClusterState; import static org.elasticsearch.xpack.core.ilm.LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY; import static org.hamcrest.Matchers.equalTo; @@ -127,7 +128,7 @@ public Void answer(InvocationOnMock invocation) throws Throwable { }).when(indicesClient).resizeIndex(Mockito.any(), Mockito.any()); SetOnce actionCompleted = new SetOnce<>(); - step.performAction(sourceIndexMetaData, null, null, new Listener() { + step.performAction(sourceIndexMetaData, emptyClusterState(), null, new Listener() { @Override public void onResponse(boolean complete) { @@ -160,20 +161,15 @@ public void testPerformActionNotComplete() throws Exception { Mockito.when(client.admin()).thenReturn(adminClient); Mockito.when(adminClient.indices()).thenReturn(indicesClient); - Mockito.doAnswer(new Answer() { - - @Override - public Void answer(InvocationOnMock invocation) throws Throwable { - @SuppressWarnings("unchecked") - ActionListener listener = (ActionListener) invocation.getArguments()[1]; - listener.onResponse(new ResizeResponse(false, false, indexMetaData.getIndex().getName())); - return null; - } - + Mockito.doAnswer(invocation -> { + @SuppressWarnings("unchecked") + ActionListener listener = (ActionListener) invocation.getArguments()[1]; + listener.onResponse(new ResizeResponse(false, false, indexMetaData.getIndex().getName())); + return null; }).when(indicesClient).resizeIndex(Mockito.any(), Mockito.any()); SetOnce actionCompleted = new SetOnce<>(); - step.performAction(indexMetaData, null, null, new Listener() { + step.performAction(indexMetaData, emptyClusterState(), null, new Listener() { @Override public void onResponse(boolean complete) { @@ -207,20 +203,15 @@ public void testPerformActionFailure() throws Exception { Mockito.when(client.admin()).thenReturn(adminClient); Mockito.when(adminClient.indices()).thenReturn(indicesClient); - Mockito.doAnswer(new Answer() { - - @Override - public Void answer(InvocationOnMock invocation) throws Throwable { - @SuppressWarnings("unchecked") - ActionListener listener = (ActionListener) invocation.getArguments()[1]; - listener.onFailure(exception); - return null; - } - + Mockito.doAnswer(invocation -> { + @SuppressWarnings("unchecked") + ActionListener listener = (ActionListener) invocation.getArguments()[1]; + listener.onFailure(exception); + return null; }).when(indicesClient).resizeIndex(Mockito.any(), Mockito.any()); SetOnce exceptionThrown = new SetOnce<>(); - step.performAction(indexMetaData, null, null, new Listener() { + step.performAction(indexMetaData, emptyClusterState(), null, new Listener() { @Override public void onResponse(boolean complete) { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/UpdateSettingsStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/UpdateSettingsStepTests.java index edabe75a2da08..0400feb60d01c 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/UpdateSettingsStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/UpdateSettingsStepTests.java @@ -20,12 +20,10 @@ import org.elasticsearch.xpack.core.ilm.Step.StepKey; import org.junit.Before; import org.mockito.Mockito; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; import static org.hamcrest.Matchers.equalTo; -public class UpdateSettingsStepTests extends AbstractStepTestCase { +public class UpdateSettingsStepTests extends AbstractStepMasterTimeoutTestCase { private Client client; @@ -71,9 +69,14 @@ public UpdateSettingsStep copyInstance(UpdateSettingsStep instance) { return new UpdateSettingsStep(instance.getKey(), instance.getNextStepKey(), instance.getClient(), instance.getSettings()); } - public void testPerformAction() throws Exception { - IndexMetaData indexMetaData = IndexMetaData.builder(randomAlphaOfLength(10)).settings(settings(Version.CURRENT)) + @Override + protected IndexMetaData getIndexMetaData() { + return IndexMetaData.builder(randomAlphaOfLength(10)).settings(settings(Version.CURRENT)) .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + } + + public void testPerformAction() throws Exception { + IndexMetaData indexMetaData = getIndexMetaData(); UpdateSettingsStep step = createRandomInstance(); @@ -83,24 +86,19 @@ public void testPerformAction() throws Exception { Mockito.when(client.admin()).thenReturn(adminClient); Mockito.when(adminClient.indices()).thenReturn(indicesClient); - Mockito.doAnswer(new Answer() { - - @Override - public Void answer(InvocationOnMock invocation) throws Throwable { - UpdateSettingsRequest request = (UpdateSettingsRequest) invocation.getArguments()[0]; - @SuppressWarnings("unchecked") - ActionListener listener = (ActionListener) invocation.getArguments()[1]; - assertThat(request.settings(), equalTo(step.getSettings())); - assertThat(request.indices(), equalTo(new String[] {indexMetaData.getIndex().getName()})); - listener.onResponse(new AcknowledgedResponse(true)); - return null; - } - + Mockito.doAnswer(invocation -> { + UpdateSettingsRequest request = (UpdateSettingsRequest) invocation.getArguments()[0]; + @SuppressWarnings("unchecked") + ActionListener listener = (ActionListener) invocation.getArguments()[1]; + assertThat(request.settings(), equalTo(step.getSettings())); + assertThat(request.indices(), equalTo(new String[] {indexMetaData.getIndex().getName()})); + listener.onResponse(new AcknowledgedResponse(true)); + return null; }).when(indicesClient).updateSettings(Mockito.any(), Mockito.any()); SetOnce actionCompleted = new SetOnce<>(); - step.performAction(indexMetaData, null, null, new Listener() { + step.performAction(indexMetaData, emptyClusterState(), null, new Listener() { @Override public void onResponse(boolean complete) { @@ -121,8 +119,7 @@ public void onFailure(Exception e) { } public void testPerformActionFailure() { - IndexMetaData indexMetaData = IndexMetaData.builder(randomAlphaOfLength(10)).settings(settings(Version.CURRENT)) - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build(); + IndexMetaData indexMetaData = getIndexMetaData(); Exception exception = new RuntimeException(); UpdateSettingsStep step = createRandomInstance(); @@ -131,23 +128,18 @@ public void testPerformActionFailure() { Mockito.when(client.admin()).thenReturn(adminClient); Mockito.when(adminClient.indices()).thenReturn(indicesClient); - Mockito.doAnswer(new Answer() { - - @Override - public Void answer(InvocationOnMock invocation) throws Throwable { - UpdateSettingsRequest request = (UpdateSettingsRequest) invocation.getArguments()[0]; - @SuppressWarnings("unchecked") - ActionListener listener = (ActionListener) invocation.getArguments()[1]; - assertThat(request.settings(), equalTo(step.getSettings())); - assertThat(request.indices(), equalTo(new String[] {indexMetaData.getIndex().getName()})); - listener.onFailure(exception); - return null; - } - + Mockito.doAnswer(invocation -> { + UpdateSettingsRequest request = (UpdateSettingsRequest) invocation.getArguments()[0]; + @SuppressWarnings("unchecked") + ActionListener listener = (ActionListener) invocation.getArguments()[1]; + assertThat(request.settings(), equalTo(step.getSettings())); + assertThat(request.indices(), equalTo(new String[] {indexMetaData.getIndex().getName()})); + listener.onFailure(exception); + return null; }).when(indicesClient).updateSettings(Mockito.any(), Mockito.any()); SetOnce exceptionThrown = new SetOnce<>(); - step.performAction(indexMetaData, null, null, new Listener() { + step.performAction(indexMetaData, emptyClusterState(), null, new Listener() { @Override public void onResponse(boolean complete) { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForFollowShardTasksStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForFollowShardTasksStepTests.java index eca28cdcd89d8..a4443259fda1b 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForFollowShardTasksStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForFollowShardTasksStepTests.java @@ -82,7 +82,7 @@ public void onResponse(boolean conditionMet, ToXContentObject informationContext public void onFailure(Exception e) { exceptionHolder[0] = e; } - }); + }, MASTER_TIMEOUT); assertThat(conditionMetHolder[0], is(true)); assertThat(informationContextHolder[0], nullValue()); @@ -118,7 +118,7 @@ public void onResponse(boolean conditionMet, ToXContentObject informationContext public void onFailure(Exception e) { exceptionHolder[0] = e; } - }); + }, MASTER_TIMEOUT); assertThat(conditionMetHolder[0], is(false)); assertThat(informationContextHolder[0], notNullValue()); @@ -153,7 +153,7 @@ public void onResponse(boolean conditionMet, ToXContentObject informationContext public void onFailure(Exception e) { exceptionHolder[0] = e; } - }); + }, MASTER_TIMEOUT); assertThat(conditionMetHolder[0], is(true)); assertThat(informationContextHolder[0], nullValue()); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForNoFollowersStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForNoFollowersStepTests.java index b80635ac2d682..36824af47a683 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForNoFollowersStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForNoFollowersStepTests.java @@ -92,7 +92,7 @@ public void onResponse(boolean conditionMet, ToXContentObject infomationContext) public void onFailure(Exception e) { fail("onFailure should not be called in this test, called with exception: " + e.getMessage()); } - }); + }, MASTER_TIMEOUT); assertTrue(conditionMetHolder.get()); assertNull(stepInfoHolder.get()); @@ -125,7 +125,7 @@ public void onResponse(boolean conditionMet, ToXContentObject infomationContext) public void onFailure(Exception e) { fail("onFailure should not be called in this test, called with exception: " + e.getMessage()); } - }); + }, MASTER_TIMEOUT); assertFalse(conditionMetHolder.get()); assertThat(Strings.toString(stepInfoHolder.get()), @@ -162,7 +162,7 @@ public void onResponse(boolean conditionMet, ToXContentObject infomationContext) public void onFailure(Exception e) { fail("onFailure should not be called in this test, called with exception: " + e.getMessage()); } - }); + }, MASTER_TIMEOUT); assertTrue(conditionMetHolder.get()); assertNull(stepInfoHolder.get()); @@ -206,7 +206,7 @@ public void onResponse(boolean conditionMet, ToXContentObject infomationContext) public void onFailure(Exception e) { exceptionHolder.set(e); } - }); + }, MASTER_TIMEOUT); assertThat(exceptionHolder.get(), equalTo(expectedException)); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForRolloverReadyStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForRolloverReadyStepTests.java index 69494cd6948a6..583d540cca395 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForRolloverReadyStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/WaitForRolloverReadyStepTests.java @@ -167,7 +167,7 @@ public void onResponse(boolean complete, ToXContentObject infomationContext) { public void onFailure(Exception e) { throw new AssertionError("Unexpected method call", e); } - }); + }, MASTER_TIMEOUT); assertEquals(true, conditionsMet.get()); @@ -199,7 +199,7 @@ public void onResponse(boolean complete, ToXContentObject informationContext) { public void onFailure(Exception e) { throw new AssertionError("Unexpected method call", e); } - }); + }, MASTER_TIMEOUT); Mockito.verify(indicesClient, Mockito.never()).rolloverIndex(Mockito.any(), Mockito.any()); } @@ -229,7 +229,7 @@ public void onResponse(boolean complete, ToXContentObject informationContext) { public void onFailure(Exception e) { throw new AssertionError("Unexpected method call", e); } - }); + }, MASTER_TIMEOUT); Mockito.verify(indicesClient, Mockito.only()).rolloverIndex(Mockito.any(), Mockito.any()); } @@ -257,7 +257,7 @@ public void onResponse(boolean complete, ToXContentObject infomationContext) { public void onFailure(Exception e) { throw new AssertionError("Unexpected method call", e); } - }); + }, MASTER_TIMEOUT); assertEquals(true, conditionsMet.get()); } @@ -286,7 +286,7 @@ public void onFailure(Exception e) { assertTrue(e instanceof IllegalStateException); correctFailureCalled.set(true); } - }); + }, MASTER_TIMEOUT); assertEquals(true, correctFailureCalled.get()); } @@ -342,7 +342,7 @@ public void onResponse(boolean complete, ToXContentObject infomationContext) { public void onFailure(Exception e) { throw new AssertionError("Unexpected method call", e); } - }); + }, MASTER_TIMEOUT); assertEquals(false, actionCompleted.get()); @@ -402,7 +402,7 @@ public void onFailure(Exception e) { assertSame(exception, e); exceptionThrown.set(true); } - }); + }, MASTER_TIMEOUT); assertEquals(true, exceptionThrown.get()); @@ -429,7 +429,7 @@ public void onResponse(boolean complete, ToXContentObject infomationContext) { public void onFailure(Exception e) { exceptionThrown.set(e); } - }); + }, MASTER_TIMEOUT); assertThat(exceptionThrown.get().getClass(), equalTo(IllegalArgumentException.class)); assertThat(exceptionThrown.get().getMessage(), equalTo(String.format(Locale.ROOT, "setting [%s] for index [%s] is empty or not defined", RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, @@ -454,7 +454,7 @@ public void onResponse(boolean complete, ToXContentObject infomationContext) { public void onFailure(Exception e) { exceptionThrown.set(e); } - }); + }, MASTER_TIMEOUT); assertThat(exceptionThrown.get().getClass(), equalTo(IllegalArgumentException.class)); assertThat(exceptionThrown.get().getMessage(), equalTo(String.format(Locale.ROOT, "%s [%s] does not point to index [%s]", RolloverAction.LIFECYCLE_ROLLOVER_ALIAS, alias, diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycle.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycle.java index 4b64e306d8f7f..82ea0757ceb93 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycle.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycle.java @@ -177,6 +177,7 @@ public List> getSettings() { LifecycleSettings.LIFECYCLE_PARSE_ORIGINATION_DATE_SETTING, LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE_SETTING, LifecycleSettings.LIFECYCLE_HISTORY_INDEX_ENABLED_SETTING, + LifecycleSettings.LIFECYCLE_STEP_MASTER_TIMEOUT_SETTING, RolloverAction.LIFECYCLE_ROLLOVER_ALIAS_SETTING, LifecycleSettings.SLM_HISTORY_INDEX_ENABLED_SETTING, LifecycleSettings.SLM_RETENTION_SCHEDULE_SETTING, diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycleRunner.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycleRunner.java index e98d9380daf5a..f18bd7c17c5ab 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycleRunner.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/IndexLifecycleRunner.java @@ -165,7 +165,7 @@ public void onResponse(boolean conditionMet, ToXContentObject stepInfo) { public void onFailure(Exception e) { moveToErrorStep(indexMetaData.getIndex(), policy, currentStep.getKey(), e); } - }); + }, AsyncActionStep.getMasterTimeout(clusterService.state())); } else { logger.trace("[{}] ignoring non periodic step execution from step transition [{}]", index, currentStep.getKey()); } diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/IndexLifecycleRunnerTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/IndexLifecycleRunnerTests.java index faa1270f479aa..61c64d4fe501b 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/IndexLifecycleRunnerTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/IndexLifecycleRunnerTests.java @@ -906,7 +906,7 @@ public void setLatch(CountDownLatch latch) { } @Override - public void evaluateCondition(IndexMetaData indexMetaData, Listener listener) { + public void evaluateCondition(IndexMetaData indexMetaData, Listener listener, TimeValue masterTimeout) { executeCount++; if (latch != null) { latch.countDown();