Skip to content

Commit 4b08bea

Browse files
authored
Move get lifecycle API to Management thread pool and make cancellable (#97248)
* Move get lifecycle API to Management thread pool and make cancellable This commit moves the `TransportGetLifecycleAction` to execute on the `Management` thread pool, and to be cancellable so that wildcard requests looping through all or a subset of policies checks for cancellation while running. Resolves #96568
1 parent f5a2af6 commit 4b08bea

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

docs/changelog/97248.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 97248
2+
summary: Move get lifecycle API to Management thread pool and make cancellable
3+
area: ILM+SLM
4+
type: enhancement
5+
issues:
6+
- 96568

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/action/GetLifecycleAction.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,17 @@
1616
import org.elasticsearch.common.io.stream.StreamInput;
1717
import org.elasticsearch.common.io.stream.StreamOutput;
1818
import org.elasticsearch.common.io.stream.Writeable;
19+
import org.elasticsearch.tasks.CancellableTask;
20+
import org.elasticsearch.tasks.Task;
21+
import org.elasticsearch.tasks.TaskId;
1922
import org.elasticsearch.xcontent.ToXContentObject;
2023
import org.elasticsearch.xcontent.XContentBuilder;
2124
import org.elasticsearch.xpack.core.ilm.LifecyclePolicy;
2225

2326
import java.io.IOException;
2427
import java.util.Arrays;
2528
import java.util.List;
29+
import java.util.Map;
2630
import java.util.Objects;
2731

2832
public class GetLifecycleAction extends ActionType<GetLifecycleAction.Response> {
@@ -113,6 +117,11 @@ public Request() {
113117
policyNames = Strings.EMPTY_ARRAY;
114118
}
115119

120+
@Override
121+
public Task createTask(long id, String type, String action, TaskId parentTaskId, Map<String, String> headers) {
122+
return new CancellableTask(id, type, action, "get-lifecycle-task", parentTaskId, headers);
123+
}
124+
116125
public String[] getPolicyNames() {
117126
return policyNames;
118127
}

x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/RestGetLifecycleAction.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.elasticsearch.common.Strings;
1212
import org.elasticsearch.rest.BaseRestHandler;
1313
import org.elasticsearch.rest.RestRequest;
14+
import org.elasticsearch.rest.action.RestCancellableNodeClient;
1415
import org.elasticsearch.rest.action.RestToXContentListener;
1516
import org.elasticsearch.xpack.core.ilm.action.GetLifecycleAction;
1617

@@ -37,6 +38,10 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient
3738
getLifecycleRequest.timeout(restRequest.paramAsTime("timeout", getLifecycleRequest.timeout()));
3839
getLifecycleRequest.masterNodeTimeout(restRequest.paramAsTime("master_timeout", getLifecycleRequest.masterNodeTimeout()));
3940

40-
return channel -> client.execute(GetLifecycleAction.INSTANCE, getLifecycleRequest, new RestToXContentListener<>(channel));
41+
return channel -> new RestCancellableNodeClient(client, restRequest.getHttpChannel()).execute(
42+
GetLifecycleAction.INSTANCE,
43+
getLifecycleRequest,
44+
new RestToXContentListener<>(channel)
45+
);
4146
}
4247
}

x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/ilm/action/TransportGetLifecycleAction.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.elasticsearch.cluster.service.ClusterService;
1919
import org.elasticsearch.common.inject.Inject;
2020
import org.elasticsearch.common.regex.Regex;
21+
import org.elasticsearch.tasks.CancellableTask;
2122
import org.elasticsearch.tasks.Task;
2223
import org.elasticsearch.threadpool.ThreadPool;
2324
import org.elasticsearch.transport.TransportService;
@@ -55,12 +56,18 @@ public TransportGetLifecycleAction(
5556
Request::new,
5657
indexNameExpressionResolver,
5758
Response::new,
58-
ThreadPool.Names.SAME
59+
ThreadPool.Names.MANAGEMENT
5960
);
6061
}
6162

6263
@Override
6364
protected void masterOperation(Task task, Request request, ClusterState state, ActionListener<Response> listener) {
65+
assert task instanceof CancellableTask : "get lifecycle requests should be cancellable";
66+
final CancellableTask cancellableTask = (CancellableTask) task;
67+
if (cancellableTask.notifyIfCancelled(listener)) {
68+
return;
69+
}
70+
6471
IndexLifecycleMetadata metadata = clusterService.state().metadata().custom(IndexLifecycleMetadata.TYPE);
6572
if (metadata == null) {
6673
if (request.getPolicyNames().length == 0) {
@@ -88,6 +95,9 @@ protected void masterOperation(Task task, Request request, ClusterState state, A
8895
for (String name : names) {
8996
if (Regex.isSimpleMatchPattern(name)) {
9097
for (Map.Entry<String, LifecyclePolicyMetadata> entry : metadata.getPolicyMetadatas().entrySet()) {
98+
if (cancellableTask.notifyIfCancelled(listener)) {
99+
return;
100+
}
91101
LifecyclePolicyMetadata policyMetadata = entry.getValue();
92102
if (Regex.simpleMatch(name, entry.getKey())) {
93103
policyResponseItemMap.put(

0 commit comments

Comments
 (0)