Skip to content

Commit afff39e

Browse files
authored
Run XPack usage actions on local node (#122933)
These actions solely need the cluster state, they can run on any node. Relates #101805
1 parent 2fcb23a commit afff39e

File tree

52 files changed

+122
-83
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+122
-83
lines changed

docs/changelog/122933.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 122933
2+
summary: Run XPack usage actions on local node
3+
area: Stats
4+
type: enhancement
5+
issues: []

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ protected abstract void localClusterStateOperation(Task task, Request request, C
6666

6767
@Override
6868
protected final void doExecute(Task task, Request request, ActionListener<Response> listener) {
69+
if (task != null) {
70+
request.setParentTask(clusterService.localNode().getId(), task.getId());
71+
}
6972
final var state = clusterService.state();
7073
final var clusterBlockException = checkBlock(request, state);
7174
if (clusterBlockException != null) {

x-pack/plugin/analytics/src/main/java/org/elasticsearch/xpack/analytics/action/AnalyticsUsageTransportAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public AnalyticsUsageTransportAction(
3838
}
3939

4040
@Override
41-
protected void masterOperation(
41+
protected void localClusterStateOperation(
4242
Task task,
4343
XPackUsageRequest request,
4444
ClusterState state,

x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/action/AnalyticsInfoTransportActionTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public void testAvailable() throws Exception {
7373
client
7474
);
7575
PlainActionFuture<XPackUsageFeatureResponse> future = new PlainActionFuture<>();
76-
usageAction.masterOperation(task, null, clusterState, future);
76+
usageAction.localClusterStateOperation(task, null, clusterState, future);
7777
XPackFeatureUsage usage = future.get().getUsage();
7878
assertThat(usage.available(), is(true));
7979

@@ -100,7 +100,7 @@ public void testEnabled() throws Exception {
100100
client
101101
);
102102
PlainActionFuture<XPackUsageFeatureResponse> future = new PlainActionFuture<>();
103-
usageAction.masterOperation(task, null, clusterState, future);
103+
usageAction.localClusterStateOperation(task, null, clusterState, future);
104104
XPackFeatureUsage usage = future.get().getUsage();
105105
assertTrue(usage.enabled());
106106

x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/CCRUsageTransportAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public CCRUsageTransportAction(
4848
}
4949

5050
@Override
51-
protected void masterOperation(
51+
protected void localClusterStateOperation(
5252
Task task,
5353
XPackUsageRequest request,
5454
ClusterState state,

x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/CCRInfoTransportActionTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public void testUsageStats() throws Exception {
152152
licenseState
153153
);
154154
PlainActionFuture<XPackUsageFeatureResponse> future = new PlainActionFuture<>();
155-
usageAction.masterOperation(null, null, clusterState, future);
155+
usageAction.localClusterStateOperation(null, null, clusterState, future);
156156
CCRInfoTransportAction.Usage ccrUsage = (CCRInfoTransportAction.Usage) future.get().getUsage();
157157
assertThat(ccrUsage.enabled(), equalTo(true));
158158
assertThat(ccrUsage.available(), equalTo(false));

x-pack/plugin/core/src/internalClusterTest/java/org/elasticsearch/xpack/core/rest/action/XPackUsageRestCancellationIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public BlockingXPackUsageAction(
150150
}
151151

152152
@Override
153-
protected void masterOperation(
153+
protected void localClusterStateOperation(
154154
Task task,
155155
XPackUsageRequest request,
156156
ClusterState state,
@@ -185,7 +185,7 @@ public NonBlockingXPackUsageAction(
185185
}
186186

187187
@Override
188-
protected void masterOperation(
188+
protected void localClusterStateOperation(
189189
Task task,
190190
XPackUsageRequest request,
191191
ClusterState state,

x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/XPackUsageRequest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
package org.elasticsearch.protocol.xpack;
88

99
import org.elasticsearch.action.ActionRequestValidationException;
10-
import org.elasticsearch.action.support.master.MasterNodeRequest;
10+
import org.elasticsearch.action.support.local.LocalClusterStateRequest;
1111
import org.elasticsearch.common.io.stream.StreamInput;
1212
import org.elasticsearch.core.TimeValue;
1313
import org.elasticsearch.tasks.CancellableTask;
@@ -17,14 +17,14 @@
1717
import java.io.IOException;
1818
import java.util.Map;
1919

20-
public class XPackUsageRequest extends MasterNodeRequest<XPackUsageRequest> {
20+
public class XPackUsageRequest extends LocalClusterStateRequest {
2121

2222
public XPackUsageRequest(TimeValue masterNodeTimeout) {
2323
super(masterNodeTimeout);
2424
}
2525

2626
public XPackUsageRequest(StreamInput in) throws IOException {
27-
super(in);
27+
super(in, false);
2828
}
2929

3030
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public HealthApiUsageTransportAction(
4242
}
4343

4444
@Override
45-
protected void masterOperation(
45+
protected void localClusterStateOperation(
4646
Task task,
4747
XPackUsageRequest request,
4848
ClusterState state,

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
public class RemoteClusterUsageTransportAction extends XPackUsageFeatureTransportAction {
2525

26+
private TransportService transportService;
27+
2628
@Inject
2729
public RemoteClusterUsageTransportAction(
2830
TransportService transportService,
@@ -31,10 +33,11 @@ public RemoteClusterUsageTransportAction(
3133
ActionFilters actionFilters
3234
) {
3335
super(XPackUsageFeatureAction.REMOTE_CLUSTERS.name(), transportService, clusterService, threadPool, actionFilters);
36+
this.transportService = transportService;
3437
}
3538

3639
@Override
37-
protected void masterOperation(
40+
protected void localClusterStateOperation(
3841
Task task,
3942
XPackUsageRequest request,
4043
ClusterState state,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public DataStreamLifecycleUsageTransportAction(
4747
}
4848

4949
@Override
50-
protected void masterOperation(
50+
protected void localClusterStateOperation(
5151
Task task,
5252
XPackUsageRequest request,
5353
ClusterState state,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public DataStreamUsageTransportAction(
3939
}
4040

4141
@Override
42-
protected void masterOperation(
42+
protected void localClusterStateOperation(
4343
Task task,
4444
XPackUsageRequest request,
4545
ClusterState state,

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

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
import org.elasticsearch.action.ActionRunnable;
1111
import org.elasticsearch.action.ActionType;
1212
import org.elasticsearch.action.support.ActionFilters;
13-
import org.elasticsearch.action.support.master.TransportMasterNodeAction;
13+
import org.elasticsearch.action.support.ChannelActionListener;
14+
import org.elasticsearch.action.support.local.TransportLocalClusterStateAction;
1415
import org.elasticsearch.client.internal.node.NodeClient;
1516
import org.elasticsearch.cluster.ClusterState;
1617
import org.elasticsearch.cluster.block.ClusterBlockException;
1718
import org.elasticsearch.cluster.service.ClusterService;
19+
import org.elasticsearch.core.UpdateForV10;
1820
import org.elasticsearch.injection.guice.Inject;
1921
import org.elasticsearch.protocol.xpack.XPackUsageRequest;
2022
import org.elasticsearch.tasks.Task;
@@ -25,11 +27,16 @@
2527
import java.util.ArrayList;
2628
import java.util.List;
2729

28-
public class TransportXPackUsageAction extends TransportMasterNodeAction<XPackUsageRequest, XPackUsageResponse> {
30+
public class TransportXPackUsageAction extends TransportLocalClusterStateAction<XPackUsageRequest, XPackUsageResponse> {
2931

3032
private final NodeClient client;
3133
private final List<ActionType<XPackUsageFeatureResponse>> usageActions;
3234

35+
/**
36+
* NB prior to 9.0 this was a TransportMasterNodeReadAction so for BwC it must be registered with the TransportService until
37+
* we no longer need to support calling this action remotely.
38+
*/
39+
@UpdateForV10(owner = UpdateForV10.Owner.DATA_MANAGEMENT)
3340
@SuppressWarnings("this-escape")
3441
@Inject
3542
public TransportXPackUsageAction(
@@ -41,16 +48,22 @@ public TransportXPackUsageAction(
4148
) {
4249
super(
4350
XPackUsageAction.NAME,
44-
transportService,
45-
clusterService,
46-
threadPool,
4751
actionFilters,
48-
XPackUsageRequest::new,
49-
XPackUsageResponse::new,
52+
transportService.getTaskManager(),
53+
clusterService,
5054
threadPool.executor(ThreadPool.Names.MANAGEMENT)
5155
);
5256
this.client = client;
5357
this.usageActions = usageActions();
58+
59+
transportService.registerRequestHandler(
60+
actionName,
61+
executor,
62+
false,
63+
true,
64+
XPackUsageRequest::new,
65+
(request, channel, task) -> executeDirect(task, request, new ChannelActionListener<>(channel))
66+
);
5467
}
5568

5669
// overrideable for tests
@@ -59,14 +72,19 @@ protected List<ActionType<XPackUsageFeatureResponse>> usageActions() {
5972
}
6073

6174
@Override
62-
protected void masterOperation(Task task, XPackUsageRequest request, ClusterState state, ActionListener<XPackUsageResponse> listener) {
75+
protected void localClusterStateOperation(
76+
Task task,
77+
XPackUsageRequest request,
78+
ClusterState state,
79+
ActionListener<XPackUsageResponse> listener
80+
) {
6381
new ActionRunnable<>(listener) {
6482
final List<XPackFeatureUsage> responses = new ArrayList<>(usageActions.size());
6583

6684
@Override
6785
protected void doRun() {
6886
if (responses.size() < usageActions().size()) {
69-
final var childRequest = new XPackUsageRequest(request.masterNodeTimeout());
87+
final var childRequest = new XPackUsageRequest(request.masterTimeout());
7088
childRequest.setParentTask(request.getParentTask());
7189
client.executeLocally(
7290
usageActions.get(responses.size()),

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

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,42 @@
77
package org.elasticsearch.xpack.core.action;
88

99
import org.elasticsearch.action.support.ActionFilters;
10-
import org.elasticsearch.action.support.master.TransportMasterNodeAction;
10+
import org.elasticsearch.action.support.ChannelActionListener;
11+
import org.elasticsearch.action.support.local.TransportLocalClusterStateAction;
1112
import org.elasticsearch.cluster.ClusterState;
1213
import org.elasticsearch.cluster.block.ClusterBlockException;
1314
import org.elasticsearch.cluster.service.ClusterService;
15+
import org.elasticsearch.core.UpdateForV10;
1416
import org.elasticsearch.protocol.xpack.XPackUsageRequest;
1517
import org.elasticsearch.threadpool.ThreadPool;
1618
import org.elasticsearch.transport.TransportService;
1719

18-
public abstract class XPackUsageFeatureTransportAction extends TransportMasterNodeAction<XPackUsageRequest, XPackUsageFeatureResponse> {
20+
public abstract class XPackUsageFeatureTransportAction extends TransportLocalClusterStateAction<
21+
XPackUsageRequest,
22+
XPackUsageFeatureResponse> {
1923

24+
/**
25+
* NB prior to 9.0 this was a TransportMasterNodeReadAction so for BwC it must be registered with the TransportService until
26+
* we no longer need to support calling this action remotely.
27+
*/
28+
@UpdateForV10(owner = UpdateForV10.Owner.DATA_MANAGEMENT)
29+
@SuppressWarnings("this-escape")
2030
public XPackUsageFeatureTransportAction(
2131
String name,
2232
TransportService transportService,
2333
ClusterService clusterService,
2434
ThreadPool threadPool,
2535
ActionFilters actionFilters
2636
) {
27-
super(
28-
name,
29-
transportService,
30-
clusterService,
31-
threadPool,
32-
actionFilters,
37+
super(name, actionFilters, transportService.getTaskManager(), clusterService, threadPool.executor(ThreadPool.Names.MANAGEMENT));
38+
39+
transportService.registerRequestHandler(
40+
actionName,
41+
executor,
42+
false,
43+
true,
3344
XPackUsageRequest::new,
34-
XPackUsageFeatureResponse::new,
35-
threadPool.executor(ThreadPool.Names.MANAGEMENT)
45+
(request, channel, task) -> executeDirect(task, request, new ChannelActionListener<>(channel))
3646
);
3747
}
3848

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/datatiers/DataTiersUsageTransportAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public DataTiersUsageTransportAction(
5757
}
5858

5959
@Override
60-
protected void masterOperation(
60+
protected void localClusterStateOperation(
6161
Task task,
6262
XPackUsageRequest request,
6363
ClusterState state,

x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/action/EnrichUsageTransportAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public EnrichUsageTransportAction(
3333
}
3434

3535
@Override
36-
protected void masterOperation(
36+
protected void localClusterStateOperation(
3737
Task task,
3838
XPackUsageRequest request,
3939
ClusterState state,

x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/EnterpriseSearchUsageTransportAction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public EnterpriseSearchUsageTransportAction(
7878
}
7979

8080
@Override
81-
protected void masterOperation(
81+
protected void localClusterStateOperation(
8282
Task task,
8383
XPackUsageRequest request,
8484
ClusterState state,
@@ -153,7 +153,7 @@ protected void masterOperation(
153153

154154
// Step 1: Fetch analytics collections count
155155
GetAnalyticsCollectionAction.Request analyticsCollectionsCountRequest = new GetAnalyticsCollectionAction.Request(
156-
request.masterNodeTimeout(),
156+
request.masterTimeout(),
157157
new String[] { "*" }
158158
);
159159

x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/EqlUsageTransportAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public EqlUsageTransportAction(
4646
}
4747

4848
@Override
49-
protected void masterOperation(
49+
protected void localClusterStateOperation(
5050
Task task,
5151
XPackUsageRequest request,
5252
ClusterState state,

x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/EqlInfoTransportActionTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public void testUsageStats() throws Exception {
100100
TransportService transportService = MockUtils.setupTransportServiceWithThreadpoolExecutor(threadPool);
101101
var usageAction = new EqlUsageTransportAction(transportService, clusterService, threadPool, mock(ActionFilters.class), client);
102102
PlainActionFuture<XPackUsageFeatureResponse> future = new PlainActionFuture<>();
103-
usageAction.masterOperation(mock(Task.class), null, null, future);
103+
usageAction.localClusterStateOperation(mock(Task.class), null, null, future);
104104
EqlFeatureSetUsage eqlUsage = (EqlFeatureSetUsage) future.get().getUsage();
105105

106106
long fooBarBaz = ObjectPath.eval("foo.bar.baz", eqlUsage.stats());

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/EsqlUsageTransportAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public EsqlUsageTransportAction(
4646
}
4747

4848
@Override
49-
protected void masterOperation(
49+
protected void localClusterStateOperation(
5050
Task task,
5151
XPackUsageRequest request,
5252
ClusterState state,

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/EsqlInfoTransportActionTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public void testUsageStats() throws Exception {
102102

103103
var usageAction = new EsqlUsageTransportAction(transportService, clusterService, threadPool, mock(ActionFilters.class), client);
104104
PlainActionFuture<XPackUsageFeatureResponse> future = new PlainActionFuture<>();
105-
usageAction.masterOperation(mock(Task.class), null, null, future);
105+
usageAction.localClusterStateOperation(mock(Task.class), null, null, future);
106106
EsqlFeatureSetUsage esqlUsage = (EsqlFeatureSetUsage) future.get().getUsage();
107107

108108
long fooBarBaz = ObjectPath.eval("foo.bar.baz", esqlUsage.stats());

x-pack/plugin/frozen-indices/src/main/java/org/elasticsearch/xpack/frozen/FrozenIndicesUsageTransportAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public FrozenIndicesUsageTransportAction(
3535
}
3636

3737
@Override
38-
protected void masterOperation(
38+
protected void localClusterStateOperation(
3939
Task task,
4040
XPackUsageRequest request,
4141
ClusterState state,

x-pack/plugin/graph/src/main/java/org/elasticsearch/xpack/graph/GraphUsageTransportAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public GraphUsageTransportAction(
4343
}
4444

4545
@Override
46-
protected void masterOperation(
46+
protected void localClusterStateOperation(
4747
Task task,
4848
XPackUsageRequest request,
4949
ClusterState state,

x-pack/plugin/graph/src/test/java/org/elasticsearch/xpack/graph/GraphInfoTransportActionTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void testAvailable() throws Exception {
5454
licenseState
5555
);
5656
PlainActionFuture<XPackUsageFeatureResponse> future = new PlainActionFuture<>();
57-
usageAction.masterOperation(null, null, null, future);
57+
usageAction.localClusterStateOperation(null, null, null, future);
5858
XPackFeatureUsage usage = future.get().getUsage();
5959
assertThat(usage.available(), is(available));
6060

@@ -93,7 +93,7 @@ public void testEnabled() throws Exception {
9393
licenseState
9494
);
9595
PlainActionFuture<XPackUsageFeatureResponse> future = new PlainActionFuture<>();
96-
usageAction.masterOperation(null, null, null, future);
96+
usageAction.localClusterStateOperation(null, null, null, future);
9797
XPackFeatureUsage usage = future.get().getUsage();
9898
assertThat(usage.enabled(), is(enabled));
9999

0 commit comments

Comments
 (0)