-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Enable mlflow-artifacts scheme as wrapper around http artifact scheme #5070
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9e00fb6
Enable mlflow-artifacts scheme as wrapper around http artifact scheme
BenWilson2 3463351
simplify scheme resolution for missing host and port replacement
BenWilson2 395bfd9
Add artifact storage path testing params
BenWilson2 b6032a5
Handle uri parsing and test different permutations
BenWilson2 243a62f
close the session in http_artifact_repo only if it is active
BenWilson2 fa3bfa4
legibility for test conditions
BenWilson2 c4c4787
lint fixes
BenWilson2 3b22de9
Add tracking uri scheme exception and simplify parsing logic
BenWilson2 cf038ed
Overrides of local tracking uri in test suite and adjust logic for ur…
BenWilson2 082c7ce
Merge branch 'master' of https://github.com/mlflow/mlflow into mlflow…
BenWilson2 b8e2af9
lint
BenWilson2 e5f8db1
Simplify tests
BenWilson2 01fb162
Fix the logic
BenWilson2 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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,96 @@ | ||
from urllib.parse import urlparse | ||
from collections import namedtuple | ||
|
||
from mlflow.store.artifact.http_artifact_repo import HttpArtifactRepository | ||
from mlflow.tracking._tracking_service.utils import get_tracking_uri | ||
from mlflow.exceptions import MlflowException | ||
|
||
|
||
def _parse_artifact_uri(artifact_uri): | ||
ParsedURI = namedtuple("ParsedURI", "scheme host port path") | ||
parsed_uri = urlparse(artifact_uri) | ||
return ParsedURI(parsed_uri.scheme, parsed_uri.hostname, parsed_uri.port, parsed_uri.path) | ||
|
||
|
||
def _check_if_host_is_numeric(hostname): | ||
if hostname: | ||
try: | ||
float(hostname) | ||
return True | ||
except ValueError: | ||
return False | ||
else: | ||
return False | ||
|
||
|
||
def _validate_port_mapped_to_hostname(uri_parse): | ||
# This check is to catch an mlflow-artifacts uri that has a port designated but no | ||
# hostname specified. `urllib.parse.urlparse` will treat such a uri as a filesystem | ||
# definition, mapping the provided port as a hostname value if this condition is not | ||
# validated. | ||
if uri_parse.host and _check_if_host_is_numeric(uri_parse.host) and not uri_parse.port: | ||
raise MlflowException( | ||
"The mlflow-artifacts uri was supplied with a port number: " | ||
f"{uri_parse.host}, but no host was defined." | ||
) | ||
|
||
|
||
def _validate_uri_scheme(scheme): | ||
allowable_schemes = {"http", "https"} | ||
if scheme not in allowable_schemes: | ||
raise MlflowException( | ||
f"The configured tracking uri scheme: '{scheme}' is invalid for use with the proxy " | ||
f"mlflow-artifact scheme. The allowed tracking schemes are: {allowable_schemes}" | ||
) | ||
|
||
|
||
class MlflowArtifactsRepository(HttpArtifactRepository): | ||
"""Scheme wrapper around HttpArtifactRepository for mlflow-artifacts server functionality""" | ||
|
||
def __init__(self, artifact_uri): | ||
|
||
super().__init__(self.resolve_uri(artifact_uri)) | ||
|
||
@classmethod | ||
def resolve_uri(cls, artifact_uri): | ||
|
||
base_url = "/api/2.0/mlflow-artifacts/artifacts" | ||
tracking_uri = get_tracking_uri() | ||
|
||
track_parse = _parse_artifact_uri(tracking_uri) | ||
|
||
uri_parse = _parse_artifact_uri(artifact_uri) | ||
|
||
# Check to ensure that a port is present with no hostname | ||
_validate_port_mapped_to_hostname(uri_parse) | ||
|
||
# Check that tracking uri is http or https | ||
_validate_uri_scheme(track_parse.scheme) | ||
|
||
if uri_parse.path == "/": # root directory; build simple path | ||
resolved = f"{base_url}{uri_parse.path}" | ||
elif uri_parse.path == base_url: # for operations like list artifacts | ||
resolved = base_url | ||
else: | ||
resolved = f"{base_url}{track_parse.path}{uri_parse.path.lstrip('/')}" | ||
|
||
if uri_parse.host and uri_parse.port: | ||
resolved_artifacts_uri = ( | ||
f"{track_parse.scheme}://{uri_parse.host}:{uri_parse.port}{resolved}" | ||
) | ||
elif uri_parse.host and not uri_parse.port: | ||
resolved_artifacts_uri = f"{track_parse.scheme}://{uri_parse.host}{resolved}" | ||
elif not uri_parse.host and not uri_parse.port and uri_parse.path == track_parse.path: | ||
resolved_artifacts_uri = ( | ||
f"{track_parse.scheme}://{track_parse.host}:{track_parse.port}{resolved}" | ||
) | ||
elif not uri_parse.host and not uri_parse.port: | ||
resolved_artifacts_uri = ( | ||
f"{track_parse.scheme}://{track_parse.host}:" f"{track_parse.port}{resolved}" | ||
) | ||
else: | ||
raise MlflowException( | ||
f"The supplied artifact uri {artifact_uri} could not be resolved." | ||
) | ||
|
||
return resolved_artifacts_uri.replace("///", "/").rstrip("/") |
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.
Can we raise an exception if the scheme of
tracking_uri
is nothttp
orhttps
(e.g.sqlite:///foo/bar
)?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.
great idea :) added! I also simplified the parsing logic and temporarily set the tracking uri in the test function to validate against a valid server address (then reverted the global variable to not affect other tests with an inadvertent side-effect)
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.
Thanks for the update! A tracking URI looks like
http://localhost:5000
and doesn't contain/api/2.0/mlflow-artifacts/artifacts
.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.
updated the tests and cleaned all of that up
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.
@BenWilson2 Sorry I wasn't clear. The resolved artifact URI needs to have
/api/2.0/mlflow-artifacts/artifacts
(e.g.http://localhost:5000/api/2.0/mlflow-artifacts/artifacts/path/to/dir
).