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

Try deepcopy combine_fn and fallback to pickling if TypeError. #32645

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

claudevdm
Copy link
Contributor

Roll forward with #32598 + fix the copy behavior if a module is being copied.


Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:

  • Mention the appropriate issue in your description (for example: addresses #123), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, comment fixes #<ISSUE NUMBER> instead.
  • Update CHANGES.md with noteworthy changes.
  • If this contribution is large, please file an Apache Individual Contributor License Agreement.

See the Contributor Guide for more tips on how to make review process smoother.

To check the build health, please visit https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md

GitHub Actions Tests Status (on master branch)

Build python source distribution and wheels
Python tests
Java tests
Go tests

See CI.md for more information about GitHub Actions CI or the workflows README to see a list of phrases to trigger workflows.

@github-actions github-actions bot added the python label Oct 3, 2024
@claudevdm claudevdm marked this pull request as ready for review October 4, 2024 18:34
@claudevdm claudevdm marked this pull request as draft October 4, 2024 18:38
Copy link
Contributor

github-actions bot commented Oct 4, 2024

Assigning reviewers. If you would like to opt out of this review, comment assign to next reviewer:

R: @liferoad for label python.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

# Deepcopy of the combine_fn to avoid sharing state between lifted
# stages when using cloudpickle.
try:
self._combine_fn_copy = copy.deepcopy(combine_fn)
Copy link
Contributor

Choose a reason for hiding this comment

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

If we:

  1. replace this line with
    self._combine_fn_copy = pickler.loads(pickler.dumps(combine_fn))

  2. use cloudpickle pickler

will we still hit https://github.com/apache/beam/issues/26209 ?

Copy link
Contributor Author

@claudevdm claudevdm Oct 4, 2024

Choose a reason for hiding this comment

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

No. The pickler.loads(pickler.dumps(a)) creates a deep copy of the instance a. For example

from apache_beam.internal import cloudpickle_pickler
class TestClass:   
def __init__(self):
     self._setup_called = False
     
   def setup(self, *args, **kwargs):
     assert not self._setup_called, 'setup should not be called twice'
     self._setup_called = True

>>> original = TestClass()
>>> pickled_a = cloudpickle_pickler.loads(cloudpickle_pickler.dumps(original))
>>> pickled_b = cloudpickle_pickler.loads(cloudpickle_pickler.dumps(original))
>>> pickled_a == pickled_b
False
>>> pickled_a.setup()
>>> pickled_a._setup_called
True
>>> pickled_b.setup()
>>> pickled_a.setup()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 7, in setup
AssertionError: setup should not be called twice

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 can write a unit test for this scenario too

Copy link
Contributor

Choose a reason for hiding this comment

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

    except TypeError as e:

Can there be other errors? Let's catch all exceptions.

          'Failed to copy combine function. Ensure python dependencies are'
          ' properly set up: %s'

I thought whether we should surface this, and I think this might create unactionable noise for the users. I wouldn't print anything if the copy, or copy via pickle was successful.

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.

2 participants