-
Notifications
You must be signed in to change notification settings - Fork 322
Add ability to download offsets that are stored along with corpus #1980
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
Open
gareth-ellis
wants to merge
8
commits into
master
Choose a base branch
from
download-offsets
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.
Open
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2d03b14
Add ability to download offsets that are stored along with corpus
gareth-ellis 22263c0
Fix tests
gareth-ellis fe71149
Reuse existing code
gareth-ellis b177067
Merge branch 'master' into download-offsets
elasticmachine 4d5fafa
Syntax
gareth-ellis 44c02ff
Lint
gareth-ellis 8c15bb3
BaseException -> Exception
gareth-ellis 206b4eb
Merge branch 'master' into download-offsets
elasticmachine 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 |
|---|---|---|
|
|
@@ -46,7 +46,7 @@ | |
| # but they are treated the same by mypy, so I'm not going to use conditional imports here | ||
| from typing_extensions import Self | ||
|
|
||
| from esrally.utils import console | ||
| from esrally.utils import console, net | ||
|
|
||
| SUPPORTED_ARCHIVE_FORMATS = [".zip", ".bz2", ".gz", ".tar", ".tar.gz", ".tgz", ".tar.bz2", ".zst"] | ||
|
|
||
|
|
@@ -520,6 +520,59 @@ def is_valid(self) -> bool: | |
| """ | ||
| return self.exists() and os.path.getmtime(self.offset_table_path) >= os.path.getmtime(self.data_file_path) | ||
|
|
||
| def try_download_from_corpus_location(self, corpus_base_url: Optional[str] = None) -> bool: | ||
| """ | ||
| Attempts to download a pre-computed offset file from the corpus location. | ||
|
|
||
| :param corpus_base_url: The base URL where the corpus data is hosted. If None, tries to infer from data file path. | ||
| :return: True if download was successful, False otherwise. | ||
| """ | ||
| if not corpus_base_url: | ||
| # Try to infer corpus URL from common Rally corpus locations | ||
| # This is a best-effort approach for common scenarios | ||
| return False | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
| data_file_name = os.path.basename(self.data_file_path) | ||
| offset_file_name = f"{data_file_name}.offset" | ||
|
|
||
| # Construct the remote offset file URL | ||
| if corpus_base_url.endswith("/"): | ||
| separator = "" | ||
| else: | ||
| separator = "/" | ||
| remote_offset_url = f"{corpus_base_url}{separator}{offset_file_name}" | ||
|
Comment on lines
+529
to
+538
Contributor
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. Or simply write these two lines. :-) |
||
|
|
||
| try: | ||
| logger.info("Attempting to download offset file from [%s]", remote_offset_url) | ||
|
|
||
| # Ensure the directory exists | ||
| os.makedirs(os.path.dirname(self.offset_table_path), exist_ok=True) | ||
|
|
||
| # Download the offset file using Rally's existing HTTP download functionality | ||
| if remote_offset_url.startswith(("http://", "https://")): | ||
| net.download_http(remote_offset_url, self.offset_table_path) | ||
| elif remote_offset_url.startswith(("s3://", "gs://")): | ||
| # Extract scheme for bucket downloads | ||
| scheme = remote_offset_url.split("://")[0] | ||
| net.download_from_bucket(scheme, remote_offset_url, self.offset_table_path) | ||
gareth-ellis marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| else: | ||
| logger.debug("Unsupported URL scheme for remote offset download: [%s]", remote_offset_url) | ||
| return False | ||
|
|
||
| logger.info("Successfully downloaded offset file from [%s]", remote_offset_url) | ||
| return True | ||
|
|
||
| except Exception as e: | ||
| logger.debug("Failed to download offset file from [%s]: %s", remote_offset_url, str(e)) | ||
| # Clean up any partially downloaded file | ||
| if os.path.exists(self.offset_table_path): | ||
| try: | ||
| os.remove(self.offset_table_path) | ||
| except Exception: | ||
| pass # Best effort cleanup | ||
| return False | ||
|
|
||
| def __enter__(self) -> Self: | ||
| self.offset_file = open(self.offset_table_path, self.mode) | ||
| return self | ||
|
|
@@ -596,16 +649,32 @@ def remove(data_file_path: str) -> None: | |
| os.remove(f"{data_file_path}.offset") | ||
|
|
||
|
|
||
| def prepare_file_offset_table(data_file_path: str) -> Optional[int]: | ||
| def prepare_file_offset_table(data_file_path: str, corpus_base_url: Optional[str] = None) -> Optional[int]: | ||
gareth-ellis marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """ | ||
| Creates a file that contains a mapping from line numbers to file offsets for the provided path. This file is used internally by | ||
| #skip_lines(data_file_path, data_file) to speed up line skipping. | ||
|
|
||
| :param data_file_path: The path to a text file that is readable by this process. | ||
| :param corpus_base_url: Optional base URL where the corpus data (and potentially offset files) are hosted. | ||
| If provided, Rally will attempt to download a pre-computed .offset file before creating one locally. | ||
| :return The number of lines read or ``None`` if it did not have to build the file offset table. | ||
| """ | ||
| file_offset_table = FileOffsetTable.create_for_data_file(data_file_path) | ||
| if not file_offset_table.is_valid(): | ||
| # First, try to download the offset file from the corpus location | ||
| if corpus_base_url: | ||
| console.info("Attempting to download offset file for [%s] from corpus location ... " % data_file_path, end="", flush=True) | ||
| if file_offset_table.try_download_from_corpus_location(corpus_base_url): | ||
| console.println("[DOWNLOADED]") | ||
| # Verify the downloaded file is valid | ||
| if file_offset_table.is_valid(): | ||
| return None | ||
| else: | ||
| console.println("[INVALID - will create locally]") | ||
| else: | ||
| console.println("[NOT FOUND - will create locally]") | ||
|
|
||
| # Fall back to creating the offset file locally | ||
| console.info("Preparing file offset table for [%s] ... " % data_file_path, end="", flush=True) | ||
| line_number = 0 | ||
| with file_offset_table: | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.