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

Make BulkIndexError and ScanError serializable #2669

Merged
merged 4 commits into from
Nov 12, 2024

Conversation

seagrine
Copy link
Contributor

@seagrine seagrine commented Oct 17, 2024

I encountered an issue with pickling/unpickling BulkindexError.

unpickle_exception\n inst = func(*args)\n ^^^^^^^^^^^\nTypeError: BulkIndexError.**init**() missing 1 required positional argument: 'errors

I'm adding an implementation of __reduce__ to this (and ScanError) so they can be pickled and unpickled correctly. Added tests for it as well.

Copy link

cla-checker-service bot commented Oct 17, 2024

💚 CLA has been signed

Copy link

A documentation preview will be available soon.

Request a new doc build by commenting
  • Rebuild this PR: run docs-build
  • Rebuild this PR and all Elastic docs: run docs-build rebuild

run docs-build is much faster than run docs-build rebuild. A rebuild should only be needed in rare situations.

If your PR continues to fail for an unknown reason, the doc build pipeline may be broken. Elastic employees can check the pipeline status here.

@seagrine seagrine marked this pull request as draft October 17, 2024 18:00
@seagrine seagrine closed this Oct 17, 2024
@seagrine seagrine deleted the serializable-errors branch October 17, 2024 18:15
@seagrine seagrine restored the serializable-errors branch October 17, 2024 18:16
@seagrine seagrine reopened this Oct 17, 2024
@seagrine seagrine marked this pull request as ready for review October 17, 2024 18:20
@pquentin
Copy link
Member

buildkite test this please


class ScanError(Exception):
scroll_id: str

def __init__(self, scroll_id: str, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)
def __init__(self, scroll_id: str, shards_message: str) -> None:
Copy link
Contributor

@miguelgrinberg miguelgrinberg Nov 12, 2024

Choose a reason for hiding this comment

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

Are the typing changes in both exeception classes and the argument changes in ScanError necessary? I think you could have just added a __reduce__ method that writes all the arguments assuming the caller is passing pickle-friendly data. Not a huge deal, but to me it seems there is no point in restricting the arguments in ScanError, given that it is a backwards-incompatible change.

Copy link
Member

Choose a reason for hiding this comment

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

They're not necessary, but **kwargs was simply not working, and *args was overly broad. Restricting to what was actually used also really improved the types. Are you thinking that other libraries could be raising those exceptions with different parameters?

Copy link
Member

Choose a reason for hiding this comment

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

https://github.com/search?q=%22raise+BulkIndexError%22+language%3APython&type=code shows that raising BulkIndexError() is pretty common, but I've only changed the type annotation here, and all the samples I've seen indeed pass a string.

I've not seen public examples of raising ScanError, but I can restore *args.

Copy link
Member

Choose a reason for hiding this comment

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

I've restored backwards-compatibility in 0db50e4 (#2669), please take another look.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's highly unlikely that someone is raising ScanError, but yes, I just thought it was a change that is unrelated to making the class work with pickle. But as I said, it's a small observation, I think it is unlikely to cause problems.

Copy link
Contributor

@miguelgrinberg miguelgrinberg left a comment

Choose a reason for hiding this comment

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

LGTM, but I've noted that some changes in this PR did not seem necessary.

@pquentin
Copy link
Member

buildkite test this please

@pquentin pquentin merged commit 08addf2 into elastic:main Nov 12, 2024
13 checks passed
github-actions bot pushed a commit that referenced this pull request Nov 12, 2024
Co-authored-by: Quentin Pradet <quentin.pradet@elastic.co>
(cherry picked from commit 08addf2)
github-actions bot pushed a commit that referenced this pull request Nov 12, 2024
Co-authored-by: Quentin Pradet <quentin.pradet@elastic.co>
(cherry picked from commit 08addf2)
@pquentin
Copy link
Member

Thank you for your contribution, it is now released as part of 8.16.0: https://github.com/elastic/elasticsearch-py/releases/tag/v8.16.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants