Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,11 @@ public static Predicate<RestApiVersion> 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;
};
}
}
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 @@ -22,7 +22,7 @@ public class NodesCapabilitiesRequest extends BaseNodesRequest {
private String path = "/";
private Set<String> parameters = Set.of();
private Set<String> capabilities = Set.of();
private RestApiVersion restApiVersion = RestApiVersion.current();
private RestApiVersion restApiVersion = null;

public NodesCapabilitiesRequest() {
// send to all nodes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<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 @@ -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(
Expand All @@ -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)
Expand All @@ -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) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should this be a new TransportVersion? Format of the message doesn't change, but there is a change in how value is handled

out.writeVInt(IMPLICIT_REST_API_VERSION);
} else {
out.writeVInt(restApiVersionMajor);
}
}
}
}
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 @@ -350,8 +350,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 @@ -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<String, HttpRouteStats> getStats() {
final Iterator<MethodHandlers> methodHandlersIterator = handlers.allNodeValues();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down