Skip to content
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

feat: Broadcast plan over port 3238 #3756

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions daft/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@

_runner: Runner | None = None

_enable_broadcast: bool = False
_broadcast_addr: str = "127.0.0.1"
_broadcast_port: int = 3238
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put this behind a DashboardConfig dataclass?


_instance: ClassVar[DaftContext | None] = None
_lock: ClassVar[threading.Lock] = threading.Lock()

Expand Down Expand Up @@ -197,6 +201,31 @@
return _DaftContext


def broadcast_metrics(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should really be more of a "enable dashboard" function

enable: bool = True,
addr: str = "127.0.0.1",
port: int = 3238,
) -> DaftContext:
"""Enable (or disable) the broadcasting of Daft metrics over the given address.

This enables the Daft Dashboard to collect and display the data which is sent to it as Daft executes queries.

Args:
enable: Whether or not to enable broadcasting Daft metrics.
addr: The IP address on which to broadcast the metrics.
port: The port on which to broadcast the metrics.

Returns:
DaftContext: Daft context after enabling or disabling metrics broadcasting.
"""
ctx = get_context()
with ctx._lock:
ctx._enable_broadcast = enable
ctx._broadcast_addr = addr
ctx._broadcast_port = port
return ctx

Check warning on line 226 in daft/context.py

View check run for this annotation

Codecov / codecov/patch

daft/context.py#L221-L226

Added lines #L221 - L226 were not covered by tests


def set_runner_ray(
address: str | None = None,
noop_if_initialized: bool = False,
Expand Down
25 changes: 25 additions & 0 deletions daft/dataframe/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,28 @@
else:
return self._result_cache.value

def explain_broadcast(self):
raunakab marked this conversation as resolved.
Show resolved Hide resolved
"""Broadcast the mermaid-formatted plan on the given port (assuming metrics-broadcasting is enabled)."""
import socket

from daft.dataframe.display import MermaidFormatter

ctx = get_context()
with ctx._lock:
raunakab marked this conversation as resolved.
Show resolved Hide resolved
if not ctx._enable_broadcast:
return

addr = ctx._broadcast_addr
port = ctx._broadcast_port

Check warning on line 173 in daft/dataframe/dataframe.py

View check run for this annotation

Codecov / codecov/patch

daft/dataframe/dataframe.py#L172-L173

Added lines #L172 - L173 were not covered by tests

is_cached = self._result_cache is not None
instance = MermaidFormatter(builder=self.__builder, show_all=True, simple=False, is_cached=is_cached)
raunakab marked this conversation as resolved.
Show resolved Hide resolved
text: str = instance._repr_markdown_()

Check warning on line 177 in daft/dataframe/dataframe.py

View check run for this annotation

Codecov / codecov/patch

daft/dataframe/dataframe.py#L175-L177

Added lines #L175 - L177 were not covered by tests

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.connect((addr, port))
raunakab marked this conversation as resolved.
Show resolved Hide resolved
sock.sendall(text.encode("utf-8"))

Check warning on line 181 in daft/dataframe/dataframe.py

View check run for this annotation

Codecov / codecov/patch

daft/dataframe/dataframe.py#L179-L181

Added lines #L179 - L181 were not covered by tests

@DataframePublicAPI
def explain(
self, show_all: bool = False, format: str = "ascii", simple: bool = False, file: Optional[io.IOBase] = None
Expand All @@ -177,6 +199,7 @@
file (Optional[io.IOBase]): Location to print the output to, or defaults to None which defaults to the default location for
print (in Python, that should be sys.stdout)
"""
self.explain_broadcast()

Check warning on line 202 in daft/dataframe/dataframe.py

View check run for this annotation

Codecov / codecov/patch

daft/dataframe/dataframe.py#L202

Added line #L202 was not covered by tests
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this correct? Why would be broadcast to the dashboard if we're not executing a query

Copy link
Contributor Author

@raunakab raunakab Feb 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I was thinking if a user just wanted to see what plan would be produced without executing a query, they could also use the dashboard UI to view the query plan.

In this case, the UI would show:

  • the query plan
  • the time it took to produce the plan
  • "N/A" for the time it took to execute the query (since .explain doesn't execute queries)

is_cached = self._result_cache is not None
if format == "mermaid":
from daft.dataframe.display import MermaidFormatter
Expand Down Expand Up @@ -2818,6 +2841,7 @@
DataFrame: DataFrame with materialized results.
"""
self._materialize_results()
self.explain_broadcast()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe consider making this a decorator for all "execution" methods (show, collect, write_parquet etc)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. A decorator makes sense here. Will update in the coming commit.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to use decorator instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will need to add this to more APIs. Do we have an exhaustive list somewhere of all the APIs we would want to add this decorator to?


assert self._result is not None
dataframe_len = len(self._result)
Expand Down Expand Up @@ -2889,6 +2913,7 @@
n: number of rows to show. Defaults to 8.
"""
dataframe_display = self._construct_show_display(n)
self.explain_broadcast()
try:
from IPython.display import display

Expand Down
Loading