Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HLRC: Add Lifecycle delete to the HLRC #33142

Merged
merged 11 commits into from
Aug 29, 2018
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.indexlifecycle.DeleteLifecyclePolicyRequest;
import org.elasticsearch.protocol.xpack.indexlifecycle.ExplainLifecycleRequest;
import org.elasticsearch.protocol.xpack.indexlifecycle.ExplainLifecycleResponse;
import org.elasticsearch.protocol.xpack.indexlifecycle.SetIndexLifecyclePolicyRequest;
Expand All @@ -39,6 +40,35 @@ public class IndexLifecycleClient {
this.restHighLevelClient = restHighLevelClient;
}

/**
* Delete a lifecycle definition
* See <a href="https://fix-me-when-we-have-docs.com">
* the docs</a> for more.
* @param request the request
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @return the response
* @throws IOException in case there is a problem sending the request or parsing back the response
*/
public AcknowledgedResponse deleteLifecyclePolicy(DeleteLifecyclePolicyRequest request,
RequestOptions options) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(request, RequestConverters::deleteLifecycle, options,
AcknowledgedResponse::fromXContent, emptySet());
}

/**
* Asynchronously delete a lifecycle definition
* See <a href="https://fix-me-when-we-have-docs.com">
* the docs</a> for more.
* @param request the request
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @param listener the listener to be notified upon request completion
*/
public void deleteLifecyclePolicyAsync(DeleteLifecyclePolicyRequest request, RequestOptions options,
ActionListener<AcknowledgedResponse> listener) {
restHighLevelClient.performRequestAsyncAndParseEntity(request, RequestConverters::deleteLifecycle, options,
AcknowledgedResponse::fromXContent, listener, emptySet());
}

/**
* Set the index lifecycle policy for an index
* See <a href="https://fix-me-when-we-have-docs.com">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.action.support.WriteRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.indexlifecycle.DeleteLifecyclePolicyRequest;
import org.elasticsearch.cluster.health.ClusterHealthStatus;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Priority;
Expand Down Expand Up @@ -1190,6 +1191,18 @@ static Request xpackUsage(XPackUsageRequest usageRequest) {
return request;
}

static Request deleteLifecycle(DeleteLifecyclePolicyRequest deleteLifecyclePolicyRequest) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to deleteLifecyclePolicy?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, +1 to rename this deleteLifecyclePolicy

Request request = new Request(HttpDelete.METHOD_NAME,
new EndpointBuilder()
.addPathPartAsIs("_ilm")
.addPathPartAsIs(deleteLifecyclePolicyRequest.getLifecycle())
.build());
Params params = new Params(request);
params.withMasterTimeout(deleteLifecyclePolicyRequest.masterNodeTimeout());
params.withTimeout(deleteLifecyclePolicyRequest.timeout());
return request;
}

static Request setIndexLifecyclePolicy(SetIndexLifecyclePolicyRequest setPolicyRequest) {
String[] indices = setPolicyRequest.indices() == null ? Strings.EMPTY_ARRAY : setPolicyRequest.indices();
Request request = new Request(HttpPut.METHOD_NAME,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@
*/
public class TimedRequest implements Validatable {

private TimeValue timeout;
private TimeValue masterTimeout;
public static final TimeValue DEFAULT_TIMEOUT = TimeValue.timeValueSeconds(30);
public static final TimeValue DEFAULT_MASTER_TIMEOUT = TimeValue.timeValueSeconds(30);

private TimeValue timeout = DEFAULT_TIMEOUT;
private TimeValue masterTimeout = DEFAULT_MASTER_TIMEOUT;

public void setTimeout(TimeValue timeout) {
this.timeout = timeout;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.client.indexlifecycle;

import org.elasticsearch.client.TimedRequest;
import org.elasticsearch.common.Strings;

import java.util.Objects;

public class DeleteLifecyclePolicyRequest extends TimedRequest {

private final String lifecycle;

public DeleteLifecyclePolicyRequest(String lifecycle) {
if (Strings.isNullOrEmpty(lifecycle)) {
throw new IllegalArgumentException("lifecycle name must be present");
}
this.lifecycle = lifecycle;
}

public String getLifecycle() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also rename these getLifecyclePolicy?

return lifecycle;
}


@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
DeleteLifecyclePolicyRequest that = (DeleteLifecyclePolicyRequest) o;
return Objects.equals(getLifecycle(), that.getLifecycle());
}

@Override
public int hashCode() {
return Objects.hash(getLifecycle());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest;
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.indexlifecycle.DeleteLifecyclePolicyRequest;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.protocol.xpack.indexlifecycle.ExplainLifecycleRequest;
import org.elasticsearch.protocol.xpack.indexlifecycle.ExplainLifecycleResponse;
Expand All @@ -36,8 +37,10 @@
import org.elasticsearch.protocol.xpack.indexlifecycle.StopILMRequest;
import org.hamcrest.Matchers;

import java.io.IOException;
import java.util.Map;

import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;

Expand Down Expand Up @@ -286,4 +289,70 @@ public void testExplainLifecycle() throws Exception {
assertFalse(squashResponse.managedByILM());
assertEquals("squash", squashResponse.getIndex());
}

public void testDeleteLifecycle() throws IOException {
String policy = randomAlphaOfLength(10);

// TODO: NORELEASE convert this to using the high level client once there are APIs for it
String jsonString = "{\n" +
" \"policy\": {\n" +
" \"phases\": {\n" +
" \"hot\": {\n" +
" \"actions\": {\n" +
" \"rollover\": {\n" +
" \"max_age\": \"50d\"\n" +
" } \n" +
" }\n" +
" },\n" +
" \"warm\": {\n" +
" \"after\": \"1000s\",\n" +
" \"actions\": {\n" +
" \"allocate\": {\n" +
" \"require\": { \"_name\": \"node-1\" },\n" +
" \"include\": {},\n" +
" \"exclude\": {}\n" +
" },\n" +
" \"shrink\": {\n" +
" \"number_of_shards\": 1\n" +
" },\n" +
" \"forcemerge\": {\n" +
" \"max_num_segments\": 1000\n" +
" }\n" +
" }\n" +
" },\n" +
" \"cold\": {\n" +
" \"after\": \"2000s\",\n" +
" \"actions\": {\n" +
" \"allocate\": {\n" +
" \"number_of_replicas\": 0\n" +
" }\n" +
" }\n" +
" },\n" +
" \"delete\": {\n" +
" \"after\": \"3000s\",\n" +
" \"actions\": {\n" +
" \"delete\": {}\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
HttpEntity entity = new NStringEntity(jsonString, ContentType.APPLICATION_JSON);
Request request = new Request("PUT", "/_ilm/" + policy);
request.setEntity(entity);
client().performRequest(request);

DeleteLifecyclePolicyRequest deleteRequest = new DeleteLifecyclePolicyRequest(policy);
assertAcked(execute(deleteRequest, highLevelClient().indexLifecycle()::deleteLifecyclePolicy,
highLevelClient().indexLifecycle()::deleteLifecyclePolicyAsync));

// TODO: NORELEASE convert this to using the high level client once there are APIs for it
Request getLifecycle = new Request("GET", "/_ilm/" + policy);
try {
client().performRequest(getLifecycle);
fail("index should not exist after being deleted");
} catch (ResponseException ex) {
assertEquals(404, ex.getResponse().getStatusLine().getStatusCode());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
import org.elasticsearch.action.support.replication.ReplicationRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestConverters.EndpointBuilder;
import org.elasticsearch.client.indexlifecycle.DeleteLifecyclePolicyRequest;
import org.elasticsearch.cluster.health.ClusterHealthStatus;
import org.elasticsearch.common.CheckedBiConsumer;
import org.elasticsearch.common.CheckedFunction;
Expand Down Expand Up @@ -2698,6 +2699,19 @@ public void testGraphExplore() throws Exception {
assertToXContentBody(graphExploreRequest, request.getEntity());
}

public void testDeleteLifecycle() {
String lifecycleName = randomAlphaOfLengthBetween(2,20);
DeleteLifecyclePolicyRequest req = new DeleteLifecyclePolicyRequest(lifecycleName);
Map<String, String> expectedParams = new HashMap<>();
setRandomMasterTimeout(req::setMasterTimeout, TimedRequest.DEFAULT_TIMEOUT, expectedParams);
setRandomTimeoutTimeValue(req::setTimeout, TimedRequest.DEFAULT_MASTER_TIMEOUT, expectedParams);

Request request = RequestConverters.deleteLifecycle(req);
assertEquals(request.getMethod(), HttpDelete.METHOD_NAME);
assertEquals(request.getEndpoint(), "/_ilm/" + lifecycleName);
assertEquals(request.getParameters(), expectedParams);
}

public void testSetIndexLifecyclePolicy() throws Exception {
SetIndexLifecyclePolicyRequest req = new SetIndexLifecyclePolicyRequest();
String policyName = randomAlphaOfLength(10);
Expand Down Expand Up @@ -2892,6 +2906,17 @@ private static void setRandomTimeout(Consumer<String> setter, TimeValue defaultT
}
}

private static void setRandomTimeoutTimeValue(Consumer<TimeValue> setter, TimeValue defaultTimeout,
Map<String, String> expectedParams) {
if (randomBoolean()) {
TimeValue timeout = TimeValue.parseTimeValue(randomTimeValue(), "random_timeout");
setter.accept(timeout);
expectedParams.put("timeout", timeout.getStringRep());
} else {
expectedParams.put("timeout", defaultTimeout.getStringRep());
}
}

private static void setRandomMasterTimeout(MasterNodeRequest<?> request, Map<String, String> expectedParams) {
if (randomBoolean()) {
String masterTimeout = randomTimeValue();
Expand All @@ -2902,6 +2927,16 @@ private static void setRandomMasterTimeout(MasterNodeRequest<?> request, Map<Str
}
}

private static void setRandomMasterTimeout(Consumer<TimeValue> setter, TimeValue defaultTimeout, Map<String, String> expectedParams) {
if (randomBoolean()) {
TimeValue masterTimeout = TimeValue.parseTimeValue(randomTimeValue(), "random_master_timeout");
setter.accept(masterTimeout);
expectedParams.put("master_timeout", masterTimeout.getStringRep());
} else {
expectedParams.put("master_timeout", defaultTimeout.getStringRep());
}
}

private static void setRandomWaitForActiveShards(Consumer<ActiveShardCount> setter, Map<String, String> expectedParams) {
setRandomWaitForActiveShards(setter, ActiveShardCount.DEFAULT, expectedParams);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.client.indexlifecycle;

import org.elasticsearch.test.ESTestCase;

public class DeleteLifecyclePolicyRequestTests extends ESTestCase {

private DeleteLifecyclePolicyRequest createTestInstance() {
return new DeleteLifecyclePolicyRequest(randomAlphaOfLengthBetween(2, 20));
}

public void testValidate() {
DeleteLifecyclePolicyRequest req = createTestInstance();
assertFalse(req.validate().isPresent());

}

public void testValidationFailure() {
try {
DeleteLifecyclePolicyRequest req = new DeleteLifecyclePolicyRequest(randomFrom("", null));
fail("should not be able to create a DeleteLifecyclePolicyRequest with null lifecycle name");
} catch (IllegalArgumentException exception) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the future, you can use

expectThrows(IllegalArgumentException.class, () -> new DeleteLifecyclePolicyRequest(randomFrom("", null)));

Instead of having to do the try/fail/catch check

// ok
}
}
}