Skip to content

Commit d1ce796

Browse files
nielsbaumangeorgewallace
authored andcommitted
Run TransportExplainLifecycleAction on local node (elastic#122885)
This action solely needs the cluster state, it can run on any node. Additionally, it needs to be cancellable to avoid doing unnecessary work after a client failure or timeout. Relates elastic#101805
1 parent 18b002a commit d1ce796

File tree

8 files changed

+151
-255
lines changed

8 files changed

+151
-255
lines changed

docs/changelog/122885.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 122885
2+
summary: Run `TransportExplainLifecycleAction` on local node
3+
area: ILM+SLM
4+
type: enhancement
5+
issues: []

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

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,22 @@
77

88
package org.elasticsearch.xpack.core.ilm;
99

10+
import org.elasticsearch.TransportVersions;
1011
import org.elasticsearch.action.ActionRequestValidationException;
12+
import org.elasticsearch.action.IndicesRequest;
1113
import org.elasticsearch.action.support.IndicesOptions;
12-
import org.elasticsearch.action.support.master.info.ClusterInfoRequest;
14+
import org.elasticsearch.action.support.local.LocalClusterStateRequest;
15+
import org.elasticsearch.common.Strings;
1316
import org.elasticsearch.common.io.stream.StreamInput;
14-
import org.elasticsearch.common.io.stream.StreamOutput;
1517
import org.elasticsearch.core.TimeValue;
18+
import org.elasticsearch.core.UpdateForV10;
19+
import org.elasticsearch.tasks.CancellableTask;
20+
import org.elasticsearch.tasks.Task;
21+
import org.elasticsearch.tasks.TaskId;
1622

1723
import java.io.IOException;
1824
import java.util.Arrays;
25+
import java.util.Map;
1926
import java.util.Objects;
2027

2128
/**
@@ -24,26 +31,58 @@
2431
* Multiple indices may be queried in the same request using the
2532
* {@link #indices(String...)} method
2633
*/
27-
public class ExplainLifecycleRequest extends ClusterInfoRequest<ExplainLifecycleRequest> {
34+
public class ExplainLifecycleRequest extends LocalClusterStateRequest implements IndicesRequest.Replaceable {
2835

36+
private String[] indices = Strings.EMPTY_ARRAY;
37+
private IndicesOptions indicesOptions;
2938
private boolean onlyErrors = false;
3039
private boolean onlyManaged = false;
3140

3241
public ExplainLifecycleRequest(TimeValue masterTimeout) {
33-
super(masterTimeout, IndicesOptions.strictExpandOpen());
42+
super(masterTimeout);
43+
indicesOptions = IndicesOptions.strictExpandOpen();
3444
}
3545

46+
/**
47+
* NB prior to 9.0 this was a TransportMasterNodeReadAction so for BwC we must remain able to read these requests until
48+
* we no longer need to support calling this action remotely.
49+
*/
50+
@UpdateForV10(owner = UpdateForV10.Owner.DATA_MANAGEMENT)
3651
public ExplainLifecycleRequest(StreamInput in) throws IOException {
3752
super(in);
53+
indices = in.readStringArray();
54+
if (in.getTransportVersion().before(TransportVersions.V_8_0_0)) {
55+
in.readStringArray();
56+
}
57+
indicesOptions = IndicesOptions.readIndicesOptions(in);
3858
onlyErrors = in.readBoolean();
3959
onlyManaged = in.readBoolean();
4060
}
4161

4262
@Override
43-
public void writeTo(StreamOutput out) throws IOException {
44-
super.writeTo(out);
45-
out.writeBoolean(onlyErrors);
46-
out.writeBoolean(onlyManaged);
63+
public ExplainLifecycleRequest indices(String... indices) {
64+
this.indices = indices;
65+
return this;
66+
}
67+
68+
public ExplainLifecycleRequest indicesOptions(IndicesOptions indicesOptions) {
69+
this.indicesOptions = indicesOptions;
70+
return this;
71+
}
72+
73+
@Override
74+
public String[] indices() {
75+
return indices;
76+
}
77+
78+
@Override
79+
public IndicesOptions indicesOptions() {
80+
return indicesOptions;
81+
}
82+
83+
@Override
84+
public boolean includeDataStreams() {
85+
return true;
4786
}
4887

4988
public boolean onlyErrors() {
@@ -69,6 +108,11 @@ public ActionRequestValidationException validate() {
69108
return null;
70109
}
71110

111+
@Override
112+
public Task createTask(long id, String type, String action, TaskId parentTaskId, Map<String, String> headers) {
113+
return new CancellableTask(id, type, action, "", parentTaskId, headers);
114+
}
115+
72116
@Override
73117
public int hashCode() {
74118
return Objects.hash(Arrays.hashCode(indices()), indicesOptions(), onlyErrors, onlyManaged);

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

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99

1010
import org.elasticsearch.action.ActionResponse;
1111
import org.elasticsearch.common.Strings;
12-
import org.elasticsearch.common.io.stream.StreamInput;
1312
import org.elasticsearch.common.io.stream.StreamOutput;
14-
import org.elasticsearch.common.util.Maps;
13+
import org.elasticsearch.core.UpdateForV10;
1514
import org.elasticsearch.xcontent.ParseField;
1615
import org.elasticsearch.xcontent.ToXContentObject;
1716
import org.elasticsearch.xcontent.XContentBuilder;
@@ -32,17 +31,6 @@ public class ExplainLifecycleResponse extends ActionResponse implements ToXConte
3231

3332
private final Map<String, IndexLifecycleExplainResponse> indexResponses;
3433

35-
public ExplainLifecycleResponse(StreamInput in) throws IOException {
36-
super(in);
37-
int size = in.readVInt();
38-
Map<String, IndexLifecycleExplainResponse> indexResponses = Maps.newMapWithExpectedSize(size);
39-
for (int i = 0; i < size; i++) {
40-
IndexLifecycleExplainResponse indexResponse = new IndexLifecycleExplainResponse(in);
41-
indexResponses.put(indexResponse.getIndex(), indexResponse);
42-
}
43-
this.indexResponses = indexResponses;
44-
}
45-
4634
public ExplainLifecycleResponse(Map<String, IndexLifecycleExplainResponse> indexResponses) {
4735
this.indexResponses = indexResponses;
4836
}
@@ -69,6 +57,11 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
6957
return builder;
7058
}
7159

60+
/**
61+
* NB prior to 9.0 this was a TransportMasterNodeReadAction so for BwC we must remain able to write these responses until
62+
* we no longer need to support calling this action remotely.
63+
*/
64+
@UpdateForV10(owner = UpdateForV10.Owner.DATA_MANAGEMENT)
7265
@Override
7366
public void writeTo(StreamOutput out) throws IOException {
7467
out.writeCollection(indexResponses.values());

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ExplainLifecycleRequestTests.java

Lines changed: 0 additions & 87 deletions
This file was deleted.

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ExplainLifecycleResponseTests.java

Lines changed: 0 additions & 96 deletions
This file was deleted.

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.elasticsearch.common.Strings;
1313
import org.elasticsearch.rest.BaseRestHandler;
1414
import org.elasticsearch.rest.RestRequest;
15+
import org.elasticsearch.rest.action.RestCancellableNodeClient;
1516
import org.elasticsearch.rest.action.RestToXContentListener;
1617
import org.elasticsearch.xpack.core.ilm.ExplainLifecycleRequest;
1718
import org.elasticsearch.xpack.core.ilm.action.ExplainLifecycleAction;
@@ -41,6 +42,10 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient
4142
explainLifecycleRequest.indicesOptions(IndicesOptions.fromRequest(restRequest, IndicesOptions.strictExpandOpen()));
4243
explainLifecycleRequest.onlyManaged(restRequest.paramAsBoolean("only_managed", false));
4344
explainLifecycleRequest.onlyErrors(restRequest.paramAsBoolean("only_errors", false));
44-
return channel -> client.execute(ExplainLifecycleAction.INSTANCE, explainLifecycleRequest, new RestToXContentListener<>(channel));
45+
return channel -> new RestCancellableNodeClient(client, restRequest.getHttpChannel()).execute(
46+
ExplainLifecycleAction.INSTANCE,
47+
explainLifecycleRequest,
48+
new RestToXContentListener<>(channel)
49+
);
4550
}
4651
}

0 commit comments

Comments
 (0)