Skip to content
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

[ML] StartTrainedModelDeployment Request query params override body params #109487

Merged
merged 21 commits into from
Jun 18, 2024

Conversation

maxhniebergall
Copy link
Member

@maxhniebergall maxhniebergall commented Jun 7, 2024

@jonathan-buttner recently discovered an issue with the change I recently made to disable inference cache in serverless where the cache would not be disabled if the start deployment request contained a body.

This was surprising to me, and upon closer inspection I noticed that there were two parallel paths for parsing the the request, one which used the body and one which used the parameters. This had the effect of ignoring path parameters if a body was included. Additionally, if a body was included, parameters such as cache_size would cause errors if a body (including empty body) was included:

        "type": "illegal_argument_exception",
        "reason": "request [/_ml/trained_models/.elser_model_2/deployment/_start] contains unrecognized parameter: [cache_size]"

Now, if a parameter is provided in both the query params and body, an exception will be thrown if the parameters are different, unless the parameter provided in the body is the same as the default parameter.

@maxhniebergall maxhniebergall added >bug :ml Machine learning v8.15.0 labels Jun 7, 2024
@elasticsearchmachine elasticsearchmachine added the Team:ML Meta label for the ML team label Jun 7, 2024
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/ml-core (Team:ML)

@elasticsearchmachine
Copy link
Collaborator

Hi @maxhniebergall, I've created a changelog YAML for you.

@maxhniebergall maxhniebergall changed the title [ML] Perform checks on request values regardless of the existence of a body [ML] Request query params override body params Jun 7, 2024
@maxhniebergall maxhniebergall changed the title [ML] Request query params override body params [ML] StartTrainedModelDeployment Request query params override body params Jun 7, 2024
@@ -0,0 +1,5 @@
pr: 109487
summary: StartTrainedModelDeployment request query params now override body params
Copy link
Contributor

Choose a reason for hiding this comment

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

How about we remove the reference to the class name (users won't know what that means). Maybe something like `Star trained model deployment API requests query params now override body params.

Copy link
Member

@davidkyle davidkyle left a comment

Choose a reason for hiding this comment

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

LGTM

Is this considered a breaking change?

No this is bug fix. The option of using providing the params in the body is not documented but people may be relying on it so let's keep it. If it is not too complicated it would be good to check if the params are in both body and query params and error if they are different.

Copy link
Contributor

@jonathan-buttner jonathan-buttner left a comment

Choose a reason for hiding this comment

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

Max and I talked about this offline but I wanted to make sure I don't forget haha.

If I specify a body setting the cache_size in serverless, this will result in false for the block: if (restRequest.hasParam(CACHE_SIZE.getPreferredName())) { and that means it'll get overridden with a cache size of 0 in serverless.

We'll need to add some logic to check if cache_size has been specified in the body and not override it in that scenario.

@maxhniebergall
Copy link
Member Author

@elasticmachine merge upstream

elasticmachine and others added 4 commits June 12, 2024 06:01
…equal to the defaults were not specified in the body. This means that errors will not be thrown if the body and query parameters don't match as long as the body parameter is the same as the default.
…astic/elasticsearch into fixDisableInferenceCacheLogic

# Conflicts:
#	x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/StartTrainedModelDeploymentAction.java
#	x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/inference/RestStartTrainedModelDeploymentAction.java
@maxhniebergall
Copy link
Member Author

@elasticmachine merge upstream

@maxhniebergall
Copy link
Member Author

Now, if a parameter is provided in both the query params and body, an exception will be thrown if the parameters are different, unless the parameter provided in the body is the same as the default parameter.

return channel -> client.execute(StartTrainedModelDeploymentAction.INSTANCE, request, new RestToXContentListener<>(channel));
}

private Object sameParamInQueryAndBody(Object bodyParam, Object queryParam, Object paramDefault) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let me know what you think of this: How about we determine which value to use by passing either the already set value from the body (request.get*()) or if that's null then a default value in the restRequest.param*() call?

I think that'd look something like this:

        var queueCap = request.getQueueCapacity();
        request.setQueueCapacity(
                restRequest.paramAsInt(QUEUE_CAPACITY.getPreferredName(), Objects.requireNonNullElse(queueCap, StartTrainedModelDeploymentAction.DEFAULT_QUEUE_CAPACITY))
        );

That way the query parameter will override the body if it is specified, or we'll take the body value, or if it wasn't set we'll either take the default already set when initializing the StartTrainedModelDeploymentAction.Request or we'll take the default passed to Objects.requireNonNullElse.

If I overlooked the logic and we still need this method then here are a few suggestions:
Let's make it static and I think it can be generic so we can remove the need to cast the return value.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think the main difference from what you're proposing is that the sameParamInQueryAndBody throws an exception if the body does not match the query param.

Making the function static and generic does make sense though, I can do do that I think.

@maxhniebergall
Copy link
Member Author

@elasticmachine merge upstream

@maxhniebergall maxhniebergall merged commit 23fc151 into main Jun 18, 2024
16 checks passed
@maxhniebergall maxhniebergall deleted the fixDisableInferenceCacheLogic branch June 18, 2024 18:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
>bug :ml Machine learning Team:ML Meta label for the ML team v8.15.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants