- Notifications
You must be signed in to change notification settings - Fork 25.5k
[ML] Checking for presence of error object when validating response #118375
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
Changes from all commits
b934664
4148ca9
dc09a1c
b70638b
3128139
2f0544f
94ba505
6b719de
ad3ade5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pr: 118375 | ||
summary: Check for presence of error object when validating streaming responses from integrations in the inference API | ||
area: Machine Learning | ||
type: enhancement | ||
issues: [] |
Original file line number | Diff line number | Diff line change |
---|---|---|
| @@ -7,16 +7,20 @@ | |
| ||
package org.elasticsearch.xpack.inference.external.http.retry; | ||
| ||
import org.apache.logging.log4j.Logger; | ||
import org.elasticsearch.ElasticsearchStatusException; | ||
import org.elasticsearch.common.Strings; | ||
import org.elasticsearch.inference.InferenceServiceResults; | ||
import org.elasticsearch.rest.RestStatus; | ||
import org.elasticsearch.xpack.inference.external.http.HttpResult; | ||
import org.elasticsearch.xpack.inference.external.request.Request; | ||
import org.elasticsearch.xpack.inference.logging.ThrottlerManager; | ||
| ||
import java.util.Objects; | ||
import java.util.function.Function; | ||
| ||
import static org.elasticsearch.core.Strings.format; | ||
import static org.elasticsearch.xpack.inference.external.http.HttpUtils.checkForEmptyBody; | ||
| ||
public abstract class BaseResponseHandler implements ResponseHandler { | ||
| ||
| @@ -27,14 +31,15 @@ public abstract class BaseResponseHandler implements ResponseHandler { | |
public static final String REDIRECTION = "Unhandled redirection"; | ||
public static final String CONTENT_TOO_LARGE = "Received a content too large status code"; | ||
public static final String UNSUCCESSFUL = "Received an unsuccessful status code"; | ||
public static final String SERVER_ERROR_OBJECT = "Received an error response"; | ||
public static final String BAD_REQUEST = "Received a bad request status code"; | ||
public static final String METHOD_NOT_ALLOWED = "Received a method not allowed status code"; | ||
| ||
protected final String requestType; | ||
private final ResponseParser parseFunction; | ||
private final Function<HttpResult, ErrorMessage> errorParseFunction; | ||
private final Function<HttpResult, ErrorResponse> errorParseFunction; | ||
| ||
public BaseResponseHandler(String requestType, ResponseParser parseFunction, Function<HttpResult, ErrorMessage> errorParseFunction) { | ||
public BaseResponseHandler(String requestType, ResponseParser parseFunction, Function<HttpResult, ErrorResponse> errorParseFunction) { | ||
this.requestType = Objects.requireNonNull(requestType); | ||
this.parseFunction = Objects.requireNonNull(parseFunction); | ||
this.errorParseFunction = Objects.requireNonNull(errorParseFunction); | ||
| @@ -54,11 +59,42 @@ public String getRequestType() { | |
return requestType; | ||
} | ||
| ||
@Override | ||
public void validateResponse(ThrottlerManager throttlerManager, Logger logger, Request request, HttpResult result) { | ||
checkForFailureStatusCode(request, result); | ||
checkForEmptyBody(throttlerManager, logger, request, result); | ||
| ||
// When the response is streamed the status code could be 200 but the error object will be set | ||
// so we need to check for that specifically | ||
checkForErrorObject(request, result); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have one potential concern for this. This will be executed for non-streaming and streaming code paths. So if for some reason we get a 200 response back (or some other non failure status code) and the response object is a valid response but also happens to have a field that the error object expects (which depends on each service implementation) then this would fail. I doubt that would happen. If we were concerned about it we could create a new method | ||
} | ||
| ||
protected abstract void checkForFailureStatusCode(Request request, HttpResult result); | ||
| ||
private void checkForErrorObject(Request request, HttpResult result) { | ||
var errorEntity = errorParseFunction.apply(result); | ||
| ||
if (errorEntity.errorStructureFound()) { | ||
// We don't really know what happened because the status code was 200 so we'll return a failure and let the | ||
// client retry if necessary | ||
// If we did want to retry here, we'll need to determine if this was a streaming request, if it was | ||
// we shouldn't retry because that would replay the entire streaming request and the client would get | ||
// duplicate chunks back | ||
throw new RetryException(false, buildError(SERVER_ERROR_OBJECT, request, result, errorEntity)); | ||
} | ||
} | ||
| ||
protected Exception buildError(String message, Request request, HttpResult result) { | ||
var errorEntityMsg = errorParseFunction.apply(result); | ||
return buildError(message, request, result, errorEntityMsg); | ||
} | ||
| ||
protected Exception buildError(String message, Request request, HttpResult result, ErrorResponse errorResponse) { | ||
var responseStatusCode = result.response().getStatusLine().getStatusCode(); | ||
| ||
if (errorEntityMsg == null) { | ||
if (errorResponse == null | ||
|| errorResponse.errorStructureFound() == false | ||
|| Strings.isNullOrEmpty(errorResponse.getErrorMessage())) { | ||
return new ElasticsearchStatusException( | ||
format( | ||
"%s for request from inference entity id [%s] status [%s]", | ||
| @@ -76,7 +112,7 @@ protected Exception buildError(String message, Request request, HttpResult resul | |
message, | ||
request.getInferenceEntityId(), | ||
responseStatusCode, | ||
errorEntityMsg.getErrorMessage() | ||
errorResponse.getErrorMessage() | ||
), | ||
toRestStatus(responseStatusCode) | ||
); | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
| ||
package org.elasticsearch.xpack.inference.external.http.retry; | ||
| ||
import java.util.Objects; | ||
| ||
public class ErrorResponse { | ||
| ||
// Denotes an error object that was not found | ||
public static final ErrorResponse UNDEFINED_ERROR = new ErrorResponse(false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reason I need this is to differentiate between an error object being present but a specific field in the object missing (for example If a nested field is missing we return a created instance with | ||
| ||
private final String errorMessage; | ||
private final boolean errorStructureFound; | ||
| ||
public ErrorResponse(String errorMessage) { | ||
this.errorMessage = Objects.requireNonNull(errorMessage); | ||
this.errorStructureFound = true; | ||
} | ||
| ||
private ErrorResponse(boolean errorStructureFound) { | ||
this.errorMessage = ""; | ||
this.errorStructureFound = errorStructureFound; | ||
} | ||
| ||
public String getErrorMessage() { | ||
return errorMessage; | ||
} | ||
| ||
public boolean errorStructureFound() { | ||
return errorStructureFound; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move up into
BaseResponseHandler
.