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 11 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]",
)
Comment on lines +213 to +217
Copy link
Member

Choose a reason for hiding this comment

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

I like the idea of having a flag you can set which allows workers to complete current tasks without accepting new ones. It's probably most useful when tasks a very large, which is not best practice, but common enough that we should make QoL improvements for those users.

I'm not sure on the drain terminology. In SLURM it means the same thing as it means here, finish existing work without accepting new work. However, in Kubernetes terminology it means evict all current work and reschedule tasks on another worker. So for Dask users in the Kubernetes community this may be confusing.

Perhaps this option could be made mode explicit like --complete-tasks-on-shutdown. Although that's pretty long, so if you can think of something better than that's great!

@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 @@ -402,6 +402,9 @@ class Worker(BaseWorker, ServerNode):
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 @@ -528,6 +531,7 @@ def __init__(
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 @@ -859,6 +863,8 @@ def __init__(
)
self.lifetime = lifetime

self.drain = drain

Worker._instances.add(self)

################
Expand Down Expand Up @@ -1685,13 +1691,30 @@ async def close_gracefully(
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(
workers=[self.address],
close_workers=False,
remove=False,
stimulus_id=f"worker-drain-{time()}",
)
if self.drain:
logger.warning(
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.")

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 @@ -1703,6 +1726,7 @@ async def close_gracefully(
)
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