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

Add drain option to dask-worker #8752

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
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: 5 additions & 0 deletions distributed/cli/dask_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,11 @@
help="Whether or not to restart the worker after the lifetime lapses. "
"This assumes that you are using the --lifetime and --nanny keywords",
)
@click.option(
"--drain/--no-drain",
default=False,
help="Let the worker finish its current work before closing [default: --no-drain]",
)
@click.option(
"--preload",
type=str,
Expand Down
24 changes: 24 additions & 0 deletions distributed/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,9 @@
lifetime_restart: bool
Whether or not to restart a worker after it has reached its lifetime
Default False
drain: bool
The worker is allowed to complete its assigned worker before closing.
Default False.
kwargs: optional
Additional parameters to ServerNode constructor

Expand Down Expand Up @@ -510,6 +513,7 @@
lifetime: Any | None = None,
lifetime_stagger: Any | None = None,
lifetime_restart: bool | None = None,
drain: bool = False,
transition_counter_max: int | Literal[False] = False,
###################################
# Parameters to WorkerMemoryManager
Expand Down Expand Up @@ -840,6 +844,8 @@
)
self.lifetime = lifetime

self.drain = drain

Worker._instances.add(self)

################
Expand Down Expand Up @@ -1671,13 +1677,30 @@
This first informs the scheduler that we're shutting down, and asks it
to move our data elsewhere. Afterwards, we close as normal
"""
# `drain` mode waits for all tasks to finish before closing
# otherwise, we close immediately and unfinished tasks will be rescheduled or cancelled

if self.status in (Status.closing, Status.closing_gracefully):
await self.finished()
await self.scheduler.retire_workers(

Check warning on line 1685 in distributed/worker.py

View check run for this annotation

Codecov / codecov/patch

distributed/worker.py#L1685

Added line #L1685 was not covered by tests
workers=[self.address],
close_workers=False,
remove=False,
stimulus_id=f"worker-drain-{time()}",
)
if self.drain:
logger.warning(

Check warning on line 1692 in distributed/worker.py

View check run for this annotation

Codecov / codecov/patch

distributed/worker.py#L1691-L1692

Added lines #L1691 - L1692 were not covered by tests
f"Draining worker, waiting on {len(self.state.all_running_tasks)} tasks."
)
while len(self.state.all_running_tasks):
await asyncio.sleep(0.1)
logger.warning("Draining has finished.")

Check warning on line 1697 in distributed/worker.py

View check run for this annotation

Codecov / codecov/patch

distributed/worker.py#L1695-L1697

Added lines #L1695 - L1697 were not covered by tests

if self.status == Status.closed:
return

logger.info("Closing worker gracefully: %s. Reason: %s", self.address, reason)

# Wait for all tasks to leave the worker and don't accept any new ones.
# Scheduler.retire_workers will set the status to closing_gracefully and push it
# back to this worker.
Expand All @@ -1689,6 +1712,7 @@
)
if restart is None:
restart = self.lifetime_restart

await self.close(nanny=not restart, reason=reason)

async def wait_until_closed(self):
Expand Down
Loading