Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions docs/changelog/122613.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 122613
summary: Update `TransportNodesCapabilitiesAction` for V9
area: Infra/REST API
type: enhancement
issues: []
16 changes: 6 additions & 10 deletions libs/core/src/main/java/org/elasticsearch/core/RestApiVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,11 @@ public static Predicate<RestApiVersion> 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;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> parameters;
private final Set<String> capabilities;
private final RestApiVersion restApiVersion;
private final Byte restApiVersionMajor;

public NodeCapabilitiesRequest(StreamInput in) throws IOException {
super(in);
Expand All @@ -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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

8.18/8.19 should be able to understand message sent by v9 node containing either 9 or 0

}

public NodeCapabilitiesRequest(
Expand All @@ -148,7 +152,9 @@ public NodeCapabilitiesRequest(
this.path = path;
this.parameters = Set.copyOf(parameters);
this.capabilities = Set.copyOf(capabilities);
this.restApiVersion = restApiVersion;
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
Expand All @@ -159,7 +165,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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

8.18/8.19 send only 7 or 8 here, any other node they can form cluster with understands it

}
}
}
18 changes: 17 additions & 1 deletion server/src/main/java/org/elasticsearch/rest/RestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,14 @@ public boolean checkSupported(
String path,
Set<String> parameters,
Set<String> 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<MethodHandlers> allHandlers = getAllHandlers(null, path);
while (allHandlers.hasNext()) {
RestHandler handler;
Expand All @@ -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<String, HttpRouteStats> getStats() {
final Iterator<MethodHandlers> methodHandlersIterator = handlers.allNodeValues();
Expand Down