forked from ray-project/ray
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[build_base] [Tune] Add more comprehensive support for remote `upload…
…_dir` w/ endpoint and params (ray-project#32479) Currently, URI handling with parameters is done in multiple places in different ways (using `urllib.parse` or splitting by `'?'` directly). In some places, it's not done at all, which **causes errors when performing cloud checkpointing.** In particular, `Trial.remote_checkpoint_dir` and `Trainable._storage_path` do not handle URI path appends correctly when URL params are present. Signed-off-by: Justin Yu <justinvyu@berkeley.edu> Signed-off-by: Edward Oakes <ed.nmi.oakes@gmail.com>
- Loading branch information
Showing
8 changed files
with
194 additions
and
79 deletions.
There are no files selected for viewing
This file contains 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 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,61 @@ | ||
from pathlib import Path | ||
import urllib.parse | ||
import os | ||
from typing import Union | ||
|
||
|
||
class URI: | ||
"""Represents a URI, supporting path appending and retrieving parent URIs. | ||
Example Usage: | ||
>>> s3_uri = URI("s3://bucket/a?scheme=http&endpoint_override=localhost%3A900") | ||
>>> s3_uri | ||
URI<s3://bucket/a?scheme=http&endpoint_override=localhost%3A900> | ||
>>> str(s3_uri / "b" / "c") | ||
's3://bucket/a/b/c?scheme=http&endpoint_override=localhost%3A900' | ||
>>> str(s3_uri.parent) | ||
's3://bucket?scheme=http&endpoint_override=localhost%3A900' | ||
>>> str(s3_uri) | ||
's3://bucket/a?scheme=http&endpoint_override=localhost%3A900' | ||
>>> s3_uri.parent.name, s3_uri.name | ||
('bucket', 'a') | ||
Args: | ||
uri: The URI to represent. | ||
Ex: s3://bucket?scheme=http&endpoint_override=localhost%3A900 | ||
Ex: file:///a/b/c/d | ||
""" | ||
|
||
def __init__(self, uri: str): | ||
self._parsed = urllib.parse.urlparse(uri) | ||
if not self._parsed.scheme: | ||
raise ValueError(f"Invalid URI: {uri}") | ||
self._path = Path(os.path.normpath(self._parsed.netloc + self._parsed.path)) | ||
|
||
@property | ||
def name(self) -> str: | ||
return self._path.name | ||
|
||
@property | ||
def parent(self) -> "URI": | ||
assert self._path.parent != ".", f"{str(self)} has no valid parent URI" | ||
return URI(self._get_str_representation(self._parsed, self._path.parent)) | ||
|
||
def __truediv__(self, path_to_append): | ||
assert isinstance(path_to_append, str) | ||
return URI( | ||
self._get_str_representation(self._parsed, self._path / path_to_append) | ||
) | ||
|
||
@classmethod | ||
def _get_str_representation( | ||
cls, parsed_uri: urllib.parse.ParseResult, path: Union[str, Path] | ||
) -> str: | ||
return parsed_uri._replace(netloc=str(path), path="").geturl() | ||
|
||
def __repr__(self): | ||
return f"URI<{str(self)}>" | ||
|
||
def __str__(self): | ||
return self._get_str_representation(self._parsed, self._path) |
This file contains 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 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 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 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.