Skip to content

Allow range of ports to be specified for Workers - #3704

Merged
mrocklin merged 9 commits into
dask:masterfrom
jrbourbeau:port-range
May 4, 2020
Merged

mrocklin merged 9 commits into
dask:masterfrom
jrbourbeau:port-range

Conversation

@jrbourbeau

Copy link
Copy Markdown
Member

This PR allows multiple ports to be specified for Worker and Nanny processes created with the dask-worker command. Here we allows users to specify the starting and stopping points for a sequential range of available ports like so <first-port>:<last-port>. So, for example, --worker-ports 9500:9600 would use ports 9500, 9501, ..., 9599, 9600.

dask-worker <scheduler-address> --nprocs 10 --worker-port 9500:9600 --nanny-port 9700:9800

This can be useful when using systems where a specific range of ports have been whitelisted for use. There was some discussion over in #899 where there seemed to be interest in adding this capability

Fixes #899

@mrocklin

Copy link
Copy Markdown
Member

Thanks for this @jrbourbeau . It's not every day that you're able to fix an issue below #1000 :)

I wonder if it's possible to make this change lower down, in the Worker class. About a year ago there was a push to remove logic from the dask-foo commands and push it into the classes themselves. This makes it easier for the other Dask subprojects to reuse this logic, even if they don't use the same dask-foo commands (I started this for dask-cuda, but I think that it ended up being useful for dask-gateway, and the more recent dask-ssh command).

@jrbourbeau

Copy link
Copy Markdown
Member Author

It's not every day that you're able to fix an issue below #1000

An oldie, but a goody : )

This makes it easier for the other Dask subprojects to reuse this logic

Yeah, that's a great point. We could push this down into the Worker class so Worker(..., port="9500:9600") would automatically select a port between 9500 and 9600 (inclusive). Since this isn't specifying an exact port, but rather a range of possible ports, we'll need to check that a given port is available and, if not, then try another port in the allowed range

@jrbourbeau jrbourbeau changed the title Allow range of ports to be specified with dask-worker CLI Allow range of ports to be specified for Workers Apr 21, 2020
@jrbourbeau

jrbourbeau commented Apr 21, 2020 •

Copy link
Copy Markdown
Member Author

I've moved the port range logic down into the Worker and Nanny classes, so we can now specify a range of ports both at the command line

dask-worker <scheduler-address> --nprocs 10 --worker-port 9500:9600

and using the Python API

w = await Worker(..., port="9500:9600")

The CI test failures are from a known flaky test (test_dont_steal_unknown_functions) and test_pid_file which I noticed popped up in master so I believe is unrelated (xref #3734 for tracking)

@TomAugspurger TomAugspurger left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Overall looks nice, just a couple questions.

Comment thread distributed/worker.py Outdated

await self.listen(
self._start_address, **self.security.get_listen_args("worker")
ports = parse_worker_ports(self._start_port)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Slightly concerned with the amount of duplication between here and the Nanny. What are your thoughts?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I spent some time trying to push this logic down into the underlying Server.listen call, but things got somewhat complicated so I decided to punt on it for now. I'll revisit this to see if we can do it in a simple way

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm completely fine with things as is if it's not straightforward to dedpulicate. I didn't look too closely.

Comment thread distributed/nanny.py
self._start_address, **self.security.get_listen_args("worker")
)
ports = parse_worker_ports(self._start_port)
for port in ports:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just for my understanding, does each nanny attempt to connect to the first port? One succeeds and the rest try the second? And so on?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, that's exactly what happens

Comment thread distributed/worker.py Outdated

await self.listen(
self._start_address, **self.security.get_listen_args("worker")
ports = parse_worker_ports(self._start_port)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm completely fine with things as is if it's not straightforward to dedpulicate. I didn't look too closely.

@jsignell

Copy link
Copy Markdown
Member

It looks like the failing tests are known flakey tests.

Comment thread distributed/worker.py Outdated
)
port = list(range(port_start, port_stop + 1))

return port

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This could be used for nannies schedulers or other servers I think. Maybe it should move to utils and we should remove worker from the name. Also, can I ask for a small docstring, maybe with a couple of examples?

Should it always return a list of integers for consistency, or would that complicate other parts of the codebase?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

+1 to moving to utils and adding documentation

Right now this function either returns a list of integers or the 1-element list [None]. I decided to keep the None option as to not break existing code paths where if None is received as a port, then some configured default port is used

Comment thread distributed/nanny.py Outdated

self._start_address = addresses_from_user_args(
host=self._start_host,
port=parse_worker_ports(self._start_port),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not sure that this is what we want. If I provide a range like "8000:9000" then I think that this will provide a list of 1000 addresses on which to listen. Is that correct? If so, that could quickly become problematic.

Instead, I would expect the port logic to occur around when we try to listen on a port, and find it to be busy.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It will provide a list of 1000 possible addresses to listen on but when we then make the call to self.listen it will start to loop through the 1000 addresses until we find one that is open, create a single listener for that address, and then return. So each listen call results in, at most, one listener being created. There's an added test, distributed/tests/test_core.py::test_multiple_addresses, which demonstrates this behavior.

I agree if we created 1000 listeners here that would be problematic. Do you think the behavior outlined above is still problematic?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

OK, I think that there might be a slight conflict here that may be my fault.

In #3288 I added the ability for the Scheduler to listen on many addresses at once. This is where the addresses_from_user_args function came from. However I never took the time to extend this functionality over to worker/nanny. The intention here was that we would iterate over many addresses from self._start_address and listen on all of them.

In this PR we're now reusing that same mechanism, but for a different purpose saying "listen on the first one that works". Everything today is fine, but the moment that we try to either have multiple networks active on the workers, or use a port range on the scheduler, we'll run into a biggish conflict I think.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Alright, thanks for the additional context. We would indeed have a conflict if we wanted to use, say, a port range and multiple interfaces on a Worker.

I'll roll back the recent changes that moved the "listen on the first address that works" logic to Server.listen and instead keep an explicit for-loop over multiple ports in the Worker/Nanny classes like in 2d7162e. I think having a single input address for Server.listen will help us better isolate any port range and/or multiple interface logic moving forward.

Comment thread distributed/worker.py
Comment on lines +1006 to +1031
ports = parse_ports(self._start_port)
for port in ports:
start_address = address_from_user_args(
host=self._start_host,
port=port,
interface=self._interface,
protocol=self._protocol,
security=self.security,
)
try:
await self.listen(
start_address, **self.security.get_listen_args("worker")
)
except OSError as e:
if len(ports) > 1 and e.errno == errno.EADDRINUSE:
continue
else:
raise e
else:
self._start_address = start_address
break
else:
raise ValueError(
f"Could not start Worker on host {self._start_host}"
f"with port {self._start_port}"
)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Does it make sense to try to move this logic into Server.listen? It would mean changing the interface of that method to something like the following I think.

async def listen(host, port, interface, protocol, security, ...)

Then maybe we can pull `address_from_user_args in, and apply this logic to the Scheduler and other future systems as well?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

(sorry to be late in reviewing by the way, if you want to just move forward and merge this that's fine with me too)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

For now I'd prefer to have this merged in as is, and then address the move to Server.listen in a follow-up PR

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

+1 from me

@mrocklin
mrocklin merged commit 8c6856e into dask:master May 4, 2020
@jrbourbeau
jrbourbeau deleted the port-range branch May 4, 2020 17:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: Worker port ranges and/with nprocs support

4 participants