Skip to content

HLRC: Add ILM Retry #33990

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.elasticsearch.client.indexlifecycle.PutLifecyclePolicyRequest;
import org.elasticsearch.client.indexlifecycle.ExplainLifecycleRequest;
import org.elasticsearch.client.indexlifecycle.ExplainLifecycleResponse;
import org.elasticsearch.client.indexlifecycle.RetryLifecyclePolicyRequest;
import org.elasticsearch.client.indexlifecycle.RemoveIndexLifecyclePolicyRequest;
import org.elasticsearch.client.indexlifecycle.RemoveIndexLifecyclePolicyResponse;
import org.elasticsearch.client.indexlifecycle.SetIndexLifecyclePolicyRequest;
Expand Down Expand Up @@ -302,4 +303,32 @@ public void explainLifecycleAsync(ExplainLifecycleRequest request, RequestOption
restHighLevelClient.performRequestAsyncAndParseEntity(request, RequestConverters::explainLifecycle, options,
ExplainLifecycleResponse::fromXContent, listener, emptySet());
}

/**
* Retry lifecycle step for given indices
* 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 retryLifecycleStep(RetryLifecyclePolicyRequest request, RequestOptions options) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(request, RequestConverters::retryLifecycle, options,
AcknowledgedResponse::fromXContent, emptySet());
}

/**
* Asynchronously retry the lifecycle step for given indices
* 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 retryLifecycleStepAsync(RetryLifecyclePolicyRequest request, RequestOptions options,
ActionListener<AcknowledgedResponse> listener) {
restHighLevelClient.performRequestAsyncAndParseEntity(request, RequestConverters::retryLifecycle, options,
AcknowledgedResponse::fromXContent, listener, emptySet());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.elasticsearch.client;

import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
Expand Down Expand Up @@ -54,6 +55,7 @@
import org.elasticsearch.client.indexlifecycle.GetLifecyclePolicyRequest;
import org.elasticsearch.client.indexlifecycle.LifecycleManagementStatusRequest;
import org.elasticsearch.client.indexlifecycle.PutLifecyclePolicyRequest;
import org.elasticsearch.client.indexlifecycle.RetryLifecyclePolicyRequest;
import org.elasticsearch.client.indexlifecycle.RemoveIndexLifecyclePolicyRequest;
import org.elasticsearch.client.indexlifecycle.SetIndexLifecyclePolicyRequest;
import org.elasticsearch.client.indexlifecycle.StartILMRequest;
Expand Down Expand Up @@ -717,6 +719,19 @@ static Request explainLifecycle(ExplainLifecycleRequest explainLifecycleRequest)
return request;
}

static Request retryLifecycle(RetryLifecyclePolicyRequest retryLifecyclePolicyRequest) {
Copy link
Contributor

Choose a reason for hiding this comment

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

These should be in some IndexLifecycleRequestConverters class. i dont think it exists yet.

Copy link
Contributor

@gwbrown gwbrown Sep 28, 2018

Choose a reason for hiding this comment

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

Yeah, ILM stuff hasn't been moved to a separate RequestConverters class yet. I'd say this is okay for this PR since we'll do a mass move at some point before merging anyway.

Copy link
Contributor

Choose a reason for hiding this comment

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

works for me

Request request = new Request(HttpPost.METHOD_NAME,
new EndpointBuilder()
.addCommaSeparatedPathParts(retryLifecyclePolicyRequest.getIndices())
.addPathPartAsIs("_ilm")
.addPathPartAsIs("retry")
.build());
Params params = new Params(request);
params.withMasterTimeout(retryLifecyclePolicyRequest.masterNodeTimeout());
params.withTimeout(retryLifecyclePolicyRequest.timeout());
return request;
}

static HttpEntity createEntity(ToXContent toXContent, XContentType xContentType) throws IOException {
BytesRef source = XContentHelper.toXContent(toXContent, xContentType, false).toBytesRef();
return new ByteArrayEntity(source.bytes, source.offset, source.length, createContentType(xContentType));
Expand Down Expand Up @@ -1102,7 +1117,12 @@ EndpointBuilder addCommaSeparatedPathParts(String[] parts) {
return this;
}

EndpointBuilder addPathPartAsIs(String... parts) {
EndpointBuilder addCommaSeparatedPathParts(List<String> parts) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

I just added this to save a toArray() call here, seemed cleaner to just add that API, but I don't have a strong opinion here :)

addPathPart(String.join(",", parts));
return this;
}

EndpointBuilder addPathPartAsIs(String ... parts) {
for (String part : parts) {
if (Strings.hasLength(part)) {
joiner.add(part);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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 java.util.Arrays;
import java.util.List;
import java.util.Objects;
import org.elasticsearch.client.TimedRequest;

public class RetryLifecyclePolicyRequest extends TimedRequest {

private final List<String> indices;

public RetryLifecyclePolicyRequest(String... indices) {
if (indices.length == 0) {
throw new IllegalArgumentException("Must at least specify one index to retry");
}
this.indices = Arrays.asList(indices);
}

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

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
RetryLifecyclePolicyRequest that = (RetryLifecyclePolicyRequest) o;
return indices.size() == that.indices.size() && indices.containsAll(that.indices);
}

@Override
public int hashCode() {
return Objects.hash(indices);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.elasticsearch.client.indexlifecycle.Phase;
import org.elasticsearch.client.indexlifecycle.PhaseExecutionInfo;
import org.elasticsearch.client.indexlifecycle.PutLifecyclePolicyRequest;
import org.elasticsearch.client.indexlifecycle.RetryLifecyclePolicyRequest;
import org.elasticsearch.client.indexlifecycle.RemoveIndexLifecyclePolicyRequest;
import org.elasticsearch.client.indexlifecycle.RemoveIndexLifecyclePolicyResponse;
import org.elasticsearch.client.indexlifecycle.RolloverAction;
Expand Down Expand Up @@ -288,4 +289,26 @@ public void testGetMultipleLifecyclePolicies() throws IOException {
.map(p -> ((LifecyclePolicyMetadata) p).getPolicy()).collect(Collectors.toList());
assertThat(retrievedPolicies, hasItems(policies));
}

public void testRetryLifecycleStep() throws IOException {
String policyName = randomAlphaOfLength(10);
LifecyclePolicy policy = createRandomPolicy(policyName);
PutLifecyclePolicyRequest putRequest = new PutLifecyclePolicyRequest(policy);
assertAcked(execute(putRequest, highLevelClient().indexLifecycle()::putLifecyclePolicy,
highLevelClient().indexLifecycle()::putLifecyclePolicyAsync));
createIndex("retry", Settings.builder().put("index.lifecycle.name", policy.getName()).build());
RetryLifecyclePolicyRequest retryRequest = new RetryLifecyclePolicyRequest("retry");
ElasticsearchStatusException ex = expectThrows(ElasticsearchStatusException.class,
() -> execute(
retryRequest, highLevelClient().indexLifecycle()::retryLifecycleStep,
highLevelClient().indexLifecycle()::retryLifecycleStepAsync
)
);
assertEquals(400, ex.status().getStatus());
assertEquals(
"Elasticsearch exception [type=illegal_argument_exception, reason=cannot retry an action for an index [retry]" +
" that has not encountered an error when running a Lifecycle Policy]",
ex.getRootCause().getMessage()
);
}
}
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.RetryLifecyclePolicyRequest;
import org.elasticsearch.client.indexlifecycle.RemoveIndexLifecyclePolicyRequest;
import org.elasticsearch.common.CheckedBiConsumer;
import org.elasticsearch.common.Strings;
Expand Down Expand Up @@ -1593,6 +1594,19 @@ public void testExplainLifecycle() throws Exception {
assertThat(request.getParameters(), equalTo(expectedParams));
}

public void testRetryLifecycle() throws Exception {
String[] indices = randomIndicesNames(1, 10);
RetryLifecyclePolicyRequest req = new RetryLifecyclePolicyRequest(indices);
Map<String, String> expectedParams = new HashMap<>();
setRandomMasterTimeout(req::setMasterTimeout, TimedRequest.DEFAULT_MASTER_NODE_TIMEOUT, expectedParams);
setRandomTimeoutTimeValue(req::setTimeout, TimedRequest.DEFAULT_ACK_TIMEOUT, expectedParams);
Request request = RequestConverters.retryLifecycle(req);
assertThat(request.getMethod(), equalTo(HttpPost.METHOD_NAME));
String idxString = Strings.arrayToCommaDelimitedString(indices);
assertThat(request.getEndpoint(), equalTo("/" + (idxString.isEmpty() ? "" : (idxString + "/")) + "_ilm/retry"));
assertThat(request.getParameters(), equalTo(expectedParams));
}

/**
* Randomize the {@link FetchSourceContext} request parameters.
*/
Expand Down