diff --git a/docs/changelog/122609.yaml b/docs/changelog/122609.yaml new file mode 100644 index 0000000000000..bd188a341bdaa --- /dev/null +++ b/docs/changelog/122609.yaml @@ -0,0 +1,5 @@ +pr: 122609 +summary: Update `TransportNodesCapabilitiesAction` for V9 +area: Infra/REST API +type: enhancement +issues: [] diff --git a/libs/core/src/main/java/org/elasticsearch/core/RestApiVersion.java b/libs/core/src/main/java/org/elasticsearch/core/RestApiVersion.java index 085887fdcdb0c..1c828a625d6f1 100644 --- a/libs/core/src/main/java/org/elasticsearch/core/RestApiVersion.java +++ b/libs/core/src/main/java/org/elasticsearch/core/RestApiVersion.java @@ -61,15 +61,11 @@ public static Predicate onOrAfter(RestApiVersion restApiVersion) }; } - public static RestApiVersion forMajor(int major) { - switch (major) { - case 8 -> { - return V_8; - } - case 9 -> { - return V_9; - } - default -> throw new IllegalArgumentException("Unknown REST API version " + major); - } + public static RestApiVersion forMajorIfKnown(int major) { + return switch (major) { + case 8 -> V_8; + case 9 -> V_9; + default -> null; + }; } } diff --git a/server/src/internalClusterTest/java/org/elasticsearch/nodescapabilities/SimpleNodesCapabilitiesIT.java b/server/src/internalClusterTest/java/org/elasticsearch/nodescapabilities/SimpleNodesCapabilitiesIT.java index 494996f7f34f3..ae029bb823e64 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/nodescapabilities/SimpleNodesCapabilitiesIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/nodescapabilities/SimpleNodesCapabilitiesIT.java @@ -12,6 +12,7 @@ import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse; import org.elasticsearch.action.admin.cluster.node.capabilities.NodesCapabilitiesRequest; import org.elasticsearch.action.admin.cluster.node.capabilities.NodesCapabilitiesResponse; +import org.elasticsearch.core.RestApiVersion; import org.elasticsearch.test.ESIntegTestCase; import java.io.IOException; @@ -43,6 +44,13 @@ public void testNodesCapabilities() throws IOException { assertThat(response.getNodes(), hasSize(2)); assertThat(response.isSupported(), isPresentWith(true)); + // check we support REST API version parameters of the capabilities API + response = clusterAdmin().nodesCapabilities( + new NodesCapabilitiesRequest().path("_capabilities").parameters("method", "path").restApiVersion(RestApiVersion.current()) + ).actionGet(); + assertThat(response.getNodes(), hasSize(2)); + assertThat(response.isSupported(), isPresentWith(true)); + // check we don't support some other parameters of the capabilities API response = clusterAdmin().nodesCapabilities(new NodesCapabilitiesRequest().path("_capabilities").parameters("method", "invalid")) .actionGet(); diff --git a/server/src/main/java/org/elasticsearch/action/admin/cluster/node/capabilities/NodesCapabilitiesRequest.java b/server/src/main/java/org/elasticsearch/action/admin/cluster/node/capabilities/NodesCapabilitiesRequest.java index bd7d9c551c31d..2526f97d58f18 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/cluster/node/capabilities/NodesCapabilitiesRequest.java +++ b/server/src/main/java/org/elasticsearch/action/admin/cluster/node/capabilities/NodesCapabilitiesRequest.java @@ -22,7 +22,7 @@ public class NodesCapabilitiesRequest extends BaseNodesRequest { private String path = "/"; private Set parameters = Set.of(); private Set capabilities = Set.of(); - private RestApiVersion restApiVersion = RestApiVersion.current(); + private RestApiVersion restApiVersion = null; public NodesCapabilitiesRequest() { // send to all nodes diff --git a/server/src/main/java/org/elasticsearch/action/admin/cluster/node/capabilities/TransportNodesCapabilitiesAction.java b/server/src/main/java/org/elasticsearch/action/admin/cluster/node/capabilities/TransportNodesCapabilitiesAction.java index beb0e1f927de2..8c4a45caaaf97 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/cluster/node/capabilities/TransportNodesCapabilitiesAction.java +++ b/server/src/main/java/org/elasticsearch/action/admin/cluster/node/capabilities/TransportNodesCapabilitiesAction.java @@ -93,17 +93,19 @@ protected NodeCapability nodeOperation(NodeCapabilitiesRequest request, Task tas request.path, request.parameters, request.capabilities, - request.restApiVersion + request.restApiVersionMajor ); return new NodeCapability(supported, transportService.getLocalNode()); } public static class NodeCapabilitiesRequest extends TransportRequest { + private static final int IMPLICIT_REST_API_VERSION = 0; + private final RestRequest.Method method; private final String path; private final Set parameters; private final Set capabilities; - private final RestApiVersion restApiVersion; + private final Byte restApiVersionMajor; public NodeCapabilitiesRequest(StreamInput in) throws IOException { super(in); @@ -112,7 +114,8 @@ public NodeCapabilitiesRequest(StreamInput in) throws IOException { path = in.readString(); parameters = in.readCollectionAsImmutableSet(StreamInput::readString); capabilities = in.readCollectionAsImmutableSet(StreamInput::readString); - restApiVersion = RestApiVersion.forMajor(in.readVInt()); + byte versionFromMessage = (byte) in.readVInt(); + restApiVersionMajor = (versionFromMessage != IMPLICIT_REST_API_VERSION) ? versionFromMessage : null; } public NodeCapabilitiesRequest( @@ -126,7 +129,7 @@ public NodeCapabilitiesRequest( this.path = path; this.parameters = Set.copyOf(parameters); this.capabilities = Set.copyOf(capabilities); - this.restApiVersion = restApiVersion; + this.restApiVersionMajor = (restApiVersion != null) ? restApiVersion.major : null; } @UpdateForV9(owner = UpdateForV9.Owner.CORE_INFRA) // 8.x blows up in a mixed cluster when trying to read RestApiVersion.forMajor(9) @@ -141,9 +144,11 @@ public void writeTo(StreamOutput out) throws IOException { out.writeString(path); out.writeCollection(parameters, StreamOutput::writeString); out.writeCollection(capabilities, StreamOutput::writeString); - // Fixme: lies! all lies! - out.writeVInt(8); - // out.writeVInt(restApiVersion.major); + if (restApiVersionMajor == null) { + out.writeVInt(IMPLICIT_REST_API_VERSION); + } else { + out.writeVInt(restApiVersionMajor); + } } } } diff --git a/server/src/main/java/org/elasticsearch/rest/RestController.java b/server/src/main/java/org/elasticsearch/rest/RestController.java index 49801499ea991..b433ca736ab95 100644 --- a/server/src/main/java/org/elasticsearch/rest/RestController.java +++ b/server/src/main/java/org/elasticsearch/rest/RestController.java @@ -350,8 +350,14 @@ public boolean checkSupported( String path, Set parameters, Set capabilities, - RestApiVersion restApiVersion + Byte restApiVersionMajor ) { + RestApiVersion restApiVersion = getRestApiVersionIfKnown(restApiVersionMajor); + // if explicitly requested RestApiVersion is not known by this node, it should respond that it doesn't support the capability + if (restApiVersion == null) { + return false; + } + Iterator allHandlers = getAllHandlers(null, path); while (allHandlers.hasNext()) { RestHandler handler; @@ -373,6 +379,16 @@ public boolean checkSupported( return false; } + private static RestApiVersion getRestApiVersionIfKnown(Byte restApiVersionMajor) { + RestApiVersion restApiVersion; + if (restApiVersionMajor == null) { + restApiVersion = RestApiVersion.current(); + } else { + restApiVersion = RestApiVersion.forMajorIfKnown(restApiVersionMajor); + } + return restApiVersion; + } + @Override public Map getStats() { final Iterator methodHandlersIterator = handlers.allNodeValues(); diff --git a/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestNodesCapabilitiesAction.java b/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestNodesCapabilitiesAction.java index 7d660e527a814..c58c4588cc2b5 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestNodesCapabilitiesAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestNodesCapabilitiesAction.java @@ -66,8 +66,11 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli NodesCapabilitiesRequest r = requestNodes.method(RestRequest.Method.valueOf(request.param("method", "GET"))) .path(path) .parameters(request.paramAsStringArray("parameters", Strings.EMPTY_ARRAY)) - .capabilities(request.paramAsStringArray("capabilities", Strings.EMPTY_ARRAY)) - .restApiVersion(request.getRestApiVersion()); + .capabilities(request.paramAsStringArray("capabilities", Strings.EMPTY_ARRAY)); + + if (request.hasExplicitRestApiVersion()) { + r.restApiVersion(request.getRestApiVersion()); + } return channel -> client.admin().cluster().nodesCapabilities(r, new NodesResponseRestListener<>(channel)); }