From 4017eede45d9bebefa1e806647115311145a8703 Mon Sep 17 00:00:00 2001 From: alexey-ivanov-es Date: Fri, 14 Feb 2025 15:53:29 +0000 Subject: [PATCH 1/4] ES-10242 Update TransportNodesCapabilitiesAction for V9 --- .../org/elasticsearch/core/RestApiVersion.java | 16 ++++++---------- .../SimpleNodesCapabilitiesIT.java | 8 ++++++++ .../TransportNodesCapabilitiesAction.java | 14 +++++++++----- .../org/elasticsearch/rest/RestController.java | 18 +++++++++++++++++- 4 files changed, 40 insertions(+), 16 deletions(-) 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 b08d32a9e8ee1..06c331418a9cd 100644 --- a/libs/core/src/main/java/org/elasticsearch/core/RestApiVersion.java +++ b/libs/core/src/main/java/org/elasticsearch/core/RestApiVersion.java @@ -62,15 +62,11 @@ public static Predicate onOrAfter(RestApiVersion restApiVersion) }; } - public static RestApiVersion forMajor(int major) { - switch (major) { - case 7 -> { - return V_7; - } - case 8 -> { - return V_8; - } - default -> throw new IllegalArgumentException("Unknown REST API version " + major); - } + public static RestApiVersion forMajorIfKnown(int major) { + return switch (major) { + case 7 -> V_7; + case 8 -> V_8; + 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..3cc002439935e 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/TransportNodesCapabilitiesAction.java b/server/src/main/java/org/elasticsearch/action/admin/cluster/node/capabilities/TransportNodesCapabilitiesAction.java index 8df34d882941a..a014f3deb1a74 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 @@ -115,17 +115,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); @@ -134,7 +136,9 @@ 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(); + // V9 can send IMPLICIT_REST_API_VERSION + restApiVersionMajor = (versionFromMessage != IMPLICIT_REST_API_VERSION) ? versionFromMessage : null; } public NodeCapabilitiesRequest( @@ -148,7 +152,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; } @Override @@ -159,7 +163,7 @@ public void writeTo(StreamOutput out) throws IOException { out.writeString(path); out.writeCollection(parameters, StreamOutput::writeString); out.writeCollection(capabilities, StreamOutput::writeString); - out.writeVInt(restApiVersion.major); + 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 0c55132b3d78c..b3bf955015591 100644 --- a/server/src/main/java/org/elasticsearch/rest/RestController.java +++ b/server/src/main/java/org/elasticsearch/rest/RestController.java @@ -385,8 +385,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; @@ -408,6 +414,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(); From f684653c5bd89af284f53112925da41a5ef077e5 Mon Sep 17 00:00:00 2001 From: Alexey Ivanov Date: Fri, 14 Feb 2025 15:57:36 +0000 Subject: [PATCH 2/4] Update docs/changelog/122613.yaml --- docs/changelog/122613.yaml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 docs/changelog/122613.yaml diff --git a/docs/changelog/122613.yaml b/docs/changelog/122613.yaml new file mode 100644 index 0000000000000..9f0220f84250c --- /dev/null +++ b/docs/changelog/122613.yaml @@ -0,0 +1,5 @@ +pr: 122613 +summary: Update `TransportNodesCapabilitiesAction` for V9 +area: Infra/REST API +type: enhancement +issues: [] From 7842c07fc69d900ed0ed58096ea5dcfc14a2703a Mon Sep 17 00:00:00 2001 From: elasticsearchmachine Date: Fri, 14 Feb 2025 16:04:14 +0000 Subject: [PATCH 3/4] [CI] Auto commit changes from spotless --- .../nodescapabilities/SimpleNodesCapabilitiesIT.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/server/src/internalClusterTest/java/org/elasticsearch/nodescapabilities/SimpleNodesCapabilitiesIT.java b/server/src/internalClusterTest/java/org/elasticsearch/nodescapabilities/SimpleNodesCapabilitiesIT.java index 3cc002439935e..ae029bb823e64 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/nodescapabilities/SimpleNodesCapabilitiesIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/nodescapabilities/SimpleNodesCapabilitiesIT.java @@ -45,9 +45,9 @@ public void testNodesCapabilities() throws IOException { 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(); + response = clusterAdmin().nodesCapabilities( + new NodesCapabilitiesRequest().path("_capabilities").parameters("method", "path").restApiVersion(RestApiVersion.current()) + ).actionGet(); assertThat(response.getNodes(), hasSize(2)); assertThat(response.isSupported(), isPresentWith(true)); From 8bfbb1895b6c75dd4576f2ea55086abad8f10d75 Mon Sep 17 00:00:00 2001 From: alexey-ivanov-es Date: Fri, 14 Feb 2025 16:07:22 +0000 Subject: [PATCH 4/4] assert that restApiVersion is never null from request --- .../node/capabilities/TransportNodesCapabilitiesAction.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 a014f3deb1a74..37ce543cccba1 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 @@ -152,7 +152,9 @@ public NodeCapabilitiesRequest( this.path = path; this.parameters = Set.copyOf(parameters); this.capabilities = Set.copyOf(capabilities); - this.restApiVersionMajor = (restApiVersion != null) ? restApiVersion.major : null; + assert restApiVersion != null; + // restApiVersionMajor can be null only if it is received as part of transport message from v9 node + this.restApiVersionMajor = restApiVersion.major; } @Override