Skip to content

Commit 509a120

Browse files
authored
Run TransportGetLifecycleAction on local node (#126002)
This action solely needs the cluster state, it can run on any node. Relates #101805
1 parent fcd7c9f commit 509a120

File tree

8 files changed

+74
-132
lines changed

8 files changed

+74
-132
lines changed

docs/changelog/126002.yaml

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

server/src/main/java/org/elasticsearch/action/support/local/LocalClusterStateRequest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import org.elasticsearch.TransportVersions;
1313
import org.elasticsearch.action.ActionRequest;
14+
import org.elasticsearch.action.ActionRequestValidationException;
1415
import org.elasticsearch.action.support.TransportAction;
1516
import org.elasticsearch.common.io.stream.StreamInput;
1617
import org.elasticsearch.common.io.stream.StreamOutput;
@@ -65,6 +66,11 @@ public final void writeTo(StreamOutput out) throws IOException {
6566
TransportAction.localOnly();
6667
}
6768

69+
@Override
70+
public ActionRequestValidationException validate() {
71+
return null;
72+
}
73+
6874
public TimeValue masterTimeout() {
6975
return masterTimeout;
7076
}

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

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import org.elasticsearch.action.ActionResponse;
1111
import org.elasticsearch.action.ActionType;
12-
import org.elasticsearch.action.support.master.AcknowledgedRequest;
12+
import org.elasticsearch.action.support.local.LocalClusterStateRequest;
1313
import org.elasticsearch.cluster.metadata.ItemUsage;
1414
import org.elasticsearch.common.Strings;
1515
import org.elasticsearch.common.collect.Iterators;
@@ -18,6 +18,7 @@
1818
import org.elasticsearch.common.io.stream.Writeable;
1919
import org.elasticsearch.common.xcontent.ChunkedToXContentObject;
2020
import org.elasticsearch.core.TimeValue;
21+
import org.elasticsearch.core.UpdateForV10;
2122
import org.elasticsearch.tasks.CancellableTask;
2223
import org.elasticsearch.tasks.Task;
2324
import org.elasticsearch.tasks.TaskId;
@@ -43,10 +44,6 @@ public static class Response extends ActionResponse implements ChunkedToXContent
4344

4445
private final List<LifecyclePolicyResponseItem> policies;
4546

46-
public Response(StreamInput in) throws IOException {
47-
this.policies = in.readCollectionAsList(LifecyclePolicyResponseItem::new);
48-
}
49-
5047
public Response(List<LifecyclePolicyResponseItem> policies) {
5148
this.policies = policies;
5249
}
@@ -55,6 +52,11 @@ public List<LifecyclePolicyResponseItem> getPolicies() {
5552
return policies;
5653
}
5754

55+
/**
56+
* NB prior to 9.1 this was a TransportMasterNodeAction so for BwC we must remain able to write these responses until
57+
* we no longer need to support calling this action remotely.
58+
*/
59+
@UpdateForV10(owner = UpdateForV10.Owner.DATA_MANAGEMENT)
5860
@Override
5961
public void writeTo(StreamOutput out) throws IOException {
6062
out.writeCollection(policies);
@@ -100,19 +102,26 @@ public Iterator<ToXContent> toXContentChunked(ToXContent.Params outerParams) {
100102
}
101103
}
102104

103-
public static class Request extends AcknowledgedRequest<Request> {
105+
public static class Request extends LocalClusterStateRequest {
104106
private final String[] policyNames;
105107

106-
public Request(TimeValue masterNodeTimeout, TimeValue ackTimeout, String... policyNames) {
107-
super(masterNodeTimeout, ackTimeout);
108+
public Request(TimeValue masterNodeTimeout, String... policyNames) {
109+
super(masterNodeTimeout);
108110
if (policyNames == null) {
109111
throw new IllegalArgumentException("ids cannot be null");
110112
}
111113
this.policyNames = policyNames;
112114
}
113115

116+
/**
117+
* NB prior to 9.1 this was a TransportMasterNodeAction so for BwC we must remain able to read these requests until
118+
* we no longer need to support calling this action remotely.
119+
*/
120+
@UpdateForV10(owner = UpdateForV10.Owner.DATA_MANAGEMENT)
114121
public Request(StreamInput in) throws IOException {
115-
super(in);
122+
super(in, false);
123+
// This used to be an AcknowledgedRequest so we need to read the ack timeout for BwC.
124+
in.readTimeValue();
116125
policyNames = in.readStringArray();
117126
}
118127

@@ -125,12 +134,6 @@ public String[] getPolicyNames() {
125134
return policyNames;
126135
}
127136

128-
@Override
129-
public void writeTo(StreamOutput out) throws IOException {
130-
super.writeTo(out);
131-
out.writeStringArray(policyNames);
132-
}
133-
134137
@Override
135138
public int hashCode() {
136139
return Arrays.hashCode(policyNames);
@@ -163,13 +166,11 @@ public LifecyclePolicyResponseItem(LifecyclePolicy lifecyclePolicy, long version
163166
this.usage = usage;
164167
}
165168

166-
LifecyclePolicyResponseItem(StreamInput in) throws IOException {
167-
this.lifecyclePolicy = new LifecyclePolicy(in);
168-
this.version = in.readVLong();
169-
this.modifiedDate = in.readString();
170-
this.usage = new ItemUsage(in);
171-
}
172-
169+
/**
170+
* NB prior to 9.1 this was a TransportMasterNodeAction so for BwC we must remain able to write these responses until
171+
* we no longer need to support calling this action remotely.
172+
*/
173+
@UpdateForV10(owner = UpdateForV10.Owner.DATA_MANAGEMENT)
173174
@Override
174175
public void writeTo(StreamOutput out) throws IOException {
175176
lifecyclePolicy.writeTo(out);

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

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

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

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@
99
import org.elasticsearch.cluster.metadata.ItemUsage;
1010
import org.elasticsearch.common.bytes.BytesReference;
1111
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
12-
import org.elasticsearch.common.io.stream.Writeable;
1312
import org.elasticsearch.common.xcontent.XContentHelper;
1413
import org.elasticsearch.core.Nullable;
1514
import org.elasticsearch.test.AbstractChunkedSerializingTestCase;
16-
import org.elasticsearch.test.AbstractWireSerializingTestCase;
15+
import org.elasticsearch.test.ESTestCase;
1716
import org.elasticsearch.xcontent.XContentBuilder;
1817
import org.elasticsearch.xpack.core.ilm.LifecycleAction;
1918
import org.elasticsearch.xpack.core.ilm.LifecycleType;
@@ -33,9 +32,8 @@
3332
import static org.hamcrest.Matchers.is;
3433
import static org.hamcrest.Matchers.notNullValue;
3534

36-
public class GetLifecycleResponseTests extends AbstractWireSerializingTestCase<Response> {
35+
public class GetLifecycleResponseTests extends ESTestCase {
3736

38-
@Override
3937
protected Response createTestInstance() {
4038
String randomPrefix = randomAlphaOfLength(5);
4139
List<LifecyclePolicyResponseItem> responseItems = new ArrayList<>();
@@ -82,11 +80,6 @@ public void testChunkCount() {
8280
AbstractChunkedSerializingTestCase.assertChunkCount(response, ignored -> 2 + response.getPolicies().size());
8381
}
8482

85-
@Override
86-
protected Writeable.Reader<Response> instanceReader() {
87-
return Response::new;
88-
}
89-
9083
protected NamedWriteableRegistry getNamedWriteableRegistry() {
9184
return new NamedWriteableRegistry(
9285
List.of(
@@ -96,35 +89,6 @@ protected NamedWriteableRegistry getNamedWriteableRegistry() {
9689
);
9790
}
9891

99-
@Override
100-
protected Response mutateInstance(Response response) {
101-
List<LifecyclePolicyResponseItem> responseItems = new ArrayList<>(response.getPolicies());
102-
if (responseItems.size() > 0) {
103-
if (randomBoolean()) {
104-
responseItems.add(
105-
new LifecyclePolicyResponseItem(
106-
randomTestLifecyclePolicy(randomAlphaOfLength(5)),
107-
randomNonNegativeLong(),
108-
randomAlphaOfLength(4),
109-
randomUsage()
110-
)
111-
);
112-
} else {
113-
responseItems.remove(0);
114-
}
115-
} else {
116-
responseItems.add(
117-
new LifecyclePolicyResponseItem(
118-
randomTestLifecyclePolicy(randomAlphaOfLength(2)),
119-
randomNonNegativeLong(),
120-
randomAlphaOfLength(4),
121-
randomUsage()
122-
)
123-
);
124-
}
125-
return new Response(responseItems);
126-
}
127-
12892
public static ItemUsage randomUsage() {
12993
return new ItemUsage(randomStringList(), randomStringList(), randomStringList());
13094
}

x-pack/plugin/ilm/src/internalClusterTest/java/org/elasticsearch/xpack/ilm/IndexLifecycleInitialisationTests.java

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,13 @@ public void testSingleNodeCluster() throws Exception {
142142
// test get-lifecycle behavior when IndexLifecycleMetadata is null
143143
GetLifecycleAction.Response getUninitializedLifecycleResponse = client().execute(
144144
GetLifecycleAction.INSTANCE,
145-
new GetLifecycleAction.Request(TEST_REQUEST_TIMEOUT, TEST_REQUEST_TIMEOUT)
145+
new GetLifecycleAction.Request(TEST_REQUEST_TIMEOUT)
146146
).get();
147147
assertThat(getUninitializedLifecycleResponse.getPolicies().size(), equalTo(0));
148148
ExecutionException exception = expectThrows(
149149
ExecutionException.class,
150-
() -> client().execute(
151-
GetLifecycleAction.INSTANCE,
152-
new GetLifecycleAction.Request(TEST_REQUEST_TIMEOUT, TEST_REQUEST_TIMEOUT, "non-existent-policy")
153-
).get()
150+
() -> client().execute(GetLifecycleAction.INSTANCE, new GetLifecycleAction.Request(TEST_REQUEST_TIMEOUT, "non-existent-policy"))
151+
.get()
154152
);
155153
assertThat(exception.getMessage(), containsString("Lifecycle policy not found: [non-existent-policy]"));
156154

@@ -163,7 +161,7 @@ public void testSingleNodeCluster() throws Exception {
163161
// assert version and modified_date
164162
GetLifecycleAction.Response getLifecycleResponse = client().execute(
165163
GetLifecycleAction.INSTANCE,
166-
new GetLifecycleAction.Request(TEST_REQUEST_TIMEOUT, TEST_REQUEST_TIMEOUT)
164+
new GetLifecycleAction.Request(TEST_REQUEST_TIMEOUT)
167165
).get();
168166
assertThat(getLifecycleResponse.getPolicies().size(), equalTo(1));
169167
GetLifecycleAction.LifecyclePolicyResponseItem responseItem = getLifecycleResponse.getPolicies().get(0);
@@ -208,7 +206,7 @@ public void testNoOpPolicyUpdates() throws Exception {
208206

209207
GetLifecycleAction.Response getLifecycleResponse = client().execute(
210208
GetLifecycleAction.INSTANCE,
211-
new GetLifecycleAction.Request(TEST_REQUEST_TIMEOUT, TEST_REQUEST_TIMEOUT)
209+
new GetLifecycleAction.Request(TEST_REQUEST_TIMEOUT)
212210
).get();
213211
assertThat(getLifecycleResponse.getPolicies().size(), equalTo(1));
214212
GetLifecycleAction.LifecyclePolicyResponseItem responseItem = getLifecycleResponse.getPolicies().get(0);
@@ -219,10 +217,7 @@ public void testNoOpPolicyUpdates() throws Exception {
219217
putLifecycleRequest = new PutLifecycleRequest(TEST_REQUEST_TIMEOUT, TEST_REQUEST_TIMEOUT, policy);
220218
assertAcked(client().execute(ILMActions.PUT, putLifecycleRequest).get());
221219

222-
getLifecycleResponse = client().execute(
223-
GetLifecycleAction.INSTANCE,
224-
new GetLifecycleAction.Request(TEST_REQUEST_TIMEOUT, TEST_REQUEST_TIMEOUT)
225-
).get();
220+
getLifecycleResponse = client().execute(GetLifecycleAction.INSTANCE, new GetLifecycleAction.Request(TEST_REQUEST_TIMEOUT)).get();
226221
assertThat(getLifecycleResponse.getPolicies().size(), equalTo(1));
227222
responseItem = getLifecycleResponse.getPolicies().get(0);
228223
assertThat(responseItem.getLifecyclePolicy(), equalTo(policy));
@@ -237,10 +232,7 @@ public void testNoOpPolicyUpdates() throws Exception {
237232
putLifecycleRequest = new PutLifecycleRequest(TEST_REQUEST_TIMEOUT, TEST_REQUEST_TIMEOUT, policy);
238233
assertAcked(client().execute(ILMActions.PUT, putLifecycleRequest).get());
239234

240-
getLifecycleResponse = client().execute(
241-
GetLifecycleAction.INSTANCE,
242-
new GetLifecycleAction.Request(TEST_REQUEST_TIMEOUT, TEST_REQUEST_TIMEOUT)
243-
).get();
235+
getLifecycleResponse = client().execute(GetLifecycleAction.INSTANCE, new GetLifecycleAction.Request(TEST_REQUEST_TIMEOUT)).get();
244236
assertThat(getLifecycleResponse.getPolicies().size(), equalTo(1));
245237
responseItem = getLifecycleResponse.getPolicies().get(0);
246238
assertThat(responseItem.getLifecyclePolicy(), equalTo(policy));
@@ -258,7 +250,7 @@ public void testExplainExecution() throws Exception {
258250

259251
GetLifecycleAction.Response getLifecycleResponse = client().execute(
260252
GetLifecycleAction.INSTANCE,
261-
new GetLifecycleAction.Request(TEST_REQUEST_TIMEOUT, TEST_REQUEST_TIMEOUT)
253+
new GetLifecycleAction.Request(TEST_REQUEST_TIMEOUT)
262254
).get();
263255
assertThat(getLifecycleResponse.getPolicies().size(), equalTo(1));
264256
GetLifecycleAction.LifecyclePolicyResponseItem responseItem = getLifecycleResponse.getPolicies().get(0);
@@ -333,7 +325,7 @@ public void testExplainParseOriginationDate() throws Exception {
333325

334326
GetLifecycleAction.Response getLifecycleResponse = client().execute(
335327
GetLifecycleAction.INSTANCE,
336-
new GetLifecycleAction.Request(TEST_REQUEST_TIMEOUT, TEST_REQUEST_TIMEOUT)
328+
new GetLifecycleAction.Request(TEST_REQUEST_TIMEOUT)
337329
).get();
338330
assertThat(getLifecycleResponse.getPolicies().size(), equalTo(1));
339331
GetLifecycleAction.LifecyclePolicyResponseItem responseItem = getLifecycleResponse.getPolicies().get(0);
@@ -466,7 +458,7 @@ public void testCreatePolicyWhenStopped() throws Exception {
466458
// assert version and modified_date
467459
GetLifecycleAction.Response getLifecycleResponse = client().execute(
468460
GetLifecycleAction.INSTANCE,
469-
new GetLifecycleAction.Request(TEST_REQUEST_TIMEOUT, TEST_REQUEST_TIMEOUT)
461+
new GetLifecycleAction.Request(TEST_REQUEST_TIMEOUT)
470462
).get();
471463
assertThat(getLifecycleResponse.getPolicies().size(), equalTo(1));
472464
GetLifecycleAction.LifecyclePolicyResponseItem responseItem = getLifecycleResponse.getPolicies().get(0);

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import java.util.List;
1919

2020
import static org.elasticsearch.rest.RestRequest.Method.GET;
21-
import static org.elasticsearch.rest.RestUtils.getAckTimeout;
2221
import static org.elasticsearch.rest.RestUtils.getMasterNodeTimeout;
2322

2423
public class RestGetLifecycleAction extends BaseRestHandler {
@@ -36,11 +35,7 @@ public String getName() {
3635
@Override
3736
protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient client) {
3837
String[] lifecycleNames = Strings.splitStringByCommaToArray(restRequest.param("name"));
39-
GetLifecycleAction.Request getLifecycleRequest = new GetLifecycleAction.Request(
40-
getMasterNodeTimeout(restRequest),
41-
getAckTimeout(restRequest),
42-
lifecycleNames
43-
);
38+
GetLifecycleAction.Request getLifecycleRequest = new GetLifecycleAction.Request(getMasterNodeTimeout(restRequest), lifecycleNames);
4439

4540
return channel -> new RestCancellableNodeClient(client, restRequest.getHttpChannel()).execute(
4641
GetLifecycleAction.INSTANCE,

0 commit comments

Comments
 (0)