Refactor _SharedCache to handle context vs non-context ownership#38620
Draft
shunping wants to merge 4 commits into
Draft
Refactor _SharedCache to handle context vs non-context ownership#38620shunping wants to merge 4 commits into
shunping wants to merge 4 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Currently,
_SharedCacheregisters all live owners as joint owners of every cached subprocess key. This works fine when all owners are within a unified context (e.g. a test class wrapping multiple runs), but it causes unintended resource sharing/leakage when there are concurrent, independent, non-context owners (such as a long-lived runner).Here is a resource leak scenario:
owner_1.owner_2and requests its startup command key.get()automatically added all currently registered live owners to the cache key's owner set.beam/sdks/python/apache_beam/utils/subprocess_server.py
Lines 117 to 118 in 361c2c4
As a result,
owner_1(the Prism runner) was registered as a joint owner of the short-lived Expansion Service's cache key.owner_2), the short-lived Expansion Service subprocess was never terminated becauseowner_1was still registered as an owner (entry.ownersis not empty).beam/sdks/python/apache_beam/utils/subprocess_server.py
Lines 104 to 105 in 361c2c4
This PR refactors
_SharedCacheinsubprocess_server.pyto explicitly distinguish between context owners (e.g., cache_subprocesses which should hold ownership over everything created under its block) and non-context owners (e.g., individual subprocess server instances like PrismRunner or expansion service subprocesses).Additionally, get() now takes an optional owner arg. Context owners automatically own all created keys, while independent non-context owners only own the keys they explicitly request.
For example, we run YAML example test with
pytest apache_beam/yaml/examples/testing/examples_test.py --log-cli-level=INFO -s.