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
Use REPL context attributes if available to avoid calling JVM methods #5132
Merged
Merged
Changes from 19 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
a035719
update get_notebook_path
harupy c1d440c
add decorator
harupy 70c5c7e
pass func to wraps
harupy 4c981cb
refactor
harupy e413f16
update docstring
harupy b83e7e2
test
harupy bf30d27
fix
harupy a610852
hardcode prefix
harupy 6278492
add _use_message_metadata_if_exists
harupy a2213e3
use context metadata
harupy 757f0e2
debug
harupy 78fa884
fix attr name
harupy 409bcce
remove print
harupy 7d07c30
fix tests
harupy 97654f7
fix docstring
harupy d8f7b0e
use get_context
harupy 9971989
rename functions
harupy 9f7dd9d
fix module name
harupy 3c3676c
fix module name
harupy 5ac0475
use boolean attributes
harupy 6804295
refactor
harupy 3ca6b30
Merge branch 'master' into use-env-var-command-metadata
harupy 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import os | ||
import logging | ||
import subprocess | ||
import functools | ||
|
||
from mlflow.exceptions import MlflowException | ||
from mlflow.utils.rest_utils import MlflowHostCreds | ||
|
@@ -11,6 +12,43 @@ | |
_logger = logging.getLogger(__name__) | ||
|
||
|
||
def _get_context_attribute(name): | ||
try: | ||
from dbruntime.databricks_repl_context import get_context | ||
|
||
return getattr(get_context(), name) | ||
except Exception: | ||
return None | ||
|
||
|
||
def _use_context_attribute_if_available(name, *, if_available=lambda x: x): | ||
""" | ||
Creates a decorator to insert a short circuit that returns `if_available(name)` | ||
if the specified context attribute is available. | ||
|
||
:param name: Context attribute name. | ||
:param if_available: Function to evaluate when the specified context attribute is available. | ||
Defaults to `lambda x: x`. | ||
:return: Decorator to insert the short circuit. | ||
""" | ||
|
||
def decorator(f): | ||
@functools.wraps(f) | ||
def wrapper(*args, **kwargs): | ||
metadata = _get_context_attribute(name) | ||
if metadata: | ||
return if_available(metadata) | ||
return f(*args, **kwargs) | ||
|
||
return wrapper | ||
|
||
return decorator | ||
|
||
|
||
def _return_true(_): | ||
return True | ||
|
||
|
||
def _get_dbutils(): | ||
try: | ||
import IPython | ||
|
@@ -50,6 +88,7 @@ def _get_context_tag(context_tag_key): | |
return None | ||
|
||
|
||
@_use_context_attribute_if_available("aclPathOfAclRoot") | ||
def acl_path_of_acl_root(): | ||
try: | ||
return _get_command_context().aclPathOfAclRoot().get() | ||
|
@@ -72,6 +111,7 @@ def is_databricks_default_tracking_uri(tracking_uri): | |
return tracking_uri.lower().strip() == "databricks" | ||
|
||
|
||
@_use_context_attribute_if_available("notebookId", if_available=_return_true) | ||
def is_in_databricks_notebook(): | ||
if _get_property_from_spark_context("spark.databricks.notebook.id") is not None: | ||
return True | ||
|
@@ -111,6 +151,7 @@ def is_dbfs_fuse_available(): | |
return False | ||
|
||
|
||
@_use_context_attribute_if_available("clusterId", if_available=_return_true) | ||
def is_in_cluster(): | ||
try: | ||
spark_session = _get_active_spark_session() | ||
|
@@ -122,6 +163,7 @@ def is_in_cluster(): | |
return False | ||
|
||
|
||
@_use_context_attribute_if_available("notebookId") | ||
def get_notebook_id(): | ||
"""Should only be called if is_in_databricks_notebook is true""" | ||
notebook_id = _get_property_from_spark_context("spark.databricks.notebook.id") | ||
|
@@ -133,6 +175,7 @@ def get_notebook_id(): | |
return None | ||
|
||
|
||
@_use_context_attribute_if_available("notebookPath") | ||
def get_notebook_path(): | ||
"""Should only be called if is_in_databricks_notebook is true""" | ||
path = _get_property_from_spark_context("spark.databricks.notebook.path") | ||
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. does this work with ephemeral notebooks within and without jobs? |
||
|
@@ -144,6 +187,7 @@ def get_notebook_path(): | |
return _get_extra_context("notebook_path") | ||
|
||
|
||
@_use_context_attribute_if_available("runtimeVersion") | ||
def get_databricks_runtime(): | ||
if is_in_databricks_runtime(): | ||
spark_session = _get_active_spark_session() | ||
|
@@ -154,13 +198,15 @@ def get_databricks_runtime(): | |
return None | ||
|
||
|
||
@_use_context_attribute_if_available("clusterId") | ||
def get_cluster_id(): | ||
spark_session = _get_active_spark_session() | ||
if spark_session is None: | ||
return None | ||
return spark_session.conf.get("spark.databricks.clusterUsageTags.clusterId") | ||
|
||
|
||
@_use_context_attribute_if_available("jobGroupId") | ||
def get_job_group_id(): | ||
try: | ||
dbutils = _get_dbutils() | ||
|
@@ -171,6 +217,7 @@ def get_job_group_id(): | |
return None | ||
|
||
|
||
@_use_context_attribute_if_available("replId") | ||
def get_repl_id(): | ||
""" | ||
:return: The ID of the current Databricks Python REPL | ||
|
@@ -198,20 +245,23 @@ def get_repl_id(): | |
pass | ||
|
||
|
||
@_use_context_attribute_if_available("jobId") | ||
def get_job_id(): | ||
try: | ||
return _get_command_context().jobId().get() | ||
except Exception: | ||
return _get_context_tag("jobId") | ||
|
||
|
||
@_use_context_attribute_if_available("idInJob") | ||
def get_job_run_id(): | ||
try: | ||
return _get_command_context().idInJob().get() | ||
except Exception: | ||
return _get_context_tag("idInJob") | ||
|
||
|
||
@_use_context_attribute_if_available("jobTaskType") | ||
def get_job_type(): | ||
"""Should only be called if is_in_databricks_job is true""" | ||
try: | ||
|
@@ -220,6 +270,7 @@ def get_job_type(): | |
return _get_context_tag("jobTaskType") | ||
|
||
|
||
@_use_context_attribute_if_available("commandRunId") | ||
def get_command_run_id(): | ||
try: | ||
return _get_command_context().commandRunId().get() | ||
|
@@ -228,6 +279,7 @@ def get_command_run_id(): | |
return None | ||
|
||
|
||
@_use_context_attribute_if_available("apiUrl") | ||
def get_webapp_url(): | ||
"""Should only be called if is_in_databricks_notebook or is_in_databricks_jobs is true""" | ||
url = _get_property_from_spark_context("spark.databricks.api.url") | ||
|
@@ -239,13 +291,15 @@ def get_webapp_url(): | |
return _get_extra_context("api_url") | ||
|
||
|
||
@_use_context_attribute_if_available("workspaceId") | ||
def get_workspace_id(): | ||
try: | ||
return _get_command_context().workspaceId().get() | ||
except Exception: | ||
return _get_context_tag("orgId") | ||
|
||
|
||
@_use_context_attribute_if_available("browserHostName") | ||
def get_browser_hostname(): | ||
try: | ||
return _get_command_context().browserHostName().get() | ||
|
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
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.
nice use of the decorator factory here. +1