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.DeleteLifecycleRequest;
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 deleteLifecycle(DeleteLifecycleRequest 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 deleteLifecycleAsync(DeleteLifecycleRequest 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
@@ -0,0 +1,49 @@
/*
* 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;

import org.elasticsearch.common.unit.TimeValue;

// TODO: This class exists as a stopgap to allow progress in the new HLRC structure.
// It should be replaced with a version of MasterNodeRequest that will likely be
// created when everything is moved from o.e.xpack.protocol to o.e.client.
public abstract class MasterTimeoutRequest<Request> implements Validatable {

public static final TimeValue DEFAULT_MASTER_NODE_TIMEOUT = TimeValue.timeValueSeconds(30);

protected TimeValue timeout = DEFAULT_MASTER_NODE_TIMEOUT;

@SuppressWarnings("unchecked")
public Request masterNodeTimeout(TimeValue timeout) {
this.timeout = timeout;
return (Request) this;
}

/**
* A timeout value in case the master has not been discovered yet or disconnected.
*/
public final Request masterNodeTimeout(String timeout) {
return masterNodeTimeout(TimeValue.parseTimeValue(timeout, null, getClass().getSimpleName() + ".masterNodeTimeout"));
}

public TimeValue masterNodeTimeout() {
return timeout;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
import org.elasticsearch.index.rankeval.RankEvalRequest;
import org.elasticsearch.protocol.xpack.XPackInfoRequest;
import org.elasticsearch.protocol.xpack.XPackUsageRequest;
import org.elasticsearch.client.indexlifecycle.DeleteLifecycleRequest;
import org.elasticsearch.protocol.xpack.license.DeleteLicenseRequest;
import org.elasticsearch.protocol.xpack.indexlifecycle.ExplainLifecycleRequest;
import org.elasticsearch.protocol.xpack.indexlifecycle.SetIndexLifecyclePolicyRequest;
Expand Down Expand Up @@ -1174,6 +1175,17 @@ static Request xpackUsage(XPackUsageRequest usageRequest) {
return request;
}

static Request deleteLifecycle(DeleteLifecycleRequest deleteLifecycleRequest) {
Request request = new Request(HttpDelete.METHOD_NAME,
new EndpointBuilder()
.addPathPartAsIs("_ilm")
.addPathPartAsIs(deleteLifecycleRequest.getLifecycle())
.build());
Params params = new Params(request);
params.withMasterTimeout(deleteLifecycleRequest.masterNodeTimeout());
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
@@ -0,0 +1,55 @@
/*
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be DeleteIndexLifecycleRequest ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

From what I saw, the classes with names including IndexLifecycle seem to relate to index<->lifecycle policy relationships (i.e. lifecycle A applies to index B), but ones with just Lifecycle relate to the management of lifecycles. If that's correct, this class is named correctly.

@dakrone, can you confirm?

Copy link
Member

Choose a reason for hiding this comment

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

I think this should be DeleteLifecyclePolicyRequest to indicate that the policy is going to be removed, and then DeleteIndexLifecycleRequest is for when we are removing a lifecycle from an index

* 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.MasterTimeoutRequest;

import java.util.Objects;

public class DeleteLifecycleRequest extends MasterTimeoutRequest<DeleteLifecycleRequest> {
Copy link
Contributor

Choose a reason for hiding this comment

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

The transport based APIs extend AcknowledgedRequest which has a timeout and ackTimeout in addition to the masterNodeTimeout.

(this does not) Curious if is this is intentional ?

The javadoc for AcknowledgedRequest states Facilitates consistency across different api. ... is that something we should carry forward while still divorcing ourselves from the transport request objects ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I based MasterTimeoutRequest on MasterNodeRequest, rather than AcknowledgedRequest, which doesn't have an ackTimeout. It's possible I should be using AcknowledgedRequest instead - the difference wasn't immediately clear.

So, to answer your question: It was intentional, but it's possible I intended the wrong thing.

Copy link
Contributor Author

@gwbrown gwbrown Aug 27, 2018

Choose a reason for hiding this comment

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

I talked to @hub-cap on Slack and his answer was that these timeouts should be based on the parameters that the requests can take. To determine this, there should be a Rest<whatever>Action class which defines those parameters. In this case, RestDeleteLifecycleAction (see here) defines both timeout and master_timeout, so this class should include both.

(Just waiting on some more feedback to make this change)

Copy link
Contributor

Choose a reason for hiding this comment

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

@gwbrown Thanks for following up. I am working a similar issue and will follow your lead here.


private final String lifecycle;

public DeleteLifecycleRequest(String lifecycle) {
this.lifecycle = Objects.requireNonNull(lifecycle, "lifecycle cannot be null");
}

public String getLifecycle() {
return lifecycle;
}


@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
DeleteLifecycleRequest that = (DeleteLifecycleRequest) 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 @@ -27,6 +27,7 @@
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.client.indexlifecycle.DeleteLifecycleRequest;
import org.elasticsearch.protocol.xpack.indexlifecycle.ExplainLifecycleRequest;
import org.elasticsearch.protocol.xpack.indexlifecycle.ExplainLifecycleResponse;
import org.elasticsearch.protocol.xpack.indexlifecycle.IndexLifecycleExplainResponse;
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);

DeleteLifecycleRequest deleteRequest = new DeleteLifecycleRequest(policy);
assertAcked(execute(deleteRequest, highLevelClient().indexLifecycle()::deleteLifecycle,
highLevelClient().indexLifecycle()::deleteLifecycleAsync));

// 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 @@ -127,6 +127,7 @@
import org.elasticsearch.index.rankeval.RatedRequest;
import org.elasticsearch.index.rankeval.RestRankEvalAction;
import org.elasticsearch.protocol.xpack.XPackInfoRequest;
import org.elasticsearch.client.indexlifecycle.DeleteLifecycleRequest;
import org.elasticsearch.protocol.xpack.indexlifecycle.ExplainLifecycleRequest;
import org.elasticsearch.protocol.xpack.indexlifecycle.SetIndexLifecyclePolicyRequest;
import org.elasticsearch.protocol.xpack.indexlifecycle.StartILMRequest;
Expand Down Expand Up @@ -2635,6 +2636,18 @@ public void testGraphExplore() throws Exception {
assertToXContentBody(graphExploreRequest, request.getEntity());
}

public void testDeleteLifecycle() {
String lifecycleName = randomAlphaOfLengthBetween(2,20);
DeleteLifecycleRequest req = new DeleteLifecycleRequest(lifecycleName);
Map<String, String> expectedParams = new HashMap<>();
setRandomMasterTimeout(req, 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 @@ -2839,6 +2852,16 @@ private static void setRandomMasterTimeout(MasterNodeRequest<?> request, Map<Str
}
}

private static void setRandomMasterTimeout(MasterTimeoutRequest<?> request, Map<String, String> expectedParams) {
if (randomBoolean()) {
String masterTimeout = randomTimeValue();
request.masterNodeTimeout(masterTimeout);
expectedParams.put("master_timeout", masterTimeout);
} else {
expectedParams.put("master_timeout", MasterNodeRequest.DEFAULT_MASTER_NODE_TIMEOUT.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 DeleteLifecycleRequestTests extends ESTestCase {

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

public void testValidate() {
DeleteLifecycleRequest req = createTestInstance();
assertTrue(req.validate() == null || req.validate().validationErrors().size() == 0);

}

public void testValidationFailure() {
try {
DeleteLifecycleRequest req = new DeleteLifecycleRequest(null);
fail("should not be able to create a DeleteLifecycleRequest with null lifecycle name");
} catch (NullPointerException exception) {
// ok
}
}
}