Skip to content

Make TransportRequest an interface #126733

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Make TransportRequest an interface
In order to support a future TransportRequest variant that accepts the
response type, TransportRequest needs to be an interface. This commit
adds AbstractTransportRequest as a concrete implementation and makes
TransportRequest a simple interface that joints together the parent
interfaces from TransportMessage.

Note that this was done entirely in Intellij using structural find and
replace.
  • Loading branch information
rjernst committed Apr 11, 2025
commit 3d3b37a459d255eefeb1859a17bc2f258a3fd894
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.transport.AbstractTransportRequest;
import org.elasticsearch.transport.TransportRequest;
import org.elasticsearch.xcontent.ToXContentObject;
import org.elasticsearch.xcontent.XContentBuilder;
Expand Down Expand Up @@ -78,10 +79,10 @@ public static class Response extends BaseNodesResponse<NodeResponse> implements
private final List<DatabaseConfigurationMetadata> databases;

public Response(
List<DatabaseConfigurationMetadata> databases,
ClusterName clusterName,
List<NodeResponse> nodes,
List<FailedNodeException> failures
List<DatabaseConfigurationMetadata> databases,
ClusterName clusterName,
List<NodeResponse> nodes,
List<FailedNodeException> failures
) {
super(clusterName, nodes, failures);
this.databases = List.copyOf(databases); // defensive copy
Expand Down Expand Up @@ -117,9 +118,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
builder.field("id", database.id()); // serialize including the id -- this is get response serialization
builder.field(VERSION.getPreferredName(), item.version());
builder.timestampFieldsFromUnixEpochMillis(
MODIFIED_DATE_MILLIS.getPreferredName(),
MODIFIED_DATE.getPreferredName(),
item.modifiedDate()
MODIFIED_DATE_MILLIS.getPreferredName(),
MODIFIED_DATE.getPreferredName(),
item.modifiedDate()
);
builder.field(DATABASE.getPreferredName(), database);
builder.endObject();
Expand All @@ -138,10 +139,10 @@ public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
Response response = (Response) o;
return Objects.equals(databases, response.databases)
&& Objects.equals(getClusterName(), response.getClusterName())
&& Objects.equals(equalsHashCodeFailures(), response.equalsHashCodeFailures())
&& Objects.equals(getNodes(), response.getNodes())
&& Objects.equals(equalsHashCodeNodesMap(), response.equalsHashCodeNodesMap());
&& Objects.equals(getClusterName(), response.getClusterName())
&& Objects.equals(equalsHashCodeFailures(), response.equalsHashCodeFailures())
&& Objects.equals(getNodes(), response.getNodes())
&& Objects.equals(equalsHashCodeNodesMap(), response.equalsHashCodeNodesMap());
}

/*
Expand All @@ -167,7 +168,7 @@ public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
EqualsHashCodeFailedNodeException other = (EqualsHashCodeFailedNodeException) o;
return Objects.equals(failedNodeException.nodeId(), other.failedNodeException.nodeId())
&& Objects.equals(failedNodeException.getMessage(), other.failedNodeException.getMessage());
&& Objects.equals(failedNodeException.getMessage(), other.failedNodeException.getMessage());
}

@Override
Expand All @@ -187,7 +188,7 @@ private synchronized Map<String, NodeResponse> equalsHashCodeNodesMap() {
}
}

public static class NodeRequest extends TransportRequest {
public static class NodeRequest extends AbstractTransportRequest {

private final String[] databaseIds;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.transport.AbstractTransportRequest;
import org.elasticsearch.transport.TransportRequest;
import org.elasticsearch.xcontent.ToXContentObject;
import org.elasticsearch.xcontent.XContentBuilder;
Expand Down Expand Up @@ -68,12 +69,13 @@ public boolean equals(Object obj) {
}
}

public static class NodeRequest extends TransportRequest {
public static class NodeRequest extends AbstractTransportRequest {
public NodeRequest(StreamInput in) throws IOException {
super(in);
}

public NodeRequest() {}
public NodeRequest() {
}
}

public static class Response extends BaseNodesResponse<NodeResponse> implements Writeable, ToXContentObject {
Expand Down Expand Up @@ -175,17 +177,17 @@ protected NodeResponse(StreamInput in) throws IOException {
databases = in.readCollectionAsImmutableSet(StreamInput::readString);
filesInTemp = in.readCollectionAsImmutableSet(StreamInput::readString);
configDatabases = in.getTransportVersion().onOrAfter(TransportVersions.V_8_0_0)
? in.readCollectionAsImmutableSet(StreamInput::readString)
: null;
? in.readCollectionAsImmutableSet(StreamInput::readString)
: null;
}

protected NodeResponse(
DiscoveryNode node,
GeoIpDownloaderStats downloaderStats,
CacheStats cacheStats,
Set<String> databases,
Set<String> filesInTemp,
Set<String> configDatabases
DiscoveryNode node,
GeoIpDownloaderStats downloaderStats,
CacheStats cacheStats,
Set<String> databases,
Set<String> filesInTemp,
Set<String> configDatabases
) {
super(node);
this.downloaderStats = downloaderStats;
Expand Down Expand Up @@ -237,10 +239,10 @@ public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
NodeResponse that = (NodeResponse) o;
return downloaderStats.equals(that.downloaderStats)
&& Objects.equals(cacheStats, that.cacheStats)
&& databases.equals(that.databases)
&& filesInTemp.equals(that.filesInTemp)
&& Objects.equals(configDatabases, that.configDatabases);
&& Objects.equals(cacheStats, that.cacheStats)
&& databases.equals(that.databases)
&& filesInTemp.equals(that.filesInTemp)
&& Objects.equals(configDatabases, that.configDatabases);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.IndexVersion;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.transport.AbstractTransportRequest;
import org.elasticsearch.transport.TransportRequest;
import org.elasticsearch.xpack.core.security.authc.Authentication;
import org.elasticsearch.xpack.core.security.authc.Authentication.RealmRef;
Expand Down Expand Up @@ -47,7 +48,7 @@ public void testGetAuthorizationInfo() {

public void testAuthorizeRunAs() {
final String action = "cluster:monitor/foo";
final TransportRequest request = new TransportRequest() {
final TransportRequest request = new AbstractTransportRequest() {
};
CustomAuthorizationEngine engine = new CustomAuthorizationEngine();
// unauthorized
Expand Down Expand Up @@ -181,7 +182,7 @@ public void testAuthorizeIndexAction() {

private RequestInfo getRequestInfo() {
final String action = "cluster:monitor/foo";
final TransportRequest request = new TransportRequest() {
final TransportRequest request = new AbstractTransportRequest() {
};
final Authentication authentication = Authentication.newRealmAuthentication(
new User("joe", "custom_superuser"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@

import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.transport.AbstractTransportRequest;
import org.elasticsearch.transport.TransportRequest;

import java.io.IOException;

public abstract class ActionRequest extends TransportRequest {
public abstract class ActionRequest extends AbstractTransportRequest {

public ActionRequest() {
super();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.tasks.Task;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.AbstractTransportRequest;
import org.elasticsearch.transport.TransportRequest;
import org.elasticsearch.transport.TransportService;

Expand All @@ -33,11 +34,11 @@
import java.util.Set;

public class TransportNodesCapabilitiesAction extends TransportNodesAction<
NodesCapabilitiesRequest,
NodesCapabilitiesResponse,
TransportNodesCapabilitiesAction.NodeCapabilitiesRequest,
NodeCapability,
Void> {
NodesCapabilitiesRequest,
NodesCapabilitiesResponse,
TransportNodesCapabilitiesAction.NodeCapabilitiesRequest,
NodeCapability,
Void> {

public static final ActionType<NodesCapabilitiesResponse> TYPE = new ActionType<>("cluster:monitor/nodes/capabilities");

Expand All @@ -46,30 +47,30 @@ public class TransportNodesCapabilitiesAction extends TransportNodesAction<

@Inject
public TransportNodesCapabilitiesAction(
ThreadPool threadPool,
ClusterService clusterService,
TransportService transportService,
ActionFilters actionFilters,
RestController restController,
FeatureService featureService
ThreadPool threadPool,
ClusterService clusterService,
TransportService transportService,
ActionFilters actionFilters,
RestController restController,
FeatureService featureService
) {
super(
TYPE.name(),
clusterService,
transportService,
actionFilters,
NodeCapabilitiesRequest::new,
threadPool.executor(ThreadPool.Names.MANAGEMENT)
TYPE.name(),
clusterService,
transportService,
actionFilters,
NodeCapabilitiesRequest::new,
threadPool.executor(ThreadPool.Names.MANAGEMENT)
);
this.restController = restController;
this.featureService = featureService;
}

@Override
protected NodesCapabilitiesResponse newResponse(
NodesCapabilitiesRequest request,
List<NodeCapability> responses,
List<FailedNodeException> failures
NodesCapabilitiesRequest request,
List<NodeCapability> responses,
List<FailedNodeException> failures
) {
return new NodesCapabilitiesResponse(clusterService.getClusterName(), responses, failures);
}
Expand Down Expand Up @@ -101,16 +102,16 @@ protected NodeCapability newNodeResponse(StreamInput in, DiscoveryNode node) thr
@Override
protected NodeCapability nodeOperation(NodeCapabilitiesRequest request, Task task) {
boolean supported = restController.checkSupported(
request.method,
request.path,
request.parameters,
request.capabilities,
request.restApiVersion
request.method,
request.path,
request.parameters,
request.capabilities,
request.restApiVersion
);
return new NodeCapability(supported, transportService.getLocalNode());
}

public static class NodeCapabilitiesRequest extends TransportRequest {
public static class NodeCapabilitiesRequest extends AbstractTransportRequest {
private final RestRequest.Method method;
private final String path;
private final Set<String> parameters;
Expand All @@ -128,11 +129,11 @@ public NodeCapabilitiesRequest(StreamInput in) throws IOException {
}

public NodeCapabilitiesRequest(
RestRequest.Method method,
String path,
Set<String> parameters,
Set<String> capabilities,
RestApiVersion restApiVersion
RestRequest.Method method,
String path,
Set<String> parameters,
Set<String> capabilities,
RestApiVersion restApiVersion
) {
this.method = method;
this.path = path;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,48 +21,50 @@
import org.elasticsearch.injection.guice.Inject;
import org.elasticsearch.tasks.Task;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.AbstractTransportRequest;
import org.elasticsearch.transport.TransportRequest;
import org.elasticsearch.transport.TransportService;

import java.io.IOException;
import java.util.List;

@UpdateForV10(owner = UpdateForV10.Owner.CORE_INFRA) // this can be removed in v10. It may be called by v8 nodes to v9 nodes.
@UpdateForV10(owner = UpdateForV10.Owner.CORE_INFRA)
// this can be removed in v10. It may be called by v8 nodes to v9 nodes.
public class TransportNodesFeaturesAction extends TransportNodesAction<
NodesFeaturesRequest,
NodesFeaturesResponse,
TransportNodesFeaturesAction.NodeFeaturesRequest,
NodeFeatures,
Void> {
NodesFeaturesRequest,
NodesFeaturesResponse,
TransportNodesFeaturesAction.NodeFeaturesRequest,
NodeFeatures,
Void> {

public static final ActionType<NodesFeaturesResponse> TYPE = new ActionType<>("cluster:monitor/nodes/features");

private final FeatureService featureService;

@Inject
public TransportNodesFeaturesAction(
ThreadPool threadPool,
ClusterService clusterService,
TransportService transportService,
ActionFilters actionFilters,
FeatureService featureService
ThreadPool threadPool,
ClusterService clusterService,
TransportService transportService,
ActionFilters actionFilters,
FeatureService featureService
) {
super(
TYPE.name(),
clusterService,
transportService,
actionFilters,
NodeFeaturesRequest::new,
threadPool.executor(ThreadPool.Names.MANAGEMENT)
TYPE.name(),
clusterService,
transportService,
actionFilters,
NodeFeaturesRequest::new,
threadPool.executor(ThreadPool.Names.MANAGEMENT)
);
this.featureService = featureService;
}

@Override
protected NodesFeaturesResponse newResponse(
NodesFeaturesRequest request,
List<NodeFeatures> responses,
List<FailedNodeException> failures
NodesFeaturesRequest request,
List<NodeFeatures> responses,
List<FailedNodeException> failures
) {
return new NodesFeaturesResponse(clusterService.getClusterName(), responses, failures);
}
Expand All @@ -82,11 +84,12 @@ protected NodeFeatures nodeOperation(NodeFeaturesRequest request, Task task) {
return new NodeFeatures(featureService.getNodeFeatures().keySet(), transportService.getLocalNode());
}

public static class NodeFeaturesRequest extends TransportRequest {
public static class NodeFeaturesRequest extends AbstractTransportRequest {
public NodeFeaturesRequest(StreamInput in) throws IOException {
super(in);
}

public NodeFeaturesRequest() {}
public NodeFeaturesRequest() {
}
}
}
Loading