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
This commit is contained in:
parent
f76ced4e14
commit
f6ce935444
|
@ -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<Request, Response> {
|
||||
|
@ -53,7 +54,12 @@ public class TransportGetLifecycleAction extends TransportMasterNodeAction<Reque
|
|||
protected void masterOperation(Request request, ClusterState state, ActionListener<Response> 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<LifecyclePolicyResponseItem> requestedPolicies;
|
||||
// if no policies explicitly provided, behave as if `*` was specified
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Reference in New Issue