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

Don't throw exceptions during DocumentField serialization #95673

Merged

Conversation

romseygeek
Copy link
Contributor

@romseygeek romseygeek commented Apr 28, 2023

As a prerequisite to making search responses use chunked REST serialization,
we need to ensure that no exceptions are thrown when toXContent gets called.
DocumentFields can currently throw exceptions if their contained values are not
serializable. This commit changes the serialization code here to replace
unserializable values with a placeholder value rather than failing the whole request.

Relates to #95661

@romseygeek romseygeek added :Search/Search Search-related issues that do not fall into other categories >refactoring v8.9.0 labels Apr 28, 2023
@romseygeek romseygeek self-assigned this Apr 28, 2023
@elasticsearchmachine elasticsearchmachine added the Team:Search Meta label for search team label Apr 28, 2023
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-search (Team:Search)

Copy link
Contributor

@iverase iverase left a comment

Choose a reason for hiding this comment

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

LGTM

I was thinking we could implement toXContent in geo_shape doc value and return the same value but then it might happen again so this is more generic?

@romseygeek
Copy link
Contributor Author

Yes, it's not just geo_shape that this can conceivably apply to, so I thought to make it as generic as possible.

@romseygeek
Copy link
Contributor Author

@elasticmachine run elasticsearch-ci/part-2

try {
builder.value(value);
} catch (RuntimeException e) {
// if the value cannot be serialized, we catch here and return a placeholder value
Copy link
Contributor

@quux00 quux00 Apr 28, 2023

Choose a reason for hiding this comment

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

Since we are catching a generic RuntimeException, we don't know if this was an intentional error (UnsupportedOperationException) or unintentional (NPE or other type of unexpected error, possibly indicating a code bug). Should we inspect the error and log it if it is not UnsupportedOperationException?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could make the placeholder value more explicit by including the exception message maybe? I'm not sure how frequent this issue actually is, or how useful an error message would be for something like a script field.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd vote for logging to help find rare bugs and avoid "swallowing" unexpected errors, but I'm still ramping up on standard practices here and don't have much context on this issue yet :-)
But this is just optional feedback.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Having thought about this, I think I'd like to leave it as it is for now - really the only useful information we could return here would be a stack trace, which isn't really very user friendly. It shouldn't be too difficult to reproduce a problem if it comes up as we will have the document source.

Copy link
Member

Choose a reason for hiding this comment

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

this reminded me of Strings#toString, but there we catch IOException which is a checked exception. What are the exceptions that can happen in practice? I see that XContentBuilder#value may throw IllegalArgumentException. We could make the builder print out a more user-friendly error?

@quux00
Copy link
Contributor

quux00 commented Apr 28, 2023

Does this issue need to reference another GH issue in the commit message (was there a previous bug report issue)?

@romseygeek
Copy link
Contributor Author

Does this issue need to reference another GH issue in the commit message (was there a previous bug report issue)?

Good point, I've updated the description to refer to the overall search chunking meta issue.

@romseygeek
Copy link
Contributor Author

@elasticmachine update branch

@romseygeek romseygeek merged commit 6365749 into elastic:main May 2, 2023
@romseygeek romseygeek deleted the documentfield/xcontent-validation branch May 2, 2023 10:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
>refactoring :Search/Search Search-related issues that do not fall into other categories Team:Search Meta label for search team v8.9.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants