From f6ce935444c6ce520f3343ef89fd312fbda835fe Mon Sep 17 00:00:00 2001 From: Tal Levy Date: Thu, 25 Oct 2018 16:00:44 -0700 Subject: [PATCH] fix `GET _ilm` response with uninitialized ILM metadata (#34881) ILM would return a resource-not-found exception when requesting policies while the IndexLifecycleMetaData is not initialized. The behavior here should not be as extreme since it is not the user's fault. This commit changes the behavior so that it succeeds and returns no policies when no policy names are explicitely specified, otherwise keep the same behavior of throwing an exception --- .../action/TransportGetLifecycleAction.java | 8 +++++++- .../IndexLifecycleInitialisationTests.java | 11 +++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/TransportGetLifecycleAction.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/TransportGetLifecycleAction.java index 638bc824000..c7660dc68d6 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/TransportGetLifecycleAction.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/indexlifecycle/action/TransportGetLifecycleAction.java @@ -28,6 +28,7 @@ import org.elasticsearch.xpack.core.indexlifecycle.action.GetLifecycleAction.Res import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.List; public class TransportGetLifecycleAction extends TransportMasterNodeAction { @@ -53,7 +54,12 @@ public class TransportGetLifecycleAction extends TransportMasterNodeAction listener) { IndexLifecycleMetadata metadata = clusterService.state().metaData().custom(IndexLifecycleMetadata.TYPE); if (metadata == null) { - listener.onFailure(new ResourceNotFoundException("Lifecycle policy not found: {}", Arrays.toString(request.getPolicyNames()))); + if (request.getPolicyNames().length == 0) { + listener.onResponse(new Response(Collections.emptyList())); + } else { + listener.onFailure(new ResourceNotFoundException("Lifecycle policy not found: {}", + Arrays.toString(request.getPolicyNames()))); + } } else { List requestedPolicies; // if no policies explicitly provided, behave as if `*` was specified diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleInitialisationTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleInitialisationTests.java index fc8598de65f..a041232d8a7 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleInitialisationTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/indexlifecycle/IndexLifecycleInitialisationTests.java @@ -54,6 +54,7 @@ import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.concurrent.ExecutionException; import java.util.stream.Collectors; import static org.elasticsearch.client.Requests.clusterHealthRequest; @@ -64,6 +65,7 @@ import static org.elasticsearch.cluster.routing.ShardRoutingState.STARTED; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.elasticsearch.xpack.core.indexlifecycle.LifecyclePolicyTestsUtils.newLockableLifecyclePolicy; import static org.hamcrest.CoreMatchers.not; +import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.greaterThanOrEqualTo; import static org.hamcrest.Matchers.lessThanOrEqualTo; @@ -145,6 +147,15 @@ public class IndexLifecycleInitialisationTests extends ESIntegTestCase { logger.info("Starting server1"); final String server_1 = internalCluster().startNode(); final String node1 = getLocalNodeId(server_1); + + // test get-lifecycle behavior when IndexLifecycleMetaData is null + GetLifecycleAction.Response getUninitializedLifecycleResponse = client().execute(GetLifecycleAction.INSTANCE, + new GetLifecycleAction.Request()).get(); + assertThat(getUninitializedLifecycleResponse.getPolicies().size(), equalTo(0)); + ExecutionException exception = expectThrows(ExecutionException.class,() -> client() + .execute(GetLifecycleAction.INSTANCE, new GetLifecycleAction.Request("non-existent-policy")).get()); + assertThat(exception.getMessage(), containsString("Lifecycle policy not found: [non-existent-policy]")); + logger.info("Creating lifecycle [test_lifecycle]"); PutLifecycleAction.Request putLifecycleRequest = new PutLifecycleAction.Request(lifecyclePolicy); long lowerBoundModifiedDate = Instant.now().toEpochMilli();