Skip to content

Commit 910e974

Browse files
HLRC: Add ILM Retry (#33990)
* HLRC: Add ILM Retry * Relates #33100
1 parent 84ef915 commit 910e974

File tree

5 files changed

+145
-1
lines changed

5 files changed

+145
-1
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/IndexLifecycleClient.java

+29
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.elasticsearch.client.indexlifecycle.PutLifecyclePolicyRequest;
3030
import org.elasticsearch.client.indexlifecycle.ExplainLifecycleRequest;
3131
import org.elasticsearch.client.indexlifecycle.ExplainLifecycleResponse;
32+
import org.elasticsearch.client.indexlifecycle.RetryLifecyclePolicyRequest;
3233
import org.elasticsearch.client.indexlifecycle.RemoveIndexLifecyclePolicyRequest;
3334
import org.elasticsearch.client.indexlifecycle.RemoveIndexLifecyclePolicyResponse;
3435
import org.elasticsearch.client.indexlifecycle.SetIndexLifecyclePolicyRequest;
@@ -302,4 +303,32 @@ public void explainLifecycleAsync(ExplainLifecycleRequest request, RequestOption
302303
restHighLevelClient.performRequestAsyncAndParseEntity(request, RequestConverters::explainLifecycle, options,
303304
ExplainLifecycleResponse::fromXContent, listener, emptySet());
304305
}
306+
307+
/**
308+
* Retry lifecycle step for given indices
309+
* See <a href="https://fix-me-when-we-have-docs.com">
310+
* the docs</a> for more.
311+
* @param request the request
312+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
313+
* @return the response
314+
* @throws IOException in case there is a problem sending the request or parsing back the response
315+
*/
316+
public AcknowledgedResponse retryLifecycleStep(RetryLifecyclePolicyRequest request, RequestOptions options) throws IOException {
317+
return restHighLevelClient.performRequestAndParseEntity(request, RequestConverters::retryLifecycle, options,
318+
AcknowledgedResponse::fromXContent, emptySet());
319+
}
320+
321+
/**
322+
* Asynchronously retry the lifecycle step for given indices
323+
* See <a href="https://fix-me-when-we-have-docs.com">
324+
* the docs</a> for more.
325+
* @param request the request
326+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
327+
* @param listener the listener to be notified upon request completion
328+
*/
329+
public void retryLifecycleStepAsync(RetryLifecyclePolicyRequest request, RequestOptions options,
330+
ActionListener<AcknowledgedResponse> listener) {
331+
restHighLevelClient.performRequestAsyncAndParseEntity(request, RequestConverters::retryLifecycle, options,
332+
AcknowledgedResponse::fromXContent, listener, emptySet());
333+
}
305334
}

client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java

+21-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.elasticsearch.client;
2121

22+
import java.util.List;
2223
import org.apache.http.HttpEntity;
2324
import org.apache.http.client.methods.HttpDelete;
2425
import org.apache.http.client.methods.HttpGet;
@@ -54,6 +55,7 @@
5455
import org.elasticsearch.client.indexlifecycle.GetLifecyclePolicyRequest;
5556
import org.elasticsearch.client.indexlifecycle.LifecycleManagementStatusRequest;
5657
import org.elasticsearch.client.indexlifecycle.PutLifecyclePolicyRequest;
58+
import org.elasticsearch.client.indexlifecycle.RetryLifecyclePolicyRequest;
5759
import org.elasticsearch.client.indexlifecycle.RemoveIndexLifecyclePolicyRequest;
5860
import org.elasticsearch.client.indexlifecycle.SetIndexLifecyclePolicyRequest;
5961
import org.elasticsearch.client.indexlifecycle.StartILMRequest;
@@ -717,6 +719,19 @@ static Request explainLifecycle(ExplainLifecycleRequest explainLifecycleRequest)
717719
return request;
718720
}
719721

722+
static Request retryLifecycle(RetryLifecyclePolicyRequest retryLifecyclePolicyRequest) {
723+
Request request = new Request(HttpPost.METHOD_NAME,
724+
new EndpointBuilder()
725+
.addCommaSeparatedPathParts(retryLifecyclePolicyRequest.getIndices())
726+
.addPathPartAsIs("_ilm")
727+
.addPathPartAsIs("retry")
728+
.build());
729+
Params params = new Params(request);
730+
params.withMasterTimeout(retryLifecyclePolicyRequest.masterNodeTimeout());
731+
params.withTimeout(retryLifecyclePolicyRequest.timeout());
732+
return request;
733+
}
734+
720735
static HttpEntity createEntity(ToXContent toXContent, XContentType xContentType) throws IOException {
721736
BytesRef source = XContentHelper.toXContent(toXContent, xContentType, false).toBytesRef();
722737
return new ByteArrayEntity(source.bytes, source.offset, source.length, createContentType(xContentType));
@@ -1102,7 +1117,12 @@ EndpointBuilder addCommaSeparatedPathParts(String[] parts) {
11021117
return this;
11031118
}
11041119

1105-
EndpointBuilder addPathPartAsIs(String... parts) {
1120+
EndpointBuilder addCommaSeparatedPathParts(List<String> parts) {
1121+
addPathPart(String.join(",", parts));
1122+
return this;
1123+
}
1124+
1125+
EndpointBuilder addPathPartAsIs(String ... parts) {
11061126
for (String part : parts) {
11071127
if (Strings.hasLength(part)) {
11081128
joiner.add(part);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.client.indexlifecycle;
21+
22+
import java.util.Arrays;
23+
import java.util.List;
24+
import java.util.Objects;
25+
import org.elasticsearch.client.TimedRequest;
26+
27+
public class RetryLifecyclePolicyRequest extends TimedRequest {
28+
29+
private final List<String> indices;
30+
31+
public RetryLifecyclePolicyRequest(String... indices) {
32+
if (indices.length == 0) {
33+
throw new IllegalArgumentException("Must at least specify one index to retry");
34+
}
35+
this.indices = Arrays.asList(indices);
36+
}
37+
38+
public List<String> getIndices() {
39+
return indices;
40+
}
41+
42+
@Override
43+
public boolean equals(Object o) {
44+
if (this == o) {
45+
return true;
46+
}
47+
if (o == null || getClass() != o.getClass()) {
48+
return false;
49+
}
50+
RetryLifecyclePolicyRequest that = (RetryLifecyclePolicyRequest) o;
51+
return indices.size() == that.indices.size() && indices.containsAll(that.indices);
52+
}
53+
54+
@Override
55+
public int hashCode() {
56+
return Objects.hash(indices);
57+
}
58+
}

client/rest-high-level/src/test/java/org/elasticsearch/client/IndexLifecycleIT.java

+23
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.elasticsearch.client.indexlifecycle.Phase;
4242
import org.elasticsearch.client.indexlifecycle.PhaseExecutionInfo;
4343
import org.elasticsearch.client.indexlifecycle.PutLifecyclePolicyRequest;
44+
import org.elasticsearch.client.indexlifecycle.RetryLifecyclePolicyRequest;
4445
import org.elasticsearch.client.indexlifecycle.RemoveIndexLifecyclePolicyRequest;
4546
import org.elasticsearch.client.indexlifecycle.RemoveIndexLifecyclePolicyResponse;
4647
import org.elasticsearch.client.indexlifecycle.RolloverAction;
@@ -288,4 +289,26 @@ public void testGetMultipleLifecyclePolicies() throws IOException {
288289
.map(p -> ((LifecyclePolicyMetadata) p).getPolicy()).collect(Collectors.toList());
289290
assertThat(retrievedPolicies, hasItems(policies));
290291
}
292+
293+
public void testRetryLifecycleStep() throws IOException {
294+
String policyName = randomAlphaOfLength(10);
295+
LifecyclePolicy policy = createRandomPolicy(policyName);
296+
PutLifecyclePolicyRequest putRequest = new PutLifecyclePolicyRequest(policy);
297+
assertAcked(execute(putRequest, highLevelClient().indexLifecycle()::putLifecyclePolicy,
298+
highLevelClient().indexLifecycle()::putLifecyclePolicyAsync));
299+
createIndex("retry", Settings.builder().put("index.lifecycle.name", policy.getName()).build());
300+
RetryLifecyclePolicyRequest retryRequest = new RetryLifecyclePolicyRequest("retry");
301+
ElasticsearchStatusException ex = expectThrows(ElasticsearchStatusException.class,
302+
() -> execute(
303+
retryRequest, highLevelClient().indexLifecycle()::retryLifecycleStep,
304+
highLevelClient().indexLifecycle()::retryLifecycleStepAsync
305+
)
306+
);
307+
assertEquals(400, ex.status().getStatus());
308+
assertEquals(
309+
"Elasticsearch exception [type=illegal_argument_exception, reason=cannot retry an action for an index [retry]" +
310+
" that has not encountered an error when running a Lifecycle Policy]",
311+
ex.getRootCause().getMessage()
312+
);
313+
}
291314
}

client/rest-high-level/src/test/java/org/elasticsearch/client/RequestConvertersTests.java

+14
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import org.elasticsearch.client.indexlifecycle.LifecyclePolicy;
6161
import org.elasticsearch.client.indexlifecycle.PutLifecyclePolicyRequest;
6262
import org.elasticsearch.client.indexlifecycle.DeleteLifecyclePolicyRequest;
63+
import org.elasticsearch.client.indexlifecycle.RetryLifecyclePolicyRequest;
6364
import org.elasticsearch.client.indexlifecycle.RemoveIndexLifecyclePolicyRequest;
6465
import org.elasticsearch.common.CheckedBiConsumer;
6566
import org.elasticsearch.common.Strings;
@@ -1593,6 +1594,19 @@ public void testExplainLifecycle() throws Exception {
15931594
assertThat(request.getParameters(), equalTo(expectedParams));
15941595
}
15951596

1597+
public void testRetryLifecycle() throws Exception {
1598+
String[] indices = randomIndicesNames(1, 10);
1599+
RetryLifecyclePolicyRequest req = new RetryLifecyclePolicyRequest(indices);
1600+
Map<String, String> expectedParams = new HashMap<>();
1601+
setRandomMasterTimeout(req::setMasterTimeout, TimedRequest.DEFAULT_MASTER_NODE_TIMEOUT, expectedParams);
1602+
setRandomTimeoutTimeValue(req::setTimeout, TimedRequest.DEFAULT_ACK_TIMEOUT, expectedParams);
1603+
Request request = RequestConverters.retryLifecycle(req);
1604+
assertThat(request.getMethod(), equalTo(HttpPost.METHOD_NAME));
1605+
String idxString = Strings.arrayToCommaDelimitedString(indices);
1606+
assertThat(request.getEndpoint(), equalTo("/" + (idxString.isEmpty() ? "" : (idxString + "/")) + "_ilm/retry"));
1607+
assertThat(request.getParameters(), equalTo(expectedParams));
1608+
}
1609+
15961610
/**
15971611
* Randomize the {@link FetchSourceContext} request parameters.
15981612
*/

0 commit comments

Comments
 (0)