Repository navigation
Description
There is an issue with how the scheduler assigns tasks from the unrannable queue to workers who meet the resource requirements joining the scheduler.
The use case is some long running complex task where some tasks require an expensive resource, say GPUs, but those resources are only provided (through Adaptive) once the tasks requiring those resources are ready to be run. Say we come to a point in the computation where 5 tasks could be run, if GPUs were available. Managing the scale_up behaviour through adaptive is fairly straightforward and allows adding new compute nodes (for instance on AWS) with the required resources. The problem appears when the first of the new workers connects.
Scheduler.addWorker will go through the list of unrunnable tasks and check if there are workers that meet the requirements, since only the first worker has thus far connected there is only one worker that meets the requirements (some possibly positive number of workers may or may not booting up and joining shortly, but that hasn't happened yet).
for ts in list(self.unrunnable):
valid = self.valid_workers(ts)
if valid is True or ws in valid:
recommendations[ts.key] = 'waiting'
The task goes through released -> waiting and then waiting -> processing, the transition_waiting_processing again calls valid_workers to get a list of workers where the task(s) can be run (this list still just contains a single worker because the other ones haven't yet connected).
The end result of all of this is that the worker who happens connect first and have the resource required by the tasks gets all the tasks dumped onto it with all the other workers, who potentially connect just seconds later, get nothing and are shutdown by the scheduler because they are idling.
In short, it appears to be the case that the purpose of the resource_requirements is to act as a hint of required peak performance (memory, GPU, whatever) from the workers, and not to be a dynamically changing resource allocation. Is this the case, and is there any interested in changing that? The resources available and resources consumed are taken into account in transition_waiting_processing, but only on the worker_state not for the scheduler in general.
If this is not the intended behaviour and should be fixed, I'm more than happy to work on this.
Activity
[-]Scheduler behaves badly when adding workers to meet resource demand [/-][+]Scheduler behaves badly when adaptively adding workers to meet resource demand [/+]mrocklin commentedon Mar 22, 2018
This is not the intended behaviour and should be fixed. Help would be welcome!
I agree with your understanding of the situation and in general I'm glad to hear from someone who is comfortable working within the scheduler code.
My first instinct on how to solve this problem would be to look at improving work stealing with resources. The first worker should get all of the tasks if its the only one around, but it should also share them once others arrive.
Doing this well is likely to require some thought. The current work stealing system is probably not as clean as it could be. We'll also want to ensure that this doesn't cost scheduler time in the common case when resources are not in use.
mattilyra commentedon Mar 22, 2018
That was my first thought as well, so I merged the branch by quakberto (see #1389) with master and tested it.
The problem with relying on work stealing to sort this out is two fold:
The scheduler keeps scaling up and down which keeps the scheduler CPU busy and the work stealing code is rarely, if ever, executed. This is partially related to the custom
should_scale_upcode I have, which tries to account for resource starvation not only forunrunnabletasks but more generally in the hope that tasks would be moved (stolen) if more machines with the resources were available. The scheduler ends up killing then-1new workers because they are idling. I haven't totally cracked why exactly the tasks aren't getting stolen, this is also going to be highly dependent on the actual workload and seems somewhat brittle especially if adaptive is meant to allow people to run highly customised loads.The work stealing only happens once the scheduler gets some info back from the workers regarding the running times (or
occupancy) of the tasks. For tasks that take some time to run, say training ML models, this lag will be long enough for the scheduler to again get rid of the workers before they get a chance to steal anything. The changes fromquakbertoare still required but they're not enough on their own.My suggestion would be to instead change
Scheduler.valid_workersto do virtual accounting of resources required/supplied. Virtual because the resources haven't actually been taken yet, there is just a proposal to do so in the future. AdditionallyScheduler.consume_resourcesandScheduler.release_resourceswould need to modified to account for the resources available in the cluster (self.resources).I've started a branch (like 5 seconds ago) with these changes and am currently testing it (https://github.com/mattilyra/distributed/tree/resource_accounting_for_valid_workers).
mrocklin commentedon Mar 22, 2018
This is a problem all on its own. The scale_up and scale_down code should either be very fast, or we should execute them in a separate thread. Blocking the scheduler is generally not recommended.
I'm inclined to handle this separately. Maybe we make it easy for schedulers to persist this information between sessions.
mattilyra commentedon Mar 22, 2018
In general I think it would be better to try to fix this to make the scheduler be aware of the resource constraints in the first place and act fairly, or at least as fairly as it can instead of dumping all work on one node and then relying others to maybe steal it in the future.
Keeping tasks in the
unrunnablestate while no resources are available to be actively used seems like a reasonable thing to do.mrocklin commentedon Mar 22, 2018
The scheduler used to operate like this, keeping tasks that could be run but weren't in the scheduler until some worker was available that had resources to run them soon. We switched away from this in an effort to improve performance. It turns out to be useful to give the workers plenty to do so that they can start communicating for dependencies earlier, have a backlog of tasks to push in if other tasks take a while to get to it etc.. This was generally a success and so we've thrown out all of the old machinery to keep tasks in the scheduler. The
unrunnablecollection is a very small version of this that's still around. I don't think that we should rely on it in this way. For example, you might have to check on all of unrunnable every time a task completed rather than every time a new worker was added. This might affect our constant-time guarantees.mattilyra commentedon Mar 22, 2018
Good point.
Is there anything in the scheduler that keeps track of workers launched but not yet connected? That could perhaps be used as an indicator to hold back on pushing all the work onto the first worker that connects.
mrocklin commentedon Mar 22, 2018
I agree that this would help with planning but no, the scheduler has no such information. Actually the scheduler has no knowledge of how to launch workers. This tends to be handled by a Cluster object.
rbubley commentedon Mar 22, 2018
It occurs to me that if the scheduler was given knowledge of the target number of instances (e.g. through
cluster.scale_uptelling it), then it could allocate tasks to putative instances before they are created; then when they join they the details of the assigned tasks could be given to them. Of course, in the meantime their tasks should remain stealable.mrocklin commentedon Mar 22, 2018
mrocklin commentedon Mar 22, 2018
Sorry if I sound like I'm shooting down lots of ideas. Generally speaking we always try to do the best we can with the information we have at any given point, and then react incrementally as new information comes to light. A lot of the scheduler is based on this (including most of our validation checks) which is why I'm probably more resistant to some of these things than one might expect.
I do genuinely think that improving load balancing / work stealing is probably the right move here. It will work even if things go awry when allocating more workers.
mattilyra commentedon Mar 23, 2018
This is exactly the use case I have. Let's say the work load I have has 15 tasks that require a space laser, because space lasers are expensive to run I only want to run the machine(s) when the tasks that require those resources are actually ready to be executed.
The current logic will allow the "space laser" work to be stolen from the first worker that connects iff the tasks are quick to run. If the tasks take longer than the scheduler IOLoop, the scheduler will kill the
n-1new "space laser" workers, because it sees them idling before anyone gets a chance to even look at what could be stolen. The task stealing will only potentially happen once the first "space laser" task has finished and the scheduler gets information about theoccupancyof those tasks. If the tasks take a long time to run, say an hour, this is huge waste of resources. If the tasks are quick to run, then the existing logic more or less works.I can think of a number of solutions - "long running" below is anything that takes longer than one tick of the scheduler IOLoop:
Allow the scheduler to preemptively schedule tasks on workers that have been launched but haven't connected yet. Perhaps the
Clusterinstance could callscheduler.add_workeronce the new workers are launched. TheWorkerState.infocould have a variable telling that the worker is currently spinning up and will be available in X seconds, X would need to be adjusted by users depending on what actual hardware the cluster is deployed on. It would make sense to have X be adjustable per worker/host as different machines may require different boot up times. If the worker does not connect within a timeout period then the tasks are moved back into unrunnable. This partially violates the act in the now principle @mrocklin pointed out above, depending on what exactly constitutes now - you could argue that now includes the information about workers that have been launched but haven't connected yet.Explicitly check for stealable tasks (if the extension exists) in
retire_workersoradd_workeror both. This won't help the long running tasks on its own, as the scheduler, or more specificallystealingwill have stale estimates of task occupancy. The task occupancy is communicated back to the scheduler only when the task completes, not live while the task is running.2.1. The worker <-> scheduler comms should therefore also be changed so that task durations are communicated back to the scheduler while the work is being done. This should probably only happen in cases where the tasks are long running, i.e. the worker should send task duration updates every 5-10 seconds.
- Could this info be added to the heartbeat? There would likely be negative consequences for the constant time guarantees in handling the heartbeat on the scheduler.
Allow tasks to be marked as always stealable. Add an optional flag to
TaskStatethat allows users to mark tasks as stealable regardless of what the logic instealingis. This may result in tasks constantly being moved back and forth between workers unless the user takes very special care of matching resources with tasks. Might complicate usingdistributedto such an extent that it actually invalidates the point of having the flag.Change the semantics of
loose_restrictionsslightly so that whenloose_restrictions=False(restrictions are strict) the scheduler will not assign tasks to workers if sufficient resources are not available. This would require a minimal change toscheduler.valid_workersto do virtual accounting of resources consumed for the potential task assignments, and actual accounting inadd_resourcesandremove_resources.mrocklin commentedon Mar 23, 2018
Here are two additional possible solutions that I see:
Maintain durable information about task duration somewhere so that schedulers can share information between instantiations. That way we're only wasteful the first time we encounter a new function type, but efficient on subsequent computations at the same institution. This is potentially troublesome because we'll have to have some shared durable resource, which may not be easily accessible on all systems. I can imagine it being quite easy on systems with a shared file system (like most HPC systems) but difficult on more ephemeral systems that tear things down every time.
When we place a task into
unknown_durationswe also set up a call back to check in after a couple seconds. That callback sees if the task is still in unknown durations and, if so defines a known duration like 5s and kicks off work stealing for that task type if applicable. This seems easy to implement, relatively innocuous, and pretty fool-proof.mattilyra commentedon Mar 23, 2018
mrocklin commentedon Mar 23, 2018
Are there reasons why load balancing / work stealing couldn't be made to handle these workloads as well? People certainly use Dask as-is today for these sorts of things. The issue you bring up about the scheduler needing some information before work stealing can kick in is a real one, but I believe surmountable.
Adding queuing systems onto the scheduler would require non-trivial amounts of work. Someone would both need to construct this logic and ensure that it played nicely with all other parts of Dask (resources, resilience, long-running-tasks, tasks that create tasks, etc..)
6 remaining items
leej3 commentedon Jan 31, 2019
I find my self in a situation that I believe requires something along these lines, but I can open a new issue if desired.
My situation: I am running a pipeline, using a task-graph assembled with delayed objects, that has some tasks that use openmp threading. To enforce acceptable behavior I have defined an arbitrary resource on the workers to prevent too many tasks executing on any given worker. I am using dask-jobqueue so it is a homogenous pool of workers for now. I am seeing the same behavior mentioned above i.e. if the cluster job scheduler starts a couple of workers before the others they are given all the work and no load-balancing occurs when the others come online. This may be aggravated by the fact that some of the earlier tasks in the pipeline can take more than an hour, so any computation of the communication ratio won't help me.
I am wondering about what the best way to implement a solution to this is. I would be happy with a basic redistribution of work ever minute (or 10) if something more comprehensive is not feasible. Would the resource definition on the workers prevent this being implemented easily? I see that the scheduler's behavior can be modified by a plugin class... would this be the place to start? Or would the above mentioned callback be the place to attack this problem? Is it testing that is preventing this current issue moving forward or a desire for a more comprehensive solution?
Any help appreciated. Thanks.
guillaumeeb commentedon Feb 4, 2019
Hm, from what I understand here, adaptive seems to be broken when using
resourcesoption in dask-worker?@leej3 could you provide in minimal reproducible example?
leej3 commentedon Feb 5, 2019
Hi @guillaumeeb. Thanks for the help.
I believe the resources to be set appropriately using dask jobqueue following the "Providing additional arguments to the dask-workers" section of the docs. To start, here is a reproducible example with just LocalCluster though.
With no resources specified
With task time of 2 seconds, and n_workers = n_tasks I expect the job to finish after 2 or 3 task times because a communication ratio must be computed by the scheduler. This appears to be true (with some extra time for overhead):
With resources specified
When resources are specified for the jobs, job redistribution does not occur:
The process and thread ids used demonstrate that only one worker executed all the tasks:
outputlesteve commentedon Feb 6, 2019
Thanks a lot for the stand-alone snippet @leej3! I can reproduce the behaviour.
lesteve commentedon Feb 6, 2019
Reading @leej3's post more closely, it is not using
.adaptso maybe this is a slightly different problem ...lesteve commentedon Feb 6, 2019
sorry I missed the
.scalein @leej3's post, so I guess this is the same problem as.adapt.Here is a slightly simpler snippet to reproduce the problem:
Output:
Summary of the issue:
leej3 commentedon Feb 6, 2019
Thanks. A nice minimization too. Based on previous comments of this issue it seems like it will be tricky to modify the scheduler to work in a desirable manner for this case without adding unacceptable overhead to the default case where resources are not specified. At least we now have a nice and simple test for it though.
lesteve commentedon Feb 6, 2019
I guess the first thing to do would be to look at @mattilyra's branch https://github.com/mattilyra/distributed/commits/resource_accounting_for_valid_workers.
This branch does not work with recent
daskbecausedask.set_optionhas been removed. I tried fixing this but got another error:I ended up rebasing @mattilyra's branch on top of master and I got errors there too (see below), so this need to be looked at in more details. Of course @mattilyra if you have some spare bandwidth it would be great if you could have a closer look since it feels like you weren't that far to have something working:
calebho commentedon May 29, 2019
I'm seeing similar behavior with my workload where all tasks are going to a single worker because other workers haven't come online yet. This thread is more than a year old, so I'm not sure what's changed since the beginning of this thread. Anyways, here are my findings w.r.t. worker stealing not working properly with resources.
From what I can tell, stealing logic is implemented in
stealing.pywith aSchedulerPlugin. Every time a worker is added, state is updated to keep track of what tasks can be stolen from it. In particular, this is thestealableinstance attribute. Every time a task transitions, this plugin checks to see whether the task is transitioning to"processing"and callsput_key_in_stealablepassing in the task state.Inside of
put_key_in_stealable, the cost of moving the task is computed usingsteal_time_ratio. The first check insteal_time_ratiois checking whether the task has hard restrictions and whether any of the restrictions are set:Back in
put_key_in_stealable, nothing happens if the returned cost isNone:This would seem to explain why no work stealing occurs when tasks are marked with resources. Any comments on the analysis here?
Moving on to a possible fix, I think it makes sense to remove the restrictions check mentioned above and add some checks before the call to
maybe_move_taskinbalanceto ensure that the thief has the resources required to steal the task. Thoughts?TomAugspurger commentedon May 31, 2019
That all tracks.
That also makes sense, but the resource restriction on workers seems to complicate things quite a bit. Though, perhaps not... perhaps we can just continue to treat things normally, and only check the resource restriction at the last moment? That's won't be ideal, since we won't prioritize workers with the resource restriction as opportunities to steal, but it shouldn't be any worse...
Allow tasks with restrictions to be stolen
Allow tasks with restrictions to be stolen
Allow tasks with restrictions to be stolen
Allow tasks with restrictions to be stolen (#3069)