-
Notifications
You must be signed in to change notification settings - Fork 9
option to pickle testcase lock files #65
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,7 @@ | |
| from .third_party.lock import LockError | ||
| from .util import cpu_count | ||
| from .util import logging | ||
| from .util import serialize | ||
| from .util.filesystem import find_work_tree | ||
| from .util.filesystem import force_remove | ||
| from .util.filesystem import mkdirp | ||
|
|
@@ -283,9 +284,9 @@ def dump_testcases(self) -> None: | |
| [case.save() for case in self.cases] | ||
| else: | ||
| cpus = cpu_count() | ||
| args = ((case.getstate(), case.lockfile) for case in self.cases) | ||
| args = ((case.lockfile, case.getstate()) for case in self.cases) | ||
| pool = multiprocessing.Pool(cpus) | ||
| pool.imap_unordered(json_dump, args, chunksize=len(self.cases) // cpus or 1) | ||
| pool.imap_unordered(dump_obj, args, chunksize=len(self.cases) // cpus or 1) | ||
| pool.close() | ||
| pool.join() | ||
| index = {case.id: [dep.id for dep in case.dependencies] for case in self.cases} | ||
|
|
@@ -326,12 +327,7 @@ def load_testcases(self, ids: list[str] | None = None) -> list[TestCase]: | |
| continue | ||
| # see TestCase.lockfile for file pattern | ||
| file = self.db.join_path("cases", id[:2], id[2:], TestCase._lockfile) | ||
| with self.db.open(file) as fh: | ||
| try: | ||
| state = json.load(fh) | ||
| except json.JSONDecodeError: | ||
| logger.warning(f"Unable to load {file}!") | ||
| continue | ||
| state = serialize.load(file) | ||
|
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. The previous implies that |
||
| state["properties"]["work_tree"] = self.work_tree | ||
| for i, dep_state in enumerate(state["properties"]["dependencies"]): | ||
| # assign dependencies from existing dependencies | ||
|
|
@@ -677,11 +673,9 @@ def handle_signal(sig, frame): | |
| raise SystemExit(sig) | ||
|
|
||
|
|
||
| def json_dump(args): | ||
| data, filename = args | ||
| mkdirp(os.path.dirname(filename)) | ||
| with open(filename, "w") as fh: | ||
| json.dump(data, fh, indent=2) | ||
| def dump_obj(args): | ||
| filename, obj = args | ||
| serialize.dump(filename, obj) | ||
|
|
||
|
|
||
| def prefix_bits(byte_array: bytes, bits: int) -> int: | ||
|
|
||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import json | ||
| import pickle # nosec B403 | ||
| import time | ||
| from pathlib import Path | ||
| from typing import Any | ||
|
|
||
| from .string import pluralize | ||
|
|
||
| PICKLE = 0 | ||
| JSON = 1 | ||
|
|
||
| protocol = JSON | ||
|
|
||
|
|
||
| def dump(file: str | Path, obj: Any) -> None: | ||
| file = Path(file) | ||
| file.parent.mkdir(parents=True, exist_ok=True) | ||
| tmp = file.with_suffix(".tmp") | ||
| if protocol == JSON: | ||
| with open(tmp, "w") as fh: | ||
| json.dump(obj, fh, indent=2) | ||
| else: | ||
| with open(tmp, "wb") as fh: | ||
| pickle.dump(obj, fh) # nosec B301 | ||
| tmp.replace(file) | ||
|
|
||
|
|
||
| def load(file: str | Path, attempts: int = 8) -> Any: | ||
| file = Path(file) | ||
| delay = 0.5 | ||
| attempt = 0 | ||
| while attempt <= attempts: | ||
| # Guard against race condition when multiple batches are running at once | ||
| attempt += 1 | ||
| try: | ||
| if protocol == JSON: | ||
| with open(file, "r") as fh: | ||
| return json.load(fh) | ||
| else: | ||
| with open(file, "rb") as fh: | ||
| return pickle.load(fh) # nosec B301 | ||
| except Exception: | ||
| time.sleep(delay) | ||
| delay *= 2 | ||
| raise FailedToLoadError( | ||
| f"Failed to load {file.name} after {attempts} {pluralize('attempt', attempts)}" | ||
| ) | ||
|
Comment on lines
+30
to
+47
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. I feel like this retry pattern is common. Should we establish a retry decorator in utils and use that throughout? |
||
|
|
||
|
|
||
| class FailedToLoadError(Exception): | ||
| pass | ||
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.
Leftover from development? Otherwise should it be logged?