-
Notifications
You must be signed in to change notification settings - Fork 20
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
Dbconnect progress #1355
Merged
Merged
Dbconnect progress #1355
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
24b15a9
Show progress bar for spark jobs from DB Connect
fjakobs b1ceb52
Inline code from pyspark
fjakobs ec6797b
Add option to disable progress through an environment variable
fjakobs 14e71af
Expose db connect progress as a settings
fjakobs 6e5d675
Reload ipy kernel on settings change
fjakobs 75bd656
Also show progress for running python files
fjakobs 013e513
fix tests
fjakobs 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 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 | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -3,6 +3,7 @@ | |||||||||||
import json | ||||||||||||
from typing import Any, Union, List | ||||||||||||
import os | ||||||||||||
import time | ||||||||||||
import shlex | ||||||||||||
import warnings | ||||||||||||
import tempfile | ||||||||||||
|
@@ -24,6 +25,7 @@ def logError(function_name: str, e: Union[str, Exception]): | |||||||||||
|
||||||||||||
try: | ||||||||||||
from IPython import get_ipython | ||||||||||||
from IPython.display import display | ||||||||||||
from IPython.core.magic import magics_class, Magics, line_magic, needs_local_scope | ||||||||||||
except Exception as e: | ||||||||||||
logError("Ipython Imports", e) | ||||||||||||
|
@@ -101,7 +103,13 @@ def __init__(self, env_name: str, default: any = None, required: bool = False): | |||||||||||
|
||||||||||||
def __get__(self, instance, owner): | ||||||||||||
if self.env_name in os.environ: | ||||||||||||
return self.transform(os.environ[self.env_name]) | ||||||||||||
if self.transform is not bool: | ||||||||||||
return self.transform(os.environ[self.env_name]) | ||||||||||||
|
||||||||||||
if os.environ[self.env_name].lower() == "true" or os.environ[self.env_name] == "1": | ||||||||||||
return True | ||||||||||||
elif os.environ[self.env_name].lower() == "false" or os.environ[self.env_name] == "0": | ||||||||||||
return False | ||||||||||||
|
||||||||||||
if self.required: | ||||||||||||
raise AttributeError( | ||||||||||||
|
@@ -117,6 +125,7 @@ def __set__(self, instance, value): | |||||||||||
class LocalDatabricksNotebookConfig: | ||||||||||||
project_root: str = EnvLoader("DATABRICKS_PROJECT_ROOT", required=True) | ||||||||||||
dataframe_display_limit: int = EnvLoader("DATABRICKS_DF_DISPLAY_LIMIT", 20) | ||||||||||||
show_progress: bool = EnvLoader("SPARK_CONNECT_PROGRESS_BAR_ENABLED", default=False) | ||||||||||||
|
||||||||||||
def __new__(cls): | ||||||||||||
annotations = cls.__dict__['__annotations__'] | ||||||||||||
|
@@ -357,6 +366,99 @@ def df_html(df): | |||||||||||
html_formatter.for_type(SparkConnectDataframe, df_html) | ||||||||||||
html_formatter.for_type(DataFrame, df_html) | ||||||||||||
|
||||||||||||
@logErrorAndContinue | ||||||||||||
@disposable | ||||||||||||
def register_spark_progress(spark, show_progress: bool): | ||||||||||||
try: | ||||||||||||
import ipywidgets as widgets | ||||||||||||
except Exception as e: | ||||||||||||
return | ||||||||||||
|
||||||||||||
class Progress: | ||||||||||||
SI_BYTE_SIZES = (1 << 60, 1 << 50, 1 << 40, 1 << 30, 1 << 20, 1 << 10, 1) | ||||||||||||
SI_BYTE_SUFFIXES = ("EiB", "PiB", "TiB", "GiB", "MiB", "KiB", "B") | ||||||||||||
|
||||||||||||
def __init__( | ||||||||||||
self | ||||||||||||
) -> None: | ||||||||||||
self._ticks = None | ||||||||||||
self._tick = None | ||||||||||||
self._started = time.time() | ||||||||||||
self._bytes_read = 0 | ||||||||||||
self._running = 0 | ||||||||||||
self.init_ui() | ||||||||||||
|
||||||||||||
def init_ui(self): | ||||||||||||
self.w_progress = widgets.IntProgress( | ||||||||||||
value=0, | ||||||||||||
min=0, | ||||||||||||
max=100, | ||||||||||||
bar_style='success', | ||||||||||||
orientation='horizontal' | ||||||||||||
) | ||||||||||||
self.w_status = widgets.Label(value="") | ||||||||||||
if show_progress: | ||||||||||||
display(widgets.HBox([self.w_progress, self.w_status])) | ||||||||||||
|
||||||||||||
def update_ticks( | ||||||||||||
self, | ||||||||||||
stages, | ||||||||||||
inflight_tasks: int | ||||||||||||
) -> None: | ||||||||||||
total_tasks = sum(map(lambda x: x.num_tasks, stages)) | ||||||||||||
completed_tasks = sum(map(lambda x: x.num_completed_tasks, stages)) | ||||||||||||
if total_tasks > 0: | ||||||||||||
self._ticks = total_tasks | ||||||||||||
self._tick = completed_tasks | ||||||||||||
self._bytes_read = sum(map(lambda x: x.num_bytes_read, stages)) | ||||||||||||
if self._tick is not None and self._tick >= 0: | ||||||||||||
self.output() | ||||||||||||
self._running = inflight_tasks | ||||||||||||
Comment on lines
+414
to
+416
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.
Suggested change
|
||||||||||||
|
||||||||||||
def output(self) -> None: | ||||||||||||
if self._tick is not None and self._ticks is not None: | ||||||||||||
percent_complete = (self._tick / self._ticks) * 100 | ||||||||||||
elapsed = int(time.time() - self._started) | ||||||||||||
scanned = self._bytes_to_string(self._bytes_read) | ||||||||||||
running = self._running | ||||||||||||
self.w_progress.value = percent_complete | ||||||||||||
self.w_status.value = f"{percent_complete:.2f}% Complete ({running} Tasks running, {elapsed}s, Scanned {scanned})" | ||||||||||||
|
||||||||||||
@staticmethod | ||||||||||||
def _bytes_to_string(size: int) -> str: | ||||||||||||
"""Helper method to convert a numeric bytes value into a human-readable representation""" | ||||||||||||
i = 0 | ||||||||||||
while i < len(Progress.SI_BYTE_SIZES) - 1 and size < 2 * Progress.SI_BYTE_SIZES[i]: | ||||||||||||
i += 1 | ||||||||||||
result = float(size) / Progress.SI_BYTE_SIZES[i] | ||||||||||||
return f"{result:.1f} {Progress.SI_BYTE_SUFFIXES[i]}" | ||||||||||||
|
||||||||||||
|
||||||||||||
class ProgressHandler: | ||||||||||||
def __init__(self): | ||||||||||||
self.op_id = "" | ||||||||||||
|
||||||||||||
def reset(self): | ||||||||||||
self.p = Progress() | ||||||||||||
|
||||||||||||
def __call__(self, | ||||||||||||
stages, | ||||||||||||
inflight_tasks: int, | ||||||||||||
operation_id, | ||||||||||||
done: bool | ||||||||||||
): | ||||||||||||
if len(stages) == 0: | ||||||||||||
return | ||||||||||||
|
||||||||||||
if self.op_id != operation_id: | ||||||||||||
self.op_id = operation_id | ||||||||||||
self.reset() | ||||||||||||
|
||||||||||||
self.p.update_ticks(stages, inflight_tasks) | ||||||||||||
|
||||||||||||
spark.clearProgressHandlers() | ||||||||||||
spark.registerProgressHandler(ProgressHandler()) | ||||||||||||
|
||||||||||||
|
||||||||||||
@logErrorAndContinue | ||||||||||||
@disposable | ||||||||||||
|
@@ -382,9 +484,16 @@ def make_matplotlib_inline(): | |||||||||||
if not load_env_from_leaf(os.getcwd()): | ||||||||||||
sys.exit(1) | ||||||||||||
cfg = LocalDatabricksNotebookConfig() | ||||||||||||
|
||||||||||||
# disable build-in progress bar | ||||||||||||
show_progress = cfg.show_progress | ||||||||||||
if "SPARK_CONNECT_PROGRESS_BAR_ENABLED" in os.environ: | ||||||||||||
del os.environ["SPARK_CONNECT_PROGRESS_BAR_ENABLED"] | ||||||||||||
|
||||||||||||
create_and_register_databricks_globals() | ||||||||||||
register_magics(cfg) | ||||||||||||
register_formatters(cfg) | ||||||||||||
register_spark_progress(globals()["spark"], show_progress) | ||||||||||||
update_sys_path(cfg) | ||||||||||||
make_matplotlib_inline() | ||||||||||||
|
||||||||||||
|
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
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.
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.
This will show a message saying notebook init failed. We can skip showing that message by not logging error here.