Skip to content

Cluster Allocation Explain API Throws IAE #130389

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

joshua-adams-1
Copy link
Contributor

@joshua-adams-1 joshua-adams-1 commented Jul 1, 2025

Throws an Illegal Argument Exception when a non-integer value is passed as the shard parameter. The legacy behaviour of allowing strings to be passed, providing they are strings containing integers, is maintained. This is a prerequisite PR to #129342

Throws an Illegal Argument Exception when a non-integer value is passed
as the `shard` parameter. The legacy behaviour of allowing strings to be
 passed, providing they are strings containing integers, is maintained.
@joshua-adams-1 joshua-adams-1 force-pushed the add-iae-when-shard-is-non-integer branch from a6a4efe to 2a697ef Compare July 3, 2025 11:53
Copy link
Member

@pxsalehi pxsalehi left a comment

Choose a reason for hiding this comment

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

Left a few comments.
Is this ready for a full review? If yes, please mark it as ready and not draft.

@@ -31,7 +31,29 @@ public class ClusterAllocationExplainRequest extends MasterNodeRequest<ClusterAl
private static final ObjectParser<ClusterAllocationExplainRequest, Void> PARSER = new ObjectParser<>("cluster/allocation/explain");
static {
PARSER.declareString(ClusterAllocationExplainRequest::setIndex, new ParseField("index"));
PARSER.declareInt(ClusterAllocationExplainRequest::setShard, new ParseField("shard"));
Copy link
Member

Choose a reason for hiding this comment

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

not clear to me what the problem with declareInt is. Is this where the lenience is and it would silently accept and convert a float?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, declareInt implicitly allows floats, doubles, and stringified floats and doubles, to be passed where an integer parameter is expected. I have removed that explicit behaviour, and then added test cases

String text = xContentParser.text();
try {
// Only accept if the string is a valid integer representation
if (text.matches("[-+]?\\d+")) {
Copy link
Member

Choose a reason for hiding this comment

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

why do you need the regex? wouldn't relying on parseInt be enough?

Copy link
Member

Choose a reason for hiding this comment

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

you could also simplify these if/else statements. If you return or throw as the last and only statement of your if, you don't need the else and can just "do the else" after the if statement.

@@ -47,6 +48,11 @@ public boolean allowSystemIndexAccessByDefault() {
return true;
}

@Override
public Set<String> supportedCapabilities() {
return Set.of("shard_parameter_non_integer_validation");
Copy link
Member

Choose a reason for hiding this comment

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

👍🏼

@@ -84,18 +158,32 @@
- match: { acknowledged: true }

- do:
catch: /x_content_parse_exception/
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we should expose this exception. It's rather cryptic and internal. I think something like bad request would be more fitting. this could be its error.type. One example I found skimming through the yaml tests is in resources/rest-api-spec/test/cluster.desired_nodes/10_basic.yml.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think it's exposed as this exception. Happy to proven wrong, but from memory I asked about this and it gets converted somewhere to a BAD_REQUEST and this is a 4XX error. I'd be aligned with changing this errors, but this is the current, prod functionality and that has, by definition, been accepted.

Example test from https://github.com/elastic/elasticsearch/pull/129342/files

"cluster shard allocation explanation test with numerical index parameter in the body":
  - do:
      indices.create:
        index: test

  - match: { acknowledged: true }

  - do:
      catch: /x_content_parse_exception/
      cluster.allocation_explain:
        body: { "index": 0, "shard": 0, "primary": true }

@pxsalehi
Copy link
Member

pxsalehi commented Jul 4, 2025

You should also add the area label for allocation (:Distributed Coordination/Allocation). And the PR type. I think >enhancement would make sense. Since then it would end up in the change log for the release.

@joshua-adams-1 joshua-adams-1 added >enhancement :Distributed Coordination/Distributed A catch all label for anything in the Distributed Coordination area. Please avoid if you can. labels Jul 4, 2025
@joshua-adams-1
Copy link
Contributor Author

You should also add the area label for allocation (:Distributed Coordination/Allocation). And the PR type. I think >enhancement would make sense. Since then it would end up in the change log for the release.

Have done!

@elasticsearchmachine
Copy link
Collaborator

Hi @joshua-adams-1, I've created a changelog YAML for you.

@joshua-adams-1
Copy link
Contributor Author

Left a few comments. Is this ready for a full review? If yes, please mark it as ready and not draft.

Ahaha forgot to publish it, whoops!

@joshua-adams-1 joshua-adams-1 marked this pull request as ready for review July 4, 2025 14:55
@elasticsearchmachine elasticsearchmachine added the Team:Distributed Coordination Meta label for Distributed Coordination team label Jul 4, 2025
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-distributed-coordination (Team:Distributed Coordination)

Copy link
Contributor

@DaveCTurner DaveCTurner left a comment

Choose a reason for hiding this comment

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

I'm afraid I don't think we should do this, at least not as proposed here. The behaviour of declareInt encodes the way that we parse integer parameters across the entire Elasticsearch API surface. It'd be super-weird to make just this one API be stricter about this parsing.

That's not to say that I think it's a good thing to accept (and truncate) non-integer arguments in our APIs in general, just that I would prefer consistency across the whole API surface. If we're going to fix this, we should fix it everywhere.

Moreover this is a breaking change. We can't justify adopting this change in a minor release since it's not fixing a bug (or at least not a bug which is serious enough to warrant a breaking change in a minor release). Instead, let's mark declareInt with an @UpdateForV10 marker and come back to this lenience when we prepare for the next major release.

@joshua-adams-1
Copy link
Contributor Author

@DaveCTurner Happy to mark it for V10. Who would be the owner of this change? Us, or core infra?

@DaveCTurner
Copy link
Contributor

I think it'd be core/infra really, the parsing code falls under :Core/Infra/REST API

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Distributed Coordination/Distributed A catch all label for anything in the Distributed Coordination area. Please avoid if you can. >enhancement Team:Distributed Coordination Meta label for Distributed Coordination team v9.2.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants