Skip to content

Commit

Permalink
Document Scheduler and Worker state machine
Browse files Browse the repository at this point in the history
  • Loading branch information
crusaderky committed Aug 24, 2022
1 parent 599708e commit 3ff0cfc
Show file tree
Hide file tree
Showing 23 changed files with 1,410 additions and 351 deletions.
33 changes: 19 additions & 14 deletions distributed/worker_state_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,10 @@ class TaskState:

#: The current state of the task
state: TaskStateState = "released"
#: The previous state of the task. It is not None iff state in (cancelled, resumed).
#: The previous state of the task. It is not None iff :attr:`state` in
#: (cancelled, resumed).
previous: Literal["executing", "long-running", "flight", None] = None
#: The next state of the task. It is not None iff state == resumed.
#: The next state of the task. It is not None iff :attr:`state` == resumed.
next: Literal["fetch", "waiting", None] = None

#: Expected duration of the task
Expand Down Expand Up @@ -562,7 +563,10 @@ class StealResponseMsg(SendMessageToScheduler):

@dataclass
class StateMachineEvent:
"""Base abstract class for all stimuli that can modify the worker state"""

__slots__ = ("stimulus_id", "handled")
#: Unique ID of the event
stimulus_id: str
#: timestamp of when the event was handled by the worker
# TODO Switch to @dataclass(slots=True), uncomment the line below, and remove the
Expand All @@ -571,6 +575,7 @@ class StateMachineEvent:
_classes: ClassVar[dict[str, type[StateMachineEvent]]] = {}

def __new__(cls, *args: Any, **kwargs: Any) -> StateMachineEvent:
"""Hack to initialize the ``handled`` attribute in Python <3.10"""
self = object.__new__(cls)
self.handled = None
return self
Expand Down Expand Up @@ -1126,10 +1131,10 @@ class WorkerState:
missing_dep_flight: set[TaskState]

#: Which tasks that are coming to us in current peer-to-peer connections.
#: This set includes exclusively:
#: - tasks with :attr:`state` == 'flight'
#: - tasks with :attr:`state` in ('cancelled', 'resumed') and
#: :attr:`previous` == 'flight`
#:
#: This set includes exclusively tasks with :attr:`~TaskState.state` == 'flight' as
#: well as tasks with :attr:`~TaskState.state` in ('cancelled', 'resumed') and
#: :attr:`~TaskState.previous` == 'flight`.
#:
#: See also :meth:`in_flight_tasks_count`.
in_flight_tasks: set[TaskState]
Expand Down Expand Up @@ -1171,10 +1176,10 @@ class WorkerState:
available_resources: dict[str, float]

#: Set of tasks that are currently running.
#: This set includes exclusively:
#: - tasks with :attr:`state` == 'executing'
#: - tasks with :attr:`state` in ('cancelled', 'resumed') and
#: :attr:`previous` == 'executing`
#:
#: This set includes exclusively tasks with :attr:`~TaskState.state` == 'executing'
#: as well as tasks with :attr:`~TaskState.state` in ('cancelled', 'resumed') and
#: :attr:`~TaskState.previous` == 'executing`.
#:
#: See also :meth:`executing_count` and :attr:`long_running`.
executing: set[TaskState]
Expand All @@ -1183,11 +1188,11 @@ class WorkerState:
#: :func:`~distributed.secede`, so they no longer count towards the maximum number
#: of concurrent tasks (nthreads).
#: These tasks do not appear in the :attr:`executing` set.
#: This set includes exclusively:
#: - tasks with :attr:`state` == 'long-running'
#: - tasks with :attr:`state` in ('cancelled', 'resumed') and
#: :attr:`previous` == 'long-running`
#:
#: This set includes exclusively tasks with
#: :attr:`~TaskState.state` == 'long-running' as well as tasks with
#: :attr:`~TaskState.state` in ('cancelled', 'resumed') and
#: :attr:`~TaskState.previous` == 'long-running`.
long_running: set[TaskState]

#: A number of tasks that this worker has run in its lifetime; this includes failed
Expand Down
9 changes: 9 additions & 0 deletions docs/source/images/run_dot.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

set -o errexit

for in_fname in *.dot
do
out_fname=${in_fname%.dot}.svg
dot -Tsvg $in_fname > $out_fname
done
7 changes: 2 additions & 5 deletions docs/source/images/task-state.dot
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@ digraph{
];
released1 [label=released];
released2 [label=released];
new -> released1;
released1 -> waiting;
waiting -> processing;
waiting -> "no-worker";
"no-worker" -> waiting;
"no-worker" -> processing;
waiting -> "no-worker" [dir=both];
processing -> memory;
processing -> error;
error -> forgotten;
error -> released2;
memory -> released2;
released2 -> forgotten;
}
150 changes: 73 additions & 77 deletions docs/source/images/task-state.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions docs/source/images/worker-cancel-state1.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
digraph{
graph [
bgcolor="#FFFFFFF00",
rankdir=LR,
];

executing1 [label="executing"];
executing2 [label="executing"];
cancelled [label="cancelled(executing)"];
resumed [label="resumed(fetch)"];

executing1 -> cancelled;
cancelled -> released;
cancelled -> executing2;
released -> forgotten;

cancelled -> resumed [dir=both];
resumed -> executing2;
resumed -> memory;
resumed -> fetch;
}
Loading

0 comments on commit 3ff0cfc

Please sign in to comment.