Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions distributed/cli/dask_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
enable_proctitle_on_children,
enable_proctitle_on_current,
)
from distributed.scheduler import DEFAULT_SCHEDULER_PORT

logger = logging.getLogger("distributed.scheduler")

Expand Down Expand Up @@ -165,9 +166,9 @@ def main(

if port is None and (not host or not re.search(r":\d", host)):
if isinstance(protocol, list):
port = [8786] + [0] * (len(protocol) - 1)
port = [DEFAULT_SCHEDULER_PORT] + [0] * (len(protocol) - 1)
else:
port = 8786
port = DEFAULT_SCHEDULER_PORT

if isinstance(protocol, list) or isinstance(port, list):
if (not isinstance(protocol, list) or not isinstance(port, list)) or len(
Expand Down
3 changes: 2 additions & 1 deletion distributed/cli/dask_ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import click

from distributed.deploy.old_ssh import SSHCluster
from distributed.scheduler import DEFAULT_SCHEDULER_PORT

logger = logging.getLogger("distributed.dask_ssh")

Expand All @@ -30,7 +31,7 @@
)
@click.option(
"--scheduler-port",
default=8786,
default=DEFAULT_SCHEDULER_PORT,
show_default=True,
type=int,
help="Specify scheduler port number.",
Expand Down
33 changes: 26 additions & 7 deletions distributed/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,13 +717,32 @@ async def listen(self, port_or_addr=None, allow_offload=True, **kwargs):
else:
addr = port_or_addr
assert isinstance(addr, str)
listener = await listen(
addr,
self.handle_comm,
deserialize=self.deserialize,
allow_offload=allow_offload,
**kwargs,
)
try:
listener = await listen(
addr,
self.handle_comm,
deserialize=self.deserialize,
allow_offload=allow_offload,
**kwargs,
)
except OSError:
fallback_port_or_addr = kwargs.get("fallback_port_or_addr", None)
if not fallback_port_or_addr:
raise
warnings.warn(
Copy link
Author

Choose a reason for hiding this comment

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

this line is what causes the test distributed/deploy/tests/test_local.py::test_Client_twice to fail, if I just comment the warning, it passes. Should I remove the warning, change it to logger invocation instead, or is there some means of making the test accept a warning occurrence?

f"Address {addr} is already in use.\n"
f"Falling back to {fallback_port_or_addr} instead",
UserWarning,
stacklevel=2,
)
listener = await listen(
fallback_port_or_addr,
self.handle_comm,
deserialize=self.deserialize,
allow_offload=allow_offload,
**kwargs,
)

self.listeners.append(listener)

def handle_comm(self, comm: Comm) -> NoOpAwaitable:
Expand Down
2 changes: 1 addition & 1 deletion distributed/deploy/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def __init__(
start=None,
host=None,
ip=None,
scheduler_port=0,
scheduler_port=None,
Copy link

Choose a reason for hiding this comment

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

Sorry, why is this changed to None from 0?

Copy link
Author

Choose a reason for hiding this comment

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

so 0 stands for "random port", whereas None stands for "user did not give value, and wishes for default behaviour" -- we can thus decide later whether default behaviour means "use the default hardcoded port" (as I did in this PR) or "use random port" (eg if that would be for any reason preferred in a later PR). Leaving 0 makes it undistinguishable between "explicitly desiring random port" and "implicitly desiring default behaviour"

Copy link

Choose a reason for hiding this comment

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

Is this documented somewhere? We could probably use some Enum between the two choices?

Copy link
Author

Choose a reason for hiding this comment

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

the default port itself is mentioned somewhere in the docs. Going with enum is possible, though I'd not call it worth it in this case, I mean, the None => default is a popular idiom across python packages

mind that this PR is just meant as a small incremental improvement to unify behaivours a bit

silence_logs=logging.WARN,
dashboard_address=":8787",
worker_dashboard_address=None,
Expand Down
20 changes: 17 additions & 3 deletions distributed/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@
"stealing": WorkStealing,
}

DEFAULT_SCHEDULER_PORT = 8786


class ClientState:
"""A simple object holding information about a client."""
Expand Down Expand Up @@ -3674,7 +3676,6 @@ class Scheduler(SchedulerState, ServerNode):
Time we expect certain functions to take, e.g. ``{'sum': 0.25}``
"""

default_port = 8786
_instances: ClassVar[weakref.WeakSet[Scheduler]] = weakref.WeakSet()

worker_ttl: float | None
Expand Down Expand Up @@ -3785,8 +3786,18 @@ def __init__(
interface=interface,
protocol=protocol,
security=security,
default_port=self.default_port,
default_port=DEFAULT_SCHEDULER_PORT,
)
if port is None:
self._fallback_start_addresses = addresses_from_user_args(
host=host,
port=0,
interface=interface,
protocol=protocol,
security=security,
)
else:
self._fallback_start_addresses = []

http_server_modules = dask.config.get("distributed.scheduler.http.routes")
show_dashboard = dashboard or (dashboard is None and dashboard_address)
Expand Down Expand Up @@ -4199,11 +4210,14 @@ async def start_unsafe(self) -> Self:

self._clear_task_state()

for addr in self._start_address:
for addr, fallback_addr in itertools.zip_longest(
self._start_address, self._fallback_start_addresses
):
await self.listen(
addr,
allow_offload=False,
handshake_overrides={"pickle-protocol": 4, "compression": None},
fallback_port_or_addr=fallback_addr,
**self.security.get_listen_args("scheduler"),
)
self.ip = get_address_host(self.listen_address)
Expand Down
3 changes: 2 additions & 1 deletion distributed/utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
from distributed.node import ServerNode
from distributed.proctitle import enable_proctitle_on_children
from distributed.protocol import deserialize
from distributed.scheduler import DEFAULT_SCHEDULER_PORT
from distributed.scheduler import TaskState as SchedulerTaskState
from distributed.security import Security
from distributed.utils import (
Expand Down Expand Up @@ -2480,7 +2481,7 @@ def _bind_port(port):
s.listen(1)
yield s

default_ports = [8786]
default_ports = [DEFAULT_SCHEDULER_PORT]

while time() - start < _TEST_TIMEOUT:
try:
Expand Down
Loading