-
Notifications
You must be signed in to change notification settings - Fork 0
USE 557 - Parallelized input record read and bitstream download #10
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
Merged
Merged
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
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,134 @@ | ||
| import logging | ||
| import threading | ||
| import time | ||
| from collections.abc import Iterator | ||
| from concurrent.futures import FIRST_COMPLETED, Future, ThreadPoolExecutor, wait | ||
|
|
||
| import requests | ||
| from dspace_rest_client.client import DSpaceClient | ||
|
|
||
| from dfh.dspace import get_dspace_client, get_presigned_url_for_bitstream | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| # module level, cross-thread object to hold thread specific DSpace client instances | ||
|
ghukill marked this conversation as resolved.
|
||
| # see: https://docs.python.org/3/library/threading.html#thread-local-data | ||
| threaded_dspace_clients = threading.local() | ||
|
|
||
|
|
||
| def record_and_fulltext_iter( | ||
| records: Iterator[dict], | ||
| *, | ||
| max_workers: int = 10, | ||
| log_progress_interval: int = 10, | ||
| ) -> Iterator[dict]: | ||
| """Yield records with fulltext fetched in parallel. | ||
|
|
||
| Uses ThreadPoolExecutor (main IO bottleneck is network) to generate pre-signed URLs | ||
| and download bitstream content in parallel. | ||
|
|
||
| The worker function _record_with_fulltext() has built-in retries. This orchestration | ||
| function with parallelizes the work is not aware of retries. | ||
| """ | ||
| pending: set[Future[dict]] = set() | ||
| completed_count = 0 | ||
|
|
||
| with ThreadPoolExecutor(max_workers=max_workers) as executor: | ||
| for record in records: | ||
| pending.add(executor.submit(_record_with_fulltext, record)) | ||
|
|
||
| if len(pending) < max_workers: | ||
| continue | ||
|
|
||
| done, pending = wait(pending, return_when=FIRST_COMPLETED) | ||
| for future in done: | ||
| completed_count += 1 | ||
| yield future.result() | ||
| if completed_count % log_progress_interval == 0: | ||
| logger.debug(f"Extracted fulltext for {completed_count} records.") | ||
|
|
||
| while pending: | ||
| done, pending = wait(pending, return_when=FIRST_COMPLETED) | ||
| for future in done: | ||
| completed_count += 1 | ||
| yield future.result() | ||
| if completed_count % log_progress_interval == 0: | ||
| logger.debug(f"Extracted fulltext for {completed_count} records.") | ||
|
|
||
|
|
||
| def _record_with_fulltext( | ||
| record: dict, | ||
| *, | ||
| retry_attempts: int = 4, | ||
| initial_backoff_seconds: float = 1.0, | ||
| backoff_factor: float = 2.0, | ||
| timeout_seconds: int = 60, | ||
| ) -> dict: | ||
| """Return a TIMDEX fulltext record for one source record. | ||
|
|
||
| The primary work here is two network requests: | ||
| 1. Hit DSpace API for a presigned S3 URL for a bitstream UUID | ||
| 2. Use presigned S3 URL to download content | ||
|
|
||
| This function is a worker function designed to be parallelized across threads. As | ||
| such, note the use of _get_dspace_client_for_thread() which ensures that it checks | ||
| the threading.local() object for a DSpaceClient instance to use that is unique and | ||
| reusable by this thread. | ||
|
|
||
| Retries and backoffs are fairly simple: all exceptions, for either network request, | ||
| that bubble up are caught, logged, and increment the retry counter. | ||
| """ | ||
| bitstream_uuid = record["fulltext_bitstream"]["uuid"] | ||
|
|
||
| fulltext = None | ||
| for attempt in range(1, retry_attempts + 1): | ||
| try: | ||
| # reuse dspace client for thread, or init if first time | ||
| dspace_client = _get_dspace_client_for_thread() | ||
|
|
||
| # generate a pre-signed URL for a bitstream UUID | ||
| pre_signed_url = get_presigned_url_for_bitstream( | ||
| dspace_client, | ||
| bitstream_uuid, | ||
| ) | ||
|
|
||
| # download bitstream content from S3 | ||
| response = requests.get(pre_signed_url, timeout=timeout_seconds) | ||
| response.raise_for_status() | ||
| fulltext = response.text | ||
|
|
||
| # break out of retries loop if successful | ||
| break | ||
|
|
||
| except Exception as exc: | ||
| if attempt == retry_attempts: | ||
| logger.exception( | ||
| f"Max retries of {retry_attempts} encountered, failed to download " | ||
| f"bitstream '{bitstream_uuid}'" | ||
| ) | ||
| break | ||
|
|
||
| sleep_seconds = initial_backoff_seconds * (backoff_factor ** (attempt - 1)) | ||
| logger.warning( | ||
| f"Retrying download for bitstream " | ||
| f"'{bitstream_uuid}' after attempt {attempt}/{retry_attempts} " | ||
| f"failed; sleeping {sleep_seconds:.1f} seconds. Cause: {exc}" | ||
| ) | ||
| time.sleep(sleep_seconds) | ||
|
|
||
| return { | ||
| "timdex_record_id": record["timdex_record_id"], | ||
| "run_id": record["run_id"], | ||
| "run_record_offset": record["run_record_offset"], | ||
| "fulltext_bitstream_uuid": bitstream_uuid, | ||
| "fulltext_bitstream_content": fulltext, | ||
| } | ||
|
|
||
|
|
||
| def _get_dspace_client_for_thread() -> DSpaceClient: | ||
| """Get thread's DSpaceClient from module level threaded_dspace_clients.""" | ||
| dspace_client = getattr(threaded_dspace_clients, "dspace_client", None) | ||
| if dspace_client is None: | ||
| dspace_client = get_dspace_client() | ||
| threaded_dspace_clients.dspace_client = dspace_client | ||
| return dspace_client | ||
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,15 @@ | ||
| from unittest import mock | ||
|
|
||
| from dfh.dspace import warm_dspace_auth | ||
|
|
||
|
|
||
| def test_warm_dspace_auth_retries_after_401_and_succeeds(): | ||
| fail_auth = mock.Mock(status_code=401, text="Unauthorized") | ||
| success_auth = mock.Mock(status_code=200, text="OK") | ||
| success_auth.json.return_value = {"authenticated": True} | ||
|
|
||
| client = mock.Mock() | ||
| client.api_get.side_effect = [fail_auth, success_auth] | ||
|
|
||
| with mock.patch("dfh.dspace.get_dspace_client", return_value=client): | ||
| assert warm_dspace_auth(initial_backoff_seconds=0.1) is None |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
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.