Repository navigation
feat: resumable network volume upload and download #655
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
justinwlin
wants to merge
23
commits into
main
Choose a base branch
from
feat/network-volume-transfer
base: main
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 all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
8829c45
feat: resumable network volume upload and download
justinwlin 86c9ddf
fix: directory HEAD 400 and resume hint only for transient failures
justinwlin 25113dd
fix: stop a download whose remote changes midway, stamp remote mtime …
justinwlin 67b9f2a
fix: harden network volume transfer resume and completion checks
justinwlin 978268e
chore: address code scanning notes in network volume transfer
justinwlin d95e161
fix: close remaining network volume transfer review gaps
justinwlin c92ac38
fix: address network volume transfer review round 1
justinwlin b923852
fix: address network volume transfer review round 2
justinwlin 8c260d2
fix: endpoint pre-check skips proxied endpoints; stub dns in its tests
justinwlin 0c8b4d3
fix: make the ctrl-c test deterministic and close its stop race
justinwlin f1fa981
refactor: move transfers onto NetworkVolume and rp volume
justinwlin b9c8f92
fix: list volumes through the shared paginated rest helper
justinwlin b085cb1
refactor: split transfer methods and fix two folder-download edge cases
justinwlin a4bc86a
fix: drop a stale sidecar before emptying a partial download
justinwlin f7299a9
fix: refuse symlinked download work files
justinwlin a5e18c3
refactor: condense the network volume transfer module
justinwlin c29e0d2
fix: retry the 524 runpod's proxy returns on slow operations
justinwlin a8431ea
feat(cli): prompt for login, volume and s3 keys; explain transfer fai…
justinwlin 675087f
fix: read saved s3 keys only as a fallback, and say how the picker works
justinwlin cd56170
fix: stop an upload mid-part on ctrl-c, and tell a rejected api key a…
justinwlin 369264c
fix(volume): validate before key prompts and recover rejected saved keys
justinwlin 721ef95
test(volume): exercise saved key replacement through CLI
justinwlin 13069a8
refactor(volume): extract transfer filesystem helpers
justinwlin 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,160 @@ | ||
| """Filesystem and checksum helpers for resumable transfers. | ||
|
|
||
| This private module keeps disk state and path safety details out of the S3 | ||
| request orchestration. The public error type remains in network_volume_transfer. | ||
| """ | ||
|
|
||
| import hashlib | ||
| import json | ||
| import os | ||
| import unicodedata | ||
| from pathlib import Path, PurePosixPath | ||
| from typing import Any, Callable, Dict, List, Optional, Tuple | ||
|
|
||
| MIB = 1024 * 1024 | ||
|
|
||
|
|
||
| def _read_json(path: Path) -> Optional[Dict[str, Any]]: | ||
| try: | ||
| data = json.loads(path.read_text(encoding="utf-8")) | ||
| except (OSError, ValueError): | ||
| return None | ||
| return data if isinstance(data, dict) else None | ||
|
|
||
|
|
||
| def _write_json(path: Path, data: Dict[str, Any], sync_dir: Callable[[Path], None]) -> None: | ||
| """replace path atomically, synced, so a crash leaves the old state or | ||
| the new one.""" | ||
| path.parent.mkdir(parents=True, exist_ok=True) | ||
| tmp = path.with_name(path.name + ".tmp") | ||
| with open(tmp, "w", encoding="utf-8") as handle: | ||
| json.dump(data, handle) | ||
| handle.flush() | ||
| os.fsync(handle.fileno()) | ||
| os.replace(tmp, path) | ||
| sync_dir(path.parent) | ||
|
|
||
|
|
||
| def _sync_dir(path: Path) -> None: | ||
| """make a rename in path survive a power loss. best effort: windows | ||
| cannot open a directory.""" | ||
| try: | ||
| fd = os.open(path, os.O_RDONLY) | ||
| except OSError: | ||
| return | ||
| try: | ||
| os.fsync(fd) | ||
| except OSError: | ||
| pass | ||
| finally: | ||
| os.close(fd) | ||
|
|
||
|
|
||
| def _md5_hex(path: Path, offset: int, length: int) -> str: | ||
| digest = hashlib.md5() # noqa: S324 - etag comparison, not security | ||
| with open(path, "rb") as handle: | ||
| handle.seek(offset) | ||
| while length > 0 and (block := handle.read(min(8 * MIB, length))): | ||
| digest.update(block) | ||
| length -= len(block) | ||
| return digest.hexdigest() | ||
|
|
||
|
|
||
| def _etag(value: Any) -> str: | ||
| return str(value or "").strip('"') | ||
|
|
||
|
|
||
| def _is_md5(etag: str) -> bool: | ||
| return len(etag) == 32 and all(c in "0123456789abcdef" for c in etag.lower()) | ||
|
|
||
|
|
||
| def _multipart_etag(parts: List[Dict[str, Any]]) -> str: | ||
| """the etag s3 (and runpod, verified) gives an object assembled from | ||
| parts: md5 of the parts' binary md5s, then "-<count>". "" when a part | ||
| etag is not an md5.""" | ||
| etags = [_etag(part["ETag"]) for part in parts] | ||
| if not all(_is_md5(etag) for etag in etags): | ||
| return "" | ||
| digest = hashlib.md5(b"".join(bytes.fromhex(etag) for etag in etags)) # noqa: S324 | ||
| return f"{digest.hexdigest()}-{len(parts)}" | ||
|
|
||
|
|
||
| def _same_version(a: os.stat_result, b: os.stat_result) -> bool: | ||
| return a.st_size == b.st_size and a.st_mtime_ns == b.st_mtime_ns | ||
|
|
||
|
|
||
| def _not_older(a: float, b: float) -> bool: | ||
| """a >= b in whole seconds; s3 dates carry no finer precision.""" | ||
| return int(a) >= int(b) | ||
|
|
||
|
|
||
| def _check_remote_path(remote_path: Optional[str]) -> None: | ||
| if remote_path and ".." in remote_path.replace("\\", "/").split("/"): | ||
| raise ValueError(f"remote path {remote_path!r} must not contain '..'") | ||
|
|
||
|
|
||
| def _check_local_names( | ||
| targets: List[Path], root: Path, suffixes: Tuple[str, ...], error_type: type[Exception] | ||
| ) -> None: | ||
| """refuse targets that would share a path on disk: two that are one name | ||
| on a case-insensitive or normalizing filesystem ("Model.bin" and | ||
| "model.bin"), a file whose name is also a folder another file lives in | ||
| ("a" beside "A/b"), or a file whose partial, sidecar or sidecar temp name | ||
| is also a target or a folder one lives in ("x" beside "x.rpdownload/y"). | ||
| names are compared nfc-normalized (apfs ignores normalization) and case | ||
| folded, which can refuse a listing a case-sensitive filesystem would | ||
| take: the safe direction.""" | ||
|
|
||
| def fold(path: PurePosixPath) -> str: | ||
| return unicodedata.normalize("NFC", str(path)).casefold() | ||
|
|
||
| rels = [PurePosixPath(target.relative_to(root).as_posix()) for target in targets] | ||
| files: Dict[str, PurePosixPath] = {} | ||
| dirs: Dict[str, PurePosixPath] = {} | ||
| for rel in rels: | ||
| if fold(rel) in files: | ||
| raise error_type( | ||
| f"cannot download both {files[fold(rel)]} and {rel}: they would be one " | ||
| "file on a case-insensitive or normalizing filesystem" | ||
| ) | ||
| files[fold(rel)] = rel | ||
| dirs.update({fold(parent): rel for parent in rel.parents if str(parent) != "."}) | ||
| for rel in rels: | ||
| if fold(rel) in dirs: | ||
| raise error_type( | ||
| f"cannot download both {rel} and {dirs[fold(rel)]}: " | ||
| "the first would have to be a file and a folder" | ||
| ) | ||
| for suffix in suffixes: | ||
| twin = fold(rel.with_name(rel.name + suffix)) | ||
| other = files.get(twin) or dirs.get(twin) | ||
| if other is not None: | ||
| raise error_type( | ||
| f"cannot download both {rel} and {other}: " | ||
| f"{rel.name + suffix} is used for the first's partial download" | ||
| ) | ||
|
|
||
|
|
||
| def _safe_target(root: Path, rel: str, error_type: type[Exception]) -> Path: | ||
| """local path for a remote relative key, refusing anything outside root. | ||
| "." and empty segments are refused too: "a/./b" would alias "a/b".""" | ||
| segments = rel.split("/") | ||
| if ( | ||
| rel.startswith("/") | ||
| or "\\" in rel | ||
| or any(seg in ("..", ".", "") for seg in segments) | ||
| or ":" in segments[0] | ||
| ): | ||
| raise error_type(f"refusing unsafe remote path: {rel!r}") | ||
| target = root.joinpath(*segments) | ||
| if root.resolve() not in target.resolve().parents: | ||
| raise error_type(f"refusing unsafe remote path: {rel!r}") | ||
| # a symlinked folder would let two remote paths share one local file, | ||
| # which the name collision check cannot see | ||
| for depth in range(1, len(segments)): | ||
| if root.joinpath(*segments[:depth]).is_symlink(): | ||
| raise error_type( | ||
| f"{root.joinpath(*segments[:depth])} is a symlink; " | ||
| "download into a folder without symlinked subfolders" | ||
| ) | ||
| return target |
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
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.
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.
Low (L2): This still lists only env vars and flags. Add the saved
s3_access_key/s3_secret_keyin~/.runpod/config.toml, the prompts, and that VOLUME can be left out to pick one.NetworkVolume.upload's docstring (runpod/apps/volume.py:109) has the same gap.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.
Not addressed at
675087f: this paragraph andapps/volume.py:109are unchanged. The new--helptext explains the picker, which covers part of the gap. Carried into round 11 as Low.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.
Not addressed at
cd56170: this file and line are unchanged. Carried into round 12.