Skip to content

Commit

Permalink
Ensure that supported params are computed once
Browse files Browse the repository at this point in the history
A few of today's REST handler implementations compute a new set of
supported parameters on each request. This is needlessly inefficient
since the set never changes. This commit fixes those implementations and
adds assertions to verify that we are returning the exact same instance
each time.

Backport of elastic#113953 to `8.x`
  • Loading branch information
DaveCTurner committed Oct 2, 2024
1 parent 259bca9 commit b2d9639
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@
@ServerlessScope(Scope.PUBLIC)
public class RestGetDataStreamsAction extends BaseRestHandler {

private static final Set<String> SUPPORTED_QUERY_PARAMETERS = Set.copyOf(
Sets.union(
RestRequest.INTERNAL_MARKER_REQUEST_PARAMETERS,
Set.of(
"name",
"include_defaults",
"timeout",
"master_timeout",
IndicesOptions.WildcardOptions.EXPAND_WILDCARDS,
IndicesOptions.ConcreteTargetOptions.IGNORE_UNAVAILABLE,
IndicesOptions.WildcardOptions.ALLOW_NO_INDICES,
IndicesOptions.GatekeeperOptions.IGNORE_THROTTLED,
"verbose"
)
)
);

@Override
public String getName() {
return "get_data_streams_action";
Expand Down Expand Up @@ -63,19 +80,6 @@ public Set<String> supportedCapabilities() {

@Override
public Set<String> supportedQueryParameters() {
return Sets.union(
RestRequest.INTERNAL_MARKER_REQUEST_PARAMETERS,
Set.of(
"name",
"include_defaults",
"timeout",
"master_timeout",
IndicesOptions.WildcardOptions.EXPAND_WILDCARDS,
IndicesOptions.ConcreteTargetOptions.IGNORE_UNAVAILABLE,
IndicesOptions.WildcardOptions.ALLOW_NO_INDICES,
IndicesOptions.GatekeeperOptions.IGNORE_THROTTLED,
"verbose"
)
);
return SUPPORTED_QUERY_PARAMETERS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public final long getUsageCount() {
public final void handleRequest(RestRequest request, RestChannel channel, NodeClient client) throws Exception {
// check if the query has any parameters that are not in the supported set (if declared)
Set<String> supported = allSupportedParameters();
assert supported == allSupportedParameters() : getName() + ": did not return same instance from allSupportedParameters()";
if (supported != null) {
var allSupported = Sets.union(
RestResponse.RESPONSE_PARAMS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,8 @@ public boolean checkSupported(

if (handler != null) {
var supportedParams = handler.supportedQueryParameters();
assert supportedParams == handler.supportedQueryParameters()
: handler.getName() + ": did not return same instance from supportedQueryParameters()";
return (supportedParams == null || supportedParams.containsAll(parameters))
&& handler.supportedCapabilities().containsAll(capabilities);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@
public class RestClusterStatsAction extends BaseRestHandler {

private static final Set<String> SUPPORTED_CAPABILITIES = Set.of("human-readable-total-docs-size");
private static final Set<String> SUPPORTED_CAPABILITIES_CCS_STATS = Sets.union(SUPPORTED_CAPABILITIES, Set.of("ccs-stats"));
private static final Set<String> SUPPORTED_CAPABILITIES_CCS_STATS = Sets.copyOf(
Sets.union(SUPPORTED_CAPABILITIES, Set.of("ccs-stats"))
);
public static final FeatureFlag CCS_TELEMETRY_FEATURE_FLAG = new FeatureFlag("ccs_telemetry");
private static final Set<String> SUPPORTED_QUERY_PARAMETERS = Set.of("include_remotes", "nodeId", REST_TIMEOUT_PARAM);

@Override
public List<Route> routes() {
Expand All @@ -47,7 +50,7 @@ public String getName() {

@Override
public Set<String> supportedQueryParameters() {
return Set.of("include_remotes", "nodeId", REST_TIMEOUT_PARAM);
return SUPPORTED_QUERY_PARAMETERS;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ public class RestNodesCapabilitiesAction extends BaseRestHandler {

public static final NodeFeature CAPABILITIES_ACTION = new NodeFeature("rest.capabilities_action");
public static final NodeFeature LOCAL_ONLY_CAPABILITIES = new NodeFeature("rest.local_only_capabilities");
private static final Set<String> SUPPORTED_QUERY_PARAMETERS = Set.of(
"timeout",
"method",
"path",
"parameters",
"capabilities",
"local_only"
);

@Override
public List<Route> routes() {
Expand All @@ -40,7 +48,7 @@ public List<Route> routes() {

@Override
public Set<String> supportedQueryParameters() {
return Set.of("timeout", "method", "path", "parameters", "capabilities", "local_only");
return SUPPORTED_QUERY_PARAMETERS;
}

@Override
Expand Down

0 comments on commit b2d9639

Please sign in to comment.