-
Notifications
You must be signed in to change notification settings - Fork 15
Add code to reconstitute deliver() from a cache area
#713
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
Draft
ponyisi
wants to merge
3
commits into
master
Choose a base branch
from
servicex-from-concentrate
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+270
−4
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| # Copyright (c) 2026, IRIS-HEP | ||
| # All rights reserved. | ||
| # | ||
| # Redistribution and use in source and binary forms, with or without | ||
| # modification, are permitted provided that the following conditions are met: | ||
| # | ||
| # * Redistributions of source code must retain the above copyright notice, this | ||
| # list of conditions and the following disclaimer. | ||
| # | ||
| # * Redistributions in binary form must reproduce the above copyright notice, | ||
| # this list of conditions and the following disclaimer in the documentation | ||
| # and/or other materials provided with the distribution. | ||
| # | ||
| # * Neither the name of the copyright holder nor the names of its | ||
| # contributors may be used to endorse or promote products derived from | ||
| # this software without specific prior written permission. | ||
| # | ||
| # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
| # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
| # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
| # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
| # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
| # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
|
||
| import os | ||
| from pathlib import Path | ||
| from typing import Optional, Union | ||
| from servicex.servicex_client import GuardList | ||
| from servicex.query_cache import QueryCache | ||
| from servicex.configuration import Configuration | ||
| from servicex.models import TransformedResults | ||
|
|
||
|
|
||
| def read_dir( | ||
| path: Optional[Union[str, Path]] = None, | ||
| config_path: Optional[Union[str, Path]] = None, | ||
| local_preferred: bool = True, | ||
| ) -> dict[str, GuardList]: | ||
| r""" | ||
| Return the transformation output results previously saved to a directory. The format | ||
| is the same as in the deliver() call. | ||
|
|
||
| This function will not trigger any additional downloads. Either local copies or URLs will | ||
| be returned, according to the policy set by `local_preferred`. | ||
|
|
||
| If multiple results with the same name were saved to the same directory, only the most | ||
| recent is returned. | ||
|
|
||
| :param path: The directory path to read from, as a string or a :py:class:`Path` object. | ||
| If None, the default cache path will be used from the ServiceX configuration. | ||
| :param config_path: If `path` is :py:const:`None`, this determines the path from which the | ||
| ServiceX configuration will be loaded to determine the default cache path. | ||
| :py:const:`None` will search the default paths. | ||
| :param local_preferred: Determines the behavior if both downloaded files and remote URL | ||
| information is present. If :py:const:`True` (default) then downloaded copies of | ||
| files are preferred, if available. If :py:const:`False` then remote URLs will | ||
| be preferred, if available. | ||
| :return: A dictionary mapping the name of each :py:class:`Sample` to a :py:class:`.GuardList` | ||
| with the file names or URLs for the outputs. | ||
| """ | ||
|
|
||
| if path is None: | ||
| # Load default | ||
| config = Configuration.read( | ||
| str(config_path) if config_path is not None else None | ||
| ) | ||
| else: | ||
| # create a simple Configuration object with just the cache path | ||
| # if directory cache non-existent, do not trigger its creation! | ||
| if not os.path.isdir(path): | ||
| raise ValueError(f"{path} is not an existing directory") | ||
| if not (Path(path) / ".servicex" / "db.json").exists(): | ||
| raise RuntimeError( | ||
| f"{path} does not contain a valid ServiceX download area" | ||
| ) | ||
| config = Configuration(api_endpoints=[], cache_path=str(path)) | ||
| cache = QueryCache(config) | ||
| transforms = cache.cached_queries() | ||
| latest_transforms: dict[str, TransformedResults] = {} | ||
| for transform in transforms: | ||
| if transform.title not in latest_transforms: | ||
| latest_transforms[transform.title] = transform | ||
| else: | ||
| current = latest_transforms[transform.title] | ||
| if transform.submit_time > current.submit_time: | ||
| latest_transforms[transform.title] = transform | ||
|
|
||
| if local_preferred: | ||
| return { | ||
| _[0]: GuardList(_[1].file_list if _[1].file_list else _[1].signed_url_list) | ||
|
Collaborator
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. Can this be refactored into functions stored in |
||
| for _ in latest_transforms.items() | ||
| } | ||
| else: | ||
| return { | ||
| _[0]: GuardList( | ||
| _[1].signed_url_list if _[1].signed_url_list else _[1].file_list | ||
| ) | ||
| for _ in latest_transforms.items() | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| # Copyright (c) 2026, IRIS-HEP | ||
| # All rights reserved. | ||
| # | ||
| # Redistribution and use in source and binary forms, with or without | ||
| # modification, are permitted provided that the following conditions are met: | ||
| # | ||
| # * Redistributions of source code must retain the above copyright notice, this | ||
| # list of conditions and the following disclaimer. | ||
| # | ||
| # * Redistributions in binary form must reproduce the above copyright notice, | ||
| # this list of conditions and the following disclaimer in the documentation | ||
| # and/or other materials provided with the distribution. | ||
| # | ||
| # * Neither the name of the copyright holder nor the names of its | ||
| # contributors may be used to endorse or promote products derived from | ||
| # this software without specific prior written permission. | ||
| # | ||
| # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
| # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
| # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
| # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
| # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
| # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| import tempfile | ||
| import datetime | ||
| import pytest | ||
| from pathlib import Path | ||
|
|
||
| from servicex.configuration import Configuration | ||
| from servicex.query_cache import QueryCache | ||
| from servicex.servicex_client import GuardList | ||
| from servicex import read_dir | ||
|
|
||
| file_uris = ["/tmp/foo1.root", "/tmp/foo2.root"] | ||
| file_uris_2 = ["/tmp/bar1.root", "/tmp/bar2.root"] | ||
| remote_urls = ["http://remote/foo1.root", "http://remote/foo2.root"] | ||
|
|
||
|
|
||
| def test_read_cache(transform_request, completed_status): | ||
| with tempfile.TemporaryDirectory() as temp_dir: | ||
| config = Configuration(cache_path=temp_dir, api_endpoints=[]) # type: ignore | ||
| cache = QueryCache(config) | ||
| cache.update_transform_status(transform_request.compute_hash(), "COMPLETE") | ||
| cache.cache_transform( | ||
| cache.transformed_results( | ||
| transform=transform_request, | ||
| completed_status=completed_status, | ||
| data_dir="/foo/bar", | ||
| file_list=file_uris, | ||
| signed_urls=remote_urls, | ||
| ) | ||
| ) | ||
|
|
||
| data = read_dir(temp_dir) | ||
| assert isinstance(data["Test submission"], GuardList) | ||
| assert str(data) == str({"Test submission": file_uris}) | ||
|
|
||
| data = read_dir(temp_dir, local_preferred=False) | ||
| assert isinstance(data["Test submission"], GuardList) | ||
| assert str(data) == str({"Test submission": remote_urls}) | ||
|
|
||
| cache.close() | ||
|
|
||
|
|
||
| def test_read_cache_with_config(transform_request, completed_status, mocker): | ||
| mocker.patch("servicex.configuration.getuser", return_value="cache_user") | ||
| with tempfile.TemporaryDirectory() as temp_dir: | ||
| mocker.patch( | ||
| "servicex.configuration.tempfile.gettempdir", return_value=str(temp_dir) | ||
| ) | ||
| cfg = Path(temp_dir) / "servicex.yaml" | ||
| cfg.write_text(f""" | ||
| api_endpoints: | ||
| - endpoint: http://localhost:5000 | ||
| name: localhost | ||
| cache_path: {temp_dir} | ||
| """) | ||
| config = Configuration(cache_path=temp_dir, api_endpoints=[]) # type: ignore | ||
| cache = QueryCache(config) | ||
| cache.update_transform_status(transform_request.compute_hash(), "COMPLETE") | ||
| cache.cache_transform( | ||
| cache.transformed_results( | ||
| transform=transform_request, | ||
| completed_status=completed_status, | ||
| data_dir="/foo/bar", | ||
| file_list=file_uris, | ||
| signed_urls=remote_urls, | ||
| ) | ||
| ) | ||
|
|
||
| data = read_dir(config_path=cfg) | ||
| assert str(data) == str({"Test submission": file_uris}) | ||
| cache.close() | ||
|
|
||
|
|
||
| def test_no_cache(): | ||
| with pytest.raises(ValueError): | ||
| read_dir("let-us-assume-this-nonexists") | ||
|
|
||
| with tempfile.TemporaryDirectory() as temp_dir: | ||
| with pytest.raises(RuntimeError): | ||
| read_dir(temp_dir) | ||
|
|
||
|
|
||
| def test_most_recent_cache(transform_request, completed_status): | ||
| with tempfile.TemporaryDirectory() as temp_dir: | ||
| config = Configuration(cache_path=temp_dir, api_endpoints=[]) # type: ignore | ||
| cache = QueryCache(config) | ||
| cache.update_transform_status(transform_request.compute_hash(), "COMPLETE") | ||
| cache.cache_transform( | ||
| cache.transformed_results( | ||
| transform=transform_request, | ||
| completed_status=completed_status, | ||
| data_dir="/foo/bar", | ||
| file_list=file_uris, | ||
| signed_urls=[], | ||
| ) | ||
| ) | ||
|
|
||
| req2 = transform_request.model_copy(update={"codegen": "uproot-2"}) | ||
| cache.update_transform_status(req2.compute_hash(), "COMPLETE") | ||
| cache.cache_transform( | ||
| cache.transformed_results( | ||
| transform=req2, | ||
| completed_status=completed_status.model_copy( | ||
| update={ | ||
| "request_id": "02c64494-4529-49a7-a4a6-95661ea3936e", | ||
| "submit_time": datetime.datetime( | ||
| 2025, 12, 1, tzinfo=datetime.timezone.utc | ||
| ), | ||
| } | ||
| ), | ||
| data_dir="/foo/bar", | ||
| file_list=file_uris_2, | ||
| signed_urls=[], | ||
| ) | ||
| ) | ||
|
|
||
| req3 = transform_request.model_copy(update={"codegen": "uproot-3"}) | ||
| cache.update_transform_status(req3.compute_hash(), "COMPLETE") | ||
| cache.cache_transform( | ||
| cache.transformed_results( | ||
| transform=req3, | ||
| completed_status=completed_status.model_copy( | ||
| update={ | ||
| "request_id": "02c64494-4529-49a7-a4a6-95661ea3936a", | ||
| "submit_time": datetime.datetime( | ||
| 1970, 12, 1, tzinfo=datetime.timezone.utc | ||
| ), | ||
| } | ||
| ), | ||
| data_dir="/foo/bar", | ||
| file_list=[], | ||
| signed_urls=remote_urls, | ||
| ) | ||
| ) | ||
|
|
||
| data = read_dir(temp_dir) | ||
| assert str(data) == str({"Test submission": file_uris_2}) |
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
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.
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.
Should the cache object be closed as in the tests (sidebar: should QueryCache be a context manager?)?