Skip to content

HLRC: Add remove index lifecycle policy #34204

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

Merged
merged 12 commits into from
Oct 16, 2018
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.elasticsearch.client.indexlifecycle.PutLifecyclePolicyRequest;
import org.elasticsearch.client.indexlifecycle.ExplainLifecycleRequest;
import org.elasticsearch.client.indexlifecycle.ExplainLifecycleResponse;
import org.elasticsearch.client.indexlifecycle.RemoveIndexLifecyclePolicyRequest;
import org.elasticsearch.client.indexlifecycle.RemoveIndexLifecyclePolicyResponse;
import org.elasticsearch.client.indexlifecycle.SetIndexLifecyclePolicyRequest;
import org.elasticsearch.client.indexlifecycle.SetIndexLifecyclePolicyResponse;
import org.elasticsearch.client.indexlifecycle.StartILMRequest;
Expand Down Expand Up @@ -161,6 +163,35 @@ public void setIndexLifecyclePolicyAsync(SetIndexLifecyclePolicyRequest request,
SetIndexLifecyclePolicyResponse::fromXContent, listener, emptySet());
}

/**
* Remove the index lifecycle policy for an index
* 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 RemoveIndexLifecyclePolicyResponse removeIndexLifecyclePolicy(RemoveIndexLifecyclePolicyRequest request,
RequestOptions options) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(request, RequestConverters::removeIndexLifecyclePolicy, options,
RemoveIndexLifecyclePolicyResponse::fromXContent, emptySet());
}

/**
* Asynchronously remove the index lifecycle policy for an index
* 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 removeIndexLifecyclePolicyAsync(RemoveIndexLifecyclePolicyRequest request, RequestOptions options,
ActionListener<RemoveIndexLifecyclePolicyResponse> listener) {
restHighLevelClient.performRequestAsyncAndParseEntity(request, RequestConverters::removeIndexLifecyclePolicy, options,
RemoveIndexLifecyclePolicyResponse::fromXContent, listener, emptySet());
}

/**
* Start the Index Lifecycle Management feature.
* 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 @@ -54,6 +54,7 @@
import org.elasticsearch.client.indexlifecycle.GetLifecyclePolicyRequest;
import org.elasticsearch.client.indexlifecycle.LifecycleManagementStatusRequest;
import org.elasticsearch.client.indexlifecycle.PutLifecyclePolicyRequest;
import org.elasticsearch.client.indexlifecycle.RemoveIndexLifecyclePolicyRequest;
import org.elasticsearch.client.indexlifecycle.SetIndexLifecyclePolicyRequest;
import org.elasticsearch.client.indexlifecycle.StartILMRequest;
import org.elasticsearch.client.indexlifecycle.StopILMRequest;
Expand Down Expand Up @@ -652,6 +653,20 @@ static Request setIndexLifecyclePolicy(SetIndexLifecyclePolicyRequest setPolicyR
return request;
}

static Request removeIndexLifecyclePolicy(RemoveIndexLifecyclePolicyRequest removePolicyRequest) {
String[] indices = removePolicyRequest.indices() == null ?
Strings.EMPTY_ARRAY : removePolicyRequest.indices().toArray(new String[] {});
Request request = new Request(HttpDelete.METHOD_NAME,
new EndpointBuilder()
.addCommaSeparatedPathParts(indices)
.addPathPartAsIs("_ilm")
.build());
Params params = new Params(request);
params.withIndicesOptions(removePolicyRequest.indicesOptions());
params.withMasterTimeout(removePolicyRequest.masterNodeTimeout());
return request;
}

static Request startILM(StartILMRequest startILMRequest) {
Request request = new Request(HttpPost.METHOD_NAME,
new EndpointBuilder()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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.action.support.IndicesOptions;
import org.elasticsearch.client.TimedRequest;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

public class RemoveIndexLifecyclePolicyRequest extends TimedRequest {

private final List<String> indices;
private final IndicesOptions indicesOptions;

public RemoveIndexLifecyclePolicyRequest(List<String> indices) {
this(indices, IndicesOptions.strictExpandOpen());
}

public RemoveIndexLifecyclePolicyRequest(List<String> indices, IndicesOptions indicesOptions) {
this.indices = Collections.unmodifiableList(Objects.requireNonNull(indices));
this.indicesOptions = Objects.requireNonNull(indicesOptions);
}

public List<String> indices() {
return indices;
}

public IndicesOptions indicesOptions() {
return indicesOptions;
}

@Override
public int hashCode() {
return Objects.hash(indices, indicesOptions);
}

@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
RemoveIndexLifecyclePolicyRequest other = (RemoveIndexLifecyclePolicyRequest) obj;
return Objects.deepEquals(indices, other.indices) &&
Objects.equals(indicesOptions, other.indicesOptions);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* 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.common.ParseField;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.XContentParser;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

public class RemoveIndexLifecyclePolicyResponse {

public static final ParseField HAS_FAILURES_FIELD = new ParseField("has_failures");
public static final ParseField FAILED_INDEXES_FIELD = new ParseField("failed_indexes");
@SuppressWarnings("unchecked")
public static final ConstructingObjectParser<RemoveIndexLifecyclePolicyResponse, Void> PARSER = new ConstructingObjectParser<>(
"change_policy_for_index_response", true, args -> new RemoveIndexLifecyclePolicyResponse((List<String>)args[0]));
static {
PARSER.declareStringArray(ConstructingObjectParser.constructorArg(), FAILED_INDEXES_FIELD);
// Needs to be declared but not used in constructing the response object
PARSER.declareBoolean(ConstructingObjectParser.constructorArg(), HAS_FAILURES_FIELD);
}

private final List<String> failedIndexes;

public RemoveIndexLifecyclePolicyResponse(List<String> failedIndexes) {
if (failedIndexes == null) {
throw new IllegalArgumentException(FAILED_INDEXES_FIELD.getPreferredName() + " cannot be null");
}
this.failedIndexes = Collections.unmodifiableList(failedIndexes);
}

public List<String> getFailedIndexes() {
return failedIndexes;
}

public boolean hasFailures() {
return failedIndexes.isEmpty() == false;
}

public static RemoveIndexLifecyclePolicyResponse fromXContent(XContentParser parser) {
return PARSER.apply(parser, null);
}

@Override
public int hashCode() {
return Objects.hash(failedIndexes);
}

@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
RemoveIndexLifecyclePolicyResponse other = (RemoveIndexLifecyclePolicyResponse) obj;
return Objects.equals(failedIndexes, other.failedIndexes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
import org.elasticsearch.client.indexlifecycle.Phase;
import org.elasticsearch.client.indexlifecycle.PhaseExecutionInfo;
import org.elasticsearch.client.indexlifecycle.PutLifecyclePolicyRequest;
import org.elasticsearch.client.indexlifecycle.RemoveIndexLifecyclePolicyRequest;
import org.elasticsearch.client.indexlifecycle.RemoveIndexLifecyclePolicyResponse;
import org.elasticsearch.client.indexlifecycle.RolloverAction;
import org.elasticsearch.client.indexlifecycle.SetIndexLifecyclePolicyRequest;
import org.elasticsearch.client.indexlifecycle.SetIndexLifecyclePolicyResponse;
Expand All @@ -52,6 +54,7 @@
import org.hamcrest.Matchers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -88,6 +91,44 @@ public void testSetIndexLifecyclePolicy() throws Exception {
assertThat(settingsResponse.getSetting("baz", "index.lifecycle.name"), equalTo(policyName));
}

public void testRemoveIndexLifecyclePolicy() throws Exception {
String policyName = randomAlphaOfLength(10);
LifecyclePolicy policy = createRandomPolicy(policyName);
PutLifecyclePolicyRequest putRequest = new PutLifecyclePolicyRequest(policy);
assertAcked(execute(putRequest, highLevelClient().indexLifecycle()::putLifecyclePolicy,
highLevelClient().indexLifecycle()::putLifecyclePolicyAsync));

createIndex("foo", Settings.builder().put("index.lifecycle.name", "bar").build());
createIndex("baz", Settings.builder().put("index.lifecycle.name", "eggplant").build());
createIndex("rbh", Settings.builder().put("index.lifecycle.name", "whatisthis").build());
SetIndexLifecyclePolicyRequest setReq = new SetIndexLifecyclePolicyRequest(policyName, "foo", "baz", "rbh");
SetIndexLifecyclePolicyResponse setResp = execute(setReq, highLevelClient().indexLifecycle()::setIndexLifecyclePolicy,
highLevelClient().indexLifecycle()::setIndexLifecyclePolicyAsync);
assertThat(setResp.hasFailures(), is(false));
assertThat(setResp.getFailedIndexes().isEmpty(), is(true));

GetSettingsRequest getSettingsRequest = new GetSettingsRequest().indices("foo", "baz", "rbh");
GetSettingsResponse settingsResponse = highLevelClient().indices().getSettings(getSettingsRequest, RequestOptions.DEFAULT);
assertThat(settingsResponse.getSetting("foo", "index.lifecycle.name"), equalTo(policyName));
assertThat(settingsResponse.getSetting("baz", "index.lifecycle.name"), equalTo(policyName));
assertThat(settingsResponse.getSetting("rbh", "index.lifecycle.name"), equalTo(policyName));

List<String> indices = new ArrayList<>();
indices.add("foo");
indices.add("rbh");
RemoveIndexLifecyclePolicyRequest removeReq = new RemoveIndexLifecyclePolicyRequest(indices);
RemoveIndexLifecyclePolicyResponse removeResp = execute(removeReq, highLevelClient().indexLifecycle()::removeIndexLifecyclePolicy,
highLevelClient().indexLifecycle()::removeIndexLifecyclePolicyAsync);
assertThat(removeResp.hasFailures(), is(false));
assertThat(removeResp.getFailedIndexes().isEmpty(), is(true));

getSettingsRequest = new GetSettingsRequest().indices("foo", "baz", "rbh");
settingsResponse = highLevelClient().indices().getSettings(getSettingsRequest, RequestOptions.DEFAULT);
assertNull(settingsResponse.getSetting("foo", "index.lifecycle.name"));
assertThat(settingsResponse.getSetting("baz", "index.lifecycle.name"), equalTo(policyName));
assertNull(settingsResponse.getSetting("rbh", "index.lifecycle.name"));
}

public void testStartStopILM() throws Exception {
String policyName = randomAlphaOfLength(10);
LifecyclePolicy policy = createRandomPolicy(policyName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import org.elasticsearch.client.indexlifecycle.LifecyclePolicy;
import org.elasticsearch.client.indexlifecycle.PutLifecyclePolicyRequest;
import org.elasticsearch.client.indexlifecycle.DeleteLifecyclePolicyRequest;
import org.elasticsearch.client.indexlifecycle.RemoveIndexLifecyclePolicyRequest;
import org.elasticsearch.common.CheckedBiConsumer;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesArray;
Expand Down Expand Up @@ -1527,6 +1528,20 @@ public void testSetIndexLifecyclePolicy() throws Exception {
assertThat(request.getParameters(), equalTo(expectedParams));
}

public void testRemoveIndexLifecyclePolicy() {
Map<String, String> expectedParams = new HashMap<>();
String[] indices = randomIndicesNames(0, 10);
IndicesOptions indicesOptions = setRandomIndicesOptions(IndicesOptions.strictExpandOpen(), expectedParams);
RemoveIndexLifecyclePolicyRequest req = new RemoveIndexLifecyclePolicyRequest(Arrays.asList(indices), indicesOptions);
setRandomMasterTimeout(req::setMasterTimeout, TimedRequest.DEFAULT_TIMEOUT, expectedParams);

Request request = RequestConverters.removeIndexLifecyclePolicy(req);
assertThat(request.getMethod(), equalTo(HttpDelete.METHOD_NAME));
String idxString = Strings.arrayToCommaDelimitedString(indices);
assertThat(request.getEndpoint(), equalTo("/" + (idxString.isEmpty() ? "" : (idxString + "/")) + "_ilm"));
assertThat(request.getParameters(), equalTo(expectedParams));
}

public void testStartILM() throws Exception {
StartILMRequest req = new StartILMRequest();
Map<String, String> expectedParams = new HashMap<>();
Expand Down Expand Up @@ -1659,6 +1674,24 @@ static void setRandomIndicesOptions(Consumer<IndicesOptions> setter, Supplier<In
}
}

static IndicesOptions setRandomIndicesOptions(IndicesOptions indicesOptions, Map<String, String> expectedParams) {
if (randomBoolean()) {
indicesOptions = IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean());
}
expectedParams.put("ignore_unavailable", Boolean.toString(indicesOptions.ignoreUnavailable()));
expectedParams.put("allow_no_indices", Boolean.toString(indicesOptions.allowNoIndices()));
if (indicesOptions.expandWildcardsOpen() && indicesOptions.expandWildcardsClosed()) {
expectedParams.put("expand_wildcards", "open,closed");
} else if (indicesOptions.expandWildcardsOpen()) {
expectedParams.put("expand_wildcards", "open");
} else if (indicesOptions.expandWildcardsClosed()) {
expectedParams.put("expand_wildcards", "closed");
} else {
expectedParams.put("expand_wildcards", "none");
}
return indicesOptions;
}

static void setRandomIncludeDefaults(GetIndexRequest request, Map<String, String> expectedParams) {
if (randomBoolean()) {
boolean includeDefaults = randomBoolean();
Expand Down
Loading