Skip to content
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- **`bench/embed`:** the same job on Ray Data (`pixi run -e ray bench-embed-ray`), and `bench-embed check`, which checks any output directory.
- **Keyed partitions in flows:** `dista.flow`'s `Partition` and `Reduce` run a shuffle, such as a group-by, inside a flow, with results by partition; `partition_of` is a stable hash to split by, and `WithLike` names a `TaskWith`'s element type as `Elem`.
- **Losses in a flow's shuffle:** a worker lost before a `Partition`'s barrier reads the inputs whose pieces it held again; one lost after it fails the flow naming the operator and the partition; `FlowStats.partition_bytes` gives each partition's bytes (from the new `Client.size_of`); a restarted head keeps every result of a re-sent `TaskMany` a rejoining worker had made, not only its first.
- **A `Partition`'s pieces stay on their workers** whatever their size, in the store and spilling past it, not in the head's memory: `remote_many(..., keep_on_worker=True)`, which a flow's `Partition` sets.

### Changed

Expand Down
72 changes: 67 additions & 5 deletions DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ Since phase 2's PR 3, scheduling lives in one place: a head process. Every progr
- **The head's event loop** `poll`s the listener and every connection. Each connection is a `Conn`: reads and writes never block, input accumulates until frames are whole, and output queues until the socket takes it. A slow peer never blocks anyone else, so the phase-1 rule limiting pipelined argument size is gone.
- **Every connected socket is `O_NONBLOCK`**, and `Stream`'s `write_all` and `read_into` wait in `poll` when it would block, so to the code that wants to wait they still do. Until 2026-11-02 dista used per-call `MSG_DONTWAIT` instead, because `fcntl` is variadic: Apple's arm64 ABI passes variadic arguments on the stack, a plain `external_call` puts them in registers, and `F_SETFL` silently set nothing. But macOS blocks a send with `MSG_DONTWAIT` once the socket's buffer is full, so a driver that stopped reading stalled the whole head: with 2,000 tasks of 60 KB results, 20 ran while the driver slept and the rest waited. `external_call[..., num_fixed_args=2]` declares the variadic split, as memory-region's `open` does, and now sets the flag; `tests/test_frame.mojo` fills a socket and checks the send returns.
- **Scheduling:** tasks queue in arrival order. Tasks waiting on inputs are held aside until every input exists. Queued tasks go to workers round-robin, up to `pipeline` in flight each (default 2); a worker with `DISTA_WORKER_SLOTS` above 1 takes its slots plus `pipeline - 1` (see "Worker slots"). Each result or error goes back to the driver that owns it.
- **Small results go through the head; large ones stay on their worker.** A result of 64 KiB or less comes back inline. The head keeps it, because a later task may take it as input, and forwards it to the owning driver. A larger result stays on the worker that computed it. The worker reports only its size; the head records where it lives and tells the owner, using located frames. The driver's `get` fetches it straight from that worker.
- **Small results go through the head; large ones stay on their worker.** A result of 64 KiB or less comes back inline. The head keeps it, because a later task may take it as input, and forwards it to the owning driver. A larger result stays on the worker that computed it. The worker reports only its size; the head records where it lives and tells the owner, using located frames. The driver's `get` fetches it straight from that worker. A `TaskMany` submitted with `keep_on_worker=True`, as a flow's `Partition` is, keeps all its results on its worker whatever their size (`KEEP_ON_WORKER`, the top bit of its result count in the frames).
- **Every worker is also an object server.** Its loop `poll`s the head, its listener, the drivers and workers fetching from it, and the workers it fetches from. A task whose inputs live elsewhere arrives as task-inputs, with each input either inline or as an address. It waits, parked, until every input has arrived, and the worker keeps serving fetches meanwhile. No worker waits on another synchronously, so two workers that need each other's results can't deadlock. With one slot, the default, running a task blocks the loop, so a fetch from a busy worker waits until its current task ends; with more, tasks run on threads and the loop keeps serving (see "Worker slots").
- **On one host, large results go through shared memory.** A worker writes each large result into a file in the node store, through memory-region's `MappedRegion`. The store is `/dev/shm` on Linux, `$TMPDIR` on macOS, or `DISTA_STORE_DIR`. Files are named `dista-<token>-<object>`, where the token is random per worker. The location the worker reports holds its address, its host name (for diagnostics), the file's path and the exact size. A reader that can see that file maps it read-only instead of fetching it. With a random token in the name, a visible file is the right one. Host names would have been the obvious test, but on GitHub's macOS runners processes on one machine reported different ones. A worker copies the bytes into the task's argument, once, from its mapping; the driver's `get` decodes straight from the mapping. A reader on another host fetches over a socket. Workers delete their files when they exit. `DISTA_SHM=0` turns the store off. In Kubernetes each pod has its own `/dev/shm`, so pods use sockets unless they share a volume for `DISTA_STORE_DIR`. Measured: a 100 MB result goes from one worker to a second worker and to the driver in about 0.2 s, with no socket bytes.
- **A cluster token** ([#124](https://github.com/magmalake/dista.mojo/issues/124), `src/dista/auth.mojo`). dista has no other authentication: anything that reached a head or a worker's object server could join, submit or fetch. With `DISTA_TOKEN` (or `DISTA_TOKEN_FILE`, its first line) set, every connection a process opens to a head or a worker starts with an `auth` frame (kind 34) carrying it, and the head and every worker's object server hand out nothing from a connection they accepted until that frame has come with the token. A missing or wrong one, or more than 8 KiB sent before it (no large frame gets a buffer of its own first), closes the connection unanswered. The comparison takes the same time wherever the bytes differ, and the token is never printed. As in Ray's token mode (`RAY_AUTH_MODE=token`), it authenticates and doesn't encrypt: a cluster still belongs on a private network, and the token keeps anything else there out. Unset, nothing changes, and an `auth` frame that arrives anyway is dropped. orca gives each run start a token of its own when one run spans machines (orca.mojo#268).
Expand Down Expand Up @@ -1132,11 +1132,11 @@ number of inputs that will have batches isn't known before then.
`Worker._finish`) stays on the worker that made it: in the node store up
to `DISTA_STORE_BYTES`, then in `DISTA_SPILL_DIR` on disk, then in memory
if both fail ("Spilling" above). Readers map it on the same host and fetch
it elsewhere. A piece of 64 KiB or less goes to the head, which keeps it
in memory until it is released and never spills it ("Roles and the
it elsewhere. A piece of 64 KiB or less would go to the head, which keeps
it in memory until it is released and never spills it ("Roles and the
head"). Many small pieces would put the shuffle in the head's memory, so
PR 3 marks a `Partition`'s pieces to stay on their worker whatever their
size.
size (*done*, "PR 3, as built").
- **When they go.** Each piece is released by the driver when its reducer
is submitted, and freed by the head and its worker once that reducer
settles. A flow that fails releases every piece, as it releases
Expand Down Expand Up @@ -1258,7 +1258,7 @@ it.
|---|---|---|---|
| 1 | **Done.** **Partition and Reduce**: the two operators, `WithLike`, `ManyLike.Part`'s bound, the checks, the two-chain executor, results by partition, raised errors failing the flow, `partition_of`. | `tests/test_flow_partition.mojo` in local mode: test_shuffle's group-by as a flow, 8 inputs of 20 batches, `parts` 4, equal to one process's `Dict`; inputs with no batches, and a partition no key reaches, each handed out once; a raising split fails the flow and leaves the stores empty; two runs give bit-identical float sums; `tests/compile_fail` cases for a `Reduce` without a `Partition`, a piece type that doesn't match, and a `Reduce` whose task doesn't take an `Int`. | ~800 |
| 2 | **Done.** **Losses**: the barrier's piece check, rerun before it, the clear failure after it, per-partition bytes in `stats`. | `tests/flow_loss.sh`, partition case: the worker holding the most pieces killed before the barrier gives exact results with at least one rerun; killed while reducers run, the flow fails with the message and the stores are empty. `tests/head_restart.sh` with a partition flow restarted before its barrier finishes exact. | ~500 |
| 3 | **Pieces stay on their worker**: a flag on the task-many frames so a `Partition`'s pieces are stored whatever their size. | 2,000 pieces of 10 KB: the head's memory doesn't grow with them. With `DISTA_STORE_BYTES` room for a quarter of the pieces, the rest spill, the result is exact and the spill files are gone when the run ends. A program that doesn't set the flag sends the bytes it sent before. | ~400 |
| 3 | **Done.** **Pieces stay on their worker**: a flag on the task-many frames so a `Partition`'s pieces are stored whatever their size. | 2,000 pieces of 10 KB: the head's memory doesn't grow with them. With `DISTA_STORE_BYTES` room for a quarter of the pieces, the rest spill, the result is exact and the spill files are gone when the run ends. A program that doesn't set the flag sends the bytes it sent before. | ~400 |
| 4 | **bench/shuffle as a flow**: `deploy/shuffle_flow.mojo`, printing the same `ANSWER` and `TOP` lines; `bench/shuffle/README.md` updated. | `TAXI_MODE=both` passes on the local table: the flow's answer equals one process's and the hand-written version's. CI builds it. | ~300 |
| 5 | **The runs on `sai`** and their write-up in `bench/shuffle/results/`, as `bench/embed` did: the flow, the hand-written dista version and Ray (`shuffle_ray.py`) on the same table, workers and CPU sets, `PARTS` 16 and 64, 3 passes each, time and peak memory (the sum of PSS over the job's processes). Then the flow again with `DISTA_STORE_BYTES` at a quarter of the pieces' bytes. | The bars below. | write-up |

Expand Down Expand Up @@ -1325,6 +1325,68 @@ it.
it, and fails the flow. The window is the time between the worker going
and the head's error reaching the driver.

**PR 3, as built.** What differs from the plan above, and why:

- **The flag on the wire** is the top bit of the `TaskMany`'s result count
(`KEEP_ON_WORKER`, 1 << 63, in `dista/frame.mojo`), not a new bit in the
flags byte or a trailing field. The count is the one field every frame
that runs a `TaskMany` carries: task-many (driver to head and head to
worker), and task-after and task-inputs with `MANY`. Task-many has no
flags byte, and a trailing field would sit after the encoded argument,
which has no length. The count is at most 2^20, so the bit was always 0:
a program that doesn't set the flag sends the frames it sent before, byte
for byte (`tests/test_partition_keep.mojo` compares them with the layout
written out by hand), and a head or worker from before the flag would
refuse a flagged count as out of range rather than misread it. Only one
binary runs on every node, so nothing more is done for mixed versions.
- **Where it travels.** The driver's frame carries it; the head keeps it
with a waiting task (`_Task.keep`) and writes it into the count of the
task-many or task-inputs frame it sends the worker, so a task retried on
another worker after its worker is lost keeps it, and a task-many frame
goes on as it came. The driver keeps each submission's payload to send
again after a head restart (`Client._subs`), so a re-sent task carries it
too. The worker reads it from the count and stores every result of the
task (`Worker._finish`), in the node store and past `DISTA_STORE_BYTES`
in the spill directory, as for a large result.
- **An empty piece still goes to the head**: the node store can't map an
empty file (`SharedMapping` refuses one). A `Seq` is never empty when
encoded, so a `Partition`'s pieces never are.
- **The API**: `remote_many(..., keep_on_worker=False)`, a keyword-only
argument on all three forms of `Client.remote_many` and
`Runtime.remote_many`, so every existing call is unchanged. `Partition`
passes `True`; nothing else does.
- **Sizes and the barrier** work as before: a kept piece's size comes from
its located frame (`Client.size_of`, `Location.size`), so
`FlowStats.partition_bytes` still counts every piece, and the worker still
answers a `TaskMany`'s pieces last first, so the first piece settled
still means all are.
- **Measured** (`tests/test_partition_keep.mojo`, local mode, 2 workers),
the head's resident memory grown from its start once every piece is made
and none released: 496 pieces of 10 KB, 6.5 MB without the flag and
0.9 MB with it; 2,000 pieces, 24.1 MB and 1.9 MB (macOS); on `sai`
(Linux), 8.6 and 0.6 MB, then 20.9 and 0.8 MB. What is left with the flag
is a location and a span per piece. The head's stats say it exactly:
without the flag it holds 2,000 small results, with it none and 2,000
locations. The test checks the counts; the memory is printed, since a
threshold on it would be flaky.
- **A cost for PR 5 to measure**: a kept piece is a file written per piece,
and a reducer on another host fetches each of its pieces from the worker
that made it, where a small one used to come inline in its task-inputs
frame from the head: 416 small fetches per reducer on AWS. A reader on
the same host maps them.
- **A lost worker now loses its small pieces too**, which used to be safe
at the head, so a loss before the barrier reads again every input whose
pieces that worker held, not only the one it was running: one test that
expected exactly one rerun (`tests/test_flow_partition.mojo`, a partition
task's worker dying) now expects at least one, with every input read once
more per rerun. This is the plan's loss model for every piece over
64 KiB already.
- **After a head restart** a kept piece is claimed back rather than re-sent
with its bytes, so until its worker says where it is it has no size, and
counts in `FlowStats.unsized_pieces` (`tests/head_restart.sh`'s partition
flow shows 4); before, small pieces always had one.
- `_run_many`'s three copies of a partition task's pieces are as they were.

**Bars for PR 5** (to confirm): every answer exact against one process;
the flow's median pass within 15% of the hand-written version's and no
slower than Ray's; the flow's peak memory within 10% of the hand-written
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ What works today:
- **Several tasks per worker:** `DISTA_WORKER_SLOTS=N` (0 for one per core) runs up to N tasks at once on a worker's threads, while it keeps serving its results; the default, 1, runs one at a time. N slots hold N tasks' inputs and results, and tasks must be thread-safe ([tests/test_slots.mojo](tests/test_slots.mojo)).
- **Worker groups:** a task type with `group="gpu"` (`Task`, `Task2`, `Task3`, `TaskWith`, `TaskMany`, `TaskStream`, `StatefulTask`) runs only on workers started with `DISTA_GROUP=gpu`, and a task without one only on workers without one. In local mode, `Runtime(workers=4, groups="gpu=1")` forks a worker of group `gpu` beside the four; a task of a group with no worker fails there at once, and waits in a cluster. `dista status` shows each worker's group ([tests/test_groups.mojo](tests/test_groups.mojo)). An `Actor` with `group=` is placed, restored and moved only on workers of its group, and `dista drain` refuses the last worker of a group while tasks of it are queued. `dista status --json` and the head's `/metrics` count each group's workers, slots and tasks (`groups`, `dista_group_*{group=...}`), and `dista scale --once --group gpu` decides for one group alone ([tests/autoscale.sh](tests/autoscale.sh) `groups`).
- **Sizing from the grant:** `DISTA_WORKER_SLOTS=auto` takes a worker's slots from the run's grant (`MORCA_GRANT_CPUS`, shared by a local run's workers) and runs only the tasks that fit the grant's `MORCA_GRANT_MEMORY_MB` (each at `DISTA_TASK_MEMORY_MB`, or what its footprint shows): the rest wait at the head for a worker with room, and the store spills at half of it. It starts at one task in flight and raises the count while throughput rises, backing off on memory pressure, spills or slower tasks, and learns what a task takes from the ones that ran alone, so no size need be given ([tests/test_sizing.mojo](tests/test_sizing.mojo)).
- **Large results stay on the worker that made them.** On the same host other processes map them from shared memory; across hosts they fetch them directly from that worker. Small ones come back through the head.
- **Large results stay on the worker that made them.** On the same host other processes map them from shared memory; across hosts they fetch them directly from that worker. Small ones come back through the head, except a `TaskMany`'s with `rt.remote_many(..., keep_on_worker=True)`, as a flow's `Partition` submits its own: those stay on their worker too, and spill, so a shuffle's many small pieces aren't held by the head ([tests/test_partition_keep.mojo](tests/test_partition_keep.mojo)).
- **Spilling:** past `DISTA_STORE_BYTES` of results, or when the store is full, a worker writes the rest to `DISTA_SPILL_DIR` on disk, and readers map or fetch them as before ([tests/test_spill.mojo](tests/test_spill.mojo)).
- **Data-aware scheduling:** a task goes to the node holding most of its inputs' bytes, unless that node has no worker free ([tests/locality.sh](tests/locality.sh)).
- **Speed:** small tasks cost about 6 µs each.
Expand Down
1 change: 1 addition & 0 deletions pixi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ test-cancel = { cmd = "sh scripts/test.sh test_cancel" }
test-wait = { cmd = "sh scripts/test.sh test_wait" }
test-stream = { cmd = "sh scripts/test.sh test_stream" }
test-flow = { cmd = "sh scripts/test.sh test_flow" }
test-partition-keep = { cmd = "sh scripts/test.sh test_partition_keep" }
test-stateful = { cmd = "sh scripts/test.sh test_stateful" }
test-groups = { cmd = "sh scripts/test.sh test_groups" }
test-head-drivers = { cmd = "sh scripts/test.sh test_head_drivers" }
Expand Down
Loading
Loading