Skip to content

Commit 47032b5

Browse files
authored
Run TransportGetStatusAction on local node (elastic#129367)
This action solely needs the cluster state, it can run on any node. Relates elastic#101805
1 parent 21d1c78 commit 47032b5

File tree

8 files changed

+100
-38
lines changed

8 files changed

+100
-38
lines changed

docs/changelog/129367.yaml

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

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,21 @@
99

1010
import org.elasticsearch.action.ActionResponse;
1111
import org.elasticsearch.action.ActionType;
12+
import org.elasticsearch.action.support.local.LocalClusterStateRequest;
1213
import org.elasticsearch.common.Strings;
1314
import org.elasticsearch.common.io.stream.StreamInput;
1415
import org.elasticsearch.common.io.stream.StreamOutput;
16+
import org.elasticsearch.core.TimeValue;
17+
import org.elasticsearch.core.UpdateForV10;
18+
import org.elasticsearch.tasks.CancellableTask;
19+
import org.elasticsearch.tasks.Task;
20+
import org.elasticsearch.tasks.TaskId;
1521
import org.elasticsearch.xcontent.ToXContentObject;
1622
import org.elasticsearch.xcontent.XContentBuilder;
1723
import org.elasticsearch.xpack.core.ilm.OperationMode;
1824

1925
import java.io.IOException;
26+
import java.util.Map;
2027
import java.util.Objects;
2128

2229
public class GetStatusAction extends ActionType<GetStatusAction.Response> {
@@ -27,6 +34,29 @@ protected GetStatusAction() {
2734
super(NAME);
2835
}
2936

37+
public static class Request extends LocalClusterStateRequest {
38+
39+
public Request(TimeValue masterTimeout) {
40+
super(masterTimeout);
41+
}
42+
43+
/**
44+
* NB prior to 9.1 this was a TransportMasterNodeAction so for BwC we must remain able to read these requests until
45+
* we no longer need to support calling this action remotely.
46+
*/
47+
@UpdateForV10(owner = UpdateForV10.Owner.DATA_MANAGEMENT)
48+
public Request(StreamInput in) throws IOException {
49+
super(in, false);
50+
// Read and ignore ack timeout.
51+
in.readTimeValue();
52+
}
53+
54+
@Override
55+
public Task createTask(long id, String type, String action, TaskId parentTaskId, Map<String, String> headers) {
56+
return new CancellableTask(id, type, action, "", parentTaskId, headers);
57+
}
58+
}
59+
3060
public static class Response extends ActionResponse implements ToXContentObject {
3161

3262
private final OperationMode mode;

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
1010
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
11-
import org.elasticsearch.action.support.master.AcknowledgedRequest;
1211
import org.elasticsearch.cluster.ClusterState;
1312
import org.elasticsearch.cluster.ProjectState;
1413
import org.elasticsearch.cluster.metadata.LifecycleExecutionState;
@@ -442,10 +441,9 @@ public void testCreatePolicyWhenStopped() throws Exception {
442441

443442
assertAcked(client().execute(ILMActions.STOP, new StopILMRequest(TEST_REQUEST_TIMEOUT, TEST_REQUEST_TIMEOUT)).get());
444443
assertBusy(() -> {
445-
OperationMode mode = client().execute(
446-
GetStatusAction.INSTANCE,
447-
new AcknowledgedRequest.Plain(TEST_REQUEST_TIMEOUT, TEST_REQUEST_TIMEOUT)
448-
).get().getMode();
444+
OperationMode mode = client().execute(GetStatusAction.INSTANCE, new GetStatusAction.Request(TEST_REQUEST_TIMEOUT))
445+
.get()
446+
.getMode();
449447
logger.info("--> waiting for STOPPED, currently: {}", mode);
450448
assertThat(mode, equalTo(OperationMode.STOPPED));
451449
});
@@ -473,7 +471,7 @@ public void testCreatePolicyWhenStopped() throws Exception {
473471
// assert ILM is still stopped
474472
GetStatusAction.Response statusResponse = client().execute(
475473
GetStatusAction.INSTANCE,
476-
new AcknowledgedRequest.Plain(TEST_REQUEST_TIMEOUT, TEST_REQUEST_TIMEOUT)
474+
new GetStatusAction.Request(TEST_REQUEST_TIMEOUT)
477475
).get();
478476
assertThat(statusResponse.getMode(), equalTo(OperationMode.STOPPED));
479477
}

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,20 @@
77

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

10-
import org.elasticsearch.action.support.master.AcknowledgedRequest;
10+
import org.elasticsearch.action.support.local.TransportLocalClusterStateAction;
1111
import org.elasticsearch.client.internal.node.NodeClient;
12+
import org.elasticsearch.common.logging.DeprecationCategory;
13+
import org.elasticsearch.common.logging.DeprecationLogger;
14+
import org.elasticsearch.core.RestApiVersion;
1215
import org.elasticsearch.rest.BaseRestHandler;
1316
import org.elasticsearch.rest.RestRequest;
17+
import org.elasticsearch.rest.action.RestCancellableNodeClient;
1418
import org.elasticsearch.rest.action.RestToXContentListener;
1519
import org.elasticsearch.xpack.core.ilm.action.GetStatusAction;
1620

1721
import java.util.List;
1822

1923
import static org.elasticsearch.rest.RestRequest.Method.GET;
20-
import static org.elasticsearch.rest.RestUtils.getAckTimeout;
2124
import static org.elasticsearch.rest.RestUtils.getMasterNodeTimeout;
2225

2326
public class RestGetStatusAction extends BaseRestHandler {
@@ -34,7 +37,25 @@ public String getName() {
3437

3538
@Override
3639
protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient client) {
37-
final var request = new AcknowledgedRequest.Plain(getMasterNodeTimeout(restRequest), getAckTimeout(restRequest));
38-
return channel -> client.execute(GetStatusAction.INSTANCE, request, new RestToXContentListener<>(channel));
40+
final var request = new GetStatusAction.Request(getMasterNodeTimeout(restRequest));
41+
if (restRequest.hasParam("timeout")) {
42+
// Consume this param just for validation when in BWC mode.
43+
final var timeout = restRequest.paramAsTime("timeout", null);
44+
if (restRequest.getRestApiVersion() != RestApiVersion.V_8) {
45+
DeprecationLogger.getLogger(TransportLocalClusterStateAction.class)
46+
.critical(
47+
DeprecationCategory.API,
48+
"TransportLocalClusterStateAction-timeout-parameter",
49+
"the [?timeout] query parameter to this API has no effect, is now deprecated, "
50+
+ "and will be removed in a future version"
51+
);
52+
}
53+
54+
}
55+
return channel -> new RestCancellableNodeClient(client, restRequest.getHttpChannel()).execute(
56+
GetStatusAction.INSTANCE,
57+
request,
58+
new RestToXContentListener<>(channel)
59+
);
3960
}
4061
}

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

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99

1010
import org.elasticsearch.action.ActionListener;
1111
import org.elasticsearch.action.support.ActionFilters;
12-
import org.elasticsearch.action.support.master.AcknowledgedRequest;
13-
import org.elasticsearch.action.support.master.TransportMasterNodeAction;
14-
import org.elasticsearch.cluster.ClusterState;
12+
import org.elasticsearch.action.support.ChannelActionListener;
13+
import org.elasticsearch.action.support.local.TransportLocalProjectMetadataAction;
14+
import org.elasticsearch.cluster.ProjectState;
1515
import org.elasticsearch.cluster.block.ClusterBlockException;
1616
import org.elasticsearch.cluster.block.ClusterBlockLevel;
1717
import org.elasticsearch.cluster.project.ProjectResolver;
1818
import org.elasticsearch.cluster.service.ClusterService;
19-
import org.elasticsearch.common.util.concurrent.EsExecutors;
19+
import org.elasticsearch.core.UpdateForV10;
2020
import org.elasticsearch.injection.guice.Inject;
2121
import org.elasticsearch.tasks.Task;
2222
import org.elasticsearch.threadpool.ThreadPool;
@@ -26,10 +26,14 @@
2626

2727
import static org.elasticsearch.xpack.core.ilm.LifecycleOperationMetadata.currentILMMode;
2828

29-
public class TransportGetStatusAction extends TransportMasterNodeAction<AcknowledgedRequest.Plain, Response> {
30-
31-
private final ProjectResolver projectResolver;
29+
public class TransportGetStatusAction extends TransportLocalProjectMetadataAction<GetStatusAction.Request, Response> {
3230

31+
/**
32+
* NB prior to 9.1 this was a TransportMasterNodeAction so for BwC it must be registered with the TransportService until
33+
* we no longer need to support calling this action remotely.
34+
*/
35+
@UpdateForV10(owner = UpdateForV10.Owner.DATA_MANAGEMENT)
36+
@SuppressWarnings("this-escape")
3337
@Inject
3438
public TransportGetStatusAction(
3539
TransportService transportService,
@@ -40,24 +44,36 @@ public TransportGetStatusAction(
4044
) {
4145
super(
4246
GetStatusAction.NAME,
43-
transportService,
44-
clusterService,
45-
threadPool,
4647
actionFilters,
47-
AcknowledgedRequest.Plain::new,
48-
Response::new,
49-
EsExecutors.DIRECT_EXECUTOR_SERVICE
48+
transportService.getTaskManager(),
49+
clusterService,
50+
threadPool.executor(ThreadPool.Names.MANAGEMENT),
51+
projectResolver
5052
);
51-
this.projectResolver = projectResolver;
53+
54+
transportService.registerRequestHandler(
55+
actionName,
56+
executor,
57+
false,
58+
true,
59+
GetStatusAction.Request::new,
60+
(request, channel, task) -> executeDirect(task, request, new ChannelActionListener<>(channel))
61+
);
62+
5263
}
5364

5465
@Override
55-
protected void masterOperation(Task task, AcknowledgedRequest.Plain request, ClusterState state, ActionListener<Response> listener) {
56-
listener.onResponse(new Response(currentILMMode(projectResolver.getProjectMetadata(state))));
66+
protected void localClusterStateOperation(
67+
Task task,
68+
GetStatusAction.Request request,
69+
ProjectState state,
70+
ActionListener<Response> listener
71+
) {
72+
listener.onResponse(new Response(currentILMMode(state.metadata())));
5773
}
5874

5975
@Override
60-
protected ClusterBlockException checkBlock(AcknowledgedRequest.Plain request, ClusterState state) {
76+
protected ClusterBlockException checkBlock(GetStatusAction.Request request, ProjectState state) {
6177
return state.blocks().globalBlockedException(ClusterBlockLevel.METADATA_WRITE);
6278
}
6379
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,7 @@ public void testModeSnapshotRestore() throws Exception {
134134
}
135135

136136
private OperationMode ilmMode() throws Exception {
137-
return client().execute(GetStatusAction.INSTANCE, new AcknowledgedRequest.Plain(TEST_REQUEST_TIMEOUT, TEST_REQUEST_TIMEOUT))
138-
.get()
139-
.getMode();
137+
return client().execute(GetStatusAction.INSTANCE, new GetStatusAction.Request(TEST_REQUEST_TIMEOUT)).get().getMode();
140138
}
141139

142140
private OperationMode slmMode() throws Exception {

x-pack/plugin/migrate/src/internalClusterTest/java/org/elasticsearch/xpack/migrate/action/CopyLifecycleIndexMetadataTransportActionIT.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import org.elasticsearch.action.datastreams.CreateDataStreamAction;
1919
import org.elasticsearch.action.index.IndexRequest;
2020
import org.elasticsearch.action.support.IndicesOptions;
21-
import org.elasticsearch.action.support.master.AcknowledgedRequest;
2221
import org.elasticsearch.cluster.metadata.ComposableIndexTemplate;
2322
import org.elasticsearch.cluster.metadata.IndexMetadata;
2423
import org.elasticsearch.cluster.metadata.LifecycleExecutionState;
@@ -139,9 +138,7 @@ public void testILMState() throws Exception {
139138
// stop ILM so source does not change after copying metadata
140139
assertAcked(safeGet(client().execute(ILMActions.STOP, new StopILMRequest(TEST_REQUEST_TIMEOUT, TEST_REQUEST_TIMEOUT))));
141140
assertBusy(() -> {
142-
var statusResponse = safeGet(
143-
client().execute(GetStatusAction.INSTANCE, new AcknowledgedRequest.Plain(TEST_REQUEST_TIMEOUT, TEST_REQUEST_TIMEOUT))
144-
);
141+
var statusResponse = safeGet(client().execute(GetStatusAction.INSTANCE, new GetStatusAction.Request(TEST_REQUEST_TIMEOUT)));
145142
assertEquals(OperationMode.STOPPED, statusResponse.getMode());
146143
});
147144

x-pack/plugin/migrate/src/internalClusterTest/java/org/elasticsearch/xpack/migrate/action/ReindexDatastreamIndexTransportActionIT.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.elasticsearch.action.ingest.PutPipelineRequest;
2828
import org.elasticsearch.action.ingest.PutPipelineTransportAction;
2929
import org.elasticsearch.action.support.IndicesOptions;
30-
import org.elasticsearch.action.support.master.AcknowledgedRequest;
3130
import org.elasticsearch.cluster.metadata.ComposableIndexTemplate;
3231
import org.elasticsearch.cluster.metadata.IndexMetadata;
3332
import org.elasticsearch.cluster.metadata.MappingMetadata;
@@ -623,9 +622,7 @@ public void testIndexLifecycleSettingNotCopied() throws Exception {
623622
private void stopILM() throws Exception {
624623
assertAcked(safeGet(client().execute(ILMActions.STOP, new StopILMRequest(TEST_REQUEST_TIMEOUT, TEST_REQUEST_TIMEOUT))));
625624
assertBusy(() -> {
626-
var statusResponse = safeGet(
627-
client().execute(GetStatusAction.INSTANCE, new AcknowledgedRequest.Plain(TEST_REQUEST_TIMEOUT, TEST_REQUEST_TIMEOUT))
628-
);
625+
var statusResponse = safeGet(client().execute(GetStatusAction.INSTANCE, new GetStatusAction.Request(TEST_REQUEST_TIMEOUT)));
629626
assertEquals(OperationMode.STOPPED, statusResponse.getMode());
630627
});
631628
}

0 commit comments

Comments
 (0)