17/28
Part 4 of 6 · Tasks that wait on othersLearn and do

A task can wait for other tasks to finish

In two reports, the second part of the round added dependencies between tasks (2 reports). One candidate solved part one and then ran out of time debugging part two (1 report). So plan this part before you need it.

Here's a running example: a pipeline for an uploaded photo.

Each arrow runs from a task to a task that waits on it
fetchparsethumbindex

index can't run until both parse and thumb are done. "Done" means acked. A dependency that is only reserved, or one that failed and is waiting to retry, still blocks.

Don't check every task's dependencies on every reserve. Keep two things instead, as Kahn's algorithm does in the topological sort lesson:

  • blocked: for each task, how many of its dependencies aren't done yet
  • dependents: for each task, the tasks that wait on it

When a task is acked, go through its dependents and lower each count by one. A task whose count reaches 0 is free, and it joins the ready heap with its own enqueue number. So once it's free, it takes its place among the waiting tasks by when it was enqueued, not by when it was freed.

Predict

fetch is acked, and then parse. thumb is still reserved. What is index's count?

Pick an answer above to see the rest