Compare commits
102
Commits
8d1605638e
..
v0.8.1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
095dead8b1 | ||
|
|
e25b27cfe0 | ||
|
|
164a83eb73 | ||
|
|
2baf73cbfe | ||
|
|
495acc0c47 | ||
|
|
8d2fed10d6 | ||
|
|
0f10415fb3 | ||
|
|
69a52a5578 | ||
|
|
8fd724a6b6 | ||
|
|
3b0e06ef13 | ||
|
|
93bc83a5b3 | ||
|
|
a9341e2d82 | ||
|
|
d300a9d536 | ||
|
|
bc0a5e8656 | ||
|
|
ea2b222cff | ||
|
|
7001f04b65 | ||
|
|
77938cd31d | ||
|
|
5ddd122711 | ||
|
|
343e53e17b | ||
|
|
9b215573de | ||
|
|
bf55cef3e3 | ||
|
|
2f88264426 | ||
|
|
6172f4231d | ||
|
|
d7082eb266 | ||
|
|
741c10337b | ||
|
|
e570138da5 | ||
|
|
415effb2e9 | ||
|
|
849a424c8e | ||
|
|
6ceb138f5f | ||
|
|
250f31265b | ||
|
|
9c8f59ca53 | ||
|
|
1002777ef3 | ||
|
|
4c0e42152f | ||
|
|
8f2d513940 | ||
|
|
16ef583455 | ||
|
|
a7f98f8d48 | ||
|
|
1282c3a08d | ||
|
|
160967939b | ||
|
|
112d6b2e65 | ||
|
|
ad4958421f | ||
|
|
c8ed858e4c | ||
|
|
bbaaa062e3 | ||
|
|
9e49038474 | ||
|
|
8a9e2b81b1 | ||
|
|
39ab92871e | ||
|
|
3850f6099b | ||
|
|
58a2fe3046 | ||
|
|
ca1c98336e | ||
|
|
95306c7f60 | ||
|
|
1262cc30e3 | ||
|
|
461fe4b768 | ||
|
|
b937f1f50f | ||
|
|
301e3463e3 | ||
|
|
410ba33d82 | ||
|
|
5fd8aecf55 | ||
|
|
7d8b9e0310 | ||
|
|
8225716b11 | ||
|
|
3cb64eefc2 | ||
|
|
0fe052bc7e | ||
|
|
a03a7ca01e | ||
|
|
d4839f1d81 | ||
|
|
2854b560d6 | ||
|
|
7b026cfe56 | ||
|
|
006a3283e7 | ||
|
|
8c764e9169 | ||
|
|
41b9d6d056 | ||
|
|
dd845f22fe | ||
|
|
36a0a9832d | ||
|
|
feda6517e5 | ||
|
|
8625ae4c35 | ||
|
|
d9addeba5e | ||
|
|
d5a3ba1934 | ||
|
|
7eae56a296 | ||
|
|
527f045e17 | ||
|
|
0ee3fe7330 | ||
|
|
9bfeb2c6a2 | ||
|
|
a3be8f0977 | ||
|
|
a2d0b7af18 | ||
|
|
a4647f368a | ||
|
|
fec760a3c0 | ||
|
|
d5b6a8f66f | ||
|
|
efbc254634 | ||
|
|
04dbac1f4b | ||
|
|
d496914d40 | ||
|
|
2668f4018f | ||
|
|
1c90a4ef5e | ||
|
|
f6641cd266 | ||
|
|
0017c5b9a1 | ||
|
|
6c2b7e91cf | ||
|
|
3e9c33377c | ||
|
|
e54c67c431 | ||
|
|
3e321eaaf3 | ||
|
|
c415f14dd0 | ||
|
|
33177a0c48 | ||
|
|
a875fa8285 | ||
|
|
531571bfa5 | ||
|
|
acf67fef06 | ||
|
|
bfa513cd6d | ||
|
|
0cf6b80396 | ||
|
|
3e316066c3 | ||
|
|
f646c5cd72 | ||
|
|
07867b91f6 |
Executable
+24
@@ -0,0 +1,24 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# smarm pre-commit gate: clippy the library (src/) with warnings as errors.
|
||||||
|
# unwrap_used / expect_used are denied (Cargo.toml [lints.clippy]): library
|
||||||
|
# code must not hide a panic behind unwrap/expect. Tests/examples are not gated.
|
||||||
|
#
|
||||||
|
# Toolchain resolution: prefer an installed cargo-clippy; on machines whose
|
||||||
|
# rust comes without the clippy component (e.g. NixOS home-manager), fall
|
||||||
|
# back to an ephemeral nix-shell toolchain. The fallback uses its own target
|
||||||
|
# dir (target/clippy) because the shell's rustc version may differ from the
|
||||||
|
# default toolchain's — mixed-compiler artifacts in one target dir are an
|
||||||
|
# E0514 hard error. MSRV (Cargo.toml rust-version) keeps the older shell
|
||||||
|
# toolchain a legitimate gate.
|
||||||
|
set -eu
|
||||||
|
[ -f "$HOME/.cargo/env" ] && . "$HOME/.cargo/env"
|
||||||
|
cd "$(git rev-parse --show-toplevel)"
|
||||||
|
if cargo clippy --version >/dev/null 2>&1; then
|
||||||
|
cargo clippy --lib -- -D warnings
|
||||||
|
elif command -v nix-shell >/dev/null 2>&1; then
|
||||||
|
nix-shell -p clippy -p cargo -p rustc \
|
||||||
|
--run 'CARGO_TARGET_DIR=target/clippy cargo clippy --lib -- -D warnings'
|
||||||
|
else
|
||||||
|
echo "pre-commit: cargo clippy unavailable and no nix-shell fallback" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
@@ -4,3 +4,4 @@ smarm_trace.json
|
|||||||
/bench_results/
|
/bench_results/
|
||||||
__pycache__/
|
__pycache__/
|
||||||
*.pyc
|
*.pyc
|
||||||
|
profile.coz
|
||||||
|
|||||||
+49
-2
@@ -1,15 +1,28 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "smarm"
|
name = "smarm"
|
||||||
version = "0.4.0"
|
version = "0.8.1"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
rust-version = "1.95"
|
rust-version = "1.95"
|
||||||
|
|
||||||
[lints.rust]
|
[lints.rust]
|
||||||
unexpected_cfgs = { level = "warn", check-cfg = ["cfg(loom)"] }
|
unexpected_cfgs = { level = "warn", check-cfg = ["cfg(loom)"] }
|
||||||
|
|
||||||
|
[lints.clippy]
|
||||||
|
# Library code must never hide a panic behind unwrap/expect. Both are denied; an
|
||||||
|
# intentional panic is written explicitly as `match { Err(e) => panic!(..) }`.
|
||||||
|
# panic!/unreachable! are deliberately left un-linted as the blessed explicit
|
||||||
|
# form. Enforced on the library target only (`cargo clippy --lib`); tests and
|
||||||
|
# examples unwrap freely and are not gated.
|
||||||
|
unwrap_used = "deny"
|
||||||
|
expect_used = "deny"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["rq-mutex"]
|
default = ["rq-mpmc"]
|
||||||
smarm-trace = []
|
smarm-trace = []
|
||||||
|
# RFC 007: native causal profiling. Zero cost when off (cf. smarm-trace): the
|
||||||
|
# hook in `maybe_preempt` and the resume-path fast-forward compile away; the
|
||||||
|
# two Slot ledger fields exist regardless and stay 0 (budget_cycles precedent).
|
||||||
|
smarm-causal = []
|
||||||
# RFC 016 Chunk 2: cycle-accurate per-actor time-budget accounting. Off by
|
# RFC 016 Chunk 2: cycle-accurate per-actor time-budget accounting. Off by
|
||||||
# default — it costs two extra RDTSC reads per actor resume on the hot path
|
# default — it costs two extra RDTSC reads per actor resume on the hot path
|
||||||
# (D6). The `ActorInfo.budget_cycles` field exists regardless; it just stays 0
|
# (D6). The `ActorInfo.budget_cycles` field exists regardless; it just stays 0
|
||||||
@@ -20,20 +33,34 @@ budget-accounting = []
|
|||||||
# and unflagged; only the optional gen_server transport sits behind this, so a
|
# and unflagged; only the optional gen_server transport sits behind this, so a
|
||||||
# release build pays nothing for an observer it never starts.
|
# release build pays nothing for an observer it never starts.
|
||||||
observer = []
|
observer = []
|
||||||
|
# RFC 010 c1: clustering. Off by default — the default build stays libc-only,
|
||||||
|
# byte-for-byte (gate checked per phase). serde is the payload contract,
|
||||||
|
# postcard the payload codec; both minimal (no default features). Everything
|
||||||
|
# cluster-shaped lives behind this flag.
|
||||||
|
cluster = ["dep:serde", "dep:postcard"]
|
||||||
# Run-queue selection: exactly one, compile-time (see src/run_queue.rs).
|
# Run-queue selection: exactly one, compile-time (see src/run_queue.rs).
|
||||||
# Non-default variants need --no-default-features (features are additive).
|
# Non-default variants need --no-default-features (features are additive).
|
||||||
rq-mutex = []
|
rq-mutex = []
|
||||||
rq-mpmc = []
|
rq-mpmc = []
|
||||||
rq-striped = []
|
rq-striped = []
|
||||||
|
|
||||||
|
[build-dependencies]
|
||||||
|
cc = "1"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
libc = "0.2"
|
libc = "0.2"
|
||||||
|
# RFC 010 §2 — only compiled under `--features cluster`.
|
||||||
|
serde = { version = "1", default-features = false, optional = true }
|
||||||
|
# `alloc` (not `std`): the seam serializes to Vec; postcard stays no_std-aligned.
|
||||||
|
postcard = { version = "1", default-features = false, features = ["alloc"], optional = true }
|
||||||
|
|
||||||
[target.'cfg(loom)'.dependencies]
|
[target.'cfg(loom)'.dependencies]
|
||||||
loom = "0.7"
|
loom = "0.7"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
libc = "0.2"
|
libc = "0.2"
|
||||||
|
# derive + std for cluster envelope tests only; the lib itself never needs them
|
||||||
|
serde = { version = "1", features = ["derive"] }
|
||||||
tokio = { version = "1", features = ["rt", "rt-multi-thread", "macros", "sync", "time"] }
|
tokio = { version = "1", features = ["rt", "rt-multi-thread", "macros", "sync", "time"] }
|
||||||
|
|
||||||
[profile.dev]
|
[profile.dev]
|
||||||
@@ -44,6 +71,14 @@ panic = "unwind"
|
|||||||
lto = "thin"
|
lto = "thin"
|
||||||
codegen-units = 1
|
codegen-units = 1
|
||||||
|
|
||||||
|
# `cargo test --profile reltest`: release codegen for the crate (same opt-level,
|
||||||
|
# same panic strategy) but no LTO at the final link. Thin LTO is what makes each
|
||||||
|
# of the ~40 test binaries cost ~12 s to link instead of ~2 s; the tests don't
|
||||||
|
# need cross-crate LTO, the benches do (they keep using `release`).
|
||||||
|
[profile.reltest]
|
||||||
|
inherits = "release"
|
||||||
|
lto = false
|
||||||
|
|
||||||
[[bench]]
|
[[bench]]
|
||||||
name = "primes"
|
name = "primes"
|
||||||
harness = false
|
harness = false
|
||||||
@@ -80,3 +115,15 @@ harness = false
|
|||||||
[[example]]
|
[[example]]
|
||||||
name = "observer"
|
name = "observer"
|
||||||
required-features = ["observer"]
|
required-features = ["observer"]
|
||||||
|
|
||||||
|
[[example]]
|
||||||
|
name = "causal_pipeline"
|
||||||
|
required-features = ["smarm-causal"]
|
||||||
|
|
||||||
|
[[example]]
|
||||||
|
name = "causal_attrib_probe"
|
||||||
|
required-features = ["smarm-causal"]
|
||||||
|
|
||||||
|
[[example]]
|
||||||
|
name = "causal_probe"
|
||||||
|
required-features = ["smarm-causal"]
|
||||||
|
|||||||
@@ -1,35 +1,38 @@
|
|||||||
# smarm
|
# smarm
|
||||||
|
|
||||||
> SMARM — Smarm, Marks Actor Runtime Machinery. A proof-of-concept green-thread actor runtime for Rust.
|
> SMARM: Smarm, Marks Actor Runtime Machinery. A proof-of-concept green-thread actor runtime for Rust.
|
||||||
|
|
||||||
Implements the core ideas in [`Achitecture.md`](.docs/Architecture.md): green-thread actors on a
|
SMARM is my attempt to implement the erlang/OTP philosophy in the Rust programming language. This has yielded a fault-tolerant, fast, and scalable runtime. This runtime allows the creation of asynchronous applications in Rust without the function coloring associated with the async/await system. It encourages the writing of simple, synchronous code, and largely elides the need for lifetime annotations.
|
||||||
shared heap, scheduled cooperatively, communicating only by `Send` messages.
|
|
||||||
Erlang's isolation model without Erlang's copying GC, Rust's zero-copy
|
|
||||||
ownership transfers without async's function colouring.
|
|
||||||
|
|
||||||
The scheduler is multi-threaded — one OS thread per available CPU, all drawing
|
|
||||||
from a shared run queue. The single-threaded `run()` entry point is kept as a
|
|
||||||
convenience wrapper around `runtime::init(Config::exact(1)).run(f)`.
|
|
||||||
|
|
||||||
## What's here
|
## Overview
|
||||||
|
|
||||||
|
SMARM implements green-thread actors on a shared heap, communicating only by `Send` messages. By sharing the heap, SMARM avoids the copying overhead of Erlang, which is safe to do due to Rust's borrow checker.
|
||||||
|
|
||||||
|
On top of the core runtime mechanics, SMARM also provides a library of primitives for making applications closely inspired by erlang/OTP. This includes generic servers (gen_servers), generic state machines (gen_statem), and supervision trees.
|
||||||
|
|
||||||
|
Supervision trees are the core primitive to allow your application to survive an unexpected panic. Supervisors are processes dedicated to monitoring other processes, which can restart these should they fail. This means that when set up properly an application may 'self-heal' when encountering unforeseen circumstances.
|
||||||
|
|
||||||
|
SMARM is not cooperatively scheduled; it uses preemption. This means a heavy task will not starve out other lighter tasks. Everything will make steady progress, which translates to very beneficial behaviour under (over)load: average latency goes up, but tail latency does not blow up.
|
||||||
|
|
||||||
|
To help diagnose these unforeseen circumstances, smarm may be compiled with its `tracing` feature, which emits a full trace using [Perfetto](https://perfetto.dev/).
|
||||||
|
|
||||||
|
Should you want to optimize your application, SMARM is unusually well poised to help. As the runtime functionally controls time, SMARM comes with a built in causal profiler, under the `causal` feature.
|
||||||
|
|
||||||
|
I also built a Phoenix-Framework inspired HTTP 1.1 library on top of SMARM called [URUS](https://git.kalsbeek.dev/Markk116/urus), which implements Pub/Sub, Channels, and basic amenities like Websockets and Server Sent Events.
|
||||||
|
|
||||||
|
## Limitations
|
||||||
|
|
||||||
|
This runtime requires naked assembly to function, and has thus far only been implemented for x86-64 assembly. It expects an operating system that supports virtual address space, and is therefore not (yet) suited for embedded targets. The IO implementation is currently based around the Linux kernel's `epoll` mechanism, meaning it requires a (GNU+)Linux distribution to run.
|
||||||
|
|
||||||
|
The preemption mechanism works by wrapping the memory allocator and checking how many CPU cycles you have used compared to your timeslice budget. This allows preemption to fire in most normal code, but tight zero-allocation loops do not get caught and require manual insertion of `check!()` if you want preemption to function.
|
||||||
|
|
||||||
|
This library is still in its early stages, and while I try my best with loom and tests, stable operation cannot be guaranteed. Therefore it is not (yet) recommended for production use.
|
||||||
|
|
||||||
|
At this moment, stack memory for each green thread is capped. Uncapping this may lead to performance benefits for deeply recursive algorithms that in a traditional async runtime might require pointer-chases through the heap. This is as yet unrealised.
|
||||||
|
|
||||||
|
At this stage, the codebase is largely LLM-generated, which is obvious if you start to read through the internals. While I did the design, and I keep the LLM under tight rein, the codebase is not in a state that I am very happy with. This also goes for the documentation.
|
||||||
|
|
||||||
| Module | What it does |
|
|
||||||
|--------------|------------------------------------------------------------------------|
|
|
||||||
| `stack` | `mmap`'d growable stack with guard page; SIGSEGV on overflow |
|
|
||||||
| `context` | `#[naked]` x86-64 context-switch shims, callee-saved regs only |
|
|
||||||
| `preempt` | Allocator-driven preemption; `check!()` macro for no-alloc loops |
|
|
||||||
| `pid` | `(index, generation)` PIDs; stale handles are detectable, not silent |
|
|
||||||
| `actor` | Trampoline + `catch_unwind` boundary at the actor entry point |
|
|
||||||
| `scheduler` | Run queue, slot table, spawn/join, parking, idle path |
|
|
||||||
| `channel` | Unbounded MPSC channel; `recv` parks the actor; `recv_timeout` bounds it; `select`/`select_timeout` park on many receivers at once (ready-index, priority order) |
|
|
||||||
| `mutex` | `Mutex<T>` with mandatory timeout; FIFO waiters; parks the green thread |
|
|
||||||
| `timer` | Min-heap of `(deadline, reason)`; `Sleep` and `WaitTimeout` reasons |
|
|
||||||
| `io` | `block_on_io` for blocking work; `wait_readable`/`wait_writable` + `read`/`write` via epoll |
|
|
||||||
| `supervisor` | `Signal::Exit`/`Panic`/`Stopped` funnelled to a parent; `OneForOne`/`OneForAll`/`RestForOne` strategies + restart-intensity cap |
|
|
||||||
| `monitor` | `monitor(pid)` → `Monitor { id, target, rx }`; one-shot `Down` via `rx`; `demonitor(&m)` tears one registration down; unidirectional death notice |
|
|
||||||
| `link` | bidirectional `link`/`unlink`; abnormal death propagates (cooperative stop, or an `ExitSignal` message under `trap_exit`) |
|
|
||||||
| `gen_server` | `call`/`call_timeout` (sync request-reply) / `cast` (async) over one inbox; `handle_info` over static info arms + `handle_down` via `Watcher`-fed monitors, selected ahead of the inbox; `ServerRef`/`ServerBuilder` + `init`/`terminate` hooks; server-down via channel closure |
|
|
||||||
| `registry` | `register`/`whereis`/`name_of`: name ↔ pid bimap; lazy generation-checked cleanup |
|
|
||||||
|
|
||||||
## Quick taste
|
## Quick taste
|
||||||
|
|
||||||
@@ -51,6 +54,29 @@ run(|| {
|
|||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Stopping actors
|
||||||
|
|
||||||
|
Two strengths, as in OTP. `request_stop(pid)` is `exit(Pid, kill)`: a cooperative
|
||||||
|
hard stop, unwinding at the actor's next observation point. `request_shutdown(pid)`
|
||||||
|
is `exit(Pid, shutdown)`: an actor that traps exits (`trap_exit()`, or
|
||||||
|
`ctx.trap_exit()` in a gen_server / `cx.trap_exit()` in a gen_statem) receives it
|
||||||
|
as a signal — `handle_shutdown` / a `shutdown` row — and may drain before stopping
|
||||||
|
itself; one that does not trap is stopped outright. Supervisors trap:
|
||||||
|
`request_shutdown(sup)` tears the tree down top-down, each child per its
|
||||||
|
`ChildSpec` `Shutdown` policy (`Timeout(d)`, `Infinity`, `BrutalKill`). The run's
|
||||||
|
root actor returning means "the program is done": every top-level actor gets a
|
||||||
|
`request_shutdown`, and `run()` returns when they are gone. From outside the
|
||||||
|
runtime (a signal thread), `Runtime::handle().request_shutdown(pid)` does the
|
||||||
|
same. `examples/graceful_shutdown.rs` shows all of it.
|
||||||
|
|
||||||
|
A gen_server or gen_statem lives until it stops, is shut down, or is killed; its
|
||||||
|
refs are addresses — dropping them never ends it (a forgotten one is swept at
|
||||||
|
root exit; with `--features smarm-trace` each such sweep is a `root_sweep`
|
||||||
|
trace line). The supervised shape is `GenServerBuilder::named(N).run()` /
|
||||||
|
`gen_statem::run_named(N, m)`: the server runs inline as the `ChildSpec` child
|
||||||
|
itself, so the supervisor's shutdown reaches it directly, a restart re-binds the
|
||||||
|
name, and the program addresses it by name.
|
||||||
|
|
||||||
## Layout
|
## Layout
|
||||||
|
|
||||||
```
|
```
|
||||||
@@ -67,12 +93,9 @@ benches/
|
|||||||
|
|
||||||
## Building and running
|
## Building and running
|
||||||
|
|
||||||
Standard Cargo. Requires Rust 1.95 or newer (the `#[naked]` attribute went stable
|
Standard Cargo. Requires Rust 1.95 or newer (the `#[naked]` attribute went stable in 1.88; we use a few unrelated post-1.88 features). I have worked hard to keep this library as dependency-free as possible. `master` is x86-64 Linux only. An experimental, **untested** aarch64 context-switch backend lives on the `arm-port` branch (extracted into a `target_arch`-gated `src/arch/`); it has not been validated on hardware yet. macOS remains on the deferred list because of the epoll dependency.
|
||||||
in 1.88; we use a few unrelated post-1.88 features). `master` is x86-64 Linux
|
|
||||||
only. An experimental, **untested** aarch64 context-switch backend lives on the
|
|
||||||
`arm-port` branch (extracted into a `target_arch`-gated `src/arch/`); it has not
|
|
||||||
been validated on hardware yet. macOS remains on the deferred list because of the
|
|
||||||
epoll dependency.
|
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
cargo test # all tests
|
cargo test # all tests
|
||||||
@@ -80,26 +103,29 @@ cargo test --test mutex # one module
|
|||||||
cargo bench # primes benchmark vs tokio
|
cargo bench # primes benchmark vs tokio
|
||||||
```
|
```
|
||||||
|
|
||||||
## What's not here
|
|
||||||
|
|
||||||
See the **Defer** section of `Architecture.md`.
|
|
||||||
`join!` for handle groups, stack growth via remap,
|
|
||||||
hierarchical timer wheel, fd-wait timeouts, `Signal::Timeout`. Each is
|
|
||||||
mechanism we know how to add; none belongs in this iteration.
|
|
||||||
|
|
||||||
## Docs
|
## Docs
|
||||||
|
|
||||||
| Document | What it covers |
|
| Document | What it covers |
|
||||||
|---|---|
|
|---|---|
|
||||||
| [`Architecture.md`](./docs/Architecture.md) | Design intent, runtime model, and deferred work |
|
| [`Architecture.md`](./docs/Architecture.md) | Design intent, runtime model, and deferred work |
|
||||||
| [`smarm - Deep Dive.html`](./docs/smarm%20-%20Deep%20Dive.html) | Generated walkthrough of the system; good starting point |
|
| [`smarm - Deep Dive.html`](./docs/smarm%20-%20Deep%20Dive.html) | Generated walkthrough of the system; good starting point if you want to learn about the internals |
|
||||||
| [`BENCHMARKS_AND_TUNING.md`](./docs/BENCHMARKS_AND_TUNING.md) | Where smarm wins and loses vs tokio, preemption knob recommendations |
|
| [`BENCHMARKS_AND_TUNING.md`](./docs/BENCHMARKS_AND_TUNING.md) | Where smarm wins and loses vs tokio, preemption knob recommendations |
|
||||||
| [`benchmarks.md`](./docs/benchmarks.md) | Raw benchmark results, methodology, and tuning experiment log |
|
| [`benchmarks.md`](./docs/benchmarks.md) | Raw benchmark results, methodology, and tuning experiment log |
|
||||||
|
|
||||||
|
## Coming up
|
||||||
|
|
||||||
|
Clustering: clustering multiple SMARM nodes together is in the pipeline.
|
||||||
|
SMARM-BEAM Interop: Running SMARM as a supervised node under the BEAM via a Rustler NIF works, including message passing and supervision trees that span the runtimes. However, this library is still too unstable to release.
|
||||||
|
SMARM is an interesting platform for implementing a 'dataflow' library, but work on this has not yet started.
|
||||||
|
|
||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
|
||||||
This is a personal proof-of-concept. There's no PR workflow. If you fork it and do something interesting, just send me an email. If it's nice, I'll upstream the changes.
|
This started as a personal proof-of-concept, but it is starting to outgrow that name. If you want to contribute, please get in contact to discuss what you want to work on. Code without prior communication is not welcome.
|
||||||
|
|
||||||
|
|
||||||
|
## A note on open source
|
||||||
|
|
||||||
|
An open source project is a gift, and by giving it, it is no longer mine. I highly enourage you to fork it, to make it your own. This repository, however, is still mine.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+85
-5
@@ -28,6 +28,15 @@ and **excised** (not worth the code cost; preserved on branch
|
|||||||
`rfc-004-spinning`). Also a false-sharing fix (`align(64)` on `SchedulerStats`)
|
`rfc-004-spinning`). Also a false-sharing fix (`align(64)` on `SchedulerStats`)
|
||||||
and a termination wake for idle siblings.
|
and a termination wake for idle siblings.
|
||||||
Commits `2708042`, `37d9319`, `eddf3fe`.
|
Commits `2708042`, `37d9319`, `eddf3fe`.
|
||||||
|
**Default flipped ON 2026-08-18** (history.md findings 17/18): the slot had
|
||||||
|
shipped default-off "until the shootout accepts it" and the flip was never
|
||||||
|
made, so every general.rs number since was slot-off. Acceptance sweep
|
||||||
|
(rq_runtime, 1/2/4/8/20 schedulers, rq-mpmc, 7 runs): ping-pong-pairs
|
||||||
|
−7% at 1T, 3.6×/5.3×/15.6×/10× faster at 2/4/8/20T, 100% slot hits, 0
|
||||||
|
displaced; yield-storm and spawn-storm within ±10% noise both ways.
|
||||||
|
general.rs on the flip: ping_pong_steady 20T 18485→1549 µs, 1T −15%,
|
||||||
|
mpsc_contention 1T −51%, ping_pong_oneshot 20T −18%; no smarm regressions.
|
||||||
|
`benches/baseline.json` regenerated slot-on.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -77,10 +86,10 @@ Delivered surface:
|
|||||||
`ServerBuilder::start` untouched; free `call` / `cast` / `whereis_server`;
|
`ServerBuilder::start` untouched; free `call` / `cast` / `whereis_server`;
|
||||||
`ServerRef::shutdown` + free `shutdown` as the sys-style synchronous stop.
|
`ServerRef::shutdown` + free `shutdown` as the sys-style synchronous stop.
|
||||||
- **Root-exit teardown** (final phase): the run's initial actor is the root;
|
- **Root-exit teardown** (final phase): the run's initial actor is the root;
|
||||||
when it exits, the scheduler's idle verdict stops the parked-forever remainder
|
when it exits the run winds down. *(Reworked with the graceful-shutdown work:
|
||||||
(deferred past the queue drain, so actors with in-flight work finish rather
|
root exit now delivers `request_shutdown` to every forest root — see
|
||||||
than unwinding on the stop). Closes the "app actor blocks AllDone" stall — see
|
"Root exit" below and `tests/root_exit.rs`.)* Closes the "app actor blocks
|
||||||
Look into, below.
|
AllDone" stall — see Look into, below.
|
||||||
|
|
||||||
Extends — does not retire — the "select exists; a unified per-process mailbox
|
Extends — does not retire — the "select exists; a unified per-process mailbox
|
||||||
still does not" invariant: 014 adds addressable *delivery*, not a unified inbox;
|
still does not" invariant: 014 adds addressable *delivery*, not a unified inbox;
|
||||||
@@ -146,7 +155,11 @@ Needs an RFC.
|
|||||||
#### Per-switch cost (context shims, epoch protocol)
|
#### Per-switch cost (context shims, epoch protocol)
|
||||||
The shootout's residual: per-wake latency is 0.16–0.18 µs at N=1 and
|
The shootout's residual: per-wake latency is 0.16–0.18 µs at N=1 and
|
||||||
0.8–1.2 µs at N=8+, dominated by the context-switch shims and the epoch
|
0.8–1.2 µs at N=8+, dominated by the context-switch shims and the epoch
|
||||||
protocol, not the queue. On current evidence this is the larger constant —
|
protocol, not the queue. **Premise corrected 2026-08-18 (finding 17): the
|
||||||
|
N=8+ figure was the slot-off futex path (one futex_wake per landed wake,
|
||||||
|
woken worker steals the pair); slot-on it is ~1.3× N=1. The shims are ~5
|
||||||
|
cycles (finding 3) and the epoch CASes ~117 cycles/roundtrip (finding 15).
|
||||||
|
Re-measure slot-on before spending anything here.** On current evidence this is the larger constant —
|
||||||
"the whole game" alongside the v0.9 work — but there is no spec yet. Needs a
|
"the whole game" alongside the v0.9 work — but there is no spec yet. Needs a
|
||||||
profiling spike (where do the cycles actually go per park/unpark round-trip)
|
profiling spike (where do the cycles actually go per park/unpark round-trip)
|
||||||
and then an RFC before it can be scheduled.
|
and then an RFC before it can be scheduled.
|
||||||
@@ -202,6 +215,15 @@ turns idle into periodic wakeups), or (b) park the branch on a condvar/futex
|
|||||||
the AllDone path signals — and at that point consider making the condvar the
|
the AllDone path signals — and at that point consider making the condvar the
|
||||||
idle primitive for the no-io runtime generally (a cross-thread unpark could
|
idle primitive for the no-io runtime generally (a cross-thread unpark could
|
||||||
signal it too, see below). Decide before any no-io deployment.
|
signal it too, see below). Decide before any no-io deployment.
|
||||||
|
#### Wake delayed to timeout: `recv_timeout`, `lock_timeout`
|
||||||
|
Same register-then-`insert_wait_timer`-then-park shape as the pre-fix `select`
|
||||||
|
paths, without `NoPreempt`. The wake isn't lost outright (the timer still
|
||||||
|
fires) but a send or grant landing in the window waits out the whole timeout;
|
||||||
|
for `Mutex::lock_timeout` that leaves the lock held-but-unused and stalls
|
||||||
|
every other waiter too. `insert_wait_timer` also allocates under the `timers`
|
||||||
|
mutex. Fix per site is the `select` one; to close the class, either give
|
||||||
|
`begin_wait` a guard-returning form, or let `unpark` on a Queued word with a
|
||||||
|
matching epoch record the notification so the next park returns at once.
|
||||||
#### Cross-thread unpark
|
#### Cross-thread unpark
|
||||||
`RuntimeInner::enqueue` does not wake idle sibling schedulers — only io
|
`RuntimeInner::enqueue` does not wake idle sibling schedulers — only io
|
||||||
completions write the wake pipe. Mid-flight this is masked (the enqueuing
|
completions write the wake pipe. Mid-flight this is masked (the enqueuing
|
||||||
@@ -260,8 +282,58 @@ path the atomic-bool workaround stood in for. Re-check the urus crud repro to
|
|||||||
confirm the workaround can be retired (the teardown is cooperative — an actor in
|
confirm the workaround can be retired (the teardown is cooperative — an actor in
|
||||||
a tight loop with no observation point still can't be stopped).
|
a tight loop with no observation point still can't be stopped).
|
||||||
|
|
||||||
|
**Update (graceful shutdown):** the RFC 014 sweep was a hard `request_stop` of
|
||||||
|
every live slot, deferred until nothing was runnable — which killed a sleeping
|
||||||
|
actor (timer pending) but drained a queued one, for no principled reason. It is
|
||||||
|
now the OTP semantics: root exit = "the program is done" = `request_shutdown`
|
||||||
|
to every **forest root** (live actor whose parent is the run or is dead), run
|
||||||
|
synchronously on the root's finalize path. Supervisors cascade with their child
|
||||||
|
`Shutdown` policies; trapping actors may `Continue`/drain (timers keep working)
|
||||||
|
and end the run when they stop themselves; non-trapping actors are stopped
|
||||||
|
outright — `join` what you need finished. No forcing sweep follows.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
### Open items from the graceful-shutdown work (not scheduled)
|
||||||
|
- ~~gen_server / gen_statem as a direct supervised child.~~ Done: lifetime is
|
||||||
|
the actor's (refs are addresses, the loop holds an inbox sender);
|
||||||
|
`NamedGenServerBuilder::run` / `gen_statem::run_named` run the loop inline as
|
||||||
|
the `ChildSpec` child; the root-exit sweep traces each leftover as
|
||||||
|
`root_sweep` under `smarm-trace`.
|
||||||
|
- Supervisor `Live` drop-guard sweep is `request_stop` (kill propagates as
|
||||||
|
kill); OTP would deliver a trappable `killed`. Chosen for boundedness.
|
||||||
|
- A root-exit shutdown reaches only actors live *at that instant*; a
|
||||||
|
non-trapping forest root that spawns before it unwinds leaves that spawn
|
||||||
|
to itself (Erlang: an unlinked spawn is nobody's child). The same holds
|
||||||
|
for children it already had: they sit under a live parent at sweep time,
|
||||||
|
so they are skipped, and stopping the parent does not reach them. A child
|
||||||
|
that loops until told otherwise then keeps the run alive (seen as a hang in
|
||||||
|
`try_spawn`'s race test whenever its root panicked mid-race). Closing it
|
||||||
|
means sweeping a forest root's live children when it finalizes after root
|
||||||
|
exit; that changes teardown semantics, so it is not done yet.
|
||||||
|
- **Supervisor start *order* is not start *readiness*.** `start_child`
|
||||||
|
spawns and moves straight on, so an earlier child is merely *scheduled*,
|
||||||
|
not initialised, when a later sibling starts. A later child that resolves
|
||||||
|
an earlier one by name (`whereis_server`) can therefore miss it — the
|
||||||
|
classic "named registry sibling, then its consumers" tree. Ordered
|
||||||
|
`OneForOne`/`RestForOne` shutdown is unaffected (reverse order is honoured
|
||||||
|
and each stop *is* awaited); this is a start-side gap only.
|
||||||
|
Making `spawn` itself block does NOT fix it — it would only shrink the
|
||||||
|
window to "child has begun executing", while the property callers need is
|
||||||
|
"child has bound its name / opened its socket", which only the child can
|
||||||
|
declare. It would also tax the hot path (one round-trip per accepted
|
||||||
|
connection) and turn every spawn into a context-switch point. OTP has the
|
||||||
|
same async `spawn` and puts the synchronisation one level up:
|
||||||
|
`gen_server:start_link` blocks the caller until `init/1` returns.
|
||||||
|
Fix shape when scheduled: a readiness ack in the supervisor's child-start
|
||||||
|
path (`ChildSpec` variant whose factory receives a ready-signal;
|
||||||
|
`NamedGenServerBuilder::run` acks after its name bind, gen_server default
|
||||||
|
acks after `init`; plain closures ack at spawn as today, i.e. opt-in with
|
||||||
|
no cost to existing children). Until then the workaround is structural:
|
||||||
|
have the registrar spawn its own consumers so the ordering is program
|
||||||
|
order inside one actor, not a cross-actor guarantee (urus v0.3 endpoint
|
||||||
|
does exactly this).
|
||||||
|
|
||||||
## Invariants & gotchas (respect these across all cycles)
|
## Invariants & gotchas (respect these across all cycles)
|
||||||
|
|
||||||
- **Shared mutex is non-reentrant.** `Sender::send` can call `unpark` →
|
- **Shared mutex is non-reentrant.** `Sender::send` can call `unpark` →
|
||||||
@@ -285,6 +357,14 @@ a tight loop with no observation point still can't be stopped).
|
|||||||
waker, decide which form it is — if its registration handle can outlive
|
waker, decide which form it is — if its registration handle can outlive
|
||||||
the wait it was created for, it MUST be epoch-stamped; a wait that can
|
the wait it was created for, it MUST be epoch-stamped; a wait that can
|
||||||
exit without parking MUST `retire_wait` first (see slot_state.rs).
|
exit without parking MUST `retire_wait` first (see slot_state.rs).
|
||||||
|
- **Register-to-park is a `NoPreempt` region.** From `begin_wait` through
|
||||||
|
`park_current`, hold `NoPreempt` (as `wait_fd`, `sleep`, `block_on_io` and
|
||||||
|
both `select` paths do). `RunningNotified` absorbs a wake that lands while
|
||||||
|
the waiter is still *Running*; a waiter preempted in the window is *Queued*,
|
||||||
|
and `unpark` on a Queued word is a no-op. A one-shot registration (an fd
|
||||||
|
arm) consumed there is a lost wake: the waiter parks with nothing left to
|
||||||
|
wake it but its timer, if it has one. Allocation is a preemption point, so
|
||||||
|
"nothing in between can yield" is not a safe assumption without the guard.
|
||||||
- **`select` exists; a unified per-process mailbox still does not.** The
|
- **`select` exists; a unified per-process mailbox still does not.** The
|
||||||
supervisor keeps its single `supervisor_channel` funnel; `recv_match`
|
supervisor keeps its single `supervisor_channel` funnel; `recv_match`
|
||||||
stays per-channel. `select` composes channels at the wait, not into one
|
stays per-channel. `select` composes channels at the wait, not into one
|
||||||
|
|||||||
+229
@@ -0,0 +1,229 @@
|
|||||||
|
# urus / smarm handoff — updated 2026-08-19 (session 3)
|
||||||
|
|
||||||
|
## TL;DR for the next session
|
||||||
|
**smarm is done for now** (5 unpushed commits on local `master`, see below).
|
||||||
|
**Next = urus v0.3 endpoint refactor.** You should NOT need to read smarm
|
||||||
|
scheduler internals; the contract you build on is fully described here and in
|
||||||
|
`smarm_full/examples/graceful_shutdown.rs` (read that file first — it is the
|
||||||
|
exact shape urus's tree will take) plus `smarm_full/tests/root_exit.rs`.
|
||||||
|
|
||||||
|
### The smarm contract urus builds on (all on local master, verified by tests)
|
||||||
|
- `request_stop(pid)` = kill (cooperative hard stop). `request_shutdown(pid)` =
|
||||||
|
polite: trapping target gets `ExitSignal{reason: Shutdown}`, non-trapping is
|
||||||
|
stopped outright. `RuntimeHandle::{request_stop,request_shutdown}` do the same
|
||||||
|
from any OS thread (signal handler); grab `rt.handle()` before `rt.run`.
|
||||||
|
- Supervisor traps; `request_shutdown(sup)` = ordered reverse-start shutdown,
|
||||||
|
per-child `ChildSpec::shutdown(Shutdown::{Timeout(d)|Infinity|BrutalKill})`
|
||||||
|
(default Timeout(5s)); sup then returns normally. `request_stop(sup)`
|
||||||
|
hard-stops children too (no orphans).
|
||||||
|
- gen_server: `ctx.trap_exit()` in init; `handle_shutdown() -> Exit|Continue`;
|
||||||
|
`handle_exit(sig)`; `ctx.stop_handle().stop()` = normal self-exit;
|
||||||
|
`terminate()` may block only on the graceful path (Exit / stop / inbox close).
|
||||||
|
`GenServerRef::shutdown()` is graceful and waits.
|
||||||
|
- gen_statem: same in event clothes — `cx.trap_exit()` in initial enter,
|
||||||
|
`shutdown` rows (default `stop`), `exit sig` rows, `cx.stop()` / `stop` tail,
|
||||||
|
optional `terminate { }` block. `GenStatemRef::shutdown()`.
|
||||||
|
- **Root exit = program done**: when the root actor returns, the runtime
|
||||||
|
`request_shutdown`s every *forest root* (live actor whose parent is the run
|
||||||
|
or dead). Supervisors cascade; trapping actors may drain (timers keep
|
||||||
|
working) and end the run when they stop; non-trapping are stopped; **no
|
||||||
|
forcing sweep** (`join` what must finish). The old "wait until nothing
|
||||||
|
runnable then kill all" deferral is gone.
|
||||||
|
- **Gotcha for urus:** a gen_server's lifetime is governed by its refs — drop
|
||||||
|
the last `GenServerRef` and the inbox closes → clean exit *even mid-drain*.
|
||||||
|
The endpoint must be pinned (named, or its ref held by the supervisor
|
||||||
|
wrapper) or it will terminate the moment the root drops its ref.
|
||||||
|
- **Known gap (ROADMAP open item):** a gen_server can't be a direct `ChildSpec`
|
||||||
|
child; use the trapping wrapper pattern in `examples/graceful_shutdown.rs::
|
||||||
|
drainer_child` (starts `under(self_pid())`, forwards shutdown, waits). Doing
|
||||||
|
an inline `GenServerBuilder::run()` first may be worth a short smarm detour —
|
||||||
|
decide with Markk.
|
||||||
|
|
||||||
|
### smarm commits this session (local master, NOT pushed, NOT tagged)
|
||||||
|
`250f312` root-exit = graceful shutdown of forest roots (tests/root_exit.rs)
|
||||||
|
`6ceb138` gen_statem shutdown parity (tests/gen_statem_shutdown.rs)
|
||||||
|
`849a424` docs + examples/graceful_shutdown.rs + README "Stopping actors"
|
||||||
|
On top of `1002777` (cross-thread wake) and `9c8f59c` (graceful shutdown).
|
||||||
|
Cargo.toml still `0.6.1`. Release cut (push, tag — v0.7 is justified by the
|
||||||
|
API surface — version bump) is Markk's. Full suite, doc tests, examples,
|
||||||
|
`cargo fmt`, `cargo clippy --lib` all clean. (`clippy --tests` has pre-existing
|
||||||
|
unwrap lints in tests/fd_select.rs, untouched.)
|
||||||
|
|
||||||
|
### Decisions taken this session (Markk)
|
||||||
|
- Root exit means "program done" (Go/tokio/OTP), not "wait for pending work";
|
||||||
|
the previously agreed "sleep(50ms) must finish" test was dropped as encoding
|
||||||
|
the wrong contract (a timer-wheel gate would re-wedge periodic-timer daemons).
|
||||||
|
- No behaviour-preserving deferral, no forcing second sweep.
|
||||||
|
- Examples/docs done in the same session; urus next session.
|
||||||
|
|
||||||
|
---
|
||||||
|
# Previous handoff (still accurate where not superseded above)
|
||||||
|
|
||||||
|
|
||||||
|
## Next-session goal
|
||||||
|
Phase 1 is **done and committed**; Phase 2 is next:
|
||||||
|
1. **smarm v0.6.2** — cross-thread wake root fix. **DONE**, committed on `master`
|
||||||
|
as `1002777`. Not yet tagged, not yet version-bumped (Cargo.toml still reads
|
||||||
|
`0.6.1`), and **not yet pushed to origin** — it exists only in the delivered
|
||||||
|
snapshot zip and the local sandbox clone. Cutting the release (push + tag
|
||||||
|
`v0.6.2` + bump `0.6.1`→`0.6.2`) is Markk's step.
|
||||||
|
2. **urus v0.3** — endpoint refactor. Working against `smarm = { path = "../smarm_full" }`
|
||||||
|
with `git update-index --skip-worktree Cargo.toml` (Markk approved); release commit
|
||||||
|
swaps back to the tag once Markk cuts it. Phase 2 plan below is STALE where it says
|
||||||
|
drain-in-terminate; the endpoint is a trapping GenServer: `handle_shutdown` →
|
||||||
|
`Continue`, enter Draining, `StopHandle::stop()` when the conn set empties.
|
||||||
|
|
||||||
|
Decisions below are locked unless marked *(confirm)*.
|
||||||
|
|
||||||
|
## Reconstruction (the sandbox resets between sessions)
|
||||||
|
A fresh sandbox has an empty home and **no Rust toolchain**. To restore:
|
||||||
|
- Install rustup/cargo. smarm reformats under **rustc 1.97.1**; urus `rust-version`
|
||||||
|
is 1.95. Use 1.97.1.
|
||||||
|
- urus: `git clone https://git.kalsbeek.dev/Markk116/urus` — `origin` is registered
|
||||||
|
and public-read. master `8bdec97` = the v0.2.x line. (Zips in outputs are stale;
|
||||||
|
prefer the remote now.)
|
||||||
|
- smarm: `git clone https://git.kalsbeek.dev/Markk116/smarm`. Latest tag **v0.6.1**
|
||||||
|
(`ca1c983`). The cross-thread wake fix is committed as `1002777` on top of the
|
||||||
|
post-v0.6.1 README commit `8f2d513` (= origin/master). **It is NOT on origin
|
||||||
|
yet** — a fresh clone won't have it until Markk pushes. Restore it from the
|
||||||
|
snapshot zip if working before the push. **v0.6.2 is not yet tagged.**
|
||||||
|
- urus pins smarm by git **tag** in `Cargo.toml` (currently `v0.6.0`). A trivial
|
||||||
|
first commit bumps it to `v0.6.1` (also picks up `try_spawn` + monitor
|
||||||
|
terminal-outcome fixes).
|
||||||
|
|
||||||
|
## Why (context — the finding that drives the plan)
|
||||||
|
urus's shutdown machinery (the `AtomicBool` listener flag + the `SHUTDOWN_POLL`
|
||||||
|
loop in `serve.rs`) is scaffolding around two smarm properties. Their statuses
|
||||||
|
differ, which is the whole point:
|
||||||
|
|
||||||
|
- **Issue A — lossy stop vs a QUEUED actor: ALREADY FIXED in smarm.** Commit
|
||||||
|
`7bab4d2` added an entry-side `check_cancelled()` in `park_current`. A
|
||||||
|
`request_stop` against a listener parked in `wait_readable_timeout` now unwinds
|
||||||
|
cleanly (it parks via `try_select_timeout → park_current`). urus's flag + its
|
||||||
|
stale "smarm's lossy stop-while-QUEUED window" comment can be deleted.
|
||||||
|
- **Issue B — foreign-thread wake is a no-op: FIXED in `1002777` (was present
|
||||||
|
through v0.6.1).** The gap: `unpark`/`unpark_at`/`request_stop` all route through
|
||||||
|
`try_with_runtime`, which reads a thread-local that is `None` on any non-scheduler
|
||||||
|
thread, so no cross-thread wake worked — a signal handler / OS thread could not
|
||||||
|
wake *or* stop a parked actor, which is why `serve.rs` polls the shutdown signal
|
||||||
|
instead of parking on it. Now closed (see Phase 1 below): urus's `SHUTDOWN_POLL`
|
||||||
|
loop can be deleted and its `Handle::shutdown` can park on a handle-driven stop.
|
||||||
|
|
||||||
|
## Phase 1 — smarm cross-thread wake (root fix) — DONE (`1002777`)
|
||||||
|
Shipped as one commit generalizing RFC 018 (a producer reaches the runtime through
|
||||||
|
a `Weak` it holds) from the IO backend to channel senders and a new handle:
|
||||||
|
- **`Runtime::handle() -> RuntimeHandle`** (`Send + Sync`), holding a
|
||||||
|
`Weak<RuntimeInner>`. Grab it before `rt.run` and hand it to the signal thread.
|
||||||
|
- **`RuntimeHandle::request_stop<A>(Pid<A>)`** — upgrades the Weak and calls
|
||||||
|
`request_stop_inner` on the inner; no-op if the runtime is gone. This is the
|
||||||
|
signal-handler-drives-shutdown path; it cascades the ordered stop down the tree
|
||||||
|
exactly like an in-runtime `request_stop`.
|
||||||
|
- **Send-wake:** the receiver captures `scheduler::runtime_weak()` into its
|
||||||
|
`parked_receiver` tuple **at park time** (not at channel creation — the resolved
|
||||||
|
sub-decision; a parked receiver is a live actor so the Weak is provably upgradable,
|
||||||
|
and it scopes the capture to when a wake is possible). `send()` and last-sender
|
||||||
|
`drop` wake via `scheduler::unpark_at_via(pid, epoch, &weak)`: thread-local path
|
||||||
|
when on a scheduler thread (preempt-gated, slot-eligible), captured Weak otherwise.
|
||||||
|
In-runtime timer wakes (recv/select) were left on `scheduler::unpark_at`.
|
||||||
|
|
||||||
|
**API scope decision (signed off):** `RuntimeHandle` exposes **`request_stop` only**.
|
||||||
|
No public `unpark`/`unpark_at` on the handle — send-wake needs no user-facing handle,
|
||||||
|
and "unpark off-runtime" is covered because `request_stop` drives `unpark` on the
|
||||||
|
upgraded inner. No `is_alive()`. Both are one-line additions if a consumer appears.
|
||||||
|
|
||||||
|
**No RFC written** — pattern was already established (RFC 018), agreed not needed.
|
||||||
|
|
||||||
|
Tests: `tests/cross_thread_wake.rs` (foreign-thread send wakes a parked receiver;
|
||||||
|
foreign-thread `request_stop` wakes+stops a parked actor; a lingering handle never
|
||||||
|
blocks all-done and degrades to a no-op once the runtime drops). Full suite green;
|
||||||
|
`cargo fmt` + `cargo clippy --lib` clean.
|
||||||
|
|
||||||
|
**Remaining release step (Markk):** push `master`, tag `v0.6.2`, bump Cargo.toml
|
||||||
|
`0.6.1`→`0.6.2`. Left paired with the tag as the release cut, not done in `1002777`.
|
||||||
|
|
||||||
|
## Phase 1b — smarm graceful shutdown (OTP lift) — DONE (`9c8f59c`, on top of `1002777`)
|
||||||
|
Decided this session (Markk): B — fix at the smarm level rather than a two-stop
|
||||||
|
split in urus. No RFC (Markk: "just implement it"). Shipped, tested, committed on
|
||||||
|
the local `master`, **not pushed, not tagged**. It should ship as the same
|
||||||
|
release as 1002777 (v0.6.2, or v0.7 given the API surface — Markk's call).
|
||||||
|
- `request_shutdown(pid)` / `RuntimeHandle::request_shutdown` = `exit(Pid, shutdown)`;
|
||||||
|
`request_stop` = `exit(Pid, kill)`. Trapping target gets `ExitSignal{reason:
|
||||||
|
DownReason::Shutdown}`; non-trapping is stopped outright.
|
||||||
|
- `ChildSpec::shutdown(Shutdown::{BrutalKill, Timeout(d), Infinity})`, default 5s.
|
||||||
|
Supervisor traps exits; `request_shutdown(sup)` = ordered top-down shutdown,
|
||||||
|
returns normally. **Also fixed**: `request_stop(sup)` used to ORPHAN children
|
||||||
|
(probe-verified; the handoff's "cascade" claim was wrong) — `Live` drop guard now
|
||||||
|
hard-stops them.
|
||||||
|
- gen_server: `ctx.trap_exit()`, `handle_shutdown() -> ShutdownAction::{Exit,
|
||||||
|
Continue}`, `handle_exit(ExitSignal)`, `ctx.stop_handle().stop()` = normal
|
||||||
|
self-exit (`{stop, normal}`; previously impossible — only abnormal `Stopped`).
|
||||||
|
`GenServerRef::shutdown()` is graceful now.
|
||||||
|
- Root finding that forced this: gen_server `terminate()` runs from a Drop guard,
|
||||||
|
mid-unwind on the stop path; any park in it = double panic = abort. So
|
||||||
|
"drain-in-terminate()" (the old Phase 2 plan) was never viable.
|
||||||
|
|
||||||
|
### ~~Next-session smarm work~~ DONE this session (see TL;DR)
|
||||||
|
1. **Root-exit sweep**: make `Pop::RootDrain` also require an empty timer wheel
|
||||||
|
(and it already requires nothing runnable; io_out is only checked for AllDone —
|
||||||
|
check whether it should gate RootDrain too). TDD: an actor in `sleep(50ms)` when
|
||||||
|
the root returns must finish, not be swept. Then a `Reservoir`-style test that a
|
||||||
|
*truly* parked-forever daemon still gets swept.
|
||||||
|
2. **gen_statem parity**: `ctx.trap_exit()`, `handle_shutdown -> ShutdownAction`,
|
||||||
|
`handle_exit`, stop handle. Mirror gen_server; mechanical.
|
||||||
|
3. **Examples review**: `examples/*.rs` predate all of this. Rework where they show
|
||||||
|
shutdown/teardown to use `request_shutdown`, `Shutdown` policies, and
|
||||||
|
`StopHandle`; `named_genserver.rs` first (uses `shutdown`). Also
|
||||||
|
`docs/smarm - Deep Dive.html` says terminate() must be non-blocking — now only
|
||||||
|
true on the unwind paths; and README could use a "Stopping actors" paragraph
|
||||||
|
(request_stop = kill, request_shutdown = shutdown, Shutdown policy).
|
||||||
|
|
||||||
|
### Open smarm items found on the way (noted, not scheduled)
|
||||||
|
- (root-exit sweep and gen_statem parity moved up to the scheduled list.)
|
||||||
|
- Sweep in the supervisor `Live` drop guard is `request_stop` (kill propagates as
|
||||||
|
kill); OTP would deliver a trappable `killed`. Chosen for boundedness.
|
||||||
|
|
||||||
|
## Phase 2 — urus v0.3: endpoint refactor (after v0.6.2 is tagged)
|
||||||
|
Target = the spec's original shape (`urus-spec.md` §2.1/§6: `listener_sup` under the
|
||||||
|
**user's** root supervisor). Deviation to unwind: `serve` owning `rt.run`.
|
||||||
|
- App owns the runtime: `smarm::init(cfg).run(|| root_sup.run())`, root e.g.
|
||||||
|
`RestForOne[ app actors…, urus::endpoint(config, pipeline) ]`. This is what kills
|
||||||
|
the `Arc<OnceLock>` idiom for the right reason (app state born in-runtime as a
|
||||||
|
supervised, ordered child).
|
||||||
|
- `urus::endpoint` = one GenServer child owning the registry + an **internal**
|
||||||
|
listener sub-supervisor + drain-in-`terminate()`. Listeners stay internal, not
|
||||||
|
app-visible peers.
|
||||||
|
- Shutdown = `request_stop` the root supervisor (or via the runtime handle from a
|
||||||
|
signal thread) → cascades down → `endpoint.terminate()` runs the drain
|
||||||
|
(`drain_timeout`, force-stop sweep).
|
||||||
|
- **DELETE:** the `AtomicBool` listener flag (A fixed) and the `SHUTDOWN_POLL` loop
|
||||||
|
+ its apologetic comment (B fixed → park, don't poll).
|
||||||
|
- Nuance: `request_stop` → `Signal::Stopped` is *abnormal* → `Transient` restarts.
|
||||||
|
Stop-without-restart = stop the **supervisor**, not the children.
|
||||||
|
- *(confirm)* Keep `serve`/`serve_with`/`serve_with_shutdown` as thin wrappers that
|
||||||
|
build the one-child tree internally, so the simple case stays one line.
|
||||||
|
- *(confirm)* Keep `Handle`/`ShutdownSignal`? Now that cross-thread wake works,
|
||||||
|
`Handle::shutdown` can map to handle-driven `request_stop` on the endpoint.
|
||||||
|
- Breaking → cut **urus v0.3**; bump the smarm pin to `v0.6.2` here.
|
||||||
|
|
||||||
|
## Working norms
|
||||||
|
- Every bash call: `export PATH=$HOME/.cargo/bin:$PATH` (once the toolchain's in).
|
||||||
|
- **TDD**: failing test first, then implement; keep suites green.
|
||||||
|
- **Hammer ritual** for ANY connection-lifecycle change (the urus #2 shutdown work
|
||||||
|
qualifies): 35× subset (`shutdown timeout reaped slowloris streaming chunked sse
|
||||||
|
stalled ws_ channels session`) + 3× full + 1× trace. `scripts/hammer.sh` does NOT
|
||||||
|
pass feature flags — loop manually with `--features phoenix`. Subset filter must
|
||||||
|
NOT use `--test integration` (session tests live in lib).
|
||||||
|
- Example smoke tests: hold the server's stdin open (`mkfifo` + `sleep > fifo`) or
|
||||||
|
the Enter-to-shutdown thread fires on EOF instantly.
|
||||||
|
- Background procs are reaped BETWEEN bash calls; `pkill -f` matches your own shell.
|
||||||
|
- Artefact store (specs): `curl -H "Authorization: Bearer sk-llmingest-2e45d80c63db24c6781e761eb2a9a58e83d9f48ef77a42185bad311d07c80e68" https://artefacts.kalsbeek.dev/artifacts/<name>`
|
||||||
|
— `urus-spec.md`, `urus-bench-spec.md`, `rfc_008-implementation-notes.md`, …
|
||||||
|
- smarm feature flags: `smarm-trace`, `smarm-causal` (urus re-exports both).
|
||||||
|
|
||||||
|
## Local cross-repo testing — KEEP OUT OF COMMITS
|
||||||
|
To test urus #2 against un-tagged smarm 0.6.2, point urus's `Cargo.toml` smarm dep
|
||||||
|
at a local path (`smarm = { path = "../smarm" }`) instead of the git tag.
|
||||||
|
- Must NOT land in commits. Guard: `git update-index --skip-worktree Cargo.toml`
|
||||||
|
after editing (undo with `--no-skip-worktree`), or stash before committing.
|
||||||
|
- The committed `Cargo.toml` stays pinned to the git tag; restore the tag (bumped to
|
||||||
|
`v0.6.2`) for the release commit.
|
||||||
+317
-265
@@ -1,314 +1,366 @@
|
|||||||
{
|
{
|
||||||
|
"catch_unwind_panics": {
|
||||||
|
"smarm 1-thread": {
|
||||||
|
"result": 10000,
|
||||||
|
"median": 122512,
|
||||||
|
"min": 113735,
|
||||||
|
"max": 124564
|
||||||
|
},
|
||||||
|
"smarm 20-thread": {
|
||||||
|
"result": 10000,
|
||||||
|
"median": 20569,
|
||||||
|
"min": 19671,
|
||||||
|
"max": 21174
|
||||||
|
},
|
||||||
|
"tokio current_thread": {
|
||||||
|
"result": 10000,
|
||||||
|
"median": 10134,
|
||||||
|
"min": 9081,
|
||||||
|
"max": 10524
|
||||||
|
},
|
||||||
|
"tokio multi-thread": {
|
||||||
|
"result": 10000,
|
||||||
|
"median": 2461,
|
||||||
|
"min": 2351,
|
||||||
|
"max": 2715
|
||||||
|
}
|
||||||
|
},
|
||||||
"chained_spawn": {
|
"chained_spawn": {
|
||||||
"smarm 1-thread": {
|
"smarm 1-thread": {
|
||||||
"result": 1000,
|
"result": 1000,
|
||||||
"median": 413,
|
"median": 498,
|
||||||
"min": 410,
|
"min": 491,
|
||||||
"max": 439
|
"max": 503
|
||||||
},
|
},
|
||||||
"smarm 24-thread": {
|
"smarm 20-thread": {
|
||||||
"result": 1000,
|
"result": 1000,
|
||||||
"median": 909,
|
"median": 2135,
|
||||||
"min": 888,
|
"min": 1881,
|
||||||
"max": 951
|
"max": 2284
|
||||||
},
|
},
|
||||||
"tokio current_thread": {
|
"tokio current_thread": {
|
||||||
"result": 1000,
|
"result": 1000,
|
||||||
"median": 62,
|
"median": 114,
|
||||||
"min": 61,
|
"min": 106,
|
||||||
"max": 62
|
"max": 119
|
||||||
},
|
},
|
||||||
"tokio multi-thread": {
|
"tokio multi-thread": {
|
||||||
"result": 1000,
|
"result": 1000,
|
||||||
"median": 197,
|
"median": 164,
|
||||||
"min": 194,
|
"min": 160,
|
||||||
"max": 210
|
"max": 186
|
||||||
}
|
|
||||||
},
|
|
||||||
"yield_many": {
|
|
||||||
"smarm 1-thread": {
|
|
||||||
"result": 200000,
|
|
||||||
"median": 16475,
|
|
||||||
"min": 16393,
|
|
||||||
"max": 16732
|
|
||||||
},
|
|
||||||
"smarm 24-thread": {
|
|
||||||
"result": 200000,
|
|
||||||
"median": 148708,
|
|
||||||
"min": 111213,
|
|
||||||
"max": 156462
|
|
||||||
},
|
|
||||||
"tokio current_thread": {
|
|
||||||
"result": 200000,
|
|
||||||
"median": 4751,
|
|
||||||
"min": 4740,
|
|
||||||
"max": 5259
|
|
||||||
},
|
|
||||||
"tokio multi-thread": {
|
|
||||||
"result": 200000,
|
|
||||||
"median": 8320,
|
|
||||||
"min": 7862,
|
|
||||||
"max": 8882
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"fan_out_compute": {
|
|
||||||
"smarm 1-thread": {
|
|
||||||
"result": 33860,
|
|
||||||
"median": 13453,
|
|
||||||
"min": 13305,
|
|
||||||
"max": 15077
|
|
||||||
},
|
|
||||||
"smarm 24-thread": {
|
|
||||||
"result": 33860,
|
|
||||||
"median": 2451,
|
|
||||||
"min": 2330,
|
|
||||||
"max": 2520
|
|
||||||
},
|
|
||||||
"tokio current_thread": {
|
|
||||||
"result": 33860,
|
|
||||||
"median": 14019,
|
|
||||||
"min": 12339,
|
|
||||||
"max": 14045
|
|
||||||
},
|
|
||||||
"tokio multi-thread": {
|
|
||||||
"result": 33860,
|
|
||||||
"median": 1500,
|
|
||||||
"min": 1426,
|
|
||||||
"max": 1600
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"ping_pong_oneshot": {
|
|
||||||
"smarm 1-thread": {
|
|
||||||
"result": 1000,
|
|
||||||
"median": 898,
|
|
||||||
"min": 782,
|
|
||||||
"max": 920
|
|
||||||
},
|
|
||||||
"smarm 24-thread": {
|
|
||||||
"result": 1000,
|
|
||||||
"median": 1494,
|
|
||||||
"min": 1489,
|
|
||||||
"max": 1546
|
|
||||||
},
|
|
||||||
"tokio current_thread": {
|
|
||||||
"result": 1000,
|
|
||||||
"median": 396,
|
|
||||||
"min": 389,
|
|
||||||
"max": 409
|
|
||||||
},
|
|
||||||
"tokio multi-thread": {
|
|
||||||
"result": 1000,
|
|
||||||
"median": 10382,
|
|
||||||
"min": 9559,
|
|
||||||
"max": 10924
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"spawn_storm_busy": {
|
|
||||||
"smarm 1-thread": {
|
|
||||||
"result": 10000,
|
|
||||||
"median": 106232,
|
|
||||||
"min": 105627,
|
|
||||||
"max": 107693
|
|
||||||
},
|
|
||||||
"smarm 24-thread": {
|
|
||||||
"result": 10000,
|
|
||||||
"median": 49198,
|
|
||||||
"min": 48894,
|
|
||||||
"max": 52606
|
|
||||||
},
|
|
||||||
"tokio current_thread": {
|
|
||||||
"result": 10000,
|
|
||||||
"median": 1140,
|
|
||||||
"min": 1018,
|
|
||||||
"max": 1169
|
|
||||||
},
|
|
||||||
"tokio multi-thread": {
|
|
||||||
"result": 10000,
|
|
||||||
"median": 18650,
|
|
||||||
"min": 16486,
|
|
||||||
"max": 19396
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"mpsc_contention": {
|
|
||||||
"smarm 1-thread": {
|
|
||||||
"result": 320000,
|
|
||||||
"median": 6173,
|
|
||||||
"min": 5446,
|
|
||||||
"max": 6226
|
|
||||||
},
|
|
||||||
"smarm 24-thread": {
|
|
||||||
"result": 320000,
|
|
||||||
"median": 36329,
|
|
||||||
"min": 35121,
|
|
||||||
"max": 37590
|
|
||||||
},
|
|
||||||
"tokio current_thread": {
|
|
||||||
"result": 320000,
|
|
||||||
"median": 5563,
|
|
||||||
"min": 5527,
|
|
||||||
"max": 6239
|
|
||||||
},
|
|
||||||
"tokio multi-thread": {
|
|
||||||
"result": 320000,
|
|
||||||
"median": 63966,
|
|
||||||
"min": 59972,
|
|
||||||
"max": 67534
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"many_timers": {
|
|
||||||
"smarm 1-thread": {
|
|
||||||
"result": 10000,
|
|
||||||
"median": 107826,
|
|
||||||
"min": 107093,
|
|
||||||
"max": 119034
|
|
||||||
},
|
|
||||||
"smarm 24-thread": {
|
|
||||||
"result": 10000,
|
|
||||||
"median": 96539,
|
|
||||||
"min": 95413,
|
|
||||||
"max": 97804
|
|
||||||
},
|
|
||||||
"tokio current_thread": {
|
|
||||||
"result": 10000,
|
|
||||||
"median": 12584,
|
|
||||||
"min": 12539,
|
|
||||||
"max": 12627
|
|
||||||
},
|
|
||||||
"tokio multi-thread": {
|
|
||||||
"result": 10000,
|
|
||||||
"median": 16183,
|
|
||||||
"min": 16024,
|
|
||||||
"max": 16541
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"multi_thread_scaling": {
|
|
||||||
"smarm 1-thread": {
|
|
||||||
"result": 33860,
|
|
||||||
"median": 15083,
|
|
||||||
"min": 15071,
|
|
||||||
"max": 15283
|
|
||||||
},
|
|
||||||
"smarm 2-thread": {
|
|
||||||
"result": 33860,
|
|
||||||
"median": 8070,
|
|
||||||
"min": 8003,
|
|
||||||
"max": 8096
|
|
||||||
},
|
|
||||||
"smarm 4-thread": {
|
|
||||||
"result": 33860,
|
|
||||||
"median": 4460,
|
|
||||||
"min": 4454,
|
|
||||||
"max": 4516
|
|
||||||
},
|
|
||||||
"smarm 24-thread": {
|
|
||||||
"result": 33860,
|
|
||||||
"median": 2333,
|
|
||||||
"min": 2294,
|
|
||||||
"max": 2348
|
|
||||||
},
|
|
||||||
"tokio multi 1-thread": {
|
|
||||||
"result": 33860,
|
|
||||||
"median": 14504,
|
|
||||||
"min": 14193,
|
|
||||||
"max": 14562
|
|
||||||
},
|
|
||||||
"tokio multi 2-thread": {
|
|
||||||
"result": 33860,
|
|
||||||
"median": 7297,
|
|
||||||
"min": 7289,
|
|
||||||
"max": 7398
|
|
||||||
},
|
|
||||||
"tokio multi 4-thread": {
|
|
||||||
"result": 33860,
|
|
||||||
"median": 3795,
|
|
||||||
"min": 3756,
|
|
||||||
"max": 3799
|
|
||||||
},
|
|
||||||
"tokio multi 24-thread": {
|
|
||||||
"result": 33860,
|
|
||||||
"median": 1544,
|
|
||||||
"min": 1520,
|
|
||||||
"max": 1610
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"deep_recursion": {
|
"deep_recursion": {
|
||||||
"smarm 1-thread": {
|
"smarm 1-thread": {
|
||||||
"result": 1,
|
"result": 1,
|
||||||
"median": 226,
|
"median": 316,
|
||||||
"min": 222,
|
"min": 314,
|
||||||
"max": 243
|
"max": 322
|
||||||
},
|
},
|
||||||
"smarm 24-thread": {
|
"smarm 20-thread": {
|
||||||
"result": 1,
|
"result": 1,
|
||||||
"median": 745,
|
"median": 758,
|
||||||
"min": 744,
|
"min": 725,
|
||||||
"max": 776
|
"max": 1222
|
||||||
},
|
},
|
||||||
"tokio current_thread": {
|
"tokio current_thread": {
|
||||||
"result": 1,
|
"result": 1,
|
||||||
"median": 11,
|
"median": 11,
|
||||||
"min": 10,
|
"min": 11,
|
||||||
"max": 13
|
"max": 14
|
||||||
},
|
},
|
||||||
"tokio multi-thread": {
|
"tokio multi-thread": {
|
||||||
"result": 1,
|
"result": 1,
|
||||||
"median": 53,
|
"median": 52,
|
||||||
"min": 53,
|
"min": 51,
|
||||||
"max": 57
|
"max": 55
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"yield_in_hot_loop": {
|
"fan_out_compute": {
|
||||||
"smarm 1-thread": {
|
"smarm 1-thread": {
|
||||||
"result": 1000000,
|
"result": 33860,
|
||||||
"median": 64849,
|
"median": 15074,
|
||||||
"min": 64396,
|
"min": 15064,
|
||||||
"max": 65283
|
"max": 15081
|
||||||
|
},
|
||||||
|
"smarm 20-thread": {
|
||||||
|
"result": 33860,
|
||||||
|
"median": 2786,
|
||||||
|
"min": 2539,
|
||||||
|
"max": 2913
|
||||||
},
|
},
|
||||||
"tokio current_thread": {
|
"tokio current_thread": {
|
||||||
"result": 1000000,
|
"result": 33860,
|
||||||
"median": 68507,
|
"median": 14040,
|
||||||
"min": 62018,
|
"min": 13749,
|
||||||
"max": 72341
|
"max": 14056
|
||||||
|
},
|
||||||
|
"tokio multi-thread": {
|
||||||
|
"result": 33860,
|
||||||
|
"median": 2404,
|
||||||
|
"min": 2136,
|
||||||
|
"max": 2436
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"many_timers": {
|
||||||
|
"smarm 1-thread": {
|
||||||
|
"result": 10000,
|
||||||
|
"median": 117779,
|
||||||
|
"min": 107641,
|
||||||
|
"max": 120785
|
||||||
|
},
|
||||||
|
"smarm 20-thread": {
|
||||||
|
"result": 10000,
|
||||||
|
"median": 54031,
|
||||||
|
"min": 53646,
|
||||||
|
"max": 54841
|
||||||
|
},
|
||||||
|
"tokio current_thread": {
|
||||||
|
"result": 10000,
|
||||||
|
"median": 12417,
|
||||||
|
"min": 12383,
|
||||||
|
"max": 12479
|
||||||
|
},
|
||||||
|
"tokio multi-thread": {
|
||||||
|
"result": 10000,
|
||||||
|
"median": 13880,
|
||||||
|
"min": 13405,
|
||||||
|
"max": 13953
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"mpsc_contention": {
|
||||||
|
"smarm 1-thread": {
|
||||||
|
"result": 320000,
|
||||||
|
"median": 3260,
|
||||||
|
"min": 2896,
|
||||||
|
"max": 3427
|
||||||
|
},
|
||||||
|
"smarm 20-thread": {
|
||||||
|
"result": 320000,
|
||||||
|
"median": 41206,
|
||||||
|
"min": 40562,
|
||||||
|
"max": 41754
|
||||||
|
},
|
||||||
|
"tokio current_thread": {
|
||||||
|
"result": 320000,
|
||||||
|
"median": 5895,
|
||||||
|
"min": 5272,
|
||||||
|
"max": 5902
|
||||||
|
},
|
||||||
|
"tokio multi-thread": {
|
||||||
|
"result": 320000,
|
||||||
|
"median": 71069,
|
||||||
|
"min": 60076,
|
||||||
|
"max": 80026
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"multi_thread_scaling": {
|
||||||
|
"smarm 1-thread": {
|
||||||
|
"result": 33860,
|
||||||
|
"median": 15163,
|
||||||
|
"min": 15125,
|
||||||
|
"max": 15172
|
||||||
|
},
|
||||||
|
"smarm 2-thread": {
|
||||||
|
"result": 33860,
|
||||||
|
"median": 7980,
|
||||||
|
"min": 7947,
|
||||||
|
"max": 8103
|
||||||
|
},
|
||||||
|
"smarm 4-thread": {
|
||||||
|
"result": 33860,
|
||||||
|
"median": 4324,
|
||||||
|
"min": 4300,
|
||||||
|
"max": 4364
|
||||||
|
},
|
||||||
|
"smarm 20-thread": {
|
||||||
|
"result": 33860,
|
||||||
|
"median": 2704,
|
||||||
|
"min": 2391,
|
||||||
|
"max": 2841
|
||||||
|
},
|
||||||
|
"tokio multi 1-thread": {
|
||||||
|
"result": 33860,
|
||||||
|
"median": 14271,
|
||||||
|
"min": 14251,
|
||||||
|
"max": 14459
|
||||||
|
},
|
||||||
|
"tokio multi 2-thread": {
|
||||||
|
"result": 33860,
|
||||||
|
"median": 7395,
|
||||||
|
"min": 7195,
|
||||||
|
"max": 7443
|
||||||
|
},
|
||||||
|
"tokio multi 4-thread": {
|
||||||
|
"result": 33860,
|
||||||
|
"median": 3740,
|
||||||
|
"min": 3709,
|
||||||
|
"max": 3752
|
||||||
|
},
|
||||||
|
"tokio multi 20-thread": {
|
||||||
|
"result": 33860,
|
||||||
|
"median": 2373,
|
||||||
|
"min": 2176,
|
||||||
|
"max": 2380
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"ping_pong_oneshot": {
|
||||||
|
"smarm 1-thread": {
|
||||||
|
"result": 1000,
|
||||||
|
"median": 935,
|
||||||
|
"min": 910,
|
||||||
|
"max": 941
|
||||||
|
},
|
||||||
|
"smarm 20-thread": {
|
||||||
|
"result": 1000,
|
||||||
|
"median": 6168,
|
||||||
|
"min": 6029,
|
||||||
|
"max": 6550
|
||||||
|
},
|
||||||
|
"tokio current_thread": {
|
||||||
|
"result": 1000,
|
||||||
|
"median": 407,
|
||||||
|
"min": 401,
|
||||||
|
"max": 430
|
||||||
|
},
|
||||||
|
"tokio multi-thread": {
|
||||||
|
"result": 1000,
|
||||||
|
"median": 9395,
|
||||||
|
"min": 8795,
|
||||||
|
"max": 9812
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"ping_pong_steady": {
|
||||||
|
"smarm 1-thread": {
|
||||||
|
"result": 10000,
|
||||||
|
"median": 1517,
|
||||||
|
"min": 1501,
|
||||||
|
"max": 1527
|
||||||
|
},
|
||||||
|
"smarm 20-thread": {
|
||||||
|
"result": 10000,
|
||||||
|
"median": 1910,
|
||||||
|
"min": 1848,
|
||||||
|
"max": 1947
|
||||||
|
},
|
||||||
|
"tokio current_thread": {
|
||||||
|
"result": 10000,
|
||||||
|
"median": 1284,
|
||||||
|
"min": 1278,
|
||||||
|
"max": 1296
|
||||||
|
},
|
||||||
|
"tokio multi-thread": {
|
||||||
|
"result": 10000,
|
||||||
|
"median": 82015,
|
||||||
|
"min": 81833,
|
||||||
|
"max": 82993
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"spawn_pair_control": {
|
||||||
|
"smarm 1-thread": {
|
||||||
|
"result": 1000,
|
||||||
|
"median": 637,
|
||||||
|
"min": 632,
|
||||||
|
"max": 661
|
||||||
|
},
|
||||||
|
"smarm 20-thread": {
|
||||||
|
"result": 1000,
|
||||||
|
"median": 5118,
|
||||||
|
"min": 5089,
|
||||||
|
"max": 5157
|
||||||
|
},
|
||||||
|
"tokio current_thread": {
|
||||||
|
"result": 1000,
|
||||||
|
"median": 288,
|
||||||
|
"min": 249,
|
||||||
|
"max": 300
|
||||||
|
},
|
||||||
|
"tokio multi-thread": {
|
||||||
|
"result": 1000,
|
||||||
|
"median": 8617,
|
||||||
|
"min": 8534,
|
||||||
|
"max": 8808
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"spawn_storm_busy": {
|
||||||
|
"smarm 1-thread": {
|
||||||
|
"result": 10000,
|
||||||
|
"median": 105937,
|
||||||
|
"min": 101927,
|
||||||
|
"max": 107104
|
||||||
|
},
|
||||||
|
"smarm 20-thread": {
|
||||||
|
"result": 10000,
|
||||||
|
"median": 15928,
|
||||||
|
"min": 15918,
|
||||||
|
"max": 16581
|
||||||
|
},
|
||||||
|
"tokio current_thread": {
|
||||||
|
"result": 10000,
|
||||||
|
"median": 1248,
|
||||||
|
"min": 1121,
|
||||||
|
"max": 1253
|
||||||
|
},
|
||||||
|
"tokio multi-thread": {
|
||||||
|
"result": 10000,
|
||||||
|
"median": 12313,
|
||||||
|
"min": 11641,
|
||||||
|
"max": 16848
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"uncontended_channel": {
|
"uncontended_channel": {
|
||||||
"smarm 1-thread": {
|
"smarm 1-thread": {
|
||||||
"result": 1000000,
|
"result": 1000000,
|
||||||
"median": 11949,
|
"median": 12452,
|
||||||
"min": 11928,
|
"min": 12315,
|
||||||
"max": 13596
|
"max": 13700
|
||||||
},
|
},
|
||||||
"tokio current_thread": {
|
"tokio current_thread": {
|
||||||
"result": 1000000,
|
"result": 1000000,
|
||||||
"median": 15083,
|
"median": 15091,
|
||||||
"min": 15038,
|
"min": 15086,
|
||||||
"max": 16994
|
"max": 16944
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"catch_unwind_panics": {
|
"yield_in_hot_loop": {
|
||||||
"smarm 1-thread": {
|
"smarm 1-thread": {
|
||||||
"result": 10000,
|
"result": 1000000,
|
||||||
"median": 110932,
|
"median": 33187,
|
||||||
"min": 110182,
|
"min": 33000,
|
||||||
"max": 124147
|
"max": 33325
|
||||||
},
|
|
||||||
"smarm 24-thread": {
|
|
||||||
"result": 10000,
|
|
||||||
"median": 13665,
|
|
||||||
"min": 13172,
|
|
||||||
"max": 13784
|
|
||||||
},
|
},
|
||||||
"tokio current_thread": {
|
"tokio current_thread": {
|
||||||
"result": 10000,
|
"result": 1000000,
|
||||||
"median": 10431,
|
"median": 74444,
|
||||||
"min": 9346,
|
"min": 67009,
|
||||||
"max": 10915
|
"max": 75753
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"yield_many": {
|
||||||
|
"smarm 1-thread": {
|
||||||
|
"result": 200000,
|
||||||
|
"median": 10921,
|
||||||
|
"min": 10828,
|
||||||
|
"max": 11041
|
||||||
|
},
|
||||||
|
"smarm 20-thread": {
|
||||||
|
"result": 200000,
|
||||||
|
"median": 44422,
|
||||||
|
"min": 43715,
|
||||||
|
"max": 44595
|
||||||
|
},
|
||||||
|
"tokio current_thread": {
|
||||||
|
"result": 200000,
|
||||||
|
"median": 5355,
|
||||||
|
"min": 5327,
|
||||||
|
"max": 5426
|
||||||
},
|
},
|
||||||
"tokio multi-thread": {
|
"tokio multi-thread": {
|
||||||
"result": 10000,
|
"result": 200000,
|
||||||
"median": 6171,
|
"median": 6170,
|
||||||
"min": 5626,
|
"min": 5982,
|
||||||
"max": 6555
|
"max": 6898
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+230
-28
@@ -13,7 +13,18 @@
|
|||||||
//! completeness.
|
//! completeness.
|
||||||
//! 4. ping_pong_oneshot — N rounds of (spawn pair, send oneshot, await).
|
//! 4. ping_pong_oneshot — N rounds of (spawn pair, send oneshot, await).
|
||||||
//! Closer to a request/response workload than channel
|
//! Closer to a request/response workload than channel
|
||||||
//! ping-pong.
|
//! ping-pong. NOTE: 2 spawns + 2 channel allocs + 2
|
||||||
|
//! joins per round; the message path is a minority
|
||||||
|
//! of it. Sections 5 and 6 split it apart.
|
||||||
|
//! 5. spawn_pair_control — section 4 with the messages removed: same
|
||||||
|
//! spawn/join shape, actors return immediately.
|
||||||
|
//! (4 − 5) ≈ per-round message-path cost.
|
||||||
|
//! 6. ping_pong_steady — ONE persistent pair, N roundtrips over unbounded
|
||||||
|
//! MPSC channels (smarm::channel vs
|
||||||
|
//! tokio::sync::mpsc::unbounded_channel — both
|
||||||
|
//! unbounded, non-blocking send). Steady-state
|
||||||
|
//! park/unpark cost per roundtrip, no spawn in the
|
||||||
|
//! loop.
|
||||||
|
|
||||||
use std::sync::atomic::{AtomicU64, Ordering};
|
use std::sync::atomic::{AtomicU64, Ordering};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
@@ -26,7 +37,9 @@ use std::time::Instant;
|
|||||||
const ITERS: u32 = 15;
|
const ITERS: u32 = 15;
|
||||||
|
|
||||||
fn available_threads() -> usize {
|
fn available_threads() -> usize {
|
||||||
std::thread::available_parallelism().map(|n| n.get()).unwrap_or(1)
|
std::thread::available_parallelism()
|
||||||
|
.map(|n| n.get())
|
||||||
|
.unwrap_or(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn env_sets() -> u32 {
|
fn env_sets() -> u32 {
|
||||||
@@ -108,17 +121,15 @@ fn bench_chained_smarm(threads: usize) -> (u64, u128) {
|
|||||||
fn bench_chained_tokio_current() -> (u64, u128) {
|
fn bench_chained_tokio_current() -> (u64, u128) {
|
||||||
let counter = Arc::new(AtomicU64::new(0));
|
let counter = Arc::new(AtomicU64::new(0));
|
||||||
let c2 = counter.clone();
|
let c2 = counter.clone();
|
||||||
let rt = tokio::runtime::Builder::new_current_thread().build().unwrap();
|
let rt = tokio::runtime::Builder::new_current_thread()
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
let local = tokio::task::LocalSet::new();
|
let local = tokio::task::LocalSet::new();
|
||||||
local.block_on(&rt, async move {
|
local.block_on(&rt, async move {
|
||||||
// Use a oneshot done channel like tokio's own chained_spawn bench.
|
// Use a oneshot done channel like tokio's own chained_spawn bench.
|
||||||
let (done_tx, done_rx) = tokio::sync::oneshot::channel();
|
let (done_tx, done_rx) = tokio::sync::oneshot::channel();
|
||||||
fn iter(
|
fn iter(c: Arc<AtomicU64>, done: tokio::sync::oneshot::Sender<()>, n: u64) {
|
||||||
c: Arc<AtomicU64>,
|
|
||||||
done: tokio::sync::oneshot::Sender<()>,
|
|
||||||
n: u64,
|
|
||||||
) {
|
|
||||||
if n == 0 {
|
if n == 0 {
|
||||||
let _ = done.send(());
|
let _ = done.send(());
|
||||||
} else {
|
} else {
|
||||||
@@ -186,7 +197,9 @@ fn bench_yield_smarm(threads: usize) -> (u64, u128) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn bench_yield_tokio_current() -> (u64, u128) {
|
fn bench_yield_tokio_current() -> (u64, u128) {
|
||||||
let rt = tokio::runtime::Builder::new_current_thread().build().unwrap();
|
let rt = tokio::runtime::Builder::new_current_thread()
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
let local = tokio::task::LocalSet::new();
|
let local = tokio::task::LocalSet::new();
|
||||||
local.block_on(&rt, async move {
|
local.block_on(&rt, async move {
|
||||||
@@ -235,11 +248,22 @@ const PRIME_N: u64 = 400_000;
|
|||||||
const PRIME_WORKERS: u64 = 64;
|
const PRIME_WORKERS: u64 = 64;
|
||||||
|
|
||||||
fn is_prime(n: u64) -> bool {
|
fn is_prime(n: u64) -> bool {
|
||||||
if n < 2 { return false; }
|
if n < 2 {
|
||||||
if n < 4 { return true; }
|
return false;
|
||||||
if n % 2 == 0 { return false; }
|
}
|
||||||
|
if n < 4 {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if n % 2 == 0 {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
let mut i = 3u64;
|
let mut i = 3u64;
|
||||||
while i * i <= n { if n % i == 0 { return false; } i += 2; }
|
while i * i <= n {
|
||||||
|
if n % i == 0 {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
i += 2;
|
||||||
|
}
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -250,7 +274,11 @@ fn count_primes(lo: u64, hi: u64) -> u64 {
|
|||||||
fn primes_slice(w: u64) -> (u64, u64) {
|
fn primes_slice(w: u64) -> (u64, u64) {
|
||||||
let per = PRIME_N / PRIME_WORKERS;
|
let per = PRIME_N / PRIME_WORKERS;
|
||||||
let lo = w * per;
|
let lo = w * per;
|
||||||
let hi = if w + 1 == PRIME_WORKERS { PRIME_N } else { lo + per };
|
let hi = if w + 1 == PRIME_WORKERS {
|
||||||
|
PRIME_N
|
||||||
|
} else {
|
||||||
|
lo + per
|
||||||
|
};
|
||||||
(lo, hi)
|
(lo, hi)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -267,7 +295,9 @@ fn bench_primes_smarm(threads: usize) -> (u64, u128) {
|
|||||||
tc.fetch_add(count_primes(lo, hi), Ordering::Relaxed);
|
tc.fetch_add(count_primes(lo, hi), Ordering::Relaxed);
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
for h in handles { h.join().unwrap(); }
|
for h in handles {
|
||||||
|
h.join().unwrap();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
(total.load(Ordering::Relaxed), start.elapsed().as_micros())
|
(total.load(Ordering::Relaxed), start.elapsed().as_micros())
|
||||||
}
|
}
|
||||||
@@ -275,7 +305,9 @@ fn bench_primes_smarm(threads: usize) -> (u64, u128) {
|
|||||||
fn bench_primes_tokio_current() -> (u64, u128) {
|
fn bench_primes_tokio_current() -> (u64, u128) {
|
||||||
let total = Arc::new(AtomicU64::new(0));
|
let total = Arc::new(AtomicU64::new(0));
|
||||||
let t2 = total.clone();
|
let t2 = total.clone();
|
||||||
let rt = tokio::runtime::Builder::new_current_thread().build().unwrap();
|
let rt = tokio::runtime::Builder::new_current_thread()
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
let local = tokio::task::LocalSet::new();
|
let local = tokio::task::LocalSet::new();
|
||||||
local.block_on(&rt, async move {
|
local.block_on(&rt, async move {
|
||||||
@@ -287,7 +319,9 @@ fn bench_primes_tokio_current() -> (u64, u128) {
|
|||||||
tc.fetch_add(count_primes(lo, hi), Ordering::Relaxed);
|
tc.fetch_add(count_primes(lo, hi), Ordering::Relaxed);
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
for h in handles { let _ = h.await; }
|
for h in handles {
|
||||||
|
let _ = h.await;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
(total.load(Ordering::Relaxed), start.elapsed().as_micros())
|
(total.load(Ordering::Relaxed), start.elapsed().as_micros())
|
||||||
}
|
}
|
||||||
@@ -309,7 +343,9 @@ fn bench_primes_tokio_multi() -> (u64, u128) {
|
|||||||
tc.fetch_add(count_primes(lo, hi), Ordering::Relaxed);
|
tc.fetch_add(count_primes(lo, hi), Ordering::Relaxed);
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
for h in handles { let _ = h.await; }
|
for h in handles {
|
||||||
|
let _ = h.await;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
(total.load(Ordering::Relaxed), start.elapsed().as_micros())
|
(total.load(Ordering::Relaxed), start.elapsed().as_micros())
|
||||||
}
|
}
|
||||||
@@ -344,7 +380,9 @@ fn bench_pp_smarm(threads: usize) -> (u64, u128) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn bench_pp_tokio_current() -> (u64, u128) {
|
fn bench_pp_tokio_current() -> (u64, u128) {
|
||||||
let rt = tokio::runtime::Builder::new_current_thread().build().unwrap();
|
let rt = tokio::runtime::Builder::new_current_thread()
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
let local = tokio::task::LocalSet::new();
|
let local = tokio::task::LocalSet::new();
|
||||||
local.block_on(&rt, async move {
|
local.block_on(&rt, async move {
|
||||||
@@ -392,9 +430,137 @@ fn bench_pp_tokio_multi() -> (u64, u128) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// main
|
// 5. spawn_pair_control — section 4 minus the messages
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
/// Target-5 instrumentation: emit the runtime's wake-path counters for one
|
||||||
|
/// run when SMARM_WAKE_DIAG is set. Off by default so sweep.py output is
|
||||||
|
/// unchanged.
|
||||||
|
fn wake_diag(section: &str, threads: usize, rt: &smarm::runtime::Runtime, us: u128) {
|
||||||
|
if std::env::var_os("SMARM_WAKE_DIAG").is_some() {
|
||||||
|
println!("DIAG,{section},{threads},{us},{}", rt.stats().wake_diag());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn bench_ctl_smarm(threads: usize) -> (u64, u128) {
|
||||||
|
let start = Instant::now();
|
||||||
|
let rt = smarm::runtime::init(bench_cfg(threads));
|
||||||
|
rt.run(|| {
|
||||||
|
for _ in 0..PP_ROUNDS {
|
||||||
|
let hb = smarm::spawn(|| {});
|
||||||
|
let ha = smarm::spawn(|| {});
|
||||||
|
ha.join().unwrap();
|
||||||
|
hb.join().unwrap();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
let us = start.elapsed().as_micros();
|
||||||
|
wake_diag("spawn_pair_control", threads, &rt, us);
|
||||||
|
(PP_ROUNDS, us)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn bench_ctl_tokio_current() -> (u64, u128) {
|
||||||
|
let rt = tokio::runtime::Builder::new_current_thread()
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
let start = Instant::now();
|
||||||
|
let local = tokio::task::LocalSet::new();
|
||||||
|
local.block_on(&rt, async move {
|
||||||
|
for _ in 0..PP_ROUNDS {
|
||||||
|
let hb = tokio::task::spawn_local(async {});
|
||||||
|
let ha = tokio::task::spawn_local(async {});
|
||||||
|
let _ = ha.await;
|
||||||
|
let _ = hb.await;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
(PP_ROUNDS, start.elapsed().as_micros())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn bench_ctl_tokio_multi() -> (u64, u128) {
|
||||||
|
let rt = tokio::runtime::Builder::new_multi_thread()
|
||||||
|
.worker_threads(available_threads())
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
let start = Instant::now();
|
||||||
|
rt.block_on(async move {
|
||||||
|
for _ in 0..PP_ROUNDS {
|
||||||
|
let hb = tokio::spawn(async {});
|
||||||
|
let ha = tokio::spawn(async {});
|
||||||
|
let _ = ha.await;
|
||||||
|
let _ = hb.await;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
(PP_ROUNDS, start.elapsed().as_micros())
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// 6. ping_pong_steady — one persistent pair, PP_STEADY roundtrips
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
const PP_STEADY: u64 = 10_000;
|
||||||
|
|
||||||
|
fn bench_steady_smarm(threads: usize) -> (u64, u128) {
|
||||||
|
let start = Instant::now();
|
||||||
|
let rt = smarm::runtime::init(bench_cfg(threads));
|
||||||
|
rt.run(|| {
|
||||||
|
let (tx_ab, rx_ab) = smarm::channel::<u64>();
|
||||||
|
let (tx_ba, rx_ba) = smarm::channel::<u64>();
|
||||||
|
let echo = smarm::spawn(move || {
|
||||||
|
for _ in 0..PP_STEADY {
|
||||||
|
let v = rx_ab.recv().unwrap();
|
||||||
|
tx_ba.send(v + 1).unwrap();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
for i in 0..PP_STEADY {
|
||||||
|
tx_ab.send(i).unwrap();
|
||||||
|
let v = rx_ba.recv().unwrap();
|
||||||
|
assert_eq!(v, i + 1);
|
||||||
|
}
|
||||||
|
echo.join().unwrap();
|
||||||
|
});
|
||||||
|
let us = start.elapsed().as_micros();
|
||||||
|
wake_diag("ping_pong_steady", threads, &rt, us);
|
||||||
|
(PP_STEADY, us)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn steady_tokio_body() {
|
||||||
|
let (tx_ab, mut rx_ab) = tokio::sync::mpsc::unbounded_channel::<u64>();
|
||||||
|
let (tx_ba, mut rx_ba) = tokio::sync::mpsc::unbounded_channel::<u64>();
|
||||||
|
let echo = tokio::spawn(async move {
|
||||||
|
for _ in 0..PP_STEADY {
|
||||||
|
let v = rx_ab.recv().await.unwrap();
|
||||||
|
tx_ba.send(v + 1).unwrap();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
for i in 0..PP_STEADY {
|
||||||
|
tx_ab.send(i).unwrap();
|
||||||
|
let v = rx_ba.recv().await.unwrap();
|
||||||
|
assert_eq!(v, i + 1);
|
||||||
|
}
|
||||||
|
echo.await.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn bench_steady_tokio_current() -> (u64, u128) {
|
||||||
|
let rt = tokio::runtime::Builder::new_current_thread()
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
let start = Instant::now();
|
||||||
|
rt.block_on(steady_tokio_body());
|
||||||
|
(PP_STEADY, start.elapsed().as_micros())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn bench_steady_tokio_multi() -> (u64, u128) {
|
||||||
|
let rt = tokio::runtime::Builder::new_multi_thread()
|
||||||
|
.worker_threads(available_threads())
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
let start = Instant::now();
|
||||||
|
rt.block_on(steady_tokio_body());
|
||||||
|
(PP_STEADY, start.elapsed().as_micros())
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// main
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// Knob helper — reads SMARM_ALLOC_INTERVAL / SMARM_TIMESLICE_CYCLES env vars
|
// Knob helper — reads SMARM_ALLOC_INTERVAL / SMARM_TIMESLICE_CYCLES env vars
|
||||||
@@ -404,10 +570,14 @@ fn bench_pp_tokio_multi() -> (u64, u128) {
|
|||||||
fn bench_cfg(threads: usize) -> smarm::runtime::Config {
|
fn bench_cfg(threads: usize) -> smarm::runtime::Config {
|
||||||
let mut cfg = smarm::runtime::Config::exact(threads);
|
let mut cfg = smarm::runtime::Config::exact(threads);
|
||||||
if let Ok(v) = std::env::var("SMARM_ALLOC_INTERVAL") {
|
if let Ok(v) = std::env::var("SMARM_ALLOC_INTERVAL") {
|
||||||
if let Ok(n) = v.parse::<u32>() { cfg = cfg.alloc_interval(n); }
|
if let Ok(n) = v.parse::<u32>() {
|
||||||
|
cfg = cfg.alloc_interval(n);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if let Ok(v) = std::env::var("SMARM_TIMESLICE_CYCLES") {
|
if let Ok(v) = std::env::var("SMARM_TIMESLICE_CYCLES") {
|
||||||
if let Ok(n) = v.parse::<u64>() { cfg = cfg.timeslice_cycles(n); }
|
if let Ok(n) = v.parse::<u64>() {
|
||||||
|
cfg = cfg.timeslice_cycles(n);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
cfg
|
cfg
|
||||||
}
|
}
|
||||||
@@ -417,30 +587,42 @@ fn main() {
|
|||||||
println!("smarm general benchmarks");
|
println!("smarm general benchmarks");
|
||||||
println!("available parallelism: {n} threads");
|
println!("available parallelism: {n} threads");
|
||||||
let sets = env_sets();
|
let sets = env_sets();
|
||||||
println!("ITERS={ITERS}×{sets} sets = {} samples (+1 warmup, discarded)", ITERS * sets);
|
println!(
|
||||||
|
"ITERS={ITERS}×{sets} sets = {} samples (+1 warmup, discarded)",
|
||||||
|
ITERS * sets
|
||||||
|
);
|
||||||
println!(
|
println!(
|
||||||
"CHAIN_DEPTH={CHAIN_DEPTH}, YIELD_TASKS={YIELD_TASKS}×{YIELD_ROUNDS}, \
|
"CHAIN_DEPTH={CHAIN_DEPTH}, YIELD_TASKS={YIELD_TASKS}×{YIELD_ROUNDS}, \
|
||||||
PRIME_N={PRIME_N}/{PRIME_WORKERS} workers, PP_ROUNDS={PP_ROUNDS}"
|
PRIME_N={PRIME_N}/{PRIME_WORKERS} workers, PP_ROUNDS={PP_ROUNDS}, \
|
||||||
|
PP_STEADY={PP_STEADY}"
|
||||||
);
|
);
|
||||||
|
|
||||||
// ---- 1. chained_spawn ----
|
// ---- 1. chained_spawn ----
|
||||||
print_header(&format!("chained_spawn: depth {CHAIN_DEPTH}"));
|
print_header(&format!("chained_spawn: depth {CHAIN_DEPTH}"));
|
||||||
run_n("smarm 1-thread", ITERS, || bench_chained_smarm(1));
|
run_n("smarm 1-thread", ITERS, || bench_chained_smarm(1));
|
||||||
run_n(&format!("smarm {n}-thread"), ITERS, || bench_chained_smarm(n));
|
run_n(&format!("smarm {n}-thread"), ITERS, || {
|
||||||
|
bench_chained_smarm(n)
|
||||||
|
});
|
||||||
run_n("tokio current_thread", ITERS, bench_chained_tokio_current);
|
run_n("tokio current_thread", ITERS, bench_chained_tokio_current);
|
||||||
run_n("tokio multi-thread", ITERS, bench_chained_tokio_multi);
|
run_n("tokio multi-thread", ITERS, bench_chained_tokio_multi);
|
||||||
|
|
||||||
// ---- 2. yield_many ----
|
// ---- 2. yield_many ----
|
||||||
print_header(&format!("yield_many: {YIELD_TASKS} tasks × {YIELD_ROUNDS} yields"));
|
print_header(&format!(
|
||||||
|
"yield_many: {YIELD_TASKS} tasks × {YIELD_ROUNDS} yields"
|
||||||
|
));
|
||||||
run_n("smarm 1-thread", ITERS, || bench_yield_smarm(1));
|
run_n("smarm 1-thread", ITERS, || bench_yield_smarm(1));
|
||||||
run_n(&format!("smarm {n}-thread"), ITERS, || bench_yield_smarm(n));
|
run_n(&format!("smarm {n}-thread"), ITERS, || bench_yield_smarm(n));
|
||||||
run_n("tokio current_thread", ITERS, bench_yield_tokio_current);
|
run_n("tokio current_thread", ITERS, bench_yield_tokio_current);
|
||||||
run_n("tokio multi-thread", ITERS, bench_yield_tokio_multi);
|
run_n("tokio multi-thread", ITERS, bench_yield_tokio_multi);
|
||||||
|
|
||||||
// ---- 3. fan_out_compute ----
|
// ---- 3. fan_out_compute ----
|
||||||
print_header(&format!("fan_out_compute: primes in [2, {PRIME_N}) across {PRIME_WORKERS}"));
|
print_header(&format!(
|
||||||
|
"fan_out_compute: primes in [2, {PRIME_N}) across {PRIME_WORKERS}"
|
||||||
|
));
|
||||||
run_n("smarm 1-thread", ITERS, || bench_primes_smarm(1));
|
run_n("smarm 1-thread", ITERS, || bench_primes_smarm(1));
|
||||||
run_n(&format!("smarm {n}-thread"), ITERS, || bench_primes_smarm(n));
|
run_n(&format!("smarm {n}-thread"), ITERS, || {
|
||||||
|
bench_primes_smarm(n)
|
||||||
|
});
|
||||||
run_n("tokio current_thread", ITERS, bench_primes_tokio_current);
|
run_n("tokio current_thread", ITERS, bench_primes_tokio_current);
|
||||||
run_n("tokio multi-thread", ITERS, bench_primes_tokio_multi);
|
run_n("tokio multi-thread", ITERS, bench_primes_tokio_multi);
|
||||||
|
|
||||||
@@ -450,4 +632,24 @@ fn main() {
|
|||||||
run_n(&format!("smarm {n}-thread"), ITERS, || bench_pp_smarm(n));
|
run_n(&format!("smarm {n}-thread"), ITERS, || bench_pp_smarm(n));
|
||||||
run_n("tokio current_thread", ITERS, bench_pp_tokio_current);
|
run_n("tokio current_thread", ITERS, bench_pp_tokio_current);
|
||||||
run_n("tokio multi-thread", ITERS, bench_pp_tokio_multi);
|
run_n("tokio multi-thread", ITERS, bench_pp_tokio_multi);
|
||||||
|
|
||||||
|
// ---- 5. spawn_pair_control ----
|
||||||
|
print_header(&format!(
|
||||||
|
"spawn_pair_control: {PP_ROUNDS} rounds, no messages"
|
||||||
|
));
|
||||||
|
run_n("smarm 1-thread", ITERS, || bench_ctl_smarm(1));
|
||||||
|
run_n(&format!("smarm {n}-thread"), ITERS, || bench_ctl_smarm(n));
|
||||||
|
run_n("tokio current_thread", ITERS, bench_ctl_tokio_current);
|
||||||
|
run_n("tokio multi-thread", ITERS, bench_ctl_tokio_multi);
|
||||||
|
|
||||||
|
// ---- 6. ping_pong_steady ----
|
||||||
|
print_header(&format!(
|
||||||
|
"ping_pong_steady: 1 pair × {PP_STEADY} roundtrips"
|
||||||
|
));
|
||||||
|
run_n("smarm 1-thread", ITERS, || bench_steady_smarm(1));
|
||||||
|
run_n(&format!("smarm {n}-thread"), ITERS, || {
|
||||||
|
bench_steady_smarm(n)
|
||||||
|
});
|
||||||
|
run_n("tokio current_thread", ITERS, bench_steady_tokio_current);
|
||||||
|
run_n("tokio multi-thread", ITERS, bench_steady_tokio_multi);
|
||||||
}
|
}
|
||||||
|
|||||||
+90
-41
@@ -64,11 +64,22 @@ const PRIME_N: u64 = 400_000;
|
|||||||
const WORKERS: u64 = 64;
|
const WORKERS: u64 = 64;
|
||||||
|
|
||||||
fn is_prime(n: u64) -> bool {
|
fn is_prime(n: u64) -> bool {
|
||||||
if n < 2 { return false; }
|
if n < 2 {
|
||||||
if n < 4 { return true; }
|
return false;
|
||||||
if n % 2 == 0 { return false; }
|
}
|
||||||
|
if n < 4 {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if n % 2 == 0 {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
let mut i = 3u64;
|
let mut i = 3u64;
|
||||||
while i * i <= n { if n % i == 0 { return false; } i += 2; }
|
while i * i <= n {
|
||||||
|
if n % i == 0 {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
i += 2;
|
||||||
|
}
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,7 +107,9 @@ fn bench_primes_smarm(threads: usize) -> (u64, u128) {
|
|||||||
tc.fetch_add(count_primes(lo, hi), Ordering::Relaxed);
|
tc.fetch_add(count_primes(lo, hi), Ordering::Relaxed);
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
for h in handles { h.join().unwrap(); }
|
for h in handles {
|
||||||
|
h.join().unwrap();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
(total.load(Ordering::Relaxed), start.elapsed().as_micros())
|
(total.load(Ordering::Relaxed), start.elapsed().as_micros())
|
||||||
}
|
}
|
||||||
@@ -104,7 +117,9 @@ fn bench_primes_smarm(threads: usize) -> (u64, u128) {
|
|||||||
fn bench_primes_tokio_current() -> (u64, u128) {
|
fn bench_primes_tokio_current() -> (u64, u128) {
|
||||||
let total = Arc::new(AtomicU64::new(0));
|
let total = Arc::new(AtomicU64::new(0));
|
||||||
let t2 = total.clone();
|
let t2 = total.clone();
|
||||||
let rt = tokio::runtime::Builder::new_current_thread().build().unwrap();
|
let rt = tokio::runtime::Builder::new_current_thread()
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
let local = tokio::task::LocalSet::new();
|
let local = tokio::task::LocalSet::new();
|
||||||
local.block_on(&rt, async move {
|
local.block_on(&rt, async move {
|
||||||
@@ -116,7 +131,9 @@ fn bench_primes_tokio_current() -> (u64, u128) {
|
|||||||
tc.fetch_add(count_primes(lo, hi), Ordering::Relaxed);
|
tc.fetch_add(count_primes(lo, hi), Ordering::Relaxed);
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
for h in handles { let _ = h.await; }
|
for h in handles {
|
||||||
|
let _ = h.await;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
(total.load(Ordering::Relaxed), start.elapsed().as_micros())
|
(total.load(Ordering::Relaxed), start.elapsed().as_micros())
|
||||||
}
|
}
|
||||||
@@ -138,17 +155,21 @@ fn bench_primes_tokio_multi() -> (u64, u128) {
|
|||||||
tc.fetch_add(count_primes(lo, hi), Ordering::Relaxed);
|
tc.fetch_add(count_primes(lo, hi), Ordering::Relaxed);
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
for h in handles { let _ = h.await; }
|
for h in handles {
|
||||||
|
let _ = h.await;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
(total.load(Ordering::Relaxed), start.elapsed().as_micros())
|
(total.load(Ordering::Relaxed), start.elapsed().as_micros())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn bench_primes_baseline() -> (u64, u128) {
|
fn bench_primes_baseline() -> (u64, u128) {
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
let total: u64 = (0..WORKERS).map(|w| {
|
let total: u64 = (0..WORKERS)
|
||||||
let (lo, hi) = primes_slice(w);
|
.map(|w| {
|
||||||
count_primes(lo, hi)
|
let (lo, hi) = primes_slice(w);
|
||||||
}).sum();
|
count_primes(lo, hi)
|
||||||
|
})
|
||||||
|
.sum();
|
||||||
(total, start.elapsed().as_micros())
|
(total, start.elapsed().as_micros())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -167,15 +188,17 @@ fn bench_pingpong_smarm(threads: usize) -> (u64, u128) {
|
|||||||
tx_a.send(0).unwrap();
|
tx_a.send(0).unwrap();
|
||||||
loop {
|
loop {
|
||||||
let v = rx_b.recv().unwrap();
|
let v = rx_b.recv().unwrap();
|
||||||
if v >= PING_ROUNDS { break; }
|
if v >= PING_ROUNDS {
|
||||||
|
break;
|
||||||
|
}
|
||||||
tx_a.send(v + 1).unwrap();
|
tx_a.send(v + 1).unwrap();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
let hb = smarm::spawn(move || {
|
let hb = smarm::spawn(move || loop {
|
||||||
loop {
|
let v = rx_a.recv().unwrap();
|
||||||
let v = rx_a.recv().unwrap();
|
tx_b.send(v + 1).unwrap();
|
||||||
tx_b.send(v + 1).unwrap();
|
if v + 1 >= PING_ROUNDS {
|
||||||
if v + 1 >= PING_ROUNDS { break; }
|
break;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
ha.join().unwrap();
|
ha.join().unwrap();
|
||||||
@@ -198,7 +221,9 @@ fn bench_pingpong_tokio_current() -> (u64, u128) {
|
|||||||
tx_a.send(0).unwrap();
|
tx_a.send(0).unwrap();
|
||||||
loop {
|
loop {
|
||||||
let v = rx_b.recv().await.unwrap();
|
let v = rx_b.recv().await.unwrap();
|
||||||
if v >= PING_ROUNDS { break; }
|
if v >= PING_ROUNDS {
|
||||||
|
break;
|
||||||
|
}
|
||||||
tx_a.send(v + 1).unwrap();
|
tx_a.send(v + 1).unwrap();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -206,7 +231,9 @@ fn bench_pingpong_tokio_current() -> (u64, u128) {
|
|||||||
loop {
|
loop {
|
||||||
let v = rx_a.recv().await.unwrap();
|
let v = rx_a.recv().await.unwrap();
|
||||||
tx_b.send(v + 1).unwrap();
|
tx_b.send(v + 1).unwrap();
|
||||||
if v + 1 >= PING_ROUNDS { break; }
|
if v + 1 >= PING_ROUNDS {
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
let _ = ha.await;
|
let _ = ha.await;
|
||||||
@@ -229,7 +256,9 @@ fn bench_pingpong_tokio_multi() -> (u64, u128) {
|
|||||||
tx_a.send(0).unwrap();
|
tx_a.send(0).unwrap();
|
||||||
loop {
|
loop {
|
||||||
let v = rx_b.recv().await.unwrap();
|
let v = rx_b.recv().await.unwrap();
|
||||||
if v >= PING_ROUNDS { break; }
|
if v >= PING_ROUNDS {
|
||||||
|
break;
|
||||||
|
}
|
||||||
tx_a.send(v + 1).unwrap();
|
tx_a.send(v + 1).unwrap();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -237,7 +266,9 @@ fn bench_pingpong_tokio_multi() -> (u64, u128) {
|
|||||||
loop {
|
loop {
|
||||||
let v = rx_a.recv().await.unwrap();
|
let v = rx_a.recv().await.unwrap();
|
||||||
tx_b.send(v + 1).unwrap();
|
tx_b.send(v + 1).unwrap();
|
||||||
if v + 1 >= PING_ROUNDS { break; }
|
if v + 1 >= PING_ROUNDS {
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
let _ = ha.await;
|
let _ = ha.await;
|
||||||
@@ -264,7 +295,9 @@ fn bench_spawn_smarm(threads: usize) -> (u64, u128) {
|
|||||||
cc.fetch_add(1, Ordering::Relaxed);
|
cc.fetch_add(1, Ordering::Relaxed);
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
for h in handles { h.join().unwrap(); }
|
for h in handles {
|
||||||
|
h.join().unwrap();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
(counter.load(Ordering::Relaxed), start.elapsed().as_micros())
|
(counter.load(Ordering::Relaxed), start.elapsed().as_micros())
|
||||||
}
|
}
|
||||||
@@ -272,7 +305,9 @@ fn bench_spawn_smarm(threads: usize) -> (u64, u128) {
|
|||||||
fn bench_spawn_tokio_current() -> (u64, u128) {
|
fn bench_spawn_tokio_current() -> (u64, u128) {
|
||||||
let counter = Arc::new(AtomicU64::new(0));
|
let counter = Arc::new(AtomicU64::new(0));
|
||||||
let c = counter.clone();
|
let c = counter.clone();
|
||||||
let rt = tokio::runtime::Builder::new_current_thread().build().unwrap();
|
let rt = tokio::runtime::Builder::new_current_thread()
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
let local = tokio::task::LocalSet::new();
|
let local = tokio::task::LocalSet::new();
|
||||||
local.block_on(&rt, async move {
|
local.block_on(&rt, async move {
|
||||||
@@ -283,7 +318,9 @@ fn bench_spawn_tokio_current() -> (u64, u128) {
|
|||||||
cc.fetch_add(1, Ordering::Relaxed);
|
cc.fetch_add(1, Ordering::Relaxed);
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
for h in handles { let _ = h.await; }
|
for h in handles {
|
||||||
|
let _ = h.await;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
(counter.load(Ordering::Relaxed), start.elapsed().as_micros())
|
(counter.load(Ordering::Relaxed), start.elapsed().as_micros())
|
||||||
}
|
}
|
||||||
@@ -304,7 +341,9 @@ fn bench_spawn_tokio_multi() -> (u64, u128) {
|
|||||||
cc.fetch_add(1, Ordering::Relaxed);
|
cc.fetch_add(1, Ordering::Relaxed);
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
for h in handles { let _ = h.await; }
|
for h in handles {
|
||||||
|
let _ = h.await;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
(counter.load(Ordering::Relaxed), start.elapsed().as_micros())
|
(counter.load(Ordering::Relaxed), start.elapsed().as_micros())
|
||||||
}
|
}
|
||||||
@@ -320,24 +359,34 @@ fn main() {
|
|||||||
println!("PRIME_N={PRIME_N}, WORKERS={WORKERS}, PING_ROUNDS={PING_ROUNDS}, SPAWN_COUNT={SPAWN_COUNT}");
|
println!("PRIME_N={PRIME_N}, WORKERS={WORKERS}, PING_ROUNDS={PING_ROUNDS}, SPAWN_COUNT={SPAWN_COUNT}");
|
||||||
|
|
||||||
// ---- Primes ----
|
// ---- Primes ----
|
||||||
print_header(&format!("Fan-out/fan-in: count primes in [2, {PRIME_N}) across {WORKERS} workers"));
|
print_header(&format!(
|
||||||
run_n("baseline (serial)", ITERS, bench_primes_baseline);
|
"Fan-out/fan-in: count primes in [2, {PRIME_N}) across {WORKERS} workers"
|
||||||
run_n("smarm single-thread", ITERS, || bench_primes_smarm(1));
|
));
|
||||||
run_n(&format!("smarm {n}-thread"), ITERS, || bench_primes_smarm(n));
|
run_n("baseline (serial)", ITERS, bench_primes_baseline);
|
||||||
run_n("tokio current_thread", ITERS, bench_primes_tokio_current);
|
run_n("smarm single-thread", ITERS, || bench_primes_smarm(1));
|
||||||
run_n("tokio multi-thread", ITERS, bench_primes_tokio_multi);
|
run_n(&format!("smarm {n}-thread"), ITERS, || {
|
||||||
|
bench_primes_smarm(n)
|
||||||
|
});
|
||||||
|
run_n("tokio current_thread", ITERS, bench_primes_tokio_current);
|
||||||
|
run_n("tokio multi-thread", ITERS, bench_primes_tokio_multi);
|
||||||
|
|
||||||
// ---- Ping-pong ----
|
// ---- Ping-pong ----
|
||||||
print_header(&format!("Ping-pong: {PING_ROUNDS} round-trips between two actors"));
|
print_header(&format!(
|
||||||
run_n("smarm single-thread", ITERS, || bench_pingpong_smarm(1));
|
"Ping-pong: {PING_ROUNDS} round-trips between two actors"
|
||||||
run_n(&format!("smarm {n}-thread"), ITERS, || bench_pingpong_smarm(n));
|
));
|
||||||
run_n("tokio current_thread", ITERS, bench_pingpong_tokio_current);
|
run_n("smarm single-thread", ITERS, || bench_pingpong_smarm(1));
|
||||||
run_n("tokio multi-thread", ITERS, bench_pingpong_tokio_multi);
|
run_n(&format!("smarm {n}-thread"), ITERS, || {
|
||||||
|
bench_pingpong_smarm(n)
|
||||||
|
});
|
||||||
|
run_n("tokio current_thread", ITERS, bench_pingpong_tokio_current);
|
||||||
|
run_n("tokio multi-thread", ITERS, bench_pingpong_tokio_multi);
|
||||||
|
|
||||||
// ---- Spawn throughput ----
|
// ---- Spawn throughput ----
|
||||||
print_header(&format!("Spawn throughput: {SPAWN_COUNT} actors spawned and joined"));
|
print_header(&format!(
|
||||||
run_n("smarm single-thread", ITERS, || bench_spawn_smarm(1));
|
"Spawn throughput: {SPAWN_COUNT} actors spawned and joined"
|
||||||
|
));
|
||||||
|
run_n("smarm single-thread", ITERS, || bench_spawn_smarm(1));
|
||||||
run_n(&format!("smarm {n}-thread"), ITERS, || bench_spawn_smarm(n));
|
run_n(&format!("smarm {n}-thread"), ITERS, || bench_spawn_smarm(n));
|
||||||
run_n("tokio current_thread", ITERS, bench_spawn_tokio_current);
|
run_n("tokio current_thread", ITERS, bench_spawn_tokio_current);
|
||||||
run_n("tokio multi-thread", ITERS, bench_spawn_tokio_multi);
|
run_n("tokio multi-thread", ITERS, bench_spawn_tokio_multi);
|
||||||
}
|
}
|
||||||
|
|||||||
+24
-7
@@ -16,12 +16,20 @@ const WORKERS: u64 = 16;
|
|||||||
const ITERATIONS: u32 = 5;
|
const ITERATIONS: u32 = 5;
|
||||||
|
|
||||||
fn is_prime(n: u64) -> bool {
|
fn is_prime(n: u64) -> bool {
|
||||||
if n < 2 { return false; }
|
if n < 2 {
|
||||||
if n < 4 { return true; }
|
return false;
|
||||||
if n % 2 == 0 { return false; }
|
}
|
||||||
|
if n < 4 {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if n % 2 == 0 {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
let mut i = 3u64;
|
let mut i = 3u64;
|
||||||
while i * i <= n {
|
while i * i <= n {
|
||||||
if n % i == 0 { return false; }
|
if n % i == 0 {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
i += 2;
|
i += 2;
|
||||||
}
|
}
|
||||||
true
|
true
|
||||||
@@ -30,7 +38,9 @@ fn is_prime(n: u64) -> bool {
|
|||||||
fn count_primes_in(lo: u64, hi: u64) -> u64 {
|
fn count_primes_in(lo: u64, hi: u64) -> u64 {
|
||||||
let mut count = 0u64;
|
let mut count = 0u64;
|
||||||
for n in lo..hi {
|
for n in lo..hi {
|
||||||
if is_prime(n) { count += 1; }
|
if is_prime(n) {
|
||||||
|
count += 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
count
|
count
|
||||||
}
|
}
|
||||||
@@ -38,7 +48,11 @@ fn count_primes_in(lo: u64, hi: u64) -> u64 {
|
|||||||
fn slice(worker: u64) -> (u64, u64) {
|
fn slice(worker: u64) -> (u64, u64) {
|
||||||
let per = N / WORKERS;
|
let per = N / WORKERS;
|
||||||
let lo = worker * per;
|
let lo = worker * per;
|
||||||
let hi = if worker + 1 == WORKERS { N } else { (worker + 1) * per };
|
let hi = if worker + 1 == WORKERS {
|
||||||
|
N
|
||||||
|
} else {
|
||||||
|
(worker + 1) * per
|
||||||
|
};
|
||||||
(lo, hi)
|
(lo, hi)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -125,7 +139,10 @@ fn main() {
|
|||||||
"Counting primes in [2, {}) across {} workers, {} iterations each\n",
|
"Counting primes in [2, {}) across {} workers, {} iterations each\n",
|
||||||
N, WORKERS, ITERATIONS
|
N, WORKERS, ITERATIONS
|
||||||
);
|
);
|
||||||
println!("{:>12} | {:>15} | {:>16} | {:>15} | {:>15}", "runtime", "primes found", "median", "min", "max");
|
println!(
|
||||||
|
"{:>12} | {:>15} | {:>16} | {:>15} | {:>15}",
|
||||||
|
"runtime", "primes found", "median", "min", "max"
|
||||||
|
);
|
||||||
println!("{}", "-".repeat(80));
|
println!("{}", "-".repeat(80));
|
||||||
|
|
||||||
run_n("baseline", ITERATIONS, bench_baseline);
|
run_n("baseline", ITERATIONS, bench_baseline);
|
||||||
|
|||||||
+44
-7
@@ -27,12 +27,19 @@ use std::sync::Arc;
|
|||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
|
||||||
fn env_usize(key: &str, default: usize) -> usize {
|
fn env_usize(key: &str, default: usize) -> usize {
|
||||||
std::env::var(key).ok().and_then(|v| v.parse().ok()).unwrap_or(default)
|
std::env::var(key)
|
||||||
|
.ok()
|
||||||
|
.and_then(|v| v.parse().ok())
|
||||||
|
.unwrap_or(default)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn env_threads() -> Vec<usize> {
|
fn env_threads() -> Vec<usize> {
|
||||||
std::env::var("SMARM_BENCH_THREADS")
|
std::env::var("SMARM_BENCH_THREADS")
|
||||||
.map(|v| v.split_whitespace().filter_map(|t| t.parse().ok()).collect())
|
.map(|v| {
|
||||||
|
v.split_whitespace()
|
||||||
|
.filter_map(|t| t.parse().ok())
|
||||||
|
.collect()
|
||||||
|
})
|
||||||
.unwrap_or_else(|_| vec![1, 2, 4])
|
.unwrap_or_else(|_| vec![1, 2, 4])
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,7 +60,11 @@ fn drive<Q: Send + Sync + 'static>(
|
|||||||
for p in 0..producers {
|
for p in 0..producers {
|
||||||
let q = q.clone();
|
let q = q.clone();
|
||||||
// Give the last producer the remainder.
|
// Give the last producer the remainder.
|
||||||
let n = if p == producers - 1 { items - per * (producers - 1) } else { per };
|
let n = if p == producers - 1 {
|
||||||
|
items - per * (producers - 1)
|
||||||
|
} else {
|
||||||
|
per
|
||||||
|
};
|
||||||
hs.push(std::thread::spawn(move || {
|
hs.push(std::thread::spawn(move || {
|
||||||
let pid = Pid::new(p as u32, 0);
|
let pid = Pid::new(p as u32, 0);
|
||||||
for _ in 0..n {
|
for _ in 0..n {
|
||||||
@@ -132,7 +143,12 @@ fn main() {
|
|||||||
for &t in &threads_sweep {
|
for &t in &threads_sweep {
|
||||||
for (p, c) in ratios_for(t) {
|
for (p, c) in ratios_for(t) {
|
||||||
for s in ["mutex", "mpmc", "striped"] {
|
for s in ["mutex", "mpmc", "striped"] {
|
||||||
cases.push(Case { structure: s, threads: t, producers: p, consumers: c });
|
cases.push(Case {
|
||||||
|
structure: s,
|
||||||
|
threads: t,
|
||||||
|
producers: p,
|
||||||
|
consumers: c,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -147,7 +163,14 @@ fn main() {
|
|||||||
if case.threads < 2 {
|
if case.threads < 2 {
|
||||||
drive_single(&*q, MutexQueue::push, MutexQueue::pop, items)
|
drive_single(&*q, MutexQueue::push, MutexQueue::pop, items)
|
||||||
} else {
|
} else {
|
||||||
drive(q, MutexQueue::push, MutexQueue::pop, case.producers, case.consumers, items)
|
drive(
|
||||||
|
q,
|
||||||
|
MutexQueue::push,
|
||||||
|
MutexQueue::pop,
|
||||||
|
case.producers,
|
||||||
|
case.consumers,
|
||||||
|
items,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
"mpmc" => {
|
"mpmc" => {
|
||||||
@@ -155,7 +178,14 @@ fn main() {
|
|||||||
if case.threads < 2 {
|
if case.threads < 2 {
|
||||||
drive_single(&*q, MpmcRing::push, MpmcRing::pop, items)
|
drive_single(&*q, MpmcRing::push, MpmcRing::pop, items)
|
||||||
} else {
|
} else {
|
||||||
drive(q, MpmcRing::push, MpmcRing::pop, case.producers, case.consumers, items)
|
drive(
|
||||||
|
q,
|
||||||
|
MpmcRing::push,
|
||||||
|
MpmcRing::pop,
|
||||||
|
case.producers,
|
||||||
|
case.consumers,
|
||||||
|
items,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
"striped" => {
|
"striped" => {
|
||||||
@@ -163,7 +193,14 @@ fn main() {
|
|||||||
if case.threads < 2 {
|
if case.threads < 2 {
|
||||||
drive_single(&*q, StripedRing::push, StripedRing::pop, items)
|
drive_single(&*q, StripedRing::push, StripedRing::pop, items)
|
||||||
} else {
|
} else {
|
||||||
drive(q, StripedRing::push, StripedRing::pop, case.producers, case.consumers, items)
|
drive(
|
||||||
|
q,
|
||||||
|
StripedRing::push,
|
||||||
|
StripedRing::pop,
|
||||||
|
case.producers,
|
||||||
|
case.consumers,
|
||||||
|
items,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
|
|||||||
+33
-4
@@ -54,12 +54,19 @@ fn variant() -> &'static str {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn env_usize(key: &str, default: usize) -> usize {
|
fn env_usize(key: &str, default: usize) -> usize {
|
||||||
std::env::var(key).ok().and_then(|v| v.parse().ok()).unwrap_or(default)
|
std::env::var(key)
|
||||||
|
.ok()
|
||||||
|
.and_then(|v| v.parse().ok())
|
||||||
|
.unwrap_or(default)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn env_threads() -> Vec<usize> {
|
fn env_threads() -> Vec<usize> {
|
||||||
std::env::var("SMARM_BENCH_THREADS")
|
std::env::var("SMARM_BENCH_THREADS")
|
||||||
.map(|v| v.split_whitespace().filter_map(|t| t.parse().ok()).collect())
|
.map(|v| {
|
||||||
|
v.split_whitespace()
|
||||||
|
.filter_map(|t| t.parse().ok())
|
||||||
|
.collect()
|
||||||
|
})
|
||||||
.unwrap_or_else(|_| vec![1, 2, 4])
|
.unwrap_or_else(|_| vec![1, 2, 4])
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,6 +90,7 @@ struct Sample {
|
|||||||
us: u128,
|
us: u128,
|
||||||
hits: u64,
|
hits: u64,
|
||||||
displacements: u64,
|
displacements: u64,
|
||||||
|
diag: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn yield_storm(threads: usize, slot: bool, actors: usize, yields: usize) -> Sample {
|
fn yield_storm(threads: usize, slot: bool, actors: usize, yields: usize) -> Sample {
|
||||||
@@ -109,6 +117,7 @@ fn yield_storm(threads: usize, slot: bool, actors: usize, yields: usize) -> Samp
|
|||||||
us,
|
us,
|
||||||
hits: stats.slot_hits(),
|
hits: stats.slot_hits(),
|
||||||
displacements: stats.slot_displacements(),
|
displacements: stats.slot_displacements(),
|
||||||
|
diag: stats.wake_diag(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -152,6 +161,7 @@ fn ping_pong_pairs(threads: usize, slot: bool, pairs: usize, roundtrips: usize)
|
|||||||
us,
|
us,
|
||||||
hits: stats.slot_hits(),
|
hits: stats.slot_hits(),
|
||||||
displacements: stats.slot_displacements(),
|
displacements: stats.slot_displacements(),
|
||||||
|
diag: stats.wake_diag(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -178,6 +188,7 @@ fn spawn_storm(threads: usize, slot: bool, spawns: usize) -> Sample {
|
|||||||
us,
|
us,
|
||||||
hits: stats.slot_hits(),
|
hits: stats.slot_hits(),
|
||||||
displacements: stats.slot_displacements(),
|
displacements: stats.slot_displacements(),
|
||||||
|
diag: stats.wake_diag(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -238,12 +249,30 @@ fn main() {
|
|||||||
);
|
);
|
||||||
println!(
|
println!(
|
||||||
"RQCSV,runtime,{},{},{},{},{},{},{}",
|
"RQCSV,runtime,{},{},{},{},{},{},{}",
|
||||||
variant(), slot_str, name, t, work, mid.us, per_s
|
variant(),
|
||||||
|
slot_str,
|
||||||
|
name,
|
||||||
|
t,
|
||||||
|
work,
|
||||||
|
mid.us,
|
||||||
|
per_s
|
||||||
|
);
|
||||||
|
println!(
|
||||||
|
"RQDIAG,{},{},{},{},{}",
|
||||||
|
variant(),
|
||||||
|
slot_str,
|
||||||
|
name,
|
||||||
|
t,
|
||||||
|
mid.diag
|
||||||
);
|
);
|
||||||
if slot {
|
if slot {
|
||||||
println!(
|
println!(
|
||||||
"RQSLOT,{},{},{},{},{}",
|
"RQSLOT,{},{},{},{},{}",
|
||||||
variant(), name, t, mid.hits, mid.displacements
|
variant(),
|
||||||
|
name,
|
||||||
|
t,
|
||||||
|
mid.hits,
|
||||||
|
mid.displacements
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+55
-19
@@ -37,7 +37,9 @@ use std::time::Instant;
|
|||||||
const ITERS: u32 = 15;
|
const ITERS: u32 = 15;
|
||||||
|
|
||||||
fn available_threads() -> usize {
|
fn available_threads() -> usize {
|
||||||
std::thread::available_parallelism().map(|n| n.get()).unwrap_or(1)
|
std::thread::available_parallelism()
|
||||||
|
.map(|n| n.get())
|
||||||
|
.unwrap_or(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn env_sets() -> u32 {
|
fn env_sets() -> u32 {
|
||||||
@@ -116,7 +118,9 @@ fn bench_recurse_smarm(threads: usize) -> (u64, u128) {
|
|||||||
fn bench_recurse_tokio_current() -> (u64, u128) {
|
fn bench_recurse_tokio_current() -> (u64, u128) {
|
||||||
let counter = Arc::new(AtomicU64::new(0));
|
let counter = Arc::new(AtomicU64::new(0));
|
||||||
let c2 = counter.clone();
|
let c2 = counter.clone();
|
||||||
let rt = tokio::runtime::Builder::new_current_thread().build().unwrap();
|
let rt = tokio::runtime::Builder::new_current_thread()
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
let local = tokio::task::LocalSet::new();
|
let local = tokio::task::LocalSet::new();
|
||||||
local.block_on(&rt, async move {
|
local.block_on(&rt, async move {
|
||||||
@@ -199,7 +203,9 @@ fn bench_hot_smarm() -> (u64, u128) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn bench_hot_tokio_current() -> (u64, u128) {
|
fn bench_hot_tokio_current() -> (u64, u128) {
|
||||||
let rt = tokio::runtime::Builder::new_current_thread().build().unwrap();
|
let rt = tokio::runtime::Builder::new_current_thread()
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
let local = tokio::task::LocalSet::new();
|
let local = tokio::task::LocalSet::new();
|
||||||
local.block_on(&rt, async move {
|
local.block_on(&rt, async move {
|
||||||
@@ -249,7 +255,9 @@ fn bench_unc_smarm() -> (u64, u128) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn bench_unc_tokio_current() -> (u64, u128) {
|
fn bench_unc_tokio_current() -> (u64, u128) {
|
||||||
let rt = tokio::runtime::Builder::new_current_thread().build().unwrap();
|
let rt = tokio::runtime::Builder::new_current_thread()
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
let local = tokio::task::LocalSet::new();
|
let local = tokio::task::LocalSet::new();
|
||||||
local.block_on(&rt, async move {
|
local.block_on(&rt, async move {
|
||||||
@@ -297,8 +305,12 @@ fn bench_panic_smarm(threads: usize) -> (u64, u128) {
|
|||||||
}
|
}
|
||||||
for h in handles {
|
for h in handles {
|
||||||
match h.join() {
|
match h.join() {
|
||||||
Ok(()) => { ok2.fetch_add(1, Ordering::Relaxed); }
|
Ok(()) => {
|
||||||
Err(_) => { err2.fetch_add(1, Ordering::Relaxed); }
|
ok2.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
Err(_) => {
|
||||||
|
err2.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -312,7 +324,9 @@ fn bench_panic_tokio_current() -> (u64, u128) {
|
|||||||
let err = Arc::new(AtomicU64::new(0));
|
let err = Arc::new(AtomicU64::new(0));
|
||||||
let ok2 = ok.clone();
|
let ok2 = ok.clone();
|
||||||
let err2 = err.clone();
|
let err2 = err.clone();
|
||||||
let rt = tokio::runtime::Builder::new_current_thread().build().unwrap();
|
let rt = tokio::runtime::Builder::new_current_thread()
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
let prev_hook = std::panic::take_hook();
|
let prev_hook = std::panic::take_hook();
|
||||||
std::panic::set_hook(Box::new(|_| {}));
|
std::panic::set_hook(Box::new(|_| {}));
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
@@ -328,8 +342,12 @@ fn bench_panic_tokio_current() -> (u64, u128) {
|
|||||||
}
|
}
|
||||||
for h in handles {
|
for h in handles {
|
||||||
match h.await {
|
match h.await {
|
||||||
Ok(()) => { ok2.fetch_add(1, Ordering::Relaxed); }
|
Ok(()) => {
|
||||||
Err(_) => { err2.fetch_add(1, Ordering::Relaxed); }
|
ok2.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
Err(_) => {
|
||||||
|
err2.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -361,8 +379,12 @@ fn bench_panic_tokio_multi() -> (u64, u128) {
|
|||||||
}
|
}
|
||||||
for h in handles {
|
for h in handles {
|
||||||
match h.await {
|
match h.await {
|
||||||
Ok(()) => { ok2.fetch_add(1, Ordering::Relaxed); }
|
Ok(()) => {
|
||||||
Err(_) => { err2.fetch_add(1, Ordering::Relaxed); }
|
ok2.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
Err(_) => {
|
||||||
|
err2.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -375,7 +397,6 @@ fn bench_panic_tokio_multi() -> (u64, u128) {
|
|||||||
// main
|
// main
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// Knob helper — reads SMARM_ALLOC_INTERVAL / SMARM_TIMESLICE_CYCLES env vars
|
// Knob helper — reads SMARM_ALLOC_INTERVAL / SMARM_TIMESLICE_CYCLES env vars
|
||||||
// so the sweep script can override the preemption knobs without recompiling.
|
// so the sweep script can override the preemption knobs without recompiling.
|
||||||
@@ -384,10 +405,14 @@ fn bench_panic_tokio_multi() -> (u64, u128) {
|
|||||||
fn bench_cfg(threads: usize) -> smarm::runtime::Config {
|
fn bench_cfg(threads: usize) -> smarm::runtime::Config {
|
||||||
let mut cfg = smarm::runtime::Config::exact(threads);
|
let mut cfg = smarm::runtime::Config::exact(threads);
|
||||||
if let Ok(v) = std::env::var("SMARM_ALLOC_INTERVAL") {
|
if let Ok(v) = std::env::var("SMARM_ALLOC_INTERVAL") {
|
||||||
if let Ok(n) = v.parse::<u32>() { cfg = cfg.alloc_interval(n); }
|
if let Ok(n) = v.parse::<u32>() {
|
||||||
|
cfg = cfg.alloc_interval(n);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if let Ok(v) = std::env::var("SMARM_TIMESLICE_CYCLES") {
|
if let Ok(v) = std::env::var("SMARM_TIMESLICE_CYCLES") {
|
||||||
if let Ok(n) = v.parse::<u64>() { cfg = cfg.timeslice_cycles(n); }
|
if let Ok(n) = v.parse::<u64>() {
|
||||||
|
cfg = cfg.timeslice_cycles(n);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
cfg
|
cfg
|
||||||
}
|
}
|
||||||
@@ -397,7 +422,10 @@ fn main() {
|
|||||||
println!("smarm smarm-favored benchmarks");
|
println!("smarm smarm-favored benchmarks");
|
||||||
println!("available parallelism: {n} threads");
|
println!("available parallelism: {n} threads");
|
||||||
let sets = env_sets();
|
let sets = env_sets();
|
||||||
println!("ITERS={ITERS}×{sets} sets = {} samples (+1 warmup, discarded)", ITERS * sets);
|
println!(
|
||||||
|
"ITERS={ITERS}×{sets} sets = {} samples (+1 warmup, discarded)",
|
||||||
|
ITERS * sets
|
||||||
|
);
|
||||||
println!(
|
println!(
|
||||||
"RECURSE_DEPTH={RECURSE_DEPTH}, HOT_YIELDS={HOT_YIELDS}×2, \
|
"RECURSE_DEPTH={RECURSE_DEPTH}, HOT_YIELDS={HOT_YIELDS}×2, \
|
||||||
UNCONT_MSGS={UNCONT_MSGS}, PANIC_TASKS={PANIC_TASKS}"
|
UNCONT_MSGS={UNCONT_MSGS}, PANIC_TASKS={PANIC_TASKS}"
|
||||||
@@ -406,22 +434,30 @@ fn main() {
|
|||||||
// ---- 9. deep_recursion ----
|
// ---- 9. deep_recursion ----
|
||||||
print_header(&format!("deep_recursion: depth {RECURSE_DEPTH}"));
|
print_header(&format!("deep_recursion: depth {RECURSE_DEPTH}"));
|
||||||
run_n("smarm 1-thread", ITERS, || bench_recurse_smarm(1));
|
run_n("smarm 1-thread", ITERS, || bench_recurse_smarm(1));
|
||||||
run_n(&format!("smarm {n}-thread"), ITERS, || bench_recurse_smarm(n));
|
run_n(&format!("smarm {n}-thread"), ITERS, || {
|
||||||
|
bench_recurse_smarm(n)
|
||||||
|
});
|
||||||
run_n("tokio current_thread", ITERS, bench_recurse_tokio_current);
|
run_n("tokio current_thread", ITERS, bench_recurse_tokio_current);
|
||||||
run_n("tokio multi-thread", ITERS, bench_recurse_tokio_multi);
|
run_n("tokio multi-thread", ITERS, bench_recurse_tokio_multi);
|
||||||
|
|
||||||
// ---- 10. yield_in_hot_loop ----
|
// ---- 10. yield_in_hot_loop ----
|
||||||
print_header(&format!("yield_in_hot_loop: 2 actors × {HOT_YIELDS} yields (single thread)"));
|
print_header(&format!(
|
||||||
|
"yield_in_hot_loop: 2 actors × {HOT_YIELDS} yields (single thread)"
|
||||||
|
));
|
||||||
run_n("smarm 1-thread", ITERS, bench_hot_smarm);
|
run_n("smarm 1-thread", ITERS, bench_hot_smarm);
|
||||||
run_n("tokio current_thread", ITERS, bench_hot_tokio_current);
|
run_n("tokio current_thread", ITERS, bench_hot_tokio_current);
|
||||||
|
|
||||||
// ---- 11. uncontended_channel ----
|
// ---- 11. uncontended_channel ----
|
||||||
print_header(&format!("uncontended_channel: 1→1, {UNCONT_MSGS} msgs (single thread)"));
|
print_header(&format!(
|
||||||
|
"uncontended_channel: 1→1, {UNCONT_MSGS} msgs (single thread)"
|
||||||
|
));
|
||||||
run_n("smarm 1-thread", ITERS, bench_unc_smarm);
|
run_n("smarm 1-thread", ITERS, bench_unc_smarm);
|
||||||
run_n("tokio current_thread", ITERS, bench_unc_tokio_current);
|
run_n("tokio current_thread", ITERS, bench_unc_tokio_current);
|
||||||
|
|
||||||
// ---- 12. catch_unwind_panics ----
|
// ---- 12. catch_unwind_panics ----
|
||||||
print_header(&format!("catch_unwind_panics: {PANIC_TASKS} tasks, 50% panic"));
|
print_header(&format!(
|
||||||
|
"catch_unwind_panics: {PANIC_TASKS} tasks, 50% panic"
|
||||||
|
));
|
||||||
run_n("smarm 1-thread", ITERS, || bench_panic_smarm(1));
|
run_n("smarm 1-thread", ITERS, || bench_panic_smarm(1));
|
||||||
run_n(&format!("smarm {n}-thread"), ITERS, || bench_panic_smarm(n));
|
run_n(&format!("smarm {n}-thread"), ITERS, || bench_panic_smarm(n));
|
||||||
run_n("tokio current_thread", ITERS, bench_panic_tokio_current);
|
run_n("tokio current_thread", ITERS, bench_panic_tokio_current);
|
||||||
|
|||||||
+30
-5
@@ -73,7 +73,10 @@ fn variant() -> &'static str {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn env_usize(key: &str, default: usize) -> usize {
|
fn env_usize(key: &str, default: usize) -> usize {
|
||||||
std::env::var(key).ok().and_then(|v| v.parse().ok()).unwrap_or(default)
|
std::env::var(key)
|
||||||
|
.ok()
|
||||||
|
.and_then(|v| v.parse().ok())
|
||||||
|
.unwrap_or(default)
|
||||||
}
|
}
|
||||||
|
|
||||||
// --------------------------------------------------------------------------
|
// --------------------------------------------------------------------------
|
||||||
@@ -226,7 +229,11 @@ fn main() {
|
|||||||
let mean_cyc = pooled_cyc.iter().map(|&v| v as f64).sum::<f64>() / n.max(1) as f64;
|
let mean_cyc = pooled_cyc.iter().map(|&v| v as f64).sum::<f64>() / n.max(1) as f64;
|
||||||
// Derived effective frequency: cycles per ns = GHz. Cross-checks the two
|
// Derived effective frequency: cycles per ns = GHz. Cross-checks the two
|
||||||
// lenses against the box's known base clock.
|
// lenses against the box's known base clock.
|
||||||
let derived_ghz = if mean_ns > 0.0 { mean_cyc / mean_ns } else { 0.0 };
|
let derived_ghz = if mean_ns > 0.0 {
|
||||||
|
mean_cyc / mean_ns
|
||||||
|
} else {
|
||||||
|
0.0
|
||||||
|
};
|
||||||
|
|
||||||
let p50 = pct(&pooled_ns, 50.0);
|
let p50 = pct(&pooled_ns, 50.0);
|
||||||
let p90 = pct(&pooled_ns, 90.0);
|
let p90 = pct(&pooled_ns, 90.0);
|
||||||
@@ -241,8 +248,14 @@ fn main() {
|
|||||||
" rounds={} warmup={} runs={} (instrumentation floor: {} ns / {} cyc, subtracted)",
|
" rounds={} warmup={} runs={} (instrumentation floor: {} ns / {} cyc, subtracted)",
|
||||||
rounds, warmup, runs, floor_ns, floor_cyc
|
rounds, warmup, runs, floor_ns, floor_cyc
|
||||||
);
|
);
|
||||||
println!(" {:<10} {:<10} {:<10} {:<10} {:<10}", "p50 ns", "p90 ns", "p99 ns", "min ns", "max ns");
|
println!(
|
||||||
println!(" {:<10} {:<10} {:<10} {:<10} {:<10}", p50, p90, p99, lo, hi);
|
" {:<10} {:<10} {:<10} {:<10} {:<10}",
|
||||||
|
"p50 ns", "p90 ns", "p99 ns", "min ns", "max ns"
|
||||||
|
);
|
||||||
|
println!(
|
||||||
|
" {:<10} {:<10} {:<10} {:<10} {:<10}",
|
||||||
|
p50, p90, p99, lo, hi
|
||||||
|
);
|
||||||
println!(
|
println!(
|
||||||
" mean {:.1} ns | mean {:.0} cyc | derived {:.3} GHz",
|
" mean {:.1} ns | mean {:.0} cyc | derived {:.3} GHz",
|
||||||
mean_ns, mean_cyc, derived_ghz
|
mean_ns, mean_cyc, derived_ghz
|
||||||
@@ -251,6 +264,18 @@ fn main() {
|
|||||||
// Greppable line — same spirit as SPINCSV.
|
// Greppable line — same spirit as SPINCSV.
|
||||||
println!(
|
println!(
|
||||||
"SWITCHCSV,{},{},{},{},{},{},{},{},{},{},{:.1},{:.0},{:.3}",
|
"SWITCHCSV,{},{},{},{},{},{},{},{},{},{},{:.1},{:.0},{:.3}",
|
||||||
variant(), mode, rounds, runs, n, p50, p90, p99, lo, hi, mean_ns, mean_cyc, derived_ghz
|
variant(),
|
||||||
|
mode,
|
||||||
|
rounds,
|
||||||
|
runs,
|
||||||
|
n,
|
||||||
|
p50,
|
||||||
|
p90,
|
||||||
|
p99,
|
||||||
|
lo,
|
||||||
|
hi,
|
||||||
|
mean_ns,
|
||||||
|
mean_cyc,
|
||||||
|
derived_ghz
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
+107
-35
@@ -36,7 +36,9 @@ use std::time::{Duration, Instant};
|
|||||||
const ITERS: u32 = 15;
|
const ITERS: u32 = 15;
|
||||||
|
|
||||||
fn available_threads() -> usize {
|
fn available_threads() -> usize {
|
||||||
std::thread::available_parallelism().map(|n| n.get()).unwrap_or(1)
|
std::thread::available_parallelism()
|
||||||
|
.map(|n| n.get())
|
||||||
|
.unwrap_or(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn env_sets() -> u32 {
|
fn env_sets() -> u32 {
|
||||||
@@ -84,8 +86,8 @@ fn run_n<F: FnMut() -> (u64, u128)>(name: &str, n: u32, mut f: F) {
|
|||||||
// 5. spawn_storm_busy — workers loaded, then storm of zero-work spawns
|
// 5. spawn_storm_busy — workers loaded, then storm of zero-work spawns
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
const STORM_BACKGROUND: u64 = 8; // number of background "busy" actors
|
const STORM_BACKGROUND: u64 = 8; // number of background "busy" actors
|
||||||
const STORM_SPAWN: u64 = 10_000; // zero-work spawns to time
|
const STORM_SPAWN: u64 = 10_000; // zero-work spawns to time
|
||||||
|
|
||||||
fn bench_storm_smarm(threads: usize) -> (u64, u128) {
|
fn bench_storm_smarm(threads: usize) -> (u64, u128) {
|
||||||
let counter = Arc::new(AtomicU64::new(0));
|
let counter = Arc::new(AtomicU64::new(0));
|
||||||
@@ -114,11 +116,15 @@ fn bench_storm_smarm(threads: usize) -> (u64, u128) {
|
|||||||
cc.fetch_add(1, Ordering::Relaxed);
|
cc.fetch_add(1, Ordering::Relaxed);
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
for h in handles { h.join().unwrap(); }
|
for h in handles {
|
||||||
|
h.join().unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
// Tear down background.
|
// Tear down background.
|
||||||
s2.store(true, Ordering::Relaxed);
|
s2.store(true, Ordering::Relaxed);
|
||||||
for h in bg_handles { h.join().unwrap(); }
|
for h in bg_handles {
|
||||||
|
h.join().unwrap();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
(counter.load(Ordering::Relaxed), start.elapsed().as_micros())
|
(counter.load(Ordering::Relaxed), start.elapsed().as_micros())
|
||||||
}
|
}
|
||||||
@@ -129,7 +135,9 @@ fn bench_storm_tokio_current() -> (u64, u128) {
|
|||||||
let c2 = counter.clone();
|
let c2 = counter.clone();
|
||||||
let s2 = stop.clone();
|
let s2 = stop.clone();
|
||||||
|
|
||||||
let rt = tokio::runtime::Builder::new_current_thread().build().unwrap();
|
let rt = tokio::runtime::Builder::new_current_thread()
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
let local = tokio::task::LocalSet::new();
|
let local = tokio::task::LocalSet::new();
|
||||||
local.block_on(&rt, async move {
|
local.block_on(&rt, async move {
|
||||||
@@ -149,9 +157,13 @@ fn bench_storm_tokio_current() -> (u64, u128) {
|
|||||||
cc.fetch_add(1, Ordering::Relaxed);
|
cc.fetch_add(1, Ordering::Relaxed);
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
for h in handles { let _ = h.await; }
|
for h in handles {
|
||||||
|
let _ = h.await;
|
||||||
|
}
|
||||||
s2.store(true, Ordering::Relaxed);
|
s2.store(true, Ordering::Relaxed);
|
||||||
for h in bg_handles { let _ = h.await; }
|
for h in bg_handles {
|
||||||
|
let _ = h.await;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
(counter.load(Ordering::Relaxed), start.elapsed().as_micros())
|
(counter.load(Ordering::Relaxed), start.elapsed().as_micros())
|
||||||
}
|
}
|
||||||
@@ -184,9 +196,13 @@ fn bench_storm_tokio_multi() -> (u64, u128) {
|
|||||||
cc.fetch_add(1, Ordering::Relaxed);
|
cc.fetch_add(1, Ordering::Relaxed);
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
for h in handles { let _ = h.await; }
|
for h in handles {
|
||||||
|
let _ = h.await;
|
||||||
|
}
|
||||||
s2.store(true, Ordering::Relaxed);
|
s2.store(true, Ordering::Relaxed);
|
||||||
for h in bg_handles { let _ = h.await; }
|
for h in bg_handles {
|
||||||
|
let _ = h.await;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
(counter.load(Ordering::Relaxed), start.elapsed().as_micros())
|
(counter.load(Ordering::Relaxed), start.elapsed().as_micros())
|
||||||
}
|
}
|
||||||
@@ -219,14 +235,21 @@ fn bench_mpsc_smarm(threads: usize) -> (u64, u128) {
|
|||||||
}
|
}
|
||||||
let _ = count; // discard; run() closure must return ()
|
let _ = count; // discard; run() closure must return ()
|
||||||
});
|
});
|
||||||
for h in prod_handles { h.join().unwrap(); }
|
for h in prod_handles {
|
||||||
|
h.join().unwrap();
|
||||||
|
}
|
||||||
let _ = consumer.join().unwrap();
|
let _ = consumer.join().unwrap();
|
||||||
});
|
});
|
||||||
(MPSC_PRODUCERS * MPSC_PER_PRODUCER, start.elapsed().as_micros())
|
(
|
||||||
|
MPSC_PRODUCERS * MPSC_PER_PRODUCER,
|
||||||
|
start.elapsed().as_micros(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn bench_mpsc_tokio_current() -> (u64, u128) {
|
fn bench_mpsc_tokio_current() -> (u64, u128) {
|
||||||
let rt = tokio::runtime::Builder::new_current_thread().build().unwrap();
|
let rt = tokio::runtime::Builder::new_current_thread()
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
let local = tokio::task::LocalSet::new();
|
let local = tokio::task::LocalSet::new();
|
||||||
local.block_on(&rt, async move {
|
local.block_on(&rt, async move {
|
||||||
@@ -248,10 +271,15 @@ fn bench_mpsc_tokio_current() -> (u64, u128) {
|
|||||||
}
|
}
|
||||||
count
|
count
|
||||||
});
|
});
|
||||||
for h in prod_handles { let _ = h.await; }
|
for h in prod_handles {
|
||||||
|
let _ = h.await;
|
||||||
|
}
|
||||||
let _ = consumer.await;
|
let _ = consumer.await;
|
||||||
});
|
});
|
||||||
(MPSC_PRODUCERS * MPSC_PER_PRODUCER, start.elapsed().as_micros())
|
(
|
||||||
|
MPSC_PRODUCERS * MPSC_PER_PRODUCER,
|
||||||
|
start.elapsed().as_micros(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn bench_mpsc_tokio_multi() -> (u64, u128) {
|
fn bench_mpsc_tokio_multi() -> (u64, u128) {
|
||||||
@@ -279,10 +307,15 @@ fn bench_mpsc_tokio_multi() -> (u64, u128) {
|
|||||||
}
|
}
|
||||||
count
|
count
|
||||||
});
|
});
|
||||||
for h in prod_handles { let _ = h.await; }
|
for h in prod_handles {
|
||||||
|
let _ = h.await;
|
||||||
|
}
|
||||||
let _ = consumer.await;
|
let _ = consumer.await;
|
||||||
});
|
});
|
||||||
(MPSC_PRODUCERS * MPSC_PER_PRODUCER, start.elapsed().as_micros())
|
(
|
||||||
|
MPSC_PRODUCERS * MPSC_PER_PRODUCER,
|
||||||
|
start.elapsed().as_micros(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
@@ -308,7 +341,9 @@ fn bench_timers_smarm(threads: usize) -> (u64, u128) {
|
|||||||
smarm::sleep(Duration::from_millis(ms));
|
smarm::sleep(Duration::from_millis(ms));
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
for h in handles { h.join().unwrap(); }
|
for h in handles {
|
||||||
|
h.join().unwrap();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
(TIMER_ACTORS, start.elapsed().as_micros())
|
(TIMER_ACTORS, start.elapsed().as_micros())
|
||||||
}
|
}
|
||||||
@@ -328,7 +363,9 @@ fn bench_timers_tokio_current() -> (u64, u128) {
|
|||||||
tokio::time::sleep(Duration::from_millis(ms)).await;
|
tokio::time::sleep(Duration::from_millis(ms)).await;
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
for h in handles { let _ = h.await; }
|
for h in handles {
|
||||||
|
let _ = h.await;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
(TIMER_ACTORS, start.elapsed().as_micros())
|
(TIMER_ACTORS, start.elapsed().as_micros())
|
||||||
}
|
}
|
||||||
@@ -348,7 +385,9 @@ fn bench_timers_tokio_multi() -> (u64, u128) {
|
|||||||
tokio::time::sleep(Duration::from_millis(ms)).await;
|
tokio::time::sleep(Duration::from_millis(ms)).await;
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
for h in handles { let _ = h.await; }
|
for h in handles {
|
||||||
|
let _ = h.await;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
(TIMER_ACTORS, start.elapsed().as_micros())
|
(TIMER_ACTORS, start.elapsed().as_micros())
|
||||||
}
|
}
|
||||||
@@ -361,11 +400,22 @@ const SCALING_N: u64 = 400_000;
|
|||||||
const SCALING_WORKERS: u64 = 64;
|
const SCALING_WORKERS: u64 = 64;
|
||||||
|
|
||||||
fn is_prime(n: u64) -> bool {
|
fn is_prime(n: u64) -> bool {
|
||||||
if n < 2 { return false; }
|
if n < 2 {
|
||||||
if n < 4 { return true; }
|
return false;
|
||||||
if n % 2 == 0 { return false; }
|
}
|
||||||
|
if n < 4 {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if n % 2 == 0 {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
let mut i = 3u64;
|
let mut i = 3u64;
|
||||||
while i * i <= n { if n % i == 0 { return false; } i += 2; }
|
while i * i <= n {
|
||||||
|
if n % i == 0 {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
i += 2;
|
||||||
|
}
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -376,7 +426,11 @@ fn count_primes(lo: u64, hi: u64) -> u64 {
|
|||||||
fn scaling_slice(w: u64) -> (u64, u64) {
|
fn scaling_slice(w: u64) -> (u64, u64) {
|
||||||
let per = SCALING_N / SCALING_WORKERS;
|
let per = SCALING_N / SCALING_WORKERS;
|
||||||
let lo = w * per;
|
let lo = w * per;
|
||||||
let hi = if w + 1 == SCALING_WORKERS { SCALING_N } else { lo + per };
|
let hi = if w + 1 == SCALING_WORKERS {
|
||||||
|
SCALING_N
|
||||||
|
} else {
|
||||||
|
lo + per
|
||||||
|
};
|
||||||
(lo, hi)
|
(lo, hi)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -393,7 +447,9 @@ fn bench_scaling_smarm(threads: usize) -> (u64, u128) {
|
|||||||
tc.fetch_add(count_primes(lo, hi), Ordering::Relaxed);
|
tc.fetch_add(count_primes(lo, hi), Ordering::Relaxed);
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
for h in handles { h.join().unwrap(); }
|
for h in handles {
|
||||||
|
h.join().unwrap();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
(total.load(Ordering::Relaxed), start.elapsed().as_micros())
|
(total.load(Ordering::Relaxed), start.elapsed().as_micros())
|
||||||
}
|
}
|
||||||
@@ -415,7 +471,9 @@ fn bench_scaling_tokio_multi(threads: usize) -> (u64, u128) {
|
|||||||
tc.fetch_add(count_primes(lo, hi), Ordering::Relaxed);
|
tc.fetch_add(count_primes(lo, hi), Ordering::Relaxed);
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
for h in handles { let _ = h.await; }
|
for h in handles {
|
||||||
|
let _ = h.await;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
(total.load(Ordering::Relaxed), start.elapsed().as_micros())
|
(total.load(Ordering::Relaxed), start.elapsed().as_micros())
|
||||||
}
|
}
|
||||||
@@ -424,7 +482,6 @@ fn bench_scaling_tokio_multi(threads: usize) -> (u64, u128) {
|
|||||||
// main
|
// main
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// Knob helper — reads SMARM_ALLOC_INTERVAL / SMARM_TIMESLICE_CYCLES env vars
|
// Knob helper — reads SMARM_ALLOC_INTERVAL / SMARM_TIMESLICE_CYCLES env vars
|
||||||
// so the sweep script can override the preemption knobs without recompiling.
|
// so the sweep script can override the preemption knobs without recompiling.
|
||||||
@@ -433,10 +490,14 @@ fn bench_scaling_tokio_multi(threads: usize) -> (u64, u128) {
|
|||||||
fn bench_cfg(threads: usize) -> smarm::runtime::Config {
|
fn bench_cfg(threads: usize) -> smarm::runtime::Config {
|
||||||
let mut cfg = smarm::runtime::Config::exact(threads);
|
let mut cfg = smarm::runtime::Config::exact(threads);
|
||||||
if let Ok(v) = std::env::var("SMARM_ALLOC_INTERVAL") {
|
if let Ok(v) = std::env::var("SMARM_ALLOC_INTERVAL") {
|
||||||
if let Ok(n) = v.parse::<u32>() { cfg = cfg.alloc_interval(n); }
|
if let Ok(n) = v.parse::<u32>() {
|
||||||
|
cfg = cfg.alloc_interval(n);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if let Ok(v) = std::env::var("SMARM_TIMESLICE_CYCLES") {
|
if let Ok(v) = std::env::var("SMARM_TIMESLICE_CYCLES") {
|
||||||
if let Ok(n) = v.parse::<u64>() { cfg = cfg.timeslice_cycles(n); }
|
if let Ok(n) = v.parse::<u64>() {
|
||||||
|
cfg = cfg.timeslice_cycles(n);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
cfg
|
cfg
|
||||||
}
|
}
|
||||||
@@ -446,7 +507,10 @@ fn main() {
|
|||||||
println!("smarm tokio-favored benchmarks");
|
println!("smarm tokio-favored benchmarks");
|
||||||
println!("available parallelism: {n} threads");
|
println!("available parallelism: {n} threads");
|
||||||
let sets = env_sets();
|
let sets = env_sets();
|
||||||
println!("ITERS={ITERS}×{sets} sets = {} samples (+1 warmup, discarded)", ITERS * sets);
|
println!(
|
||||||
|
"ITERS={ITERS}×{sets} sets = {} samples (+1 warmup, discarded)",
|
||||||
|
ITERS * sets
|
||||||
|
);
|
||||||
println!(
|
println!(
|
||||||
"STORM_BACKGROUND={STORM_BACKGROUND}, STORM_SPAWN={STORM_SPAWN}, \
|
"STORM_BACKGROUND={STORM_BACKGROUND}, STORM_SPAWN={STORM_SPAWN}, \
|
||||||
MPSC={MPSC_PRODUCERS}×{MPSC_PER_PRODUCER}, \
|
MPSC={MPSC_PRODUCERS}×{MPSC_PER_PRODUCER}, \
|
||||||
@@ -477,7 +541,9 @@ fn main() {
|
|||||||
"many_timers: {TIMER_ACTORS} actors sleeping {TIMER_MIN_MS}–{TIMER_MAX_MS} ms"
|
"many_timers: {TIMER_ACTORS} actors sleeping {TIMER_MIN_MS}–{TIMER_MAX_MS} ms"
|
||||||
));
|
));
|
||||||
run_n("smarm 1-thread", ITERS, || bench_timers_smarm(1));
|
run_n("smarm 1-thread", ITERS, || bench_timers_smarm(1));
|
||||||
run_n(&format!("smarm {n}-thread"), ITERS, || bench_timers_smarm(n));
|
run_n(&format!("smarm {n}-thread"), ITERS, || {
|
||||||
|
bench_timers_smarm(n)
|
||||||
|
});
|
||||||
run_n("tokio current_thread", ITERS, bench_timers_tokio_current);
|
run_n("tokio current_thread", ITERS, bench_timers_tokio_current);
|
||||||
run_n("tokio multi-thread", ITERS, bench_timers_tokio_multi);
|
run_n("tokio multi-thread", ITERS, bench_timers_tokio_multi);
|
||||||
|
|
||||||
@@ -487,13 +553,19 @@ fn main() {
|
|||||||
));
|
));
|
||||||
let sweep: Vec<usize> = {
|
let sweep: Vec<usize> = {
|
||||||
let mut v = vec![1usize, 2, 4];
|
let mut v = vec![1usize, 2, 4];
|
||||||
if n > 4 && !v.contains(&n) { v.push(n); }
|
if n > 4 && !v.contains(&n) {
|
||||||
|
v.push(n);
|
||||||
|
}
|
||||||
v.into_iter().filter(|t| *t <= n).collect()
|
v.into_iter().filter(|t| *t <= n).collect()
|
||||||
};
|
};
|
||||||
for t in &sweep {
|
for t in &sweep {
|
||||||
run_n(&format!("smarm {t}-thread"), ITERS, || bench_scaling_smarm(*t));
|
run_n(&format!("smarm {t}-thread"), ITERS, || {
|
||||||
|
bench_scaling_smarm(*t)
|
||||||
|
});
|
||||||
}
|
}
|
||||||
for t in &sweep {
|
for t in &sweep {
|
||||||
run_n(&format!("tokio multi {t}-thread"), ITERS, || bench_scaling_tokio_multi(*t));
|
run_n(&format!("tokio multi {t}-thread"), ITERS, || {
|
||||||
|
bench_scaling_tokio_multi(*t)
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
fn main() {
|
||||||
|
// RFC 019 §7 test canary (agreed Q3): compiled without stack-clash
|
||||||
|
// protection so its 96 KiB local is a genuine one-displacement guard
|
||||||
|
// jumper; distro-hardened compilers would otherwise probe it page-wise
|
||||||
|
// and defeat the test's purpose.
|
||||||
|
cc::Build::new()
|
||||||
|
.file("canary/canary.c")
|
||||||
|
.flag_if_supported("-fno-stack-clash-protection")
|
||||||
|
.compile("smarm_canary");
|
||||||
|
println!("cargo:rerun-if-changed=canary/canary.c");
|
||||||
|
|
||||||
|
// RFC 010 c6d — build_hash inputs. The compile-time facts a peer must
|
||||||
|
// share for a mesh link: the exact toolchain and the declared (enabled)
|
||||||
|
// feature set. Emitted as a plain string; the hashing (FNV-1a folded
|
||||||
|
// with PROTO_VERSION) happens in src/cluster.rs where the protocol
|
||||||
|
// version actually lives — parsing it out of a source file here would
|
||||||
|
// be a second, fragile copy. Always emitted, even for non-cluster
|
||||||
|
// builds: one env var costs the default build nothing.
|
||||||
|
let rustc = std::env::var("RUSTC").unwrap_or_else(|_| "rustc".to_string());
|
||||||
|
let version = std::process::Command::new(&rustc)
|
||||||
|
.arg("-V")
|
||||||
|
.output()
|
||||||
|
.ok()
|
||||||
|
.map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string())
|
||||||
|
.filter(|v| !v.is_empty())
|
||||||
|
.unwrap_or_else(|| "rustc-unknown".to_string());
|
||||||
|
let mut feats: Vec<String> = std::env::vars()
|
||||||
|
.filter_map(|(k, _)| k.strip_prefix("CARGO_FEATURE_").map(str::to_string))
|
||||||
|
.collect();
|
||||||
|
feats.sort();
|
||||||
|
println!(
|
||||||
|
"cargo:rustc-env=SMARM_BUILD_HASH_INPUTS={version};features={}",
|
||||||
|
feats.join(",")
|
||||||
|
);
|
||||||
|
println!("cargo:rerun-if-env-changed=RUSTC");
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
/* RFC 019 §7 FFI canary: an honest unprobed C frame with a 96 KiB local,
|
||||||
|
* touched from its LOW end first — the exact "one sub rsp steps over a small
|
||||||
|
* guard" pattern the RFC's motivating incident hit (a cargo-vendored gz
|
||||||
|
* build; cc-invoked builds do not enable -fstack-clash-protection, and this
|
||||||
|
* file pins that off explicitly so the canary stays a canary even on
|
||||||
|
* hardened-default toolchains). */
|
||||||
|
void smarm_canary_burn(void) {
|
||||||
|
volatile char buf[96 * 1024];
|
||||||
|
buf[0] = 1; /* deepest address first */
|
||||||
|
for (unsigned i = 0; i < sizeof buf; i += 4096) {
|
||||||
|
buf[i] = (char)i;
|
||||||
|
}
|
||||||
|
buf[sizeof buf - 1] = 1;
|
||||||
|
}
|
||||||
@@ -75,7 +75,7 @@ genuine advantage over tokio's task abort model.
|
|||||||
|
|
||||||
### Spawn-heavy workloads (19–70×)
|
### Spawn-heavy workloads (19–70×)
|
||||||
|
|
||||||
Every smarm actor `mmap`s a 64 KiB stack with a guard page. This is
|
Every smarm actor `mmap`s a 64 KiB stack reserve with a 64 KiB PROT_NONE guard below (both per-actor configurable since RFC 019; the reserve is demand-paged). This is
|
||||||
a syscall. Tokio tasks are heap-allocated state machines — no stack,
|
a syscall. Tokio tasks are heap-allocated state machines — no stack,
|
||||||
no syscall, ~100 bytes each. For workloads that spawn thousands of
|
no syscall, ~100 bytes each. For workloads that spawn thousands of
|
||||||
short-lived actors per second, this is a structural disadvantage.
|
short-lived actors per second, this is a structural disadvantage.
|
||||||
|
|||||||
@@ -1620,8 +1620,9 @@
|
|||||||
wait: <code>select</code> priority is <strong>Down arms › Watcher arm › info channels (declaration order) ›
|
wait: <code>select</code> priority is <strong>Down arms › Watcher arm › info channels (declaration order) ›
|
||||||
inbox</strong>, rebuilt each turn. A hot inbox can't starve a death notice or a system message;
|
inbox</strong>, rebuilt each turn. A hot inbox can't starve a death notice or a system message;
|
||||||
conversely a hot info channel <em>can</em> starve the inbox — deliberately. A closed info arm is
|
conversely a hot info channel <em>can</em> starve the inbox — deliberately. A closed info arm is
|
||||||
silently dropped from the set; a closed <em>inbox</em> (every <code>ServerRef</code> gone) is graceful
|
silently dropped from the set. The inbox never closes — the loop holds one sender for its whole
|
||||||
shutdown.</p>
|
life, so a <code>GenServerRef</code> is an address, not an owner: the server ends only by
|
||||||
|
<code>StopHandle::stop</code>, a shutdown, a hard stop, or a panic.</p>
|
||||||
|
|
||||||
<h3>Death needs no monitor</h3>
|
<h3>Death needs no monitor</h3>
|
||||||
<p>Server death detection falls out of channel closure. Already dead → the inbox is closed and
|
<p>Server death detection falls out of channel closure. Already dead → the inbox is closed and
|
||||||
@@ -1700,7 +1701,7 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="module-card">
|
<div class="module-card">
|
||||||
<div class="module-name" style="color:var(--red)">Panics in <code>terminate()</code></div>
|
<div class="module-name" style="color:var(--red)">Panics in <code>terminate()</code></div>
|
||||||
<p>gen_server's <code>terminate()</code> runs from a drop guard, possibly mid-unwind. A panic inside it during an unwind is a double panic → process abort, no supervision tree to save you. Keep it cheap, non-blocking, non-panicking.</p>
|
<p>gen_server's <code>terminate()</code> runs from a drop guard, possibly mid-unwind. A panic inside it during an unwind is a double panic → process abort, no supervision tree to save you. On the panic and hard-stop paths keep it cheap, non-blocking, non-panicking. Only the graceful path (<code>handle_shutdown → Exit</code>, <code>StopHandle::stop</code>) runs it outside an unwind, where it may do real work.</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="module-card">
|
<div class="module-card">
|
||||||
<div class="module-name" style="color:var(--yellow)">Cold locks are leaf locks</div>
|
<div class="module-name" style="color:var(--yellow)">Cold locks are leaf locks</div>
|
||||||
|
|||||||
@@ -0,0 +1,200 @@
|
|||||||
|
//! Attribution-efficiency probe (RFC 007 follow-up).
|
||||||
|
//!
|
||||||
|
//! Original hypothesis: the ~4pt impact shortfall on the 24-core
|
||||||
|
//! validation (+29.3/+83.5 vs theoretical +33/+100) is a constant
|
||||||
|
//! attribution efficiency eff ≈ 0.91 from site-exit tail truncation.
|
||||||
|
//! The guard-drop flush closed that leak, yet eff held at ~0.93 —
|
||||||
|
//! RESOLVED (2026-07-13 sweep): the residual is runnable off-CPU time
|
||||||
|
//! inside the site (~4.9 slice-expiry yields/entry x ~5.6µs runqueue
|
||||||
|
//! wait), wall time the ground truth below counts but on-CPU
|
||||||
|
//! attribution correctly skips. The offcpu audit bucket now counts it;
|
||||||
|
//! `eff+offcpu` printed per window should sit at ~1.00 — the
|
||||||
|
//! closed-books check.
|
||||||
|
//!
|
||||||
|
//! Measurement: same pipeline as `causal_pipeline`, but the `reserve`
|
||||||
|
//! actor also measures its raw in-site time directly (rdtsc at guard
|
||||||
|
//! enter/exit) and counts site entries. For each experiment window at
|
||||||
|
//! pct%:
|
||||||
|
//!
|
||||||
|
//! eff = (Δglobal_delay / (pct/100)) / Δin_site_cycles
|
||||||
|
//!
|
||||||
|
//! and the missing time per site entry localizes the leak:
|
||||||
|
//!
|
||||||
|
//! tail_us/entry = (Δin_site − Δglobal_delay/(pct/100)) / Δentries
|
||||||
|
//!
|
||||||
|
//! A constant eff across 25/50% with tail/entry in the tens of µs
|
||||||
|
//! localizes a per-entry mechanism; `eff+offcpu` ≈ 1.00 confirms the
|
||||||
|
//! runnable-gap account and rules out any remaining silent loss.
|
||||||
|
//!
|
||||||
|
//! Run: cargo run --release --example causal_attrib_probe --features smarm-causal
|
||||||
|
|
||||||
|
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
|
||||||
|
use std::sync::Arc;
|
||||||
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
|
fn rdtsc() -> u64 {
|
||||||
|
// x86_64 only — same clock the ledger uses.
|
||||||
|
unsafe { core::arch::x86_64::_rdtsc() }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Same fixed-work loop as causal_pipeline (dependent LCG, preemptible).
|
||||||
|
fn work_iters(iters: u64) {
|
||||||
|
let mut acc = 0x2545_f491_4f6c_dd1du64;
|
||||||
|
let mut i = 0u64;
|
||||||
|
while i < iters {
|
||||||
|
let chunk_end = (i + 256).min(iters);
|
||||||
|
while i < chunk_end {
|
||||||
|
acc = acc.wrapping_mul(6364136223846793005).wrapping_add(i);
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
std::hint::black_box(acc);
|
||||||
|
smarm::check!();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn calibrate_iters_per_us() -> u64 {
|
||||||
|
let n = 8_000_000u64;
|
||||||
|
let t = Instant::now();
|
||||||
|
work_iters(n);
|
||||||
|
(n / (t.elapsed().as_micros().max(1) as u64)).max(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
static IN_SITE_CYCLES: AtomicU64 = AtomicU64::new(0);
|
||||||
|
static SITE_ENTRIES: AtomicU64 = AtomicU64::new(0);
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let per_us = calibrate_iters_per_us();
|
||||||
|
println!("calibration: {per_us} work iters/µs");
|
||||||
|
let work_us = move |us: u64| work_iters(us * per_us);
|
||||||
|
|
||||||
|
let cores = std::thread::available_parallelism()
|
||||||
|
.map(|n| n.get())
|
||||||
|
.unwrap_or(1);
|
||||||
|
println!("cores: {cores}");
|
||||||
|
if cores < 4 {
|
||||||
|
println!("probe: SKIPPED (needs the stages in parallel)");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
smarm::init(smarm::Config::default()).run(move || {
|
||||||
|
let stop = Arc::new(AtomicBool::new(false));
|
||||||
|
let (tx_ab, rx_ab) = smarm::channel::<u64>();
|
||||||
|
let (tx_bc, rx_bc) = smarm::channel::<u64>();
|
||||||
|
|
||||||
|
let stop_p = stop.clone();
|
||||||
|
let producer = smarm::spawn(move || {
|
||||||
|
let mut i = 0u64;
|
||||||
|
while !stop_p.load(Ordering::Relaxed) {
|
||||||
|
{
|
||||||
|
let _g = smarm::causal_site!("serialize");
|
||||||
|
work_us(200);
|
||||||
|
}
|
||||||
|
if tx_ab.send(i).is_err() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Reserve: the target — instrumented with ground-truth in-site time.
|
||||||
|
let reserve = smarm::spawn(move || {
|
||||||
|
while let Ok(item) = rx_ab.recv() {
|
||||||
|
{
|
||||||
|
let t0 = rdtsc();
|
||||||
|
let _g = smarm::causal_site!("reserve");
|
||||||
|
work_us(400);
|
||||||
|
// Measured before guard drop: exactly the span the
|
||||||
|
// ledger should be attributing.
|
||||||
|
IN_SITE_CYCLES.fetch_add(rdtsc().saturating_sub(t0), Ordering::Relaxed);
|
||||||
|
SITE_ENTRIES.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
if tx_bc.send(item).is_err() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let notify = smarm::spawn(move || {
|
||||||
|
while rx_bc.recv().is_ok() {
|
||||||
|
{
|
||||||
|
let _g = smarm::causal_site!("notify");
|
||||||
|
work_us(50);
|
||||||
|
}
|
||||||
|
smarm::progress!("orders-processed");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let stop_bg = stop.clone();
|
||||||
|
let background = smarm::spawn(move || {
|
||||||
|
while !stop_bg.load(Ordering::Relaxed) {
|
||||||
|
let _g = smarm::causal_site!("background-compaction");
|
||||||
|
work_us(500);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
smarm::sleep(Duration::from_millis(300));
|
||||||
|
|
||||||
|
let hz = smarm::causal::tsc_hz();
|
||||||
|
println!("tsc_hz: {:.3} GHz", hz / 1e9);
|
||||||
|
|
||||||
|
// Manual windows so ledger/ground-truth snapshots align exactly.
|
||||||
|
for &pct in &[25u32, 50, 50, 25] {
|
||||||
|
let g0 = smarm::causal::global_delay_cycles();
|
||||||
|
let s0 = IN_SITE_CYCLES.load(Ordering::Relaxed);
|
||||||
|
let e0 = SITE_ENTRIES.load(Ordering::Relaxed);
|
||||||
|
let a0 = smarm::causal::ledger_counters();
|
||||||
|
smarm::causal::begin_experiment_for_test("reserve", pct);
|
||||||
|
smarm::sleep(Duration::from_millis(1000));
|
||||||
|
smarm::causal::end_experiment_for_test();
|
||||||
|
let audit = smarm::causal::ledger_counters().delta_since(&a0);
|
||||||
|
let injected = smarm::causal::global_delay_cycles() - g0;
|
||||||
|
let in_site = IN_SITE_CYCLES.load(Ordering::Relaxed) - s0;
|
||||||
|
let entries = SITE_ENTRIES.load(Ordering::Relaxed) - e0;
|
||||||
|
|
||||||
|
let attributed = injected as f64 / (pct as f64 / 100.0);
|
||||||
|
let eff = attributed / in_site as f64;
|
||||||
|
// Books-closure check: add back the runnable off-CPU gaps the
|
||||||
|
// audit counted (delta terms -> raw via /pct) — should be ~1.00.
|
||||||
|
let eff_closed = (injected as f64 + audit.offcpu_in_site_cycles as f64)
|
||||||
|
/ (pct as f64 / 100.0)
|
||||||
|
/ in_site as f64;
|
||||||
|
let missing = in_site as f64 - attributed;
|
||||||
|
let tail_us = if entries > 0 {
|
||||||
|
missing / entries as f64 / hz * 1e6
|
||||||
|
} else {
|
||||||
|
f64::NAN
|
||||||
|
};
|
||||||
|
println!(
|
||||||
|
"pct {pct:>2}% in_site {:>8.1}ms attributed {:>8.1}ms eff {eff:.3} eff+offcpu {eff_closed:.3} entries {entries} missing/entry {tail_us:.1}µs",
|
||||||
|
in_site as f64 / hz * 1e3,
|
||||||
|
attributed / hz * 1e3,
|
||||||
|
);
|
||||||
|
// RFC 007 deficit hunt: name the losses. Drop/discard columns are
|
||||||
|
// in would-be delta terms — divide by pct/100 to compare with the
|
||||||
|
// missing attribution above.
|
||||||
|
let ms = |c: u64| c as f64 / hz * 1e3;
|
||||||
|
println!(
|
||||||
|
" audit: absorbed {:>7.1}ms forgiven {:>6.1}ms drop park {:>5.2}ms/{:<5} yield {:>5.2}ms/{:<5} offcpu {:>6.2}ms/{:<5} discard >max {:>5.2}ms/{:<3} unarmed {}",
|
||||||
|
ms(audit.spin_absorbed_cycles),
|
||||||
|
ms(audit.park_forgiven_cycles),
|
||||||
|
ms(audit.drop_park_cycles),
|
||||||
|
audit.drop_park_n,
|
||||||
|
ms(audit.drop_yield_cycles),
|
||||||
|
audit.drop_yield_n,
|
||||||
|
ms(audit.offcpu_in_site_cycles),
|
||||||
|
audit.offcpu_in_site_n,
|
||||||
|
ms(audit.discard_overmax_cycles),
|
||||||
|
audit.discard_overmax_n,
|
||||||
|
audit.discard_unarmed_n
|
||||||
|
);
|
||||||
|
smarm::sleep(Duration::from_millis(150));
|
||||||
|
}
|
||||||
|
|
||||||
|
stop.store(true, Ordering::Relaxed);
|
||||||
|
producer.join().unwrap();
|
||||||
|
reserve.join().unwrap();
|
||||||
|
notify.join().unwrap();
|
||||||
|
background.join().unwrap();
|
||||||
|
println!("probe: DONE");
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -0,0 +1,296 @@
|
|||||||
|
//! Causal-profiling demo (RFC 007): a pipeline where conventional profiling
|
||||||
|
//! lies and causal profiling doesn't.
|
||||||
|
//!
|
||||||
|
//! producer --(serialize ~200µs/item)--> reserve --(~400µs/item)--> notify
|
||||||
|
//! background: an actor burning CPU constantly, fully off the critical path
|
||||||
|
//!
|
||||||
|
//! `reserve` is the true bottleneck. `serialize` is hot but overlapped with
|
||||||
|
//! `reserve`'s backlog, and `background` is the hottest code in the process
|
||||||
|
//! while contributing nothing to throughput. A cycle profiler ranks them
|
||||||
|
//! background > reserve ≈ 2×serialize; the causal report instead shows
|
||||||
|
//! throughput responding to virtual speedups of `reserve` and (near-)ignoring
|
||||||
|
//! `serialize` and `background`.
|
||||||
|
//!
|
||||||
|
//! Stage cost is fixed *work* (a calibrated arithmetic loop), not fixed wall
|
||||||
|
//! time. This matters: a timed busy-wait absorbs injected causal delay into
|
||||||
|
//! its own budget and finishes on schedule regardless, making every
|
||||||
|
//! experiment read as a no-op (found live on a 24-core run: dead-flat
|
||||||
|
//! deltas). Real workloads are work-shaped, so the demo must be too.
|
||||||
|
//!
|
||||||
|
//! Run:
|
||||||
|
//! cargo run --release --example causal_pipeline --features smarm-causal
|
||||||
|
//!
|
||||||
|
//! Modes (`SMARM_CAUSAL_MODE`), for probing what the guard placement leaves
|
||||||
|
//! out of the measurement (a site speeds up only what it wraps; `recv`/`send`
|
||||||
|
//! on the serialized stage sit outside the canonical guard):
|
||||||
|
//! work (default) — guard wraps only the 400µs of work.
|
||||||
|
//! wide — guard widened over recv + work + send, the whole
|
||||||
|
//! serialized per-item path.
|
||||||
|
//! occupancy — no experiments; times each segment of reserve's loop
|
||||||
|
//! at baseline and reports the unguarded per-item
|
||||||
|
//! overhead δ plus the impact ceiling it implies.
|
||||||
|
//!
|
||||||
|
//! Result (24-core run, 2026-07-13, job f9305cbb): δ measured 0.3µs/item —
|
||||||
|
//! 0.1% of the serialized path — and `wide` does not move the @50% cell
|
||||||
|
//! (+83.5/+86.3 vs work's +81.5/+86.7). This demo's +84-vs-+100 @50%
|
||||||
|
//! shortfall is therefore NOT unguarded stage time; it is controller-side:
|
||||||
|
//! injected delay reaches ~327ms of the ideal 350ms over the 700ms window,
|
||||||
|
//! plus a ~3% real-throughput dip while experiments run. Contrast urus's
|
||||||
|
//! causal_bench, where the same arithmetic identified a real ~70µs/request
|
||||||
|
//! unguarded remainder (recv/reply outside the store guard). Sites measure
|
||||||
|
//! what they wrap — and the occupancy probe tells you which case you're in.
|
||||||
|
//!
|
||||||
|
//! Prints a summary, writes `profile.coz` (Coz plot-compatible), and — given
|
||||||
|
//! enough cores for the pipeline to actually run in parallel — checks the
|
||||||
|
//! expected separation and exits nonzero if it doesn't hold, so a CI box can
|
||||||
|
//! run this as a smoke test.
|
||||||
|
|
||||||
|
use std::sync::atomic::{AtomicBool, Ordering};
|
||||||
|
use std::sync::Arc;
|
||||||
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
|
/// LCG-mix `iters` times in dependent sequence (unvectorizable, un-elidable),
|
||||||
|
/// staying preemptible — and causal-sampleable/delayable — via `check!()`.
|
||||||
|
fn work_iters(iters: u64) {
|
||||||
|
let mut acc = 0x2545_f491_4f6c_dd1du64;
|
||||||
|
let mut i = 0u64;
|
||||||
|
while i < iters {
|
||||||
|
let chunk_end = (i + 256).min(iters);
|
||||||
|
while i < chunk_end {
|
||||||
|
acc = acc.wrapping_mul(6364136223846793005).wrapping_add(i);
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
std::hint::black_box(acc);
|
||||||
|
smarm::check!();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Measure how many `work_iters` iterations fit in a microsecond on this
|
||||||
|
/// machine, so stage costs below are meaningful in time while staying
|
||||||
|
/// work-shaped.
|
||||||
|
fn calibrate_iters_per_us() -> u64 {
|
||||||
|
let n = 8_000_000u64;
|
||||||
|
let t = Instant::now();
|
||||||
|
work_iters(n);
|
||||||
|
(n / (t.elapsed().as_micros().max(1) as u64)).max(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Guard placement for the `reserve` stage — see module doc.
|
||||||
|
#[derive(Clone, Copy, PartialEq)]
|
||||||
|
enum Mode {
|
||||||
|
Work,
|
||||||
|
Wide,
|
||||||
|
Occupancy,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mode = match std::env::var("SMARM_CAUSAL_MODE").as_deref() {
|
||||||
|
Err(_) | Ok("") | Ok("work") => Mode::Work,
|
||||||
|
Ok("wide") => Mode::Wide,
|
||||||
|
Ok("occupancy") => Mode::Occupancy,
|
||||||
|
Ok(other) => {
|
||||||
|
eprintln!("unknown SMARM_CAUSAL_MODE {other:?} (work|wide|occupancy)");
|
||||||
|
std::process::exit(2);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
println!(
|
||||||
|
"mode: {}",
|
||||||
|
match mode {
|
||||||
|
Mode::Work => "work",
|
||||||
|
Mode::Wide => "wide",
|
||||||
|
Mode::Occupancy => "occupancy",
|
||||||
|
}
|
||||||
|
);
|
||||||
|
let per_us = calibrate_iters_per_us();
|
||||||
|
println!("calibration: {per_us} work iters/µs");
|
||||||
|
let work_us = move |us: u64| work_iters(us * per_us);
|
||||||
|
|
||||||
|
let mut failures: Vec<String> = Vec::new();
|
||||||
|
|
||||||
|
smarm::init(smarm::Config::default()).run(move || {
|
||||||
|
let stop = Arc::new(AtomicBool::new(false));
|
||||||
|
|
||||||
|
let (tx_ab, rx_ab) = smarm::channel::<u64>();
|
||||||
|
let (tx_bc, rx_bc) = smarm::channel::<u64>();
|
||||||
|
|
||||||
|
// Producer: hot serialization, but upstream of the bottleneck.
|
||||||
|
let stop_p = stop.clone();
|
||||||
|
let producer = smarm::spawn(move || {
|
||||||
|
let mut i = 0u64;
|
||||||
|
while !stop_p.load(Ordering::Relaxed) {
|
||||||
|
{
|
||||||
|
let _g = smarm::causal_site!("serialize");
|
||||||
|
work_us(200);
|
||||||
|
}
|
||||||
|
if tx_ab.send(i).is_err() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
// tx_ab drops here; downstream drains and exits.
|
||||||
|
});
|
||||||
|
|
||||||
|
// Reserve: the true bottleneck (~400µs of work per item).
|
||||||
|
let reserve = smarm::spawn(move || match mode {
|
||||||
|
Mode::Work => {
|
||||||
|
while let Ok(item) = rx_ab.recv() {
|
||||||
|
{
|
||||||
|
let _g = smarm::causal_site!("reserve");
|
||||||
|
work_us(400);
|
||||||
|
}
|
||||||
|
if tx_bc.send(item).is_err() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Whole serialized per-item path under the guard: a virtual
|
||||||
|
// speedup now also compresses recv/send, so the @50% cell should
|
||||||
|
// recover the theoretical 2× that `work` mode's placement caps.
|
||||||
|
Mode::Wide => loop {
|
||||||
|
let _g = smarm::causal_site!("reserve");
|
||||||
|
let Ok(item) = rx_ab.recv() else { break };
|
||||||
|
work_us(400);
|
||||||
|
if tx_bc.send(item).is_err() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
// Time each segment at baseline; the recv+send remainder δ is
|
||||||
|
// the serialized time a `work`-placed guard cannot speed up.
|
||||||
|
Mode::Occupancy => {
|
||||||
|
let (mut recv_ns, mut work_ns, mut send_ns, mut n) = (0u64, 0u64, 0u64, 0u64);
|
||||||
|
loop {
|
||||||
|
let t0 = Instant::now();
|
||||||
|
let Ok(item) = rx_ab.recv() else { break };
|
||||||
|
let t1 = Instant::now();
|
||||||
|
{
|
||||||
|
let _g = smarm::causal_site!("reserve");
|
||||||
|
work_us(400);
|
||||||
|
}
|
||||||
|
let t2 = Instant::now();
|
||||||
|
if tx_bc.send(item).is_err() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
recv_ns += (t1 - t0).as_nanos() as u64;
|
||||||
|
work_ns += (t2 - t1).as_nanos() as u64;
|
||||||
|
send_ns += t2.elapsed().as_nanos() as u64;
|
||||||
|
n += 1;
|
||||||
|
}
|
||||||
|
let items = n.max(1) as f64;
|
||||||
|
let (r, w, s) = (
|
||||||
|
recv_ns as f64 / items / 1e3,
|
||||||
|
work_ns as f64 / items / 1e3,
|
||||||
|
send_ns as f64 / items / 1e3,
|
||||||
|
);
|
||||||
|
let delta = r + s;
|
||||||
|
let total = w + delta;
|
||||||
|
println!("occupancy: {n} items; per item recv {r:.1}µs + work(guarded) {w:.1}µs + send {s:.1}µs");
|
||||||
|
println!(
|
||||||
|
"occupancy: unguarded δ = {delta:.1}µs/item = {:.1}% of the serialized path",
|
||||||
|
100.0 * delta / total
|
||||||
|
);
|
||||||
|
for pct in [25u32, 50] {
|
||||||
|
let f = 1.0 - f64::from(pct) / 100.0;
|
||||||
|
println!(
|
||||||
|
"occupancy: predicted reserve impact @{pct}% -> {:+.1}% (ceiling if δ were guarded: {:+.1}%)",
|
||||||
|
100.0 * (total / (f * w + delta) - 1.0),
|
||||||
|
100.0 * (1.0 / f - 1.0)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Notify: light tail stage; marks the unit of useful work.
|
||||||
|
let notify = smarm::spawn(move || {
|
||||||
|
while rx_bc.recv().is_ok() {
|
||||||
|
{
|
||||||
|
let _g = smarm::causal_site!("notify");
|
||||||
|
work_us(50);
|
||||||
|
}
|
||||||
|
smarm::progress!("orders-processed");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Background: hottest code in the process, zero throughput relevance.
|
||||||
|
let stop_bg = stop.clone();
|
||||||
|
let background = smarm::spawn(move || {
|
||||||
|
while !stop_bg.load(Ordering::Relaxed) {
|
||||||
|
let _g = smarm::causal_site!("background-compaction");
|
||||||
|
work_us(500);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Warm up so queues reach steady state before measuring.
|
||||||
|
smarm::sleep(Duration::from_millis(300));
|
||||||
|
|
||||||
|
if mode == Mode::Occupancy {
|
||||||
|
// No experiments: hold steady state for a window, then drain and
|
||||||
|
// let the reserve actor print its segment report.
|
||||||
|
smarm::sleep(Duration::from_millis(1500));
|
||||||
|
stop.store(true, Ordering::Relaxed);
|
||||||
|
producer.join().unwrap();
|
||||||
|
reserve.join().unwrap();
|
||||||
|
notify.join().unwrap();
|
||||||
|
background.join().unwrap();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let results = smarm::causal::run_experiments(&smarm::causal::ExperimentPlan {
|
||||||
|
speedups_pct: vec![0, 25, 50],
|
||||||
|
experiment: Duration::from_millis(700),
|
||||||
|
cooldown: Duration::from_millis(150),
|
||||||
|
});
|
||||||
|
|
||||||
|
stop.store(true, Ordering::Relaxed);
|
||||||
|
producer.join().unwrap();
|
||||||
|
reserve.join().unwrap();
|
||||||
|
notify.join().unwrap();
|
||||||
|
background.join().unwrap();
|
||||||
|
|
||||||
|
print!("{}", smarm::causal::render_summary(&results));
|
||||||
|
// RFC 007 deficit hunt: SMARM_CAUSAL_AUDIT=1 appends the per-cell
|
||||||
|
// ledger audit (injected/absorbed/forgiven + drop and discard
|
||||||
|
// buckets) without touching the pinned summary format.
|
||||||
|
if std::env::var_os("SMARM_CAUSAL_AUDIT").is_some() {
|
||||||
|
print!("{}", smarm::causal::render_ledger_audit(&results));
|
||||||
|
}
|
||||||
|
let coz = smarm::causal::render_coz(&results);
|
||||||
|
match std::fs::write("profile.coz", coz) {
|
||||||
|
Ok(()) => println!("\nwrote profile.coz"),
|
||||||
|
Err(e) => eprintln!("\nfailed to write profile.coz: {e}"),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verdict. The separation only exists when the four pipeline actors
|
||||||
|
// actually run in parallel; on a small box, report and skip.
|
||||||
|
let cores = std::thread::available_parallelism().map(|n| n.get()).unwrap_or(1);
|
||||||
|
if cores < 4 {
|
||||||
|
println!("verdict: SKIPPED ({cores} cores; separation needs the stages in parallel)");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let impact = |site: &str| {
|
||||||
|
smarm::causal::impact_pct(&results, site, 25, "orders-processed")
|
||||||
|
};
|
||||||
|
let mut expect = |site: &str, ok: &dyn Fn(f64) -> bool, want: &str| match impact(site) {
|
||||||
|
Some(p) => {
|
||||||
|
let verdict = if ok(p) { "ok" } else { "FAIL" };
|
||||||
|
println!("verdict: {site} @25% -> {p:+.1}% (want {want}) {verdict}");
|
||||||
|
if !ok(p) {
|
||||||
|
failures.push(format!("{site}: {p:+.1}% (want {want})"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
println!("verdict: {site} @25% -> missing cell FAIL");
|
||||||
|
failures.push(format!("{site}: missing cell"));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
expect("reserve", &|p| p > 15.0, "> +15%");
|
||||||
|
expect("serialize", &|p| p < 10.0, "< +10%");
|
||||||
|
expect("background-compaction", &|p| p < 10.0, "< +10%");
|
||||||
|
|
||||||
|
if failures.is_empty() {
|
||||||
|
println!("verdict: PASS — causal separation holds");
|
||||||
|
} else {
|
||||||
|
println!("verdict: FAIL — {}", failures.join("; "));
|
||||||
|
std::process::exit(1);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -0,0 +1,145 @@
|
|||||||
|
//! Diagnostic probe for RFC 007 on a target box. Measures, in order:
|
||||||
|
//! 1. TSC frequency against `Instant` (the crate assumes 3 GHz).
|
||||||
|
//! 2. TSC sanity under actor migration: distribution of wall time actually
|
||||||
|
//! spent in `burn_us(400)` across many runs — a bimodal/short tail means
|
||||||
|
//! cross-core TSC offsets are cutting burns short.
|
||||||
|
//! 3. Pipeline stage rates with no experiment running (who is the real
|
||||||
|
//! bottleneck?).
|
||||||
|
//! 4. The same rates during a 50% experiment on `background-compaction`
|
||||||
|
//! (a correct implementation must slow every stage; an off-critical-path
|
||||||
|
//! target must reduce end-to-end throughput proportionally).
|
||||||
|
//!
|
||||||
|
//! Run: cargo run --release --example causal_probe --features smarm-causal
|
||||||
|
|
||||||
|
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
|
||||||
|
use std::sync::Arc;
|
||||||
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
|
fn burn_us(us: u64) {
|
||||||
|
let cycles = us * 3_000;
|
||||||
|
let start = smarm::preempt::rdtsc();
|
||||||
|
while smarm::preempt::rdtsc().saturating_sub(start) < cycles {
|
||||||
|
smarm::check!();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// 1. TSC calibration (plain OS thread, before the runtime starts).
|
||||||
|
let c0 = smarm::preempt::rdtsc();
|
||||||
|
let t0 = Instant::now();
|
||||||
|
std::thread::sleep(Duration::from_millis(200));
|
||||||
|
let hz = (smarm::preempt::rdtsc() - c0) as f64 / t0.elapsed().as_secs_f64();
|
||||||
|
println!("tsc_hz: {:.3e} (crate assumes 3.0e9)", hz);
|
||||||
|
|
||||||
|
smarm::init(smarm::Config::default()).run(move || {
|
||||||
|
// 2. burn_us(400) wall-time distribution inside a migrating actor.
|
||||||
|
let h = smarm::spawn(|| {
|
||||||
|
let mut samples: Vec<u64> = (0..500)
|
||||||
|
.map(|_| {
|
||||||
|
let t = Instant::now();
|
||||||
|
burn_us(400);
|
||||||
|
t.elapsed().as_micros() as u64
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
samples.sort_unstable();
|
||||||
|
println!(
|
||||||
|
"burn_us(400) wall us: min {} p10 {} p50 {} p90 {} max {}",
|
||||||
|
samples[0], samples[50], samples[250], samples[450], samples[499]
|
||||||
|
);
|
||||||
|
});
|
||||||
|
h.join().unwrap();
|
||||||
|
|
||||||
|
// 3+4. Pipeline with per-stage counters.
|
||||||
|
let stop = Arc::new(AtomicBool::new(false));
|
||||||
|
let produced = Arc::new(AtomicU64::new(0));
|
||||||
|
let reserved = Arc::new(AtomicU64::new(0));
|
||||||
|
let notified = Arc::new(AtomicU64::new(0));
|
||||||
|
|
||||||
|
let (tx_ab, rx_ab) = smarm::channel::<u64>();
|
||||||
|
let (tx_bc, rx_bc) = smarm::channel::<u64>();
|
||||||
|
|
||||||
|
let stop_p = stop.clone();
|
||||||
|
let produced2 = produced.clone();
|
||||||
|
let producer = smarm::spawn(move || {
|
||||||
|
let mut i = 0u64;
|
||||||
|
while !stop_p.load(Ordering::Relaxed) {
|
||||||
|
{
|
||||||
|
let _g = smarm::causal_site!("serialize");
|
||||||
|
burn_us(200);
|
||||||
|
}
|
||||||
|
if tx_ab.send(i).is_err() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
produced2.fetch_add(1, Ordering::Relaxed);
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let reserved2 = reserved.clone();
|
||||||
|
let reserve = smarm::spawn(move || {
|
||||||
|
while let Ok(item) = rx_ab.recv() {
|
||||||
|
{
|
||||||
|
let _g = smarm::causal_site!("reserve");
|
||||||
|
burn_us(400);
|
||||||
|
}
|
||||||
|
reserved2.fetch_add(1, Ordering::Relaxed);
|
||||||
|
if tx_bc.send(item).is_err() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let notified2 = notified.clone();
|
||||||
|
let notify = smarm::spawn(move || {
|
||||||
|
while rx_bc.recv().is_ok() {
|
||||||
|
{
|
||||||
|
let _g = smarm::causal_site!("notify");
|
||||||
|
burn_us(50);
|
||||||
|
}
|
||||||
|
notified2.fetch_add(1, Ordering::Relaxed);
|
||||||
|
smarm::progress!("orders-processed");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let stop_bg = stop.clone();
|
||||||
|
let background = smarm::spawn(move || {
|
||||||
|
while !stop_bg.load(Ordering::Relaxed) {
|
||||||
|
let _g = smarm::causal_site!("background-compaction");
|
||||||
|
burn_us(500);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
smarm::sleep(Duration::from_millis(300));
|
||||||
|
|
||||||
|
let window = |label: &str| {
|
||||||
|
let (p0, r0, n0) = (
|
||||||
|
produced.load(Ordering::Relaxed),
|
||||||
|
reserved.load(Ordering::Relaxed),
|
||||||
|
notified.load(Ordering::Relaxed),
|
||||||
|
);
|
||||||
|
let d0 = smarm::causal::global_delay_cycles();
|
||||||
|
let t = Instant::now();
|
||||||
|
smarm::sleep(Duration::from_millis(700));
|
||||||
|
let secs = t.elapsed().as_secs_f64();
|
||||||
|
println!(
|
||||||
|
"{label}: produced {:.0}/s reserved {:.0}/s notified {:.0}/s injected {:.0}ms(assumed-3GHz)",
|
||||||
|
(produced.load(Ordering::Relaxed) - p0) as f64 / secs,
|
||||||
|
(reserved.load(Ordering::Relaxed) - r0) as f64 / secs,
|
||||||
|
(notified.load(Ordering::Relaxed) - n0) as f64 / secs,
|
||||||
|
(smarm::causal::global_delay_cycles() - d0) as f64 / 3.0e9 * 1e3,
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
window("no-experiment ");
|
||||||
|
smarm::causal::begin_experiment_for_test("background-compaction", 50);
|
||||||
|
window("bg-comp @ 50% ");
|
||||||
|
smarm::causal::end_experiment_for_test();
|
||||||
|
window("post-experiment");
|
||||||
|
|
||||||
|
stop.store(true, Ordering::Relaxed);
|
||||||
|
producer.join().unwrap();
|
||||||
|
reserve.join().unwrap();
|
||||||
|
notify.join().unwrap();
|
||||||
|
background.join().unwrap();
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -1,11 +1,9 @@
|
|||||||
//! SPIKE (throwaway) — RFC 017 "fused" approach.
|
//! The hand-written **expansion target** of the `gen_statem!` macro: the same
|
||||||
|
//! machine as `examples/gen_statem_macro.rs`, written out in full so the
|
||||||
|
//! primitives can be judged standing on their own. The macro generates exactly
|
||||||
|
//! this shape; nothing here needs the macro to be correct or safe.
|
||||||
//!
|
//!
|
||||||
//! This is the hand-written *expansion target* of the eventual `statem!` macro,
|
//! The design:
|
||||||
//! written out in full so the primitives can be judged standing on their own.
|
|
||||||
//! A macro would generate exactly this from a `transitions { … }` block; nothing
|
|
||||||
//! here needs the macro to be correct or safe.
|
|
||||||
//!
|
|
||||||
//! The design, as settled over the prior iterations:
|
|
||||||
//!
|
//!
|
||||||
//! * States and events are real enums. An invalid state is unrepresentable;
|
//! * States and events are real enums. An invalid state is unrepresentable;
|
||||||
//! there are no bitflags, no `u32` superpositions, no unsafe unions.
|
//! there are no bitflags, no `u32` superpositions, no unsafe unions.
|
||||||
@@ -27,18 +25,18 @@
|
|||||||
//! wired in is dead code — and dead_code is denied below, making an orphan
|
//! wired in is dead code — and dead_code is denied below, making an orphan
|
||||||
//! handler a compile error.
|
//! handler a compile error.
|
||||||
//!
|
//!
|
||||||
//! * Drops onto the committed `Machine` / `Resolution` / `Cx` primitives with
|
//! * Drops onto the `Machine` / `Resolution` / `Cx` primitives with no change
|
||||||
//! no change to `src/statem.rs`.
|
//! to `src/gen_statem.rs`.
|
||||||
//!
|
//!
|
||||||
//! Default build is clean and runs. A BREAK-CASE MENU at the bottom documents
|
//! Default build is clean and runs. A BREAK-CASE MENU at the bottom documents
|
||||||
//! how to make each of the four guarantees fire.
|
//! how to make each of the four guarantees fire.
|
||||||
//!
|
//!
|
||||||
//! Run: `cargo run --example statem_fused`
|
//! Run: `cargo run --example gen_statem_expanded`
|
||||||
|
|
||||||
#![deny(dead_code, unreachable_patterns)]
|
#![deny(dead_code, unreachable_patterns)]
|
||||||
|
|
||||||
|
use smarm::gen_statem::{spawn, Cx, GenStatemRef, Machine, Reply, Resolution, Step};
|
||||||
use smarm::run;
|
use smarm::run;
|
||||||
use smarm::statem::{spawn, Cx, Machine, Reply, Resolution, StatemRef};
|
|
||||||
|
|
||||||
// === user types ============================================================
|
// === user types ============================================================
|
||||||
|
|
||||||
@@ -52,6 +50,7 @@ enum Door {
|
|||||||
struct Data {
|
struct Data {
|
||||||
enters: u32, // total state entries (incl. initial)
|
enters: u32, // total state entries (incl. initial)
|
||||||
pushes: u32, // times a push closed the door
|
pushes: u32, // times a push closed the door
|
||||||
|
knocks: u32, // knocks answered (a Locked knock is postponed, then counted)
|
||||||
}
|
}
|
||||||
|
|
||||||
enum Cast {
|
enum Cast {
|
||||||
@@ -59,17 +58,24 @@ enum Cast {
|
|||||||
Pull,
|
Pull,
|
||||||
Lock,
|
Lock,
|
||||||
Unlock(u32), // carries a key
|
Unlock(u32), // carries a key
|
||||||
|
Knock, // counted when the door is reachable; postponed while Locked
|
||||||
}
|
}
|
||||||
|
|
||||||
enum Call {
|
enum Call {
|
||||||
GetState(Reply<Door>),
|
GetState(Reply<Door>),
|
||||||
GetEnters(Reply<u32>),
|
GetEnters(Reply<u32>),
|
||||||
GetPushes(Reply<u32>),
|
GetPushes(Reply<u32>),
|
||||||
|
GetKnocks(Reply<u32>),
|
||||||
}
|
}
|
||||||
|
|
||||||
enum Ev {
|
enum Ev {
|
||||||
Cast(Cast),
|
Cast(Cast),
|
||||||
Call(Call),
|
Call(Call),
|
||||||
|
// The runtime's internal events. `Info` is out-of-band (here unused, so
|
||||||
|
// `()`); `StateTimeout` / `Timeout` are timer fires the loop feeds back in.
|
||||||
|
Info(()),
|
||||||
|
StateTimeout,
|
||||||
|
Timeout(&'static str),
|
||||||
}
|
}
|
||||||
|
|
||||||
const CODE: u32 = 1234;
|
const CODE: u32 = 1234;
|
||||||
@@ -114,37 +120,69 @@ struct DoorSm {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl DoorSm {
|
impl DoorSm {
|
||||||
fn start(init: Door) -> StatemRef<DoorSm> {
|
fn start(init: Door) -> GenStatemRef<DoorSm> {
|
||||||
spawn(DoorSm {
|
spawn(DoorSm {
|
||||||
state: init,
|
state: init,
|
||||||
data: Data { enters: 0, pushes: 0 },
|
data: Data {
|
||||||
|
enters: 0,
|
||||||
|
pushes: 0,
|
||||||
|
knocks: 0,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn enter(&mut self, _cx: &mut Cx<Ev>) {
|
fn enter(&mut self, cx: &mut Cx<Ev>) {
|
||||||
self.data.enters += 1;
|
self.data.enters += 1;
|
||||||
|
// A state-timeout: an Open door auto-closes after a quiet window. The
|
||||||
|
// loop auto-resets it on any transition, so it fires only if the door is
|
||||||
|
// still Open when it elapses.
|
||||||
|
if self.state == Door::Open {
|
||||||
|
cx.state_timeout(std::time::Duration::from_millis(5));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Machine for DoorSm {
|
impl Machine for DoorSm {
|
||||||
type Ev = Ev;
|
type Ev = Ev;
|
||||||
|
|
||||||
|
fn state_timeout_ev() -> Ev {
|
||||||
|
Ev::StateTimeout
|
||||||
|
}
|
||||||
|
|
||||||
|
fn timeout_ev(name: &'static str) -> Ev {
|
||||||
|
Ev::Timeout(name)
|
||||||
|
}
|
||||||
|
|
||||||
fn on_start(&mut self, cx: &mut Cx<Ev>) {
|
fn on_start(&mut self, cx: &mut Cx<Ev>) {
|
||||||
self.enter(cx);
|
self.enter(cx);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle(&mut self, ev: Ev, cx: &mut Cx<Ev>) {
|
fn handle(&mut self, ev: Ev, cx: &mut Cx<Ev>) -> Step<Ev> {
|
||||||
let prev = self.state;
|
let prev = self.state;
|
||||||
|
|
||||||
// ---- the transition table -----------------------------------------
|
// ---- phase 1: postpone routing (borrow-only) ----------------------
|
||||||
// This match is total over (Door, Ev). Read it as the declared graph:
|
// A deferred event is handed back untouched for the loop's postpone
|
||||||
// each `=> To(x)` is an edge, each `=> Unhandled` an explicit refusal.
|
// queue; no handler code runs on it. Here: a knock at a locked door.
|
||||||
|
// (The macro emits this as a `match (state, &ev)` yielding a bool; the
|
||||||
|
// hand-written form can just test directly.)
|
||||||
|
if let (Door::Locked, Ev::Cast(Cast::Knock)) = (prev, &ev) {
|
||||||
|
return Step::Postponed(ev);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- phase 2: the consuming transition table ----------------------
|
||||||
|
// Total over (Door, Ev). Read it as the declared graph: each `=> To(x)`
|
||||||
|
// is an edge, each `=> Unhandled` an explicit refusal. The postponed
|
||||||
|
// pair above reappears as `unreachable!` so the match stays total.
|
||||||
let res: Resolution<Door> = match (self.state, ev) {
|
let res: Resolution<Door> = match (self.state, ev) {
|
||||||
// --- Open -------------------------------------------------------
|
// --- Open -------------------------------------------------------
|
||||||
(Door::Open, Ev::Cast(Cast::Push)) => {
|
(Door::Open, Ev::Cast(Cast::Push)) => {
|
||||||
on_push(&mut self.data);
|
on_push(&mut self.data);
|
||||||
Resolution::To(Door::Closed)
|
Resolution::To(Door::Closed)
|
||||||
}
|
}
|
||||||
|
(Door::Open, Ev::Cast(Cast::Knock)) => {
|
||||||
|
self.data.knocks += 1;
|
||||||
|
Resolution::To(prev)
|
||||||
|
}
|
||||||
(Door::Open, Ev::Cast(Cast::Pull | Cast::Lock | Cast::Unlock(_))) => {
|
(Door::Open, Ev::Cast(Cast::Pull | Cast::Lock | Cast::Unlock(_))) => {
|
||||||
Resolution::Unhandled
|
Resolution::Unhandled
|
||||||
}
|
}
|
||||||
@@ -152,15 +190,19 @@ impl Machine for DoorSm {
|
|||||||
// --- Closed -----------------------------------------------------
|
// --- Closed -----------------------------------------------------
|
||||||
(Door::Closed, Ev::Cast(Cast::Pull)) => Resolution::To(Door::Open),
|
(Door::Closed, Ev::Cast(Cast::Pull)) => Resolution::To(Door::Open),
|
||||||
(Door::Closed, Ev::Cast(Cast::Lock)) => Resolution::To(Door::Locked),
|
(Door::Closed, Ev::Cast(Cast::Lock)) => Resolution::To(Door::Locked),
|
||||||
|
(Door::Closed, Ev::Cast(Cast::Knock)) => {
|
||||||
|
self.data.knocks += 1;
|
||||||
|
Resolution::To(prev)
|
||||||
|
}
|
||||||
(Door::Closed, Ev::Cast(Cast::Push | Cast::Unlock(_))) => Resolution::Unhandled,
|
(Door::Closed, Ev::Cast(Cast::Push | Cast::Unlock(_))) => Resolution::Unhandled,
|
||||||
|
|
||||||
// --- Locked (branching row: handler picks within UnlockOutcome) -
|
// --- Locked (branching row: handler picks within UnlockOutcome) -
|
||||||
(Door::Locked, Ev::Cast(Cast::Unlock(key))) => {
|
(Door::Locked, Ev::Cast(Cast::Unlock(key))) => Resolution::To(on_unlock(key).into()),
|
||||||
Resolution::To(on_unlock(key).into())
|
// Routed out in phase 1; listed only to keep this match total.
|
||||||
}
|
(Door::Locked, Ev::Cast(Cast::Knock)) => {
|
||||||
(Door::Locked, Ev::Cast(Cast::Push | Cast::Pull | Cast::Lock)) => {
|
unreachable!("postponed event is replayed, not dispatched here")
|
||||||
Resolution::Unhandled
|
|
||||||
}
|
}
|
||||||
|
(Door::Locked, Ev::Cast(Cast::Push | Cast::Pull | Cast::Lock)) => Resolution::Unhandled,
|
||||||
|
|
||||||
// --- state-independent queries (reply, then stay) ---------------
|
// --- state-independent queries (reply, then stay) ---------------
|
||||||
(_, Ev::Call(Call::GetState(r))) => {
|
(_, Ev::Call(Call::GetState(r))) => {
|
||||||
@@ -175,17 +217,38 @@ impl Machine for DoorSm {
|
|||||||
r.reply(self.data.pushes);
|
r.reply(self.data.pushes);
|
||||||
Resolution::To(prev)
|
Resolution::To(prev)
|
||||||
}
|
}
|
||||||
|
(_, Ev::Call(Call::GetKnocks(r))) => {
|
||||||
|
r.reply(self.data.knocks);
|
||||||
|
Resolution::To(prev)
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- timeouts: an Open door auto-closes; others have none armed --
|
||||||
|
(Door::Open, Ev::StateTimeout) => Resolution::To(Door::Closed),
|
||||||
|
(_, Ev::StateTimeout) => Resolution::Unhandled,
|
||||||
|
(_, Ev::Timeout(name)) => {
|
||||||
|
// No named timeout is armed in this run; a real handler would
|
||||||
|
// dispatch on `name`. Acknowledge it to exercise the field.
|
||||||
|
let _ = name;
|
||||||
|
Resolution::Unhandled
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- out-of-band info: silent drop (the gen_server default) ------
|
||||||
|
(_, Ev::Info(_)) => Resolution::Unhandled,
|
||||||
};
|
};
|
||||||
|
|
||||||
// ---- apply the resolution -----------------------------------------
|
// ---- apply the resolution -----------------------------------------
|
||||||
match res {
|
match res {
|
||||||
Resolution::To(s) if s == prev => {} // stay: no enter
|
Resolution::To(s) if s == prev => Step::Stayed, // stay: no enter
|
||||||
Resolution::To(s) => {
|
Resolution::To(s) => {
|
||||||
self.state = s; // sole writer of the state cell
|
self.state = s; // sole writer of the state cell
|
||||||
|
cx.__reset_state_timeout(); // auto-reset across transitions
|
||||||
self.enter(cx);
|
self.enter(cx);
|
||||||
|
Step::Transitioned
|
||||||
|
}
|
||||||
|
Resolution::Unhandled => {
|
||||||
|
cx.on_unhandled();
|
||||||
|
Step::Stayed
|
||||||
}
|
}
|
||||||
Resolution::Postpone => unreachable!("postpone lands in chunk 3"),
|
|
||||||
Resolution::Unhandled => cx.on_unhandled(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -195,20 +258,29 @@ fn main() {
|
|||||||
let door = DoorSm::start(Door::Closed);
|
let door = DoorSm::start(Door::Closed);
|
||||||
|
|
||||||
door.send(Ev::Cast(Cast::Lock)).unwrap(); // Closed -> Locked
|
door.send(Ev::Cast(Cast::Lock)).unwrap(); // Closed -> Locked
|
||||||
|
door.send(Ev::Cast(Cast::Knock)).unwrap(); // Locked: postponed (not yet counted)
|
||||||
door.send(Ev::Cast(Cast::Push)).unwrap(); // Locked: Push invalid -> Unhandled
|
door.send(Ev::Cast(Cast::Push)).unwrap(); // Locked: Push invalid -> Unhandled
|
||||||
door.send(Ev::Cast(Cast::Unlock(0))).unwrap(); // Locked: wrong key -> stay
|
door.send(Ev::Cast(Cast::Unlock(0))).unwrap(); // Locked: wrong key -> stay (knock still deferred)
|
||||||
door.send(Ev::Cast(Cast::Unlock(CODE))).unwrap(); // Locked -> Closed
|
door.send(Ev::Cast(Cast::Unlock(CODE))).unwrap(); // Locked -> Closed; deferred Knock replays here
|
||||||
door.send(Ev::Cast(Cast::Pull)).unwrap(); // Closed -> Open
|
door.send(Ev::Cast(Cast::Push)).unwrap(); // Closed: Push invalid -> Unhandled
|
||||||
door.send(Ev::Cast(Cast::Push)).unwrap(); // Open -> Closed (pushes=1)
|
door.send(Ev::Cast(Cast::Pull)).unwrap(); // Closed -> Open (arms 5ms auto-close)
|
||||||
|
door.send(Ev::Info(())).unwrap(); // out-of-band: silently dropped
|
||||||
|
|
||||||
|
// Wait past the auto-close window: the state-timeout fires and the door
|
||||||
|
// closes itself, with no further input. (`smarm::sleep` parks the actor
|
||||||
|
// without blocking a worker thread, so the timer wheel keeps turning.)
|
||||||
|
smarm::sleep(std::time::Duration::from_millis(40));
|
||||||
|
|
||||||
let st = door.call(|r| Ev::Call(Call::GetState(r))).unwrap();
|
let st = door.call(|r| Ev::Call(Call::GetState(r))).unwrap();
|
||||||
let enters = door.call(|r| Ev::Call(Call::GetEnters(r))).unwrap();
|
let enters = door.call(|r| Ev::Call(Call::GetEnters(r))).unwrap();
|
||||||
let pushes = door.call(|r| Ev::Call(Call::GetPushes(r))).unwrap();
|
let pushes = door.call(|r| Ev::Call(Call::GetPushes(r))).unwrap();
|
||||||
|
let knocks = door.call(|r| Ev::Call(Call::GetKnocks(r))).unwrap();
|
||||||
|
|
||||||
println!("state={st:?} enters={enters} pushes={pushes}");
|
println!("state={st:?} enters={enters} pushes={pushes} knocks={knocks}");
|
||||||
assert_eq!(st, Door::Closed);
|
assert_eq!(st, Door::Closed); // auto-closed by the state-timeout
|
||||||
assert_eq!(enters, 5); // Closed(start) + Locked + Closed + Open + Closed
|
assert_eq!(enters, 5); // Closed(start) + Locked + Closed + Open + Closed
|
||||||
assert_eq!(pushes, 1);
|
assert_eq!(pushes, 0); // no push ever closed it this run
|
||||||
|
assert_eq!(knocks, 1); // the locked-door knock, replayed once unlocked
|
||||||
println!("ok");
|
println!("ok");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -1,27 +1,27 @@
|
|||||||
//! RFC 017 — the **same** machine as `examples/statem_fused.rs`, written through
|
//! The **same** machine as `examples/gen_statem_expanded.rs`, written through the
|
||||||
//! the `gen_statem!` macro. Diff this file against that one to see exactly what
|
//! `gen_statem!` macro. Diff this file against that one to see exactly what the
|
||||||
//! the macro buys: every `// ===` section there that was boilerplate (the `Ev`
|
//! macro buys: every `// ===` section there that was boilerplate (the `Ev`
|
||||||
//! enum, the `DoorSm` struct, `start`, the whole `Machine` impl, the `enter`
|
//! enum, the `DoorSm` struct, `start`, the whole `Machine` impl, the `enter`
|
||||||
//! dispatch, the stay/transition apply-tail) collapses into the invocation
|
//! dispatch, the stay/transition apply-tail) collapses into the invocation
|
||||||
//! below. What stays hand-written is what carries meaning: the four types, the
|
//! below. What stays hand-written is what carries meaning: the four types, the
|
||||||
//! per-state successor enum, and the handler fns.
|
//! per-state successor enum, and the handler fns.
|
||||||
//!
|
//!
|
||||||
//! The point of the exercise is that the macro is *pure sugar*: the four
|
//! The point of the exercise is that the macro is *pure sugar*: the four
|
||||||
//! compile-time guarantees the fused spike demonstrates are properties of the
|
//! compile-time guarantees the hand-written form demonstrates are properties of
|
||||||
//! emitted code, not of the macro, so they survive expansion unchanged. The
|
//! the emitted code, not of the macro, so they survive expansion unchanged. The
|
||||||
//! BREAK-CASE MENU at the bottom is the same four cases, re-expressed against
|
//! BREAK-CASE MENU at the bottom is the same four cases, re-expressed against
|
||||||
//! the macro surface — flip any one on and the compiler fires identically.
|
//! the macro surface — flip any one on and the compiler fires identically.
|
||||||
//!
|
//!
|
||||||
//! Run: `cargo run --example statem_macro`
|
//! Run: `cargo run --example gen_statem_macro`
|
||||||
|
|
||||||
#![deny(dead_code)] // guarantee #3 (orphan handlers); the macro denies the
|
#![deny(dead_code)] // guarantee #3 (orphan handlers); the macro denies the
|
||||||
// dispatch's own unreachable_patterns internally.
|
// dispatch's own unreachable_patterns internally.
|
||||||
|
|
||||||
use smarm::gen_statem;
|
use smarm::gen_statem;
|
||||||
|
use smarm::gen_statem::Reply;
|
||||||
use smarm::run;
|
use smarm::run;
|
||||||
use smarm::statem::Reply;
|
|
||||||
|
|
||||||
// === user types (identical to statem_fused.rs) =============================
|
// === user types (identical to gen_statem_expanded.rs) =========================
|
||||||
|
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
||||||
enum Door {
|
enum Door {
|
||||||
@@ -33,6 +33,7 @@ enum Door {
|
|||||||
struct Data {
|
struct Data {
|
||||||
enters: u32, // total state entries (incl. initial)
|
enters: u32, // total state entries (incl. initial)
|
||||||
pushes: u32, // times a push closed the door
|
pushes: u32, // times a push closed the door
|
||||||
|
knocks: u32, // knocks answered (a Locked knock is postponed, then counted)
|
||||||
}
|
}
|
||||||
|
|
||||||
enum Cast {
|
enum Cast {
|
||||||
@@ -40,12 +41,14 @@ enum Cast {
|
|||||||
Pull,
|
Pull,
|
||||||
Lock,
|
Lock,
|
||||||
Unlock(u32), // carries a key
|
Unlock(u32), // carries a key
|
||||||
|
Knock, // counted when the door is reachable; postponed while Locked
|
||||||
}
|
}
|
||||||
|
|
||||||
enum Call {
|
enum Call {
|
||||||
GetState(Reply<Door>),
|
GetState(Reply<Door>),
|
||||||
GetEnters(Reply<u32>),
|
GetEnters(Reply<u32>),
|
||||||
GetPushes(Reply<u32>),
|
GetPushes(Reply<u32>),
|
||||||
|
GetKnocks(Reply<u32>),
|
||||||
}
|
}
|
||||||
|
|
||||||
const CODE: u32 = 1234;
|
const CODE: u32 = 1234;
|
||||||
@@ -87,11 +90,11 @@ fn on_unlock(key: u32) -> UnlockOutcome {
|
|||||||
|
|
||||||
gen_statem! {
|
gen_statem! {
|
||||||
machine: DoorSm { state: Door, data: Data };
|
machine: DoorSm { state: Door, data: Data };
|
||||||
event: Ev { cast: Cast, call: Call };
|
event: Ev { cast: Cast, call: Call, info: () };
|
||||||
|
|
||||||
// You name the bindings the bodies use; the macro can't lend you its own
|
// You name the bindings the bodies use; the macro can't lend you its own
|
||||||
// `self`/`cx` across macro hygiene. `data` = &mut Data, `prev` = current
|
// `self`/`cx` across macro hygiene. `data` = &mut Data, `prev` = current
|
||||||
// state tag, `cx` = context handle (unused in chunk 1).
|
// state tag, `cx` = context handle (unused here).
|
||||||
context(data, prev, cx);
|
context(data, prev, cx);
|
||||||
|
|
||||||
enter {
|
enter {
|
||||||
@@ -100,15 +103,20 @@ gen_statem! {
|
|||||||
|
|
||||||
on Door::Open => {
|
on Door::Open => {
|
||||||
cast Cast::Push => { on_push(data); Door::Closed },
|
cast Cast::Push => { on_push(data); Door::Closed },
|
||||||
|
cast Cast::Knock => { data.knocks += 1; prev },
|
||||||
cast Cast::Pull | Cast::Lock | Cast::Unlock(_) => unhandled,
|
cast Cast::Pull | Cast::Lock | Cast::Unlock(_) => unhandled,
|
||||||
}
|
}
|
||||||
on Door::Closed => {
|
on Door::Closed => {
|
||||||
cast Cast::Pull => Door::Open,
|
cast Cast::Pull => Door::Open,
|
||||||
cast Cast::Lock => Door::Locked,
|
cast Cast::Lock => Door::Locked,
|
||||||
|
cast Cast::Knock => { data.knocks += 1; prev },
|
||||||
cast Cast::Push | Cast::Unlock(_) => unhandled,
|
cast Cast::Push | Cast::Unlock(_) => unhandled,
|
||||||
}
|
}
|
||||||
on Door::Locked => {
|
on Door::Locked => {
|
||||||
cast Cast::Unlock(key) => on_unlock(key), // branch -> UnlockOutcome
|
cast Cast::Unlock(key) => on_unlock(key), // branch -> UnlockOutcome
|
||||||
|
// A knock at a locked door waits: defer it until the door is reachable,
|
||||||
|
// where the replay counts it.
|
||||||
|
cast Cast::Knock => postpone,
|
||||||
cast Cast::Push | Cast::Pull | Cast::Lock => unhandled,
|
cast Cast::Push | Cast::Pull | Cast::Lock => unhandled,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -117,35 +125,50 @@ gen_statem! {
|
|||||||
call Call::GetState(r) => { r.reply(prev); prev },
|
call Call::GetState(r) => { r.reply(prev); prev },
|
||||||
call Call::GetEnters(r) => { r.reply(data.enters); prev },
|
call Call::GetEnters(r) => { r.reply(data.enters); prev },
|
||||||
call Call::GetPushes(r) => { r.reply(data.pushes); prev },
|
call Call::GetPushes(r) => { r.reply(data.pushes); prev },
|
||||||
|
call Call::GetKnocks(r) => { r.reply(data.knocks); prev },
|
||||||
|
// This machine arms no timeouts, so refuse them everywhere. (Info has a
|
||||||
|
// built-in silent-drop default, so it needs no row.)
|
||||||
|
state_timeout => unhandled,
|
||||||
|
timeout _ => unhandled,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
run(|| {
|
run(|| {
|
||||||
let door = DoorSm::start(Door::Closed, Data { enters: 0, pushes: 0 });
|
let door = DoorSm::start(
|
||||||
|
Door::Closed,
|
||||||
|
Data {
|
||||||
|
enters: 0,
|
||||||
|
pushes: 0,
|
||||||
|
knocks: 0,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
door.send(Ev::Cast(Cast::Lock)).unwrap(); // Closed -> Locked
|
door.send(Ev::Cast(Cast::Lock)).unwrap(); // Closed -> Locked
|
||||||
|
door.send(Ev::Cast(Cast::Knock)).unwrap(); // Locked: postponed (not yet counted)
|
||||||
door.send(Ev::Cast(Cast::Push)).unwrap(); // Locked: Push invalid -> Unhandled
|
door.send(Ev::Cast(Cast::Push)).unwrap(); // Locked: Push invalid -> Unhandled
|
||||||
door.send(Ev::Cast(Cast::Unlock(0))).unwrap(); // Locked: wrong key -> stay
|
door.send(Ev::Cast(Cast::Unlock(0))).unwrap(); // Locked: wrong key -> stay (knock still deferred)
|
||||||
door.send(Ev::Cast(Cast::Unlock(CODE))).unwrap(); // Locked -> Closed
|
door.send(Ev::Cast(Cast::Unlock(CODE))).unwrap(); // Locked -> Closed; deferred Knock replays here
|
||||||
door.send(Ev::Cast(Cast::Pull)).unwrap(); // Closed -> Open
|
door.send(Ev::Cast(Cast::Pull)).unwrap(); // Closed -> Open
|
||||||
door.send(Ev::Cast(Cast::Push)).unwrap(); // Open -> Closed (pushes=1)
|
door.send(Ev::Cast(Cast::Push)).unwrap(); // Open -> Closed (pushes=1)
|
||||||
|
|
||||||
let st = door.call(|r| Ev::Call(Call::GetState(r))).unwrap();
|
let st = door.call(|r| Ev::Call(Call::GetState(r))).unwrap();
|
||||||
let enters = door.call(|r| Ev::Call(Call::GetEnters(r))).unwrap();
|
let enters = door.call(|r| Ev::Call(Call::GetEnters(r))).unwrap();
|
||||||
let pushes = door.call(|r| Ev::Call(Call::GetPushes(r))).unwrap();
|
let pushes = door.call(|r| Ev::Call(Call::GetPushes(r))).unwrap();
|
||||||
|
let knocks = door.call(|r| Ev::Call(Call::GetKnocks(r))).unwrap();
|
||||||
|
|
||||||
println!("state={st:?} enters={enters} pushes={pushes}");
|
println!("state={st:?} enters={enters} pushes={pushes} knocks={knocks}");
|
||||||
assert_eq!(st, Door::Closed);
|
assert_eq!(st, Door::Closed);
|
||||||
assert_eq!(enters, 5); // Closed(start) + Locked + Closed + Open + Closed
|
assert_eq!(enters, 5); // Closed(start) + Locked + Closed + Open + Closed
|
||||||
assert_eq!(pushes, 1);
|
assert_eq!(pushes, 1);
|
||||||
|
assert_eq!(knocks, 1); // the locked-door knock, replayed once unlocked
|
||||||
println!("ok");
|
println!("ok");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// ===========================================================================
|
// ===========================================================================
|
||||||
// BREAK-CASE MENU — the four guarantees, through the macro. Each fires exactly
|
// BREAK-CASE MENU — the four guarantees, through the macro. Each fires exactly
|
||||||
// as it does in the hand-written statem_fused.rs.
|
// as it does in the hand-written gen_statem_expanded.rs.
|
||||||
//
|
//
|
||||||
// 1. ORPHAN HANDLER (dead_code -> error):
|
// 1. ORPHAN HANDLER (dead_code -> error):
|
||||||
// add `fn on_slam(_d: &mut Data) {}` and don't reference it.
|
// add `fn on_slam(_d: &mut Data) {}` and don't reference it.
|
||||||
@@ -157,8 +180,8 @@ fn main() {
|
|||||||
// NOTE: this example is a separate crate from `smarm`, so rustc's
|
// NOTE: this example is a separate crate from `smarm`, so rustc's
|
||||||
// in_external_macro rule SILENCES this lint here even though the macro
|
// in_external_macro rule SILENCES this lint here even though the macro
|
||||||
// denies it — the duplicate compiles. The guarantee is real only for
|
// denies it — the duplicate compiles. The guarantee is real only for
|
||||||
// machines defined inside the smarm crate (see the unit test in
|
// machines defined inside the `smarm` crate itself. This is the documented
|
||||||
// src/statem.rs). This is the documented macro_rules! limitation.
|
// macro_rules! limitation.
|
||||||
//
|
//
|
||||||
// 3. MISSING PAIR (non-exhaustive match, E0004):
|
// 3. MISSING PAIR (non-exhaustive match, E0004):
|
||||||
// delete the `cast Cast::Push | Cast::Pull | Cast::Lock => unhandled,`
|
// delete the `cast Cast::Push | Cast::Pull | Cast::Lock => unhandled,`
|
||||||
@@ -0,0 +1,139 @@
|
|||||||
|
//! Graceful shutdown, end to end: a supervised app tree, a server that
|
||||||
|
//! drains before it exits, and the two ways the whole thing winds down.
|
||||||
|
//!
|
||||||
|
//! Stopping an actor comes in two strengths, as in OTP:
|
||||||
|
//! - `request_stop(pid)` = `exit(Pid, kill)`: cooperative hard stop,
|
||||||
|
//! unwinds at the next observation point.
|
||||||
|
//! - `request_shutdown(pid)` = `exit(Pid, shutdown)`: a trapping target gets
|
||||||
|
//! an `ExitSignal { reason: Shutdown }` and winds
|
||||||
|
//! down on its own terms; a non-trapping one is
|
||||||
|
//! stopped outright.
|
||||||
|
//!
|
||||||
|
//! A supervisor traps exits. `request_shutdown(sup)` runs its ordered
|
||||||
|
//! shutdown — children in reverse start order, each per its `ChildSpec`
|
||||||
|
//! `Shutdown` policy (`Timeout(d)` default 5s, `Infinity`, `BrutalKill`) —
|
||||||
|
//! and the supervisor then returns normally.
|
||||||
|
//!
|
||||||
|
//! Two triggers are shown:
|
||||||
|
//! 1. **Root exit.** The run's root actor returning means "the program is
|
||||||
|
//! done": the runtime delivers `request_shutdown` to every top-level actor
|
||||||
|
//! (here: the supervisor). Trapping actors may keep running to drain and
|
||||||
|
//! end the run when they stop themselves; non-trapping ones are stopped.
|
||||||
|
//! 2. **An outside thread** (e.g. a signal handler) driving it via
|
||||||
|
//! `RuntimeHandle::request_shutdown` on the supervisor — the root then
|
||||||
|
//! just waits for the tree to come down.
|
||||||
|
|
||||||
|
use smarm::gen_server::{
|
||||||
|
GenServer, GenServerBuilder, GenServerCtx, GenServerName, ShutdownAction, StopHandle,
|
||||||
|
TimerHandle,
|
||||||
|
};
|
||||||
|
use smarm::supervisor::{ChildSpec, OneForOne, Restart, Shutdown};
|
||||||
|
use smarm::{sleep, spawn};
|
||||||
|
use std::thread;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
/// A server with in-flight work: on shutdown it stops accepting, finishes what
|
||||||
|
/// it has (simulated with a ticking timer), then ends itself.
|
||||||
|
struct Drainer {
|
||||||
|
pending: u32,
|
||||||
|
stop: Option<StopHandle<Drainer>>,
|
||||||
|
timer: Option<TimerHandle<Drainer>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl GenServer for Drainer {
|
||||||
|
type Call = ();
|
||||||
|
type Reply = ();
|
||||||
|
type Cast = ();
|
||||||
|
type Info = ();
|
||||||
|
type Timer = ();
|
||||||
|
|
||||||
|
fn init(&mut self, ctx: &GenServerCtx<Self>) {
|
||||||
|
ctx.trap_exit(); // opt in: shutdown arrives as handle_shutdown
|
||||||
|
self.stop = Some(ctx.stop_handle());
|
||||||
|
self.timer = Some(ctx.timer());
|
||||||
|
}
|
||||||
|
fn handle_call(&mut self, _: ()) {}
|
||||||
|
fn handle_cast(&mut self, _: ()) {}
|
||||||
|
fn handle_shutdown(&mut self) -> ShutdownAction {
|
||||||
|
println!(
|
||||||
|
"drainer: shutdown requested, {} items pending",
|
||||||
|
self.pending
|
||||||
|
);
|
||||||
|
self.timer
|
||||||
|
.as_ref()
|
||||||
|
.unwrap()
|
||||||
|
.tick_every(Duration::from_millis(20), ());
|
||||||
|
ShutdownAction::Continue // keep serving until drained
|
||||||
|
}
|
||||||
|
fn handle_timer(&mut self, _: ()) {
|
||||||
|
self.pending -= 1;
|
||||||
|
if self.pending == 0 {
|
||||||
|
println!("drainer: drained, stopping");
|
||||||
|
self.stop.as_ref().unwrap().stop(); // normal exit
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn terminate(&mut self) {
|
||||||
|
// Graceful path: this runs on the normal path and may block.
|
||||||
|
println!("drainer: terminate");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The server's name: how the rest of the app reaches it (and the only handle
|
||||||
|
/// that survives a restart).
|
||||||
|
const DRAINER: GenServerName<Drainer> = GenServerName::new("drainer");
|
||||||
|
|
||||||
|
fn app_tree() -> OneForOne {
|
||||||
|
OneForOne::new()
|
||||||
|
.child(
|
||||||
|
ChildSpec::new(Restart::Permanent, || {
|
||||||
|
// A plain worker that does not trap: stopped outright on shutdown.
|
||||||
|
loop {
|
||||||
|
sleep(Duration::from_millis(10));
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.shutdown(Shutdown::Timeout(Duration::from_millis(100))),
|
||||||
|
)
|
||||||
|
// A gen_server is a direct child: `named(N).run()` runs the loop as
|
||||||
|
// the child actor itself, so the supervisor's shutdown arrives as
|
||||||
|
// `handle_shutdown` and a restart re-binds the name.
|
||||||
|
.child(
|
||||||
|
ChildSpec::new(Restart::Permanent, || {
|
||||||
|
GenServerBuilder::new(Drainer {
|
||||||
|
pending: 3,
|
||||||
|
stop: None,
|
||||||
|
timer: None,
|
||||||
|
})
|
||||||
|
.named(DRAINER)
|
||||||
|
.run()
|
||||||
|
.expect("drainer name is free");
|
||||||
|
})
|
||||||
|
.shutdown(Shutdown::Infinity),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
println!("--- 1. root exit drives the shutdown ---");
|
||||||
|
smarm::run(|| {
|
||||||
|
spawn(|| app_tree().run());
|
||||||
|
sleep(Duration::from_millis(50)); // the app "runs" for a while
|
||||||
|
// Returning here asks the supervisor to shut down; the run ends when
|
||||||
|
// the tree — drainer included — is gone.
|
||||||
|
});
|
||||||
|
|
||||||
|
println!("--- 2. an outside thread drives the shutdown ---");
|
||||||
|
let rt = smarm::init(smarm::Config::default());
|
||||||
|
let handle = rt.handle(); // Send + Sync; grab it before run
|
||||||
|
rt.run(move || {
|
||||||
|
let sup = spawn(|| app_tree().run());
|
||||||
|
let sup_pid = sup.pid();
|
||||||
|
// Stand-in for a SIGTERM handler thread.
|
||||||
|
thread::spawn(move || {
|
||||||
|
thread::sleep(Duration::from_millis(50));
|
||||||
|
println!("signal thread: requesting shutdown");
|
||||||
|
handle.request_shutdown(sup_pid);
|
||||||
|
});
|
||||||
|
sup.join()
|
||||||
|
.expect("supervisor returns normally after ordered shutdown");
|
||||||
|
println!("supervisor down; root returns");
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -2,11 +2,13 @@
|
|||||||
//!
|
//!
|
||||||
//! A gen_server is multi-message (call / cast over one inbox), so it is named
|
//! A gen_server is multi-message (call / cast over one inbox), so it is named
|
||||||
//! by the *server* type rather than by a single message type. Registering it
|
//! by the *server* type rather than by a single message type. Registering it
|
||||||
//! under a [`ServerName`] lets clients `call` and `cast` by name, resolving on
|
//! under a [`GenServerName`] lets clients `call` and `cast` by name, resolving on
|
||||||
//! every use — so the address keeps working across a supervised restart, with
|
//! every use — so the address keeps working across a supervised restart, with
|
||||||
//! no stale [`ServerRef`] to refresh.
|
//! no stale [`GenServerRef`] to refresh.
|
||||||
|
|
||||||
use smarm::{call, cast, run, whereis_server, GenServer, ServerBuilder, ServerName, ServerRef};
|
use smarm::{
|
||||||
|
call, cast, run, whereis_server, GenServer, GenServerBuilder, GenServerName, GenServerRef,
|
||||||
|
};
|
||||||
|
|
||||||
/// A counter server: synchronous `Get`, asynchronous `Inc` / `Add`.
|
/// A counter server: synchronous `Get`, asynchronous `Inc` / `Add`.
|
||||||
struct Counter {
|
struct Counter {
|
||||||
@@ -41,13 +43,13 @@ impl GenServer for Counter {
|
|||||||
|
|
||||||
/// A durable name typed by the server, so by-name `call` / `cast` check against
|
/// A durable name typed by the server, so by-name `call` / `cast` check against
|
||||||
/// `Counter`'s `Call` / `Cast` / `Reply`.
|
/// `Counter`'s `Call` / `Cast` / `Reply`.
|
||||||
const COUNTER: ServerName<Counter> = ServerName::new("counter");
|
const COUNTER: GenServerName<Counter> = GenServerName::new("counter");
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
run(|| {
|
run(|| {
|
||||||
// Start the server and bind its name in one step. A named start is
|
// Start the server and bind its name in one step. A named start is
|
||||||
// fallible: the name may already be held by another live server.
|
// fallible: the name may already be held by another live server.
|
||||||
ServerBuilder::new(Counter { n: 0 })
|
GenServerBuilder::new(Counter { n: 0 })
|
||||||
.named(COUNTER)
|
.named(COUNTER)
|
||||||
.start()
|
.start()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
@@ -60,10 +62,16 @@ fn main() {
|
|||||||
assert_eq!(call(COUNTER, Query::Get).unwrap(), 42);
|
assert_eq!(call(COUNTER, Query::Get).unwrap(), 42);
|
||||||
|
|
||||||
// When you want a handle to hold or pass on rather than resolve per
|
// When you want a handle to hold or pass on rather than resolve per
|
||||||
// call, recover a typed `ServerRef` from the name.
|
// call, recover a typed `GenServerRef` from the name.
|
||||||
let svc: Option<ServerRef<Counter>> = whereis_server(COUNTER);
|
let svc: Option<GenServerRef<Counter>> = whereis_server(COUNTER);
|
||||||
if let Some(svc) = svc {
|
if let Some(svc) = svc {
|
||||||
let _ = svc.call(Query::Get);
|
let _ = svc.call(Query::Get);
|
||||||
|
// A named server is pinned alive by the registry, so dropping refs
|
||||||
|
// does not end it. Stop it explicitly: `shutdown()` asks politely
|
||||||
|
// (a trapping server drains first; this one is stopped outright)
|
||||||
|
// and waits until it is gone. Left running, the root's return
|
||||||
|
// would shut it down the same way — see examples/graceful_shutdown.rs.
|
||||||
|
svc.shutdown();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-3
@@ -15,7 +15,9 @@
|
|||||||
//! `call`, nothing more.
|
//! `call`, nothing more.
|
||||||
|
|
||||||
use smarm::observer::{self, ObserverReply, ObserverRequest};
|
use smarm::observer::{self, ObserverReply, ObserverRequest};
|
||||||
use smarm::{channel, register, run, spawn, ActorState, Name, RuntimeSnapshot, RuntimeTree, TreeNode};
|
use smarm::{
|
||||||
|
channel, register, run, spawn, ActorState, Name, RuntimeSnapshot, RuntimeTree, TreeNode,
|
||||||
|
};
|
||||||
|
|
||||||
const ECHO: Name<u64> = Name::new("echo");
|
const ECHO: Name<u64> = Name::new("echo");
|
||||||
|
|
||||||
@@ -31,7 +33,11 @@ fn state_glyph(s: ActorState) -> &'static str {
|
|||||||
|
|
||||||
/// A `ps`-style table over the flat snapshot.
|
/// A `ps`-style table over the flat snapshot.
|
||||||
fn print_snapshot(snap: &RuntimeSnapshot) {
|
fn print_snapshot(snap: &RuntimeSnapshot) {
|
||||||
println!("snapshot (format v{}, {} actors)", snap.format_version, snap.actors.len());
|
println!(
|
||||||
|
"snapshot (format v{}, {} actors)",
|
||||||
|
snap.format_version,
|
||||||
|
snap.actors.len()
|
||||||
|
);
|
||||||
println!(
|
println!(
|
||||||
" {:<10} {:<9} {:<10} {:>4} {:>4} {:>4} {:>4} {:>5} {}",
|
" {:<10} {:<9} {:<10} {:>4} {:>4} {:>4} {:>4} {:>5} {}",
|
||||||
"pid", "state", "parent", "mon", "lnk", "joi", "mbox", "msgs", "names"
|
"pid", "state", "parent", "mon", "lnk", "joi", "mbox", "msgs", "names"
|
||||||
@@ -52,7 +58,11 @@ fn print_snapshot(snap: &RuntimeSnapshot) {
|
|||||||
a.joiners,
|
a.joiners,
|
||||||
a.mailbox_depth,
|
a.mailbox_depth,
|
||||||
a.messages_received,
|
a.messages_received,
|
||||||
if a.names.is_empty() { "-".to_string() } else { a.names.join(",") },
|
if a.names.is_empty() {
|
||||||
|
"-".to_string()
|
||||||
|
} else {
|
||||||
|
a.names.join(",")
|
||||||
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,152 +0,0 @@
|
|||||||
//! A hand-written `gen_statem`, written directly against the chunk-1 primitives
|
|
||||||
//! in `smarm::statem` — no macro. This is the RFC 017 `Switch` example, and its
|
|
||||||
//! purpose is to be the **evaluation artifact**: it shows exactly the shape the
|
|
||||||
//! deferred `statem!` macro would have to generate, so we can judge what the
|
|
||||||
//! macro actually buys before committing to building one.
|
|
||||||
//!
|
|
||||||
//! Run with: `cargo run --example statem_switch`
|
|
||||||
//!
|
|
||||||
//! Sections are tagged // USER (you'd hand-write this with or without a macro)
|
|
||||||
//! and // MACRO (the boilerplate a `statem!` would emit for you).
|
|
||||||
|
|
||||||
use smarm::run;
|
|
||||||
use smarm::statem::{self, Cx, Machine, Reply, Resolution, StatemRef};
|
|
||||||
|
|
||||||
// ---- USER: the four hand-written types ------------------------------------
|
|
||||||
// In the macro world these are unchanged — the macro never generates them.
|
|
||||||
|
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
|
||||||
enum Switch {
|
|
||||||
Off,
|
|
||||||
On,
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Counts {
|
|
||||||
flips: u32,
|
|
||||||
enters: u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
enum SwitchCast {
|
|
||||||
Flip,
|
|
||||||
}
|
|
||||||
|
|
||||||
enum SwitchCall {
|
|
||||||
GetCount(Reply<u32>),
|
|
||||||
GetEnters(Reply<u32>),
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---- MACRO: the unified event ---------------------------------------------
|
|
||||||
// The user's two message enums folded together. Chunk 1 has no internal
|
|
||||||
// variants yet; chunks 2–3 add `StateTimeout` / `Timeout(name)` here.
|
|
||||||
|
|
||||||
enum Ev {
|
|
||||||
Cast(SwitchCast),
|
|
||||||
Call(SwitchCall),
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---- MACRO: the machine struct + state cell -------------------------------
|
|
||||||
// `state` is the private cell; the `handle` body below is its sole writer.
|
|
||||||
|
|
||||||
struct SwitchSm {
|
|
||||||
state: Switch,
|
|
||||||
data: Counts,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SwitchSm {
|
|
||||||
// MACRO: `Switch::start` in the RFC; spawns the actor, hands back a ref.
|
|
||||||
fn start(init: Switch, data: Counts) -> StatemRef<SwitchSm> {
|
|
||||||
statem::spawn(SwitchSm { state: init, data })
|
|
||||||
}
|
|
||||||
|
|
||||||
// MACRO: the `enter` dispatch, assembled from the per-state `enter` arms.
|
|
||||||
// USER wrote the arm bodies (`data.enters += 1`); the macro wrote the match
|
|
||||||
// and the `()` return. `enter` runs side effects only — it cannot transition.
|
|
||||||
fn enter(&mut self, s: Switch, _cx: &mut Cx<Ev>) {
|
|
||||||
match s {
|
|
||||||
Switch::Off => self.data.enters += 1,
|
|
||||||
Switch::On => self.data.enters += 1,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Machine for SwitchSm {
|
|
||||||
type Ev = Ev;
|
|
||||||
|
|
||||||
// MACRO: run the initial state's enter.
|
|
||||||
fn on_start(&mut self, cx: &mut Cx<Ev>) {
|
|
||||||
let s = self.state;
|
|
||||||
self.enter(s, cx);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn handle(&mut self, ev: Ev, cx: &mut Cx<Ev>) {
|
|
||||||
let prev = self.state;
|
|
||||||
|
|
||||||
// MACRO: the `(state, event)` dispatch table. USER wrote each arm tail
|
|
||||||
// (the body + the ending state tag); the macro qualified bare tags
|
|
||||||
// (`On` -> `Switch::On`), wrapped them in `.into()`, assembled the match,
|
|
||||||
// and — in the real macro — would have edge-checked each tail tag
|
|
||||||
// against the `transitions { Off => On, On => Off }` table at expand
|
|
||||||
// time. Here that check is on the honour system.
|
|
||||||
let next: Resolution<Switch> = match (self.state, ev) {
|
|
||||||
(Switch::Off, Ev::Cast(SwitchCast::Flip)) => {
|
|
||||||
self.data.flips += 1;
|
|
||||||
Switch::On.into()
|
|
||||||
}
|
|
||||||
(Switch::On, Ev::Cast(SwitchCast::Flip)) => Switch::Off.into(),
|
|
||||||
(Switch::Off, Ev::Call(SwitchCall::GetCount(r))) => {
|
|
||||||
r.reply(self.data.flips);
|
|
||||||
Switch::Off.into() // stay = return the current tag
|
|
||||||
}
|
|
||||||
(Switch::On, Ev::Call(SwitchCall::GetCount(r))) => {
|
|
||||||
r.reply(self.data.flips);
|
|
||||||
Switch::On.into()
|
|
||||||
}
|
|
||||||
(Switch::Off, Ev::Call(SwitchCall::GetEnters(r))) => {
|
|
||||||
r.reply(self.data.enters);
|
|
||||||
Switch::Off.into()
|
|
||||||
}
|
|
||||||
(Switch::On, Ev::Call(SwitchCall::GetEnters(r))) => {
|
|
||||||
r.reply(self.data.enters);
|
|
||||||
Switch::On.into()
|
|
||||||
}
|
|
||||||
// The macro would emit `_ => Resolution::Unhandled` here for a
|
|
||||||
// machine whose table is non-exhaustive; this one covers every
|
|
||||||
// (state, event) pair, so an added arm would be unreachable.
|
|
||||||
};
|
|
||||||
|
|
||||||
// MACRO: the resolution dispatch — identical in every generated machine.
|
|
||||||
match next {
|
|
||||||
Resolution::To(s) if s == prev => {} // stay: no enter, no reset
|
|
||||||
Resolution::To(s) => {
|
|
||||||
self.state = s; // <- sole writer of the state cell
|
|
||||||
// chunk 2: self.timers.clear_state_timeout();
|
|
||||||
self.enter(s, cx);
|
|
||||||
// chunk 3: cx.replay(&mut self.postponed);
|
|
||||||
}
|
|
||||||
Resolution::Postpone => {} // chunk 3
|
|
||||||
Resolution::Unhandled => cx.on_unhandled(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
run(|| {
|
|
||||||
// USER: the client side. With a macro these would be `sw.cast(..)` /
|
|
||||||
// `sw.call(SwitchCall::GetCount)`; without it, the `Ev::Cast` / `Ev::Call`
|
|
||||||
// wrap is explicit — note how mechanical it is.
|
|
||||||
let sw = SwitchSm::start(Switch::Off, Counts { flips: 0, enters: 0 });
|
|
||||||
|
|
||||||
sw.send(Ev::Cast(SwitchCast::Flip)).unwrap(); // Off -> On
|
|
||||||
sw.send(Ev::Cast(SwitchCast::Flip)).unwrap(); // On -> Off
|
|
||||||
|
|
||||||
let flips = sw.call(|r| Ev::Call(SwitchCall::GetCount(r))).unwrap();
|
|
||||||
let enters = sw.call(|r| Ev::Call(SwitchCall::GetEnters(r))).unwrap();
|
|
||||||
|
|
||||||
// RFC's stated end state after two flips: Counts { flips: 1, enters: 3 }
|
|
||||||
// (initial Off entry + On entry + Off entry).
|
|
||||||
println!("after two flips: flips={flips}, enters={enters}");
|
|
||||||
assert_eq!(flips, 1, "turned On once across the two flips");
|
|
||||||
assert_eq!(enters, 3, "initial Off + On + Off entries");
|
|
||||||
println!("ok");
|
|
||||||
});
|
|
||||||
}
|
|
||||||
+20
-5
@@ -72,7 +72,10 @@ pub fn clear_current_pid() {
|
|||||||
CURRENT_PID.with(|c| c.set(None));
|
CURRENT_PID.with(|c| c.set(None));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Actor-side TLS accessor: `#[inline(never)]` + fence, see `context` docs.
|
||||||
|
#[inline(never)]
|
||||||
pub fn current_pid() -> Option<Pid> {
|
pub fn current_pid() -> Option<Pid> {
|
||||||
|
crate::context::tls_fence();
|
||||||
CURRENT_PID.with(|c| c.get())
|
CURRENT_PID.with(|c| c.get())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,11 +96,13 @@ pub fn take_last_outcome() -> Option<Outcome> {
|
|||||||
/// unwinding to cross the boundary, but `catch_unwind` here means unwinding
|
/// unwinding to cross the boundary, but `catch_unwind` here means unwinding
|
||||||
/// never actually does.
|
/// never actually does.
|
||||||
pub extern "C-unwind" fn trampoline() {
|
pub extern "C-unwind" fn trampoline() {
|
||||||
let b = CURRENT_ACTOR_BOX.with(|c| c.borrow_mut().take())
|
let b = match CURRENT_ACTOR_BOX.with(|c| c.borrow_mut().take()) {
|
||||||
.expect("trampoline entered without a closure set");
|
Some(b) => b,
|
||||||
|
None => panic!("smarm: trampoline entered without a closure set (core corrupt)"),
|
||||||
|
};
|
||||||
|
|
||||||
let outcome = match panic::catch_unwind(panic::AssertUnwindSafe(b)) {
|
let outcome = match panic::catch_unwind(panic::AssertUnwindSafe(b)) {
|
||||||
Ok(()) => Outcome::Exit,
|
Ok(()) => Outcome::Exit,
|
||||||
Err(payload) => {
|
Err(payload) => {
|
||||||
if payload.is::<StopSentinel>() {
|
if payload.is::<StopSentinel>() {
|
||||||
Outcome::Stopped
|
Outcome::Stopped
|
||||||
@@ -107,8 +112,7 @@ pub extern "C-unwind" fn trampoline() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
LAST_OUTCOME.with(|r| *r.borrow_mut() = Some(outcome));
|
publish_outcome(outcome);
|
||||||
ACTOR_DONE.with(|c| c.set(true));
|
|
||||||
|
|
||||||
// Hand control back. The scheduler will tear down our slot and never
|
// Hand control back. The scheduler will tear down our slot and never
|
||||||
// resume us again.
|
// resume us again.
|
||||||
@@ -117,6 +121,17 @@ pub extern "C-unwind" fn trampoline() {
|
|||||||
unreachable!("scheduler resumed a done actor");
|
unreachable!("scheduler resumed a done actor");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Record the outcome for the scheduler that is about to be switched to. Kept
|
||||||
|
/// out of line: the actor may have migrated threads while its closure ran, so
|
||||||
|
/// the TLS base `trampoline` computed at entry must not be reused here (see
|
||||||
|
/// `context` module docs on thread-locals and migration).
|
||||||
|
#[inline(never)]
|
||||||
|
fn publish_outcome(outcome: Outcome) {
|
||||||
|
crate::context::tls_fence();
|
||||||
|
LAST_OUTCOME.with(|r| *r.borrow_mut() = Some(outcome));
|
||||||
|
ACTOR_DONE.with(|c| c.set(true));
|
||||||
|
}
|
||||||
|
|
||||||
/// One actor's worth of state. Owned by the scheduler's slot table.
|
/// One actor's worth of state. Owned by the scheduler's slot table.
|
||||||
pub struct Actor {
|
pub struct Actor {
|
||||||
/// The PID this actor was assigned at spawn time.
|
/// The PID this actor was assigned at spawn time.
|
||||||
|
|||||||
+977
@@ -0,0 +1,977 @@
|
|||||||
|
//! Native causal profiling (RFC 007). Enabled by `--features smarm-causal`;
|
||||||
|
//! zero cost without it (same discipline as `smarm-trace`).
|
||||||
|
//!
|
||||||
|
//! The Coz algorithm, transposed onto actors: to estimate what speeding up
|
||||||
|
//! code site S by p% would do to throughput, we instead *slow everything
|
||||||
|
//! else down* by p% of the time spent in S, and watch the progress-point
|
||||||
|
//! rates respond. Where Coz must inject real `usleep`s into OS threads from
|
||||||
|
//! the outside, smarm owns every clock that matters:
|
||||||
|
//!
|
||||||
|
//! - Sampling and delay injection happen at `maybe_preempt`'s amortised
|
||||||
|
//! cadence — an existing, safe hook (never inside a prep-to-park region).
|
||||||
|
//! - Injected delay is subtracted from the actor's timeslice
|
||||||
|
//! (`preempt::extend_timeslice`), so experiments don't perturb scheduling.
|
||||||
|
//! - Delay bookkeeping is *actor*-granular: each `Slot` carries an absorbed-
|
||||||
|
//! delay ledger, compared against a global ledger. Parked actors absorb
|
||||||
|
//! accrued delay for free on resume (Coz's blocked-thread rule) — waiting
|
||||||
|
//! is never penalised.
|
||||||
|
//!
|
||||||
|
//! v1 scope (per RFC discussion): explicit scoped sites (`causal_site!`)
|
||||||
|
//! rather than PC sampling (jar Q1 stays open); throughput progress points
|
||||||
|
//! only; timer-heap deadlines are *not* shifted (documented gap — long
|
||||||
|
//! experiments can make real-time timeouts fire early in virtual terms);
|
||||||
|
//! multi-scheduler coherence is best-effort via global atomics.
|
||||||
|
//!
|
||||||
|
//! Usage:
|
||||||
|
//! ```ignore
|
||||||
|
//! let _g = smarm::causal_site!("inventory-reserve"); // in suspect code
|
||||||
|
//! smarm::progress!("orders-processed"); // per unit of work
|
||||||
|
//! let results = smarm::causal::run_experiments(&Default::default());
|
||||||
|
//! print!("{}", smarm::causal::render_summary(&results));
|
||||||
|
//! std::fs::write("profile.coz", smarm::causal::render_coz(&results))?;
|
||||||
|
//! ```
|
||||||
|
|
||||||
|
#[cfg(feature = "smarm-causal")]
|
||||||
|
mod inner {
|
||||||
|
use crate::preempt;
|
||||||
|
use std::cell::Cell;
|
||||||
|
use std::sync::atomic::{AtomicU64, Ordering};
|
||||||
|
use std::sync::{Mutex, OnceLock};
|
||||||
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------
|
||||||
|
// Global state
|
||||||
|
// -----------------------------------------------------------------------
|
||||||
|
|
||||||
|
/// Active experiment, packed `(site_id << 32) | speedup_pct`. 0 = idle.
|
||||||
|
/// A single word so the hot path reads one atomic; experiments are global
|
||||||
|
/// across scheduler threads (jar Q7, v1: plain Relaxed atomics).
|
||||||
|
static EXPERIMENT: AtomicU64 = AtomicU64::new(0);
|
||||||
|
|
||||||
|
/// Monotone experiment-window counter, bumped by every `begin()`. Lets
|
||||||
|
/// the offcpu-gap stash (RFC 007) tell apart two windows with an
|
||||||
|
/// identical site+pct word — live in the attrib probe's 50,50 schedule
|
||||||
|
/// — so a gap straddling `end()`/`begin()` never counts a cooldown.
|
||||||
|
static EXPERIMENT_EPOCH: AtomicU64 = AtomicU64::new(0);
|
||||||
|
|
||||||
|
/// Global virtual-delay ledger, in TSC cycles: the total delay every
|
||||||
|
/// actor *should* have experienced since startup. Grows while a sample
|
||||||
|
/// lands in the experiment's target site; each actor's `Slot` ledger
|
||||||
|
/// chases it by spin-absorbing at preemption checks.
|
||||||
|
static GLOBAL_DELAY: AtomicU64 = AtomicU64::new(0);
|
||||||
|
|
||||||
|
/// Registered site names; site id = index + 1 (0 = "no site").
|
||||||
|
static SITES: OnceLock<Mutex<Vec<&'static str>>> = OnceLock::new();
|
||||||
|
|
||||||
|
/// Registered progress points (leaked for `'static`, like trace's drain
|
||||||
|
/// state — the set is small and lives for the process).
|
||||||
|
static PROGRESS: OnceLock<Mutex<Vec<&'static ProgressPoint>>> = OnceLock::new();
|
||||||
|
|
||||||
|
thread_local! {
|
||||||
|
/// TSC at this thread's previous causal check, the sample "period"
|
||||||
|
/// denominator. Re-armed on every actor resume so scheduler time and
|
||||||
|
/// a previous actor's tail never count toward a sample. 0 = unarmed.
|
||||||
|
static LAST_SAMPLE_TSC: Cell<u64> = const { Cell::new(0) };
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Guard against TSC weirdness (migration between unsynced sockets,
|
||||||
|
/// virtualisation steps): a single sample interval larger than this is
|
||||||
|
/// discarded rather than believed. ~33ms at 3 GHz — far beyond any real
|
||||||
|
/// gap between preemption checks inside a slice.
|
||||||
|
const MAX_SAMPLE_CYCLES: u64 = 100_000_000;
|
||||||
|
|
||||||
|
/// Cap on delay spun in one visit, so one check can never wedge an actor
|
||||||
|
/// for a human-visible pause; the remainder is absorbed on later visits.
|
||||||
|
/// ~3ms at 3 GHz.
|
||||||
|
const MAX_SPIN_PER_VISIT: u64 = 10_000_000;
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------
|
||||||
|
// Ledger audit (RFC 007 deficit hunt): where injected delay is born,
|
||||||
|
// paid, and forgiven — and where would-be attribution is silently lost
|
||||||
|
// (deschedule tails, clamp discards). Monotone Relaxed totals, read via
|
||||||
|
// `ledger_counters()`; `run_experiments` windows them into
|
||||||
|
// `ExperimentResult`. Measure-only: nothing here changes injection or
|
||||||
|
// absorption behaviour.
|
||||||
|
// -----------------------------------------------------------------------
|
||||||
|
|
||||||
|
/// Cycles bystanders actually spun to pay down the global ledger.
|
||||||
|
static SPIN_ABSORBED_CYCLES: AtomicU64 = AtomicU64::new(0);
|
||||||
|
/// Cycles waived at wake after a real park (the blocked-thread rule).
|
||||||
|
static PARK_FORGIVEN_CYCLES: AtomicU64 = AtomicU64::new(0);
|
||||||
|
/// Would-be attribution lost when the target-site actor parks mid-site.
|
||||||
|
static DROP_PARK_CYCLES: AtomicU64 = AtomicU64::new(0);
|
||||||
|
static DROP_PARK_N: AtomicU64 = AtomicU64::new(0);
|
||||||
|
/// Same loss at yields (explicit, slice-expiry, or a park that requeued).
|
||||||
|
static DROP_YIELD_CYCLES: AtomicU64 = AtomicU64::new(0);
|
||||||
|
static DROP_YIELD_N: AtomicU64 = AtomicU64::new(0);
|
||||||
|
/// Samples discarded by the TSC-weirdness clamp, in would-be delta terms.
|
||||||
|
static DISCARD_OVERMAX_CYCLES: AtomicU64 = AtomicU64::new(0);
|
||||||
|
static DISCARD_OVERMAX_N: AtomicU64 = AtomicU64::new(0);
|
||||||
|
/// In-site samples dropped because the thread's clock was unarmed.
|
||||||
|
static DISCARD_UNARMED_N: AtomicU64 = AtomicU64::new(0);
|
||||||
|
/// Would-be attribution over runnable off-CPU gaps inside the target
|
||||||
|
/// site (yield-descheduled -> resumed within the same window). Not a
|
||||||
|
/// loss: on-CPU-only attribution is the Coz model — queue-wait is not
|
||||||
|
/// shrunk by speeding the site's code — but counted so the audit books
|
||||||
|
/// close against wall in-site time (the located @50 "deficit").
|
||||||
|
static OFFCPU_IN_SITE_CYCLES: AtomicU64 = AtomicU64::new(0);
|
||||||
|
static OFFCPU_IN_SITE_N: AtomicU64 = AtomicU64::new(0);
|
||||||
|
|
||||||
|
fn sites() -> &'static Mutex<Vec<&'static str>> {
|
||||||
|
SITES.get_or_init(|| Mutex::new(Vec::new()))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn progress_points() -> &'static Mutex<Vec<&'static ProgressPoint>> {
|
||||||
|
PROGRESS.get_or_init(|| Mutex::new(Vec::new()))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Recover from lock poisoning: all these registries hold plain data that
|
||||||
|
/// is valid at every instruction boundary, so a panicked registrant can't
|
||||||
|
/// leave them torn.
|
||||||
|
fn lock_unpoisoned<T>(m: &Mutex<T>) -> std::sync::MutexGuard<'_, T> {
|
||||||
|
match m.lock() {
|
||||||
|
Ok(g) => g,
|
||||||
|
Err(poisoned) => poisoned.into_inner(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------
|
||||||
|
// Sites
|
||||||
|
// -----------------------------------------------------------------------
|
||||||
|
|
||||||
|
/// Register (or look up) a causal site by name; returns its nonzero id.
|
||||||
|
/// Called once per `causal_site!` expansion via a `OnceLock`, so the
|
||||||
|
/// mutex is off every hot path.
|
||||||
|
pub fn site_id(name: &'static str) -> u32 {
|
||||||
|
let mut v = lock_unpoisoned(sites());
|
||||||
|
if let Some(pos) = v.iter().position(|n| *n == name) {
|
||||||
|
return (pos + 1) as u32;
|
||||||
|
}
|
||||||
|
v.push(name);
|
||||||
|
v.len() as u32
|
||||||
|
}
|
||||||
|
|
||||||
|
fn site_name(id: u32) -> Option<String> {
|
||||||
|
if id == 0 {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let v = lock_unpoisoned(sites());
|
||||||
|
v.get((id - 1) as usize).map(|s| (*s).to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// RAII marker: while alive, the current *actor* (not thread — the id
|
||||||
|
/// lives in its `Slot` and survives preemption/migration) is "inside"
|
||||||
|
/// the site. Nesting restores the outer site on drop. Inert outside an
|
||||||
|
/// actor (scheduler/OS-thread stacks).
|
||||||
|
pub struct SiteGuard {
|
||||||
|
/// Slot of the actor that entered, null if entered outside an actor.
|
||||||
|
/// Valid for the guard's whole life: the guard lives on the actor's
|
||||||
|
/// stack, and a slot is never reclaimed while its actor is alive —
|
||||||
|
/// the same argument as `preempt::check_cancelled`.
|
||||||
|
slot: *const crate::runtime::Slot,
|
||||||
|
prev: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SiteGuard {
|
||||||
|
/// Enter `site` for the on-CPU actor.
|
||||||
|
pub fn enter(site: u32) -> Self {
|
||||||
|
let slot = preempt::current_slot_ptr();
|
||||||
|
if slot.is_null() {
|
||||||
|
return SiteGuard { slot, prev: 0 };
|
||||||
|
}
|
||||||
|
// SAFETY: non-null ⇒ points at the on-CPU actor's slot; see the
|
||||||
|
// field docs for the lifetime argument.
|
||||||
|
let prev = unsafe { (*slot).causal_site() };
|
||||||
|
unsafe { (*slot).set_causal_site(site) };
|
||||||
|
site_transition(slot, prev, site);
|
||||||
|
SiteGuard { slot, prev }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for SiteGuard {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
if !self.slot.is_null() {
|
||||||
|
// SAFETY (both): as in `enter` — the actor (and thus its
|
||||||
|
// slot) is alive for as long as this guard is on its stack.
|
||||||
|
let site = unsafe { (*self.slot).causal_site() };
|
||||||
|
unsafe { (*self.slot).set_causal_site(self.prev) };
|
||||||
|
site_transition(self.slot, site, self.prev);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Name of the site the on-CPU actor is currently inside, if any.
|
||||||
|
/// (Introspection/testing; not a hot path.)
|
||||||
|
pub fn current_site_name() -> Option<String> {
|
||||||
|
let slot = preempt::current_slot_ptr();
|
||||||
|
if slot.is_null() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
// SAFETY: on-CPU actor's slot, valid for the whole resume.
|
||||||
|
site_name(unsafe { (*slot).causal_site() })
|
||||||
|
}
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------
|
||||||
|
// Progress points
|
||||||
|
// -----------------------------------------------------------------------
|
||||||
|
|
||||||
|
/// A named throughput counter. One per distinct name; `progress!` call
|
||||||
|
/// sites sharing a name share the counter.
|
||||||
|
pub struct ProgressPoint {
|
||||||
|
name: &'static str,
|
||||||
|
count: AtomicU64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ProgressPoint {
|
||||||
|
/// The hot path: one Relaxed RMW. (Contended across actors by design
|
||||||
|
/// — a progress point is a global rate meter.)
|
||||||
|
#[inline]
|
||||||
|
pub fn bump(&self) {
|
||||||
|
self.count.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Register (or look up) a progress point. Called once per `progress!`
|
||||||
|
/// expansion via a `OnceLock`; the mutex is off the hot path.
|
||||||
|
pub fn register_progress(name: &'static str) -> &'static ProgressPoint {
|
||||||
|
let mut v = lock_unpoisoned(progress_points());
|
||||||
|
if let Some(p) = v.iter().find(|p| p.name == name) {
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
let p: &'static ProgressPoint = Box::leak(Box::new(ProgressPoint {
|
||||||
|
name,
|
||||||
|
count: AtomicU64::new(0),
|
||||||
|
}));
|
||||||
|
v.push(p);
|
||||||
|
p
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Snapshot of all progress points as `(name, count)`.
|
||||||
|
pub fn progress_snapshot() -> Vec<(String, u64)> {
|
||||||
|
lock_unpoisoned(progress_points())
|
||||||
|
.iter()
|
||||||
|
.map(|p| (p.name.to_string(), p.count.load(Ordering::Relaxed)))
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------
|
||||||
|
// The hot hook: sample + absorb
|
||||||
|
// -----------------------------------------------------------------------
|
||||||
|
|
||||||
|
/// Called from `maybe_preempt` at the amortised timeslice-check cadence,
|
||||||
|
/// under the `PREEMPTION_ENABLED` gate (so never in a prep-to-park or
|
||||||
|
/// no-preempt region — spinning here is as safe as yielding is).
|
||||||
|
///
|
||||||
|
/// One Relaxed load and out when no experiment is running.
|
||||||
|
#[inline]
|
||||||
|
pub(crate) fn check() {
|
||||||
|
let exp = EXPERIMENT.load(Ordering::Relaxed);
|
||||||
|
if exp == 0 {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
cold_check(exp);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The experiment-active path, kept out of the inlined fast path.
|
||||||
|
#[cold]
|
||||||
|
#[inline(never)]
|
||||||
|
fn cold_check(exp: u64) {
|
||||||
|
crate::context::tls_fence();
|
||||||
|
let slot = preempt::current_slot_ptr();
|
||||||
|
if slot.is_null() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// Serialised: `now` closes an interval attributed to a code site; a
|
||||||
|
// speculative early read would drop that site's tail (RFC 007).
|
||||||
|
let now = preempt::rdtsc_serialising();
|
||||||
|
let last = LAST_SAMPLE_TSC.with(|c| c.replace(now));
|
||||||
|
let target_site = (exp >> 32) as u32;
|
||||||
|
let pct = exp & 0xffff_ffff;
|
||||||
|
|
||||||
|
// SAFETY (both derefs below): non-null ⇒ the on-CPU actor's slot,
|
||||||
|
// never reclaimed while the actor runs — see `check_cancelled`.
|
||||||
|
let my_site = unsafe { (*slot).causal_site() };
|
||||||
|
|
||||||
|
if my_site == target_site && pct > 0 {
|
||||||
|
// A sample landed in the target site: everyone else must fall
|
||||||
|
// behind by pct% of the sampled interval. Grow the global ledger
|
||||||
|
// and credit ourselves the same amount — the credited gap *is*
|
||||||
|
// the virtual speedup.
|
||||||
|
if last == 0 {
|
||||||
|
// Unarmed clock: no interval to attribute — count the loss.
|
||||||
|
DISCARD_UNARMED_N.fetch_add(1, Ordering::Relaxed);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// SAFETY: `slot` is the on-CPU actor's slot (checked non-null
|
||||||
|
// above); see `check_cancelled` for the lifetime argument.
|
||||||
|
unsafe { attribute(slot, now.saturating_sub(last), pct) };
|
||||||
|
} else {
|
||||||
|
// Not the winner: chase the global ledger by spinning off the
|
||||||
|
// difference, then push the slice start forward so injected
|
||||||
|
// delay never counts as compute (the clock correction that Coz
|
||||||
|
// cannot do from outside).
|
||||||
|
let global = GLOBAL_DELAY.load(Ordering::Relaxed);
|
||||||
|
let mine = unsafe { (*slot).causal_delay() };
|
||||||
|
if mine >= global {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let spin = (global - mine).min(MAX_SPIN_PER_VISIT);
|
||||||
|
let start = preempt::rdtsc();
|
||||||
|
while preempt::rdtsc().saturating_sub(start) < spin {
|
||||||
|
core::hint::spin_loop();
|
||||||
|
}
|
||||||
|
SPIN_ABSORBED_CYCLES.fetch_add(spin, Ordering::Relaxed);
|
||||||
|
unsafe { (*slot).set_causal_delay(mine.wrapping_add(spin)) };
|
||||||
|
preempt::extend_timeslice(spin);
|
||||||
|
// The spin is not part of the next sample interval either.
|
||||||
|
LAST_SAMPLE_TSC.with(|c| c.set(preempt::rdtsc()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Attribute one target-site sample of `interval` cycles at `pct`%:
|
||||||
|
/// grow the global ledger and credit the sampling actor's own ledger by
|
||||||
|
/// the same amount — the credited gap *is* the virtual speedup. Shared
|
||||||
|
/// by the cold check and the guard-boundary flush. Applies the same
|
||||||
|
/// clamps as sampling always has: zero intervals and clock hiccups are
|
||||||
|
/// discarded, not the run.
|
||||||
|
///
|
||||||
|
/// SAFETY: `slot` must point at the on-CPU actor's slot (the
|
||||||
|
/// `check_cancelled` lifetime argument).
|
||||||
|
unsafe fn attribute(slot: *const crate::runtime::Slot, interval: u64, pct: u64) {
|
||||||
|
if interval == 0 {
|
||||||
|
return; // now == last: nothing to attribute, nothing lost
|
||||||
|
}
|
||||||
|
if interval > MAX_SAMPLE_CYCLES {
|
||||||
|
// TSC-weirdness clamp: the sample is discarded, not the run.
|
||||||
|
// Count the loss in would-be delta terms so the audit's columns
|
||||||
|
// compare directly against `injected_cycles`.
|
||||||
|
DISCARD_OVERMAX_N.fetch_add(1, Ordering::Relaxed);
|
||||||
|
DISCARD_OVERMAX_CYCLES.fetch_add(interval.saturating_mul(pct) / 100, Ordering::Relaxed);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let delta = interval.saturating_mul(pct) / 100;
|
||||||
|
GLOBAL_DELAY.fetch_add(delta, Ordering::Relaxed);
|
||||||
|
let mine = (*slot).causal_delay();
|
||||||
|
(*slot).set_causal_delay(mine.wrapping_add(delta));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Site-boundary hook, called by `SiteGuard` enter/drop when the
|
||||||
|
/// actor's current site changes from `old` to `new`. Sample-only —
|
||||||
|
/// never spins — so it is safe anywhere, including no-preempt regions
|
||||||
|
/// where `check()` cannot run.
|
||||||
|
///
|
||||||
|
/// - Leaving the experiment's target site: flush the pending interval.
|
||||||
|
/// Cold checks only sample when they happen to fire in-site, so the
|
||||||
|
/// tail between the last check and the guard drop was otherwise
|
||||||
|
/// discarded on every site entry — measured live at ~22-29µs/entry,
|
||||||
|
/// ~6-7% of all target time (eff 0.93), which under-reported every
|
||||||
|
/// impact (+83.5% where theory says +100%).
|
||||||
|
/// - Entering the target site: re-arm the sample clock, so time spent
|
||||||
|
/// *before* the site can never be attributed to it by the first
|
||||||
|
/// in-site check (the symmetric over-attribution).
|
||||||
|
#[inline(never)]
|
||||||
|
fn site_transition(slot: *const crate::runtime::Slot, old: u32, new: u32) {
|
||||||
|
crate::context::tls_fence();
|
||||||
|
let exp = EXPERIMENT.load(Ordering::Relaxed);
|
||||||
|
if exp == 0 || old == new {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let target = (exp >> 32) as u32;
|
||||||
|
let pct = exp & 0xffff_ffff;
|
||||||
|
if old == target && new != target {
|
||||||
|
// Serialised: guard exit bounds the site's interval exactly.
|
||||||
|
let now = preempt::rdtsc_serialising();
|
||||||
|
let last = LAST_SAMPLE_TSC.with(|c| c.replace(now));
|
||||||
|
if pct > 0 {
|
||||||
|
if last != 0 {
|
||||||
|
// SAFETY: forwarded from the guard, which holds the on-CPU
|
||||||
|
// actor's slot for its whole life (see `SiteGuard::slot`).
|
||||||
|
unsafe { attribute(slot, now.saturating_sub(last), pct) };
|
||||||
|
} else {
|
||||||
|
DISCARD_UNARMED_N.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if new == target && old != target {
|
||||||
|
// Serialised: an early arm would let pre-site work leak into the
|
||||||
|
// first in-site interval — the over-attribution this guards.
|
||||||
|
LAST_SAMPLE_TSC.with(|c| c.set(preempt::rdtsc_serialising()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Resume-path hook (scheduler thread, actor off-CPU). Two duties:
|
||||||
|
///
|
||||||
|
/// - If the last deschedule was a *real park*, time blocked absorbs any
|
||||||
|
/// delay accrued meanwhile for free — Coz's blocked-thread rule, which
|
||||||
|
/// keeps experiments from punishing actors for waiting. An actor that
|
||||||
|
/// merely yielded (slice expiry) was runnable the whole time and keeps
|
||||||
|
/// its debt: it must pay by spinning at its next check. Forgiving on
|
||||||
|
/// every resume would make any yield-cadence actor delay-immune and
|
||||||
|
/// experiments inert (found live on a 24-core run: nothing slowed).
|
||||||
|
/// - If the deschedule was a *yield* in the live experiment's target
|
||||||
|
/// site, count the off-CPU gap it opened into the offcpu audit bucket
|
||||||
|
/// (RFC 007: the located @50 deficit — runnable queue-wait is wall
|
||||||
|
/// time in-site that on-CPU attribution correctly skips). Same-window
|
||||||
|
/// only, enforced by the experiment epoch; measure-only.
|
||||||
|
/// - Arm this thread's sample clock so the first interval of the resume
|
||||||
|
/// excludes scheduler time.
|
||||||
|
#[inline]
|
||||||
|
pub(crate) fn on_resume(slot: &crate::runtime::Slot) {
|
||||||
|
let (desched_tsc, desched_epoch) = slot.take_causal_desched();
|
||||||
|
if desched_tsc != 0 && desched_epoch == EXPERIMENT_EPOCH.load(Ordering::Relaxed) {
|
||||||
|
// Same epoch ⇒ no `begin()` since the stash; a nonzero word ⇒
|
||||||
|
// no `end()` either — the gap closed inside its own window.
|
||||||
|
let exp = EXPERIMENT.load(Ordering::Relaxed);
|
||||||
|
if exp != 0 {
|
||||||
|
let pct = exp & 0xffff_ffff;
|
||||||
|
let gap = preempt::rdtsc()
|
||||||
|
.saturating_sub(desched_tsc)
|
||||||
|
.min(MAX_SAMPLE_CYCLES);
|
||||||
|
OFFCPU_IN_SITE_CYCLES.fetch_add(gap.saturating_mul(pct) / 100, Ordering::Relaxed);
|
||||||
|
OFFCPU_IN_SITE_N.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if slot.take_causal_parked() {
|
||||||
|
let global = GLOBAL_DELAY.load(Ordering::Relaxed);
|
||||||
|
let mine = slot.causal_delay();
|
||||||
|
if mine < global {
|
||||||
|
PARK_FORGIVEN_CYCLES.fetch_add(global - mine, Ordering::Relaxed);
|
||||||
|
slot.set_causal_delay(global);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
LAST_SAMPLE_TSC.with(|c| c.set(preempt::rdtsc()));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Deschedule-path hook (scheduler side, same OS thread the actor just
|
||||||
|
/// ran on). If an experiment is live and the departing actor sits in the
|
||||||
|
/// target site, the sample tail `[last sample -> now]` is about to be
|
||||||
|
/// lost: nothing flushes it here, and `on_resume` re-arms the clock
|
||||||
|
/// before the actor runs again. Measure-only (RFC 007 deficit hunt) —
|
||||||
|
/// tally the would-be attribution into the park/yield drop buckets and
|
||||||
|
/// leave behaviour untouched. The interval is capped at
|
||||||
|
/// MAX_SAMPLE_CYCLES: past that the flush would have discarded it anyway
|
||||||
|
/// (counted separately). `now` includes the few hundred ns of scheduler
|
||||||
|
/// bookkeeping since the actor actually stopped — an acceptable
|
||||||
|
/// overcount for a diagnostic.
|
||||||
|
///
|
||||||
|
/// Slice-expiry yields sample at the same checkpoint that deschedules
|
||||||
|
/// them, so their tails are ~zero by construction; a fat yield bucket
|
||||||
|
/// therefore points at explicit `yield_now` calls or requeued parks.
|
||||||
|
///
|
||||||
|
/// Yields additionally stash the deschedule instant on the slot so
|
||||||
|
/// `on_resume` can count the runnable off-CPU gap (offcpu bucket).
|
||||||
|
pub(crate) fn on_deschedule(slot: &crate::runtime::Slot, real_park: bool) {
|
||||||
|
let exp = EXPERIMENT.load(Ordering::Relaxed);
|
||||||
|
if exp == 0 {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let target = (exp >> 32) as u32;
|
||||||
|
let pct = exp & 0xffff_ffff;
|
||||||
|
if pct == 0 || slot.causal_site() != target {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let now = preempt::rdtsc();
|
||||||
|
if !real_park {
|
||||||
|
// Runnable gap opens here; `on_resume` closes and counts it
|
||||||
|
// (offcpu bucket). Parks are excluded: blocked time is already
|
||||||
|
// represented by forgiveness, and blocked wall time is not
|
||||||
|
// queue-wait.
|
||||||
|
slot.set_causal_desched(now, EXPERIMENT_EPOCH.load(Ordering::Relaxed));
|
||||||
|
}
|
||||||
|
let last = LAST_SAMPLE_TSC.with(|c| c.get());
|
||||||
|
if last == 0 {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let interval = now.saturating_sub(last).min(MAX_SAMPLE_CYCLES);
|
||||||
|
let would_be = interval.saturating_mul(pct) / 100;
|
||||||
|
if real_park {
|
||||||
|
DROP_PARK_N.fetch_add(1, Ordering::Relaxed);
|
||||||
|
DROP_PARK_CYCLES.fetch_add(would_be, Ordering::Relaxed);
|
||||||
|
} else {
|
||||||
|
DROP_YIELD_N.fetch_add(1, Ordering::Relaxed);
|
||||||
|
DROP_YIELD_CYCLES.fetch_add(would_be, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------
|
||||||
|
// Experiments
|
||||||
|
// -----------------------------------------------------------------------
|
||||||
|
|
||||||
|
fn begin(site: u32, pct: u32) {
|
||||||
|
EXPERIMENT_EPOCH.fetch_add(1, Ordering::Relaxed);
|
||||||
|
EXPERIMENT.store(((site as u64) << 32) | pct as u64, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn end() {
|
||||||
|
EXPERIMENT.store(0, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Total virtual delay injected so far, in TSC cycles.
|
||||||
|
pub fn global_delay_cycles() -> u64 {
|
||||||
|
GLOBAL_DELAY.load(Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Cumulative ledger-audit totals since startup (RFC 007 deficit hunt).
|
||||||
|
/// All monotone; window a span by snapshotting before/after and taking
|
||||||
|
/// `delta_since`. Cycle fields are in would-be-injected delta terms so
|
||||||
|
/// they compare directly against `injected_cycles`.
|
||||||
|
#[derive(Clone, Copy, Debug, Default)]
|
||||||
|
pub struct LedgerCounters {
|
||||||
|
pub spin_absorbed_cycles: u64,
|
||||||
|
pub park_forgiven_cycles: u64,
|
||||||
|
pub drop_park_cycles: u64,
|
||||||
|
pub drop_park_n: u64,
|
||||||
|
pub drop_yield_cycles: u64,
|
||||||
|
pub drop_yield_n: u64,
|
||||||
|
pub discard_overmax_cycles: u64,
|
||||||
|
pub discard_overmax_n: u64,
|
||||||
|
pub discard_unarmed_n: u64,
|
||||||
|
pub offcpu_in_site_cycles: u64,
|
||||||
|
pub offcpu_in_site_n: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl LedgerCounters {
|
||||||
|
/// Field-wise difference against an earlier snapshot.
|
||||||
|
pub fn delta_since(&self, before: &LedgerCounters) -> LedgerCounters {
|
||||||
|
LedgerCounters {
|
||||||
|
spin_absorbed_cycles: self
|
||||||
|
.spin_absorbed_cycles
|
||||||
|
.saturating_sub(before.spin_absorbed_cycles),
|
||||||
|
park_forgiven_cycles: self
|
||||||
|
.park_forgiven_cycles
|
||||||
|
.saturating_sub(before.park_forgiven_cycles),
|
||||||
|
drop_park_cycles: self
|
||||||
|
.drop_park_cycles
|
||||||
|
.saturating_sub(before.drop_park_cycles),
|
||||||
|
drop_park_n: self.drop_park_n.saturating_sub(before.drop_park_n),
|
||||||
|
drop_yield_cycles: self
|
||||||
|
.drop_yield_cycles
|
||||||
|
.saturating_sub(before.drop_yield_cycles),
|
||||||
|
drop_yield_n: self.drop_yield_n.saturating_sub(before.drop_yield_n),
|
||||||
|
discard_overmax_cycles: self
|
||||||
|
.discard_overmax_cycles
|
||||||
|
.saturating_sub(before.discard_overmax_cycles),
|
||||||
|
discard_overmax_n: self
|
||||||
|
.discard_overmax_n
|
||||||
|
.saturating_sub(before.discard_overmax_n),
|
||||||
|
discard_unarmed_n: self
|
||||||
|
.discard_unarmed_n
|
||||||
|
.saturating_sub(before.discard_unarmed_n),
|
||||||
|
offcpu_in_site_cycles: self
|
||||||
|
.offcpu_in_site_cycles
|
||||||
|
.saturating_sub(before.offcpu_in_site_cycles),
|
||||||
|
offcpu_in_site_n: self
|
||||||
|
.offcpu_in_site_n
|
||||||
|
.saturating_sub(before.offcpu_in_site_n),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Snapshot the cumulative audit counters.
|
||||||
|
pub fn ledger_counters() -> LedgerCounters {
|
||||||
|
LedgerCounters {
|
||||||
|
spin_absorbed_cycles: SPIN_ABSORBED_CYCLES.load(Ordering::Relaxed),
|
||||||
|
park_forgiven_cycles: PARK_FORGIVEN_CYCLES.load(Ordering::Relaxed),
|
||||||
|
drop_park_cycles: DROP_PARK_CYCLES.load(Ordering::Relaxed),
|
||||||
|
drop_park_n: DROP_PARK_N.load(Ordering::Relaxed),
|
||||||
|
drop_yield_cycles: DROP_YIELD_CYCLES.load(Ordering::Relaxed),
|
||||||
|
drop_yield_n: DROP_YIELD_N.load(Ordering::Relaxed),
|
||||||
|
discard_overmax_cycles: DISCARD_OVERMAX_CYCLES.load(Ordering::Relaxed),
|
||||||
|
discard_overmax_n: DISCARD_OVERMAX_N.load(Ordering::Relaxed),
|
||||||
|
discard_unarmed_n: DISCARD_UNARMED_N.load(Ordering::Relaxed),
|
||||||
|
offcpu_in_site_cycles: OFFCPU_IN_SITE_CYCLES.load(Ordering::Relaxed),
|
||||||
|
offcpu_in_site_n: OFFCPU_IN_SITE_N.load(Ordering::Relaxed),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Absorbed-delay ledger of the on-CPU actor (testing/introspection).
|
||||||
|
pub fn my_absorbed_delay_cycles() -> u64 {
|
||||||
|
let slot = preempt::current_slot_ptr();
|
||||||
|
if slot.is_null() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
// SAFETY: on-CPU actor's slot; see `check_cancelled`.
|
||||||
|
unsafe { (*slot).causal_delay() }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Test support: start an experiment targeting `site_name` at `pct`%
|
||||||
|
/// virtual speedup. Registers the site if needed.
|
||||||
|
pub fn begin_experiment_for_test(name: &'static str, pct: u32) {
|
||||||
|
begin(site_id(name), pct);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Test support: stop the running experiment.
|
||||||
|
pub fn end_experiment_for_test() {
|
||||||
|
end();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Test support: grow the global delay ledger directly, as if target-site
|
||||||
|
/// samples had attributed `cycles` — deterministic driver for the timer
|
||||||
|
/// virtual-time tests. Calibrates the TSC eagerly so conversion later
|
||||||
|
/// never stalls a scheduler loop.
|
||||||
|
pub fn inject_delay_cycles_for_test(cycles: u64) {
|
||||||
|
let _ = tsc_hz();
|
||||||
|
GLOBAL_DELAY.fetch_add(cycles, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Convert ledger cycles to wall time at the measured TSC rate.
|
||||||
|
pub fn cycles_to_duration(cycles: u64) -> Duration {
|
||||||
|
Duration::from_secs_f64(cycles as f64 / tsc_hz())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Controller parameters: which speedups to try per site, and the
|
||||||
|
/// experiment/cooldown windows.
|
||||||
|
pub struct ExperimentPlan {
|
||||||
|
pub speedups_pct: Vec<u32>,
|
||||||
|
pub experiment: Duration,
|
||||||
|
pub cooldown: Duration,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for ExperimentPlan {
|
||||||
|
fn default() -> Self {
|
||||||
|
ExperimentPlan {
|
||||||
|
speedups_pct: vec![0, 25, 50],
|
||||||
|
experiment: Duration::from_millis(500),
|
||||||
|
cooldown: Duration::from_millis(100),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// One completed experiment cell.
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct ExperimentResult {
|
||||||
|
pub site: String,
|
||||||
|
pub speedup_pct: u32,
|
||||||
|
pub duration: Duration,
|
||||||
|
/// Progress-point deltas over the window, `(name, count)`.
|
||||||
|
pub deltas: Vec<(String, u64)>,
|
||||||
|
/// Virtual delay injected during the window (cycles).
|
||||||
|
pub injected_cycles: u64,
|
||||||
|
// Ledger-audit deltas over the window (RFC 007 deficit hunt); see
|
||||||
|
// `LedgerCounters` for field semantics. `spin_absorbed_cycles > 0`
|
||||||
|
// in a 0% cell means the window paid debt left over from an earlier
|
||||||
|
// one — the baseline-contamination signature.
|
||||||
|
pub spin_absorbed_cycles: u64,
|
||||||
|
pub park_forgiven_cycles: u64,
|
||||||
|
pub drop_park_cycles: u64,
|
||||||
|
pub drop_park_n: u64,
|
||||||
|
pub drop_yield_cycles: u64,
|
||||||
|
pub drop_yield_n: u64,
|
||||||
|
pub discard_overmax_cycles: u64,
|
||||||
|
pub discard_overmax_n: u64,
|
||||||
|
pub discard_unarmed_n: u64,
|
||||||
|
pub offcpu_in_site_cycles: u64,
|
||||||
|
pub offcpu_in_site_n: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Run the plan synchronously on the calling (OS) thread: for every
|
||||||
|
/// registered site × speedup, run one experiment window and record
|
||||||
|
/// progress-point deltas, with a cooldown between cells. Sites and
|
||||||
|
/// progress points must already be registered (the workload has to be
|
||||||
|
/// running); the caller owns workload start/stop.
|
||||||
|
///
|
||||||
|
/// v1 controller: exhaustive sweep, fixed windows, no adaptive site
|
||||||
|
/// selection or confidence stopping (jar Q5).
|
||||||
|
///
|
||||||
|
/// Callable from a plain OS thread *or* from inside an actor: sleeping
|
||||||
|
/// parks the green thread when we're on one (so no scheduler thread is
|
||||||
|
/// blocked), and falls back to `thread::sleep` otherwise.
|
||||||
|
pub fn run_experiments(plan: &ExperimentPlan) -> Vec<ExperimentResult> {
|
||||||
|
fn controller_sleep(d: Duration) {
|
||||||
|
if preempt::current_slot_ptr().is_null() {
|
||||||
|
std::thread::sleep(d);
|
||||||
|
} else {
|
||||||
|
// Wall-anchored: the controller's window/cooldown sleeps
|
||||||
|
// *define* the experiment's wall length; letting them chase
|
||||||
|
// the delay it is itself injecting would stretch every window
|
||||||
|
// (observed ~2x at 50% speedup). Deltas are rate-normalized
|
||||||
|
// either way — this fixes cost, not bias.
|
||||||
|
crate::scheduler::sleep_wall(d);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Calibrate before any window so report rendering never has to sleep.
|
||||||
|
let _ = tsc_hz();
|
||||||
|
let site_list: Vec<(u32, String)> = {
|
||||||
|
let v = lock_unpoisoned(sites());
|
||||||
|
v.iter()
|
||||||
|
.enumerate()
|
||||||
|
.map(|(i, n)| ((i + 1) as u32, (*n).to_string()))
|
||||||
|
.collect()
|
||||||
|
};
|
||||||
|
let mut out = Vec::new();
|
||||||
|
for (sid, sname) in &site_list {
|
||||||
|
for &pct in &plan.speedups_pct {
|
||||||
|
let before = progress_snapshot();
|
||||||
|
let injected_before = global_delay_cycles();
|
||||||
|
let audit_before = ledger_counters();
|
||||||
|
let t0 = Instant::now();
|
||||||
|
begin(*sid, pct);
|
||||||
|
controller_sleep(plan.experiment);
|
||||||
|
end();
|
||||||
|
// Snapshot immediately: injection and spin freeze at `end()`
|
||||||
|
// (checks gate on the experiment word), but forgiveness does
|
||||||
|
// not — a later snapshot would leak cooldown wakes into the
|
||||||
|
// window.
|
||||||
|
let audit = ledger_counters().delta_since(&audit_before);
|
||||||
|
let elapsed = t0.elapsed();
|
||||||
|
let after = progress_snapshot();
|
||||||
|
let deltas = after
|
||||||
|
.iter()
|
||||||
|
.map(|(n, c)| {
|
||||||
|
let b = before
|
||||||
|
.iter()
|
||||||
|
.find(|(bn, _)| bn == n)
|
||||||
|
.map(|(_, bc)| *bc)
|
||||||
|
.unwrap_or(0);
|
||||||
|
(n.clone(), c.saturating_sub(b))
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
out.push(ExperimentResult {
|
||||||
|
site: sname.clone(),
|
||||||
|
speedup_pct: pct,
|
||||||
|
duration: elapsed,
|
||||||
|
deltas,
|
||||||
|
injected_cycles: global_delay_cycles() - injected_before,
|
||||||
|
spin_absorbed_cycles: audit.spin_absorbed_cycles,
|
||||||
|
park_forgiven_cycles: audit.park_forgiven_cycles,
|
||||||
|
drop_park_cycles: audit.drop_park_cycles,
|
||||||
|
drop_park_n: audit.drop_park_n,
|
||||||
|
drop_yield_cycles: audit.drop_yield_cycles,
|
||||||
|
drop_yield_n: audit.drop_yield_n,
|
||||||
|
discard_overmax_cycles: audit.discard_overmax_cycles,
|
||||||
|
discard_overmax_n: audit.discard_overmax_n,
|
||||||
|
discard_unarmed_n: audit.discard_unarmed_n,
|
||||||
|
offcpu_in_site_cycles: audit.offcpu_in_site_cycles,
|
||||||
|
offcpu_in_site_n: audit.offcpu_in_site_n,
|
||||||
|
});
|
||||||
|
controller_sleep(plan.cooldown);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------
|
||||||
|
// Reports
|
||||||
|
// -----------------------------------------------------------------------
|
||||||
|
|
||||||
|
/// Measured TSC frequency (Hz), calibrated once. The crate-wide 3 GHz
|
||||||
|
/// constant is fine for the *relative* timeslice check, but report
|
||||||
|
/// normalisation divides wall time by injected time, so a 20% Hz error
|
||||||
|
/// skews every impact number — measured live: a 3.7 GHz box inflated all
|
||||||
|
/// baselines uniformly. Calibrated against `Instant` over ~50ms on first
|
||||||
|
/// use; `run_experiments` triggers it before its first window (using the
|
||||||
|
/// park-aware sleep, so no scheduler thread is blocked when called from
|
||||||
|
/// an actor).
|
||||||
|
static TSC_HZ_MEASURED: OnceLock<f64> = OnceLock::new();
|
||||||
|
|
||||||
|
/// Measured TSC frequency in Hz. Calibrates on first call (~50ms).
|
||||||
|
pub fn tsc_hz() -> f64 {
|
||||||
|
*TSC_HZ_MEASURED.get_or_init(|| {
|
||||||
|
let c0 = preempt::rdtsc();
|
||||||
|
let t0 = Instant::now();
|
||||||
|
let d = Duration::from_millis(50);
|
||||||
|
if preempt::current_slot_ptr().is_null() {
|
||||||
|
std::thread::sleep(d);
|
||||||
|
} else {
|
||||||
|
// Wall-anchored: calibration divides TSC delta by *wall*
|
||||||
|
// elapsed; a virtual sleep dilated by concurrent injection
|
||||||
|
// would still measure correctly (elapsed() is wall) but
|
||||||
|
// waste window time — and must never depend on the ledger
|
||||||
|
// it exists to convert.
|
||||||
|
crate::scheduler::sleep_wall(d);
|
||||||
|
}
|
||||||
|
(preempt::rdtsc().wrapping_sub(c0)) as f64 / t0.elapsed().as_secs_f64()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Normalized rate for one cell: count over the *virtual* window
|
||||||
|
/// (wall − injected) — Coz's normalization: injected delay does not
|
||||||
|
/// exist in the virtual timeline. A bottleneck site keeps its raw count
|
||||||
|
/// while shrinking the divisor → positive impact; a fully overlapped
|
||||||
|
/// site loses count proportionally → ~zero.
|
||||||
|
fn normalized_rate(r: &ExperimentResult, point: &str) -> Option<f64> {
|
||||||
|
let count = r.deltas.iter().find(|(n, _)| n == point).map(|(_, c)| *c)?;
|
||||||
|
let injected_secs = r.injected_cycles as f64 / tsc_hz();
|
||||||
|
let virtual_secs = (r.duration.as_secs_f64() - injected_secs).max(1e-9);
|
||||||
|
Some(count as f64 / virtual_secs)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Impact of virtually speeding up `site` by `speedup_pct` on progress
|
||||||
|
/// point `point`, in percent relative to that site's own 0% baseline
|
||||||
|
/// cell. `None` if either cell or the point is missing, or the baseline
|
||||||
|
/// rate is zero. This is the machine-readable form of the summary's
|
||||||
|
/// "vs baseline" column, for programmatic checks (CI, examples).
|
||||||
|
pub fn impact_pct(
|
||||||
|
results: &[ExperimentResult],
|
||||||
|
site: &str,
|
||||||
|
speedup_pct: u32,
|
||||||
|
point: &str,
|
||||||
|
) -> Option<f64> {
|
||||||
|
let cell = results
|
||||||
|
.iter()
|
||||||
|
.find(|r| r.site == site && r.speedup_pct == speedup_pct)?;
|
||||||
|
let base = results
|
||||||
|
.iter()
|
||||||
|
.find(|r| r.site == site && r.speedup_pct == 0)?;
|
||||||
|
let rate = normalized_rate(cell, point)?;
|
||||||
|
let b = normalized_rate(base, point)?;
|
||||||
|
if b <= 0.0 {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
Some((rate / b - 1.0) * 100.0)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Human-readable summary: per (site, progress point), the throughput at
|
||||||
|
/// each virtual speedup and the change relative to that site's own 0%
|
||||||
|
/// baseline. A near-zero column across speedups means: optimising this
|
||||||
|
/// site buys you nothing — the RFC's headline answer.
|
||||||
|
///
|
||||||
|
/// Ends with a one-line fidelity note (RFC 007 Validation): reported
|
||||||
|
/// impacts are conservative — attribution counts on-CPU site time only,
|
||||||
|
/// so runnable queue-wait inside the site (the located @50 "deficit",
|
||||||
|
/// eff ≈ 0.93 live) is never injected and gains are lower bounds; site
|
||||||
|
/// *rankings* are unaffected.
|
||||||
|
pub fn render_summary(results: &[ExperimentResult]) -> String {
|
||||||
|
use std::fmt::Write;
|
||||||
|
let mut s = String::new();
|
||||||
|
let _ = writeln!(s, "== smarm causal profile ==");
|
||||||
|
let mut sites_seen: Vec<&str> = Vec::new();
|
||||||
|
for r in results {
|
||||||
|
if !sites_seen.contains(&r.site.as_str()) {
|
||||||
|
sites_seen.push(&r.site);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for site in sites_seen {
|
||||||
|
let _ = writeln!(s, "site {site}");
|
||||||
|
for r in results.iter().filter(|r| r.site == site) {
|
||||||
|
for (name, _) in &r.deltas {
|
||||||
|
let rate = match normalized_rate(r, name) {
|
||||||
|
Some(x) => x,
|
||||||
|
None => continue,
|
||||||
|
};
|
||||||
|
let rel = impact_pct(results, site, r.speedup_pct, name)
|
||||||
|
.map(|p| format!("{p:+.1}%"))
|
||||||
|
.unwrap_or_else(|| "n/a".to_string());
|
||||||
|
let _ = writeln!(
|
||||||
|
s,
|
||||||
|
" speedup {:>3}% {name:<24} {rate:>12.1}/s vs baseline {rel} (injected {:.1}ms)",
|
||||||
|
r.speedup_pct,
|
||||||
|
r.injected_cycles as f64 / tsc_hz() * 1e3
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !results.is_empty() {
|
||||||
|
let _ = writeln!(
|
||||||
|
s,
|
||||||
|
"note: impacts are lower bounds — site time counts on-CPU only (runnable queue-wait is not attributed); rankings unaffected"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
s
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Ledger-audit companion to `render_summary` (RFC 007 deficit hunt):
|
||||||
|
/// per cell, where the window's virtual delay went — born (injected),
|
||||||
|
/// paid (absorbed), waived (forgiven at wake) — and the attribution the
|
||||||
|
/// sampler lost: tails dropped at parks/yields inside the target site,
|
||||||
|
/// plus clamp discards. Cycle columns in ms at the calibrated TSC rate.
|
||||||
|
/// `absorbed` above `injected` in a cell (0% especially) means it paid
|
||||||
|
/// debt left over from earlier windows.
|
||||||
|
pub fn render_ledger_audit(results: &[ExperimentResult]) -> String {
|
||||||
|
use std::fmt::Write;
|
||||||
|
let hz = tsc_hz();
|
||||||
|
let ms = |c: u64| c as f64 / hz * 1e3;
|
||||||
|
let mut s = String::new();
|
||||||
|
let _ = writeln!(s, "== smarm causal ledger audit ==");
|
||||||
|
for r in results {
|
||||||
|
let _ = writeln!(
|
||||||
|
s,
|
||||||
|
"site {:<22} @{:>2}% injected {:>7.1}ms absorbed {:>7.1}ms forgiven {:>7.1}ms \
|
||||||
|
drop park {:>6.2}ms/{:<4} yield {:>6.2}ms/{:<4} offcpu {:>6.2}ms/{:<5} \
|
||||||
|
discard >max {:>6.2}ms/{:<3} unarmed {}",
|
||||||
|
r.site,
|
||||||
|
r.speedup_pct,
|
||||||
|
ms(r.injected_cycles),
|
||||||
|
ms(r.spin_absorbed_cycles),
|
||||||
|
ms(r.park_forgiven_cycles),
|
||||||
|
ms(r.drop_park_cycles),
|
||||||
|
r.drop_park_n,
|
||||||
|
ms(r.drop_yield_cycles),
|
||||||
|
r.drop_yield_n,
|
||||||
|
ms(r.offcpu_in_site_cycles),
|
||||||
|
r.offcpu_in_site_n,
|
||||||
|
ms(r.discard_overmax_cycles),
|
||||||
|
r.discard_overmax_n,
|
||||||
|
r.discard_unarmed_n
|
||||||
|
);
|
||||||
|
}
|
||||||
|
s
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Coz-compatible profile text (`profile.coz`), so Coz's existing plot
|
||||||
|
/// tooling renders our experiments — the RFC's "don't build a UI" call.
|
||||||
|
pub fn render_coz(results: &[ExperimentResult]) -> String {
|
||||||
|
use std::fmt::Write;
|
||||||
|
let mut s = String::new();
|
||||||
|
let _ = writeln!(s, "startup\ttime=0");
|
||||||
|
for r in results {
|
||||||
|
let _ = writeln!(
|
||||||
|
s,
|
||||||
|
"experiment\tselected={}\tspeedup={:.2}\tduration={}\tselected-samples=1",
|
||||||
|
r.site,
|
||||||
|
r.speedup_pct as f64 / 100.0,
|
||||||
|
r.duration.as_nanos()
|
||||||
|
);
|
||||||
|
for (name, count) in &r.deltas {
|
||||||
|
let _ = writeln!(s, "throughput-point\tname={name}\tdelta={count}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
s
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "smarm-causal")]
|
||||||
|
pub use inner::*;
|
||||||
|
|
||||||
|
/// Mark one unit of useful work complete at a named throughput progress
|
||||||
|
/// point (RFC 007). One Relaxed increment when `smarm-causal` is on; nothing
|
||||||
|
/// at all when it's off.
|
||||||
|
#[cfg(feature = "smarm-causal")]
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! progress {
|
||||||
|
($name:literal) => {{
|
||||||
|
static __SMARM_PP: ::std::sync::OnceLock<&'static $crate::causal::ProgressPoint> =
|
||||||
|
::std::sync::OnceLock::new();
|
||||||
|
__SMARM_PP
|
||||||
|
.get_or_init(|| $crate::causal::register_progress($name))
|
||||||
|
.bump();
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(feature = "smarm-causal"))]
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! progress {
|
||||||
|
($name:literal) => {{}};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Enter a named causal-profiling site for the current actor; the returned
|
||||||
|
/// guard exits it (restoring any enclosing site) on drop. Site identity is
|
||||||
|
/// stored in the actor's slot, so it survives preemption and migration.
|
||||||
|
/// Expands to a unit no-op without `smarm-causal`.
|
||||||
|
#[cfg(feature = "smarm-causal")]
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! causal_site {
|
||||||
|
($name:literal) => {{
|
||||||
|
static __SMARM_SITE: ::std::sync::OnceLock<u32> = ::std::sync::OnceLock::new();
|
||||||
|
$crate::causal::SiteGuard::enter(
|
||||||
|
*__SMARM_SITE.get_or_init(|| $crate::causal::site_id($name)),
|
||||||
|
)
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(feature = "smarm-causal"))]
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! causal_site {
|
||||||
|
($name:literal) => {
|
||||||
|
()
|
||||||
|
};
|
||||||
|
}
|
||||||
+420
-234
@@ -1,73 +1,175 @@
|
|||||||
//! Unbounded MPSC channels.
|
//! Unbounded multi-producer, single-consumer channels: how actors talk to
|
||||||
|
//! each other.
|
||||||
//!
|
//!
|
||||||
//! Inner state is `Arc<RawMutex<Inner<T>>>` so channels can be sent across OS
|
//! A channel is a queue with a typed [`Sender`] on one end and a typed
|
||||||
//! threads (required for the multi-scheduler runtime where a sender and
|
//! [`Receiver`] on the other. Any number of actors can hold a clone of the
|
||||||
//! receiver may run on different scheduler threads simultaneously).
|
//! `Sender` and push messages onto the same queue; exactly one [`Receiver`]
|
||||||
|
//! reads them back out, in the order they arrived. This is the basic wiring
|
||||||
|
//! smarm's other actor primitives (`gen_server`, `pg`, the registry) are all
|
||||||
|
//! built out of, and it is directly usable on its own for a worker that just
|
||||||
|
//! needs an inbox.
|
||||||
//!
|
//!
|
||||||
//! ## Why `RawMutex` (Channel class), not `std::sync::Mutex`
|
//! ## A first channel
|
||||||
//!
|
//!
|
||||||
//! An actor holding a guard with preemption *enabled* can be timesliced
|
//! ```
|
||||||
//! inside the critical section and resume on a different OS thread — the
|
//! use smarm::{channel, run, spawn};
|
||||||
//! pthread mutex would then be released from a thread that didn't lock it,
|
|
||||||
//! which is UB (Linux futexes happen to tolerate it, but it's not
|
|
||||||
//! guaranteed). `RawMutex` disables preemption for the guard's span and is
|
|
||||||
//! cross-thread-release sound by construction, closing the hole. It also
|
|
||||||
//! cannot poison. Channel locks form their own [`LockClass::Channel`]
|
|
||||||
//! (raw_mutex.rs): they may be taken under a cold (Leaf) lock — finalize and
|
|
||||||
//! `monitor()` clone senders that live in slots — but nothing may be locked
|
|
||||||
//! under them, which the debug build enforces. `recv_match` runs its user
|
|
||||||
//! predicate under this lock: keep it cheap, pure, and channel-free.
|
|
||||||
//!
|
//!
|
||||||
//! Semantics:
|
//! run(|| {
|
||||||
//! - Senders are clonable; the last sender drop closes the channel.
|
//! let (tx, rx) = channel::<u64>();
|
||||||
//! - `Receiver::recv` on an empty open channel parks the receiver.
|
//!
|
||||||
//! - `Receiver::recv` on an empty closed channel returns `Err(RecvError)`.
|
//! let worker = spawn(move || {
|
||||||
//! - `Sender::send` on an open channel always succeeds.
|
//! // Blocks until a message arrives.
|
||||||
//! - `Sender::send` on a closed channel (receiver dropped) returns
|
//! let n = rx.recv().unwrap();
|
||||||
//! `Err(SendError(value))`.
|
//! assert_eq!(n, 42);
|
||||||
//! - When a send pushes to a previously empty queue and a receiver is
|
//!
|
||||||
//! parked, the receiver is unparked.
|
//! // Once every Sender is dropped, recv() reports the channel closed
|
||||||
|
//! // instead of blocking forever.
|
||||||
|
//! assert!(rx.recv().is_err());
|
||||||
|
//! });
|
||||||
|
//!
|
||||||
|
//! tx.send(42).unwrap();
|
||||||
|
//! drop(tx); // last sender gone: the channel is now closed
|
||||||
|
//! worker.join().unwrap();
|
||||||
|
//! });
|
||||||
|
//! ```
|
||||||
|
//!
|
||||||
|
//! ## Sending
|
||||||
|
//!
|
||||||
|
//! [`Sender`] is cheaply clonable: hand a clone to every actor that needs to
|
||||||
|
//! push messages into this queue. The channel stays open as long as at least
|
||||||
|
//! one clone exists; [`Sender::send`] never blocks and always succeeds while
|
||||||
|
//! the channel is open, since the queue is unbounded. Once the [`Receiver`]
|
||||||
|
//! has been dropped, `send` returns the message back to you in
|
||||||
|
//! [`SendError`] instead of delivering it.
|
||||||
|
//!
|
||||||
|
//! ## Receiving
|
||||||
|
//!
|
||||||
|
//! There is exactly one [`Receiver`] per channel (it is not clonable).
|
||||||
|
//! [`Receiver::recv`] returns the next message in arrival order, parking the
|
||||||
|
//! calling actor if the queue is currently empty. Once every `Sender` has
|
||||||
|
//! been dropped and the queue has been drained, `recv` stops parking and
|
||||||
|
//! returns [`RecvError`] instead, so a receiver never blocks forever waiting
|
||||||
|
//! on senders that are never coming back.
|
||||||
|
//!
|
||||||
|
//! Beyond plain `recv`, three variants cover the common needs:
|
||||||
|
//!
|
||||||
|
//! - [`Receiver::try_recv`]: never parks: reports an empty-but-open channel
|
||||||
|
//! as `Ok(None)` instead of waiting.
|
||||||
|
//! - [`Receiver::recv_timeout`]: parks, but gives up and returns
|
||||||
|
//! [`RecvTimeoutError::Timeout`] if no message arrives before a deadline.
|
||||||
|
//! - [`Receiver::recv_match`] / [`Receiver::try_recv_match`]: selective
|
||||||
|
//! receive. Instead of taking whatever is at the front of the queue, pick
|
||||||
|
//! out the first message matching a predicate, leaving the rest queued in
|
||||||
|
//! order. Handy for an actor that wants to prioritise one kind of message
|
||||||
|
//! over others already waiting.
|
||||||
|
//!
|
||||||
|
//! ## Waiting on several channels: `select`
|
||||||
|
//!
|
||||||
|
//! [`select`] parks an actor across several receivers at once and reports
|
||||||
|
//! the index of the first one that is ready (has a message queued, or has
|
||||||
|
//! been closed). [`select_timeout`] adds a deadline, the way `recv_timeout`
|
||||||
|
//! does for a single channel. See their docs for the full contract,
|
||||||
|
//! including the priority-order and no-fairness guarantee.
|
||||||
|
//!
|
||||||
|
//! ## Implementation notes
|
||||||
|
//!
|
||||||
|
//! The queue and its bookkeeping live behind `Arc<RawMutex<Inner<T>>>`
|
||||||
|
//! rather than a `std::sync::Mutex`, so that a channel can be freely shared
|
||||||
|
//! and sent across the OS threads backing the multi-scheduler runtime.
|
||||||
|
//! `RawMutex` matters here for a subtler reason too: an ordinary pthread
|
||||||
|
//! mutex can be released from a different OS thread than the one that took
|
||||||
|
//! it (smarm's preemption can migrate a timesliced actor between scheduler
|
||||||
|
//! threads mid-critical-section), and doing that to a `std::sync::Mutex` is
|
||||||
|
//! undefined behavior. `RawMutex` disables preemption for the guard's short
|
||||||
|
//! lifetime instead, so the release always happens on the thread that
|
||||||
|
//! acquired it, and it has no poisoning to worry about besides. Channel
|
||||||
|
//! locks are cheap and are never held across another lock acquisition or a
|
||||||
|
//! blocking call; the predicate passed to `recv_match` runs under this lock,
|
||||||
|
//! which is why it needs to stay cheap, pure, and must not call back into
|
||||||
|
//! the same channel.
|
||||||
|
|
||||||
use crate::pid::Pid;
|
use crate::pid::Pid;
|
||||||
use crate::raw_mutex::RawMutex;
|
use crate::raw_mutex::RawMutex;
|
||||||
|
use crate::runtime::RuntimeInner;
|
||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
use std::sync::Arc;
|
use std::sync::{Arc, Weak};
|
||||||
|
|
||||||
|
/// Create a new channel and return its `(Sender, Receiver)` halves.
|
||||||
|
///
|
||||||
|
/// The channel is unbounded (no capacity limit) and open until every
|
||||||
|
/// `Sender` has been dropped.
|
||||||
pub fn channel<T>() -> (Sender<T>, Receiver<T>) {
|
pub fn channel<T>() -> (Sender<T>, Receiver<T>) {
|
||||||
let inner = Arc::new(RawMutex::new_channel(Inner {
|
let inner = Arc::new(RawMutex::new_channel(Inner {
|
||||||
queue: VecDeque::new(),
|
queue: VecDeque::new(),
|
||||||
parked_receiver: None,
|
parked_receiver: None,
|
||||||
|
rt: None,
|
||||||
senders: 1,
|
senders: 1,
|
||||||
receiver_alive: true,
|
receiver_alive: true,
|
||||||
}));
|
}));
|
||||||
(Sender { inner: inner.clone() }, Receiver { inner })
|
(
|
||||||
|
Sender {
|
||||||
|
inner: inner.clone(),
|
||||||
|
},
|
||||||
|
Receiver { inner },
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Inner<T> {
|
struct Inner<T> {
|
||||||
queue: VecDeque<T>,
|
queue: VecDeque<T>,
|
||||||
/// The parked receiver's `(pid, park-epoch)`. The epoch is the slot
|
/// The parked receiver's `(pid, park-epoch)`, if one is currently
|
||||||
/// word's runtime-wide wait identity (see slot_state.rs): wakers call
|
/// waiting. The epoch identifies exactly which wait this is, so a waker
|
||||||
/// `unpark_at(pid, epoch)`, so an entry left over from an already-woken
|
/// left over from a wait that already ended (a losing `select` arm, a
|
||||||
/// wait — a `select` loser arm, a satisfied `recv_timeout`'s timer — is
|
/// `recv_timeout` whose timer fired after it was already satisfied) is
|
||||||
/// inert: the wake fails the word's epoch CAS and no-ops. This replaces
|
/// inert and does nothing when it fires.
|
||||||
/// the old per-channel `cur_wait`/`next_wait_seq`/`timed_out` trio: wait
|
|
||||||
/// identity now exists exactly once, in the slot word.
|
|
||||||
parked_receiver: Option<(Pid, u32)>,
|
parked_receiver: Option<(Pid, u32)>,
|
||||||
|
/// The receiver's runtime, captured the first time it parks (so provably
|
||||||
|
/// alive then) and kept for the life of the channel: it lets a sender on a
|
||||||
|
/// foreign OS thread wake the receiver without the `RUNTIME` thread-local,
|
||||||
|
/// which is unset off a scheduler thread. Captured once rather than per
|
||||||
|
/// park because `Arc::downgrade` + drop is a locked RMW pair on a shared
|
||||||
|
/// counter, and parking is the channel hot path. A `Receiver` never
|
||||||
|
/// migrates between runtimes — it is pinned to its actor — so one capture
|
||||||
|
/// stays correct for every later park.
|
||||||
|
rt: Option<Weak<RuntimeInner>>,
|
||||||
senders: usize,
|
senders: usize,
|
||||||
receiver_alive: bool,
|
receiver_alive: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T> Inner<T> {
|
||||||
|
/// Capture the receiver's runtime if we have not already. Called under the
|
||||||
|
/// channel lock at every park site; after the first park it is one branch
|
||||||
|
/// on an `Option`, no atomics.
|
||||||
|
fn note_runtime(&mut self) {
|
||||||
|
if self.rt.is_none() {
|
||||||
|
self.rt = crate::scheduler::runtime_weak();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The sending half of a channel, created by [`channel`]. Clonable: every
|
||||||
|
/// clone pushes onto the same queue, and the channel stays open as long as
|
||||||
|
/// any clone is alive. Dropping the last `Sender` closes the channel, which
|
||||||
|
/// wakes a parked [`Receiver`] so it can observe the closure.
|
||||||
pub struct Sender<T> {
|
pub struct Sender<T> {
|
||||||
inner: Arc<RawMutex<Inner<T>>>,
|
inner: Arc<RawMutex<Inner<T>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The receiving half of a channel, created by [`channel`]. Not clonable:
|
||||||
|
/// a channel has exactly one receiver. Reads messages in the order they
|
||||||
|
/// were sent, via [`recv`](Receiver::recv) and its variants.
|
||||||
pub struct Receiver<T> {
|
pub struct Receiver<T> {
|
||||||
inner: Arc<RawMutex<Inner<T>>>,
|
inner: Arc<RawMutex<Inner<T>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returned by [`Sender::send`] when the channel's [`Receiver`] has already
|
||||||
|
/// been dropped. Carries the message back so it is never silently lost;
|
||||||
|
/// recover it with `.0` or by matching.
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
pub struct SendError<T>(pub T);
|
pub struct SendError<T>(pub T);
|
||||||
|
|
||||||
|
/// Returned by [`Receiver::recv`] (and the other receive methods, in their
|
||||||
|
/// own error types) when the channel is closed: every `Sender` has been
|
||||||
|
/// dropped and no message is left queued.
|
||||||
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
||||||
pub struct RecvError;
|
pub struct RecvError;
|
||||||
|
|
||||||
@@ -84,8 +186,8 @@ impl std::error::Error for RecvError {}
|
|||||||
pub enum RecvTimeoutError {
|
pub enum RecvTimeoutError {
|
||||||
/// The deadline passed with no message available.
|
/// The deadline passed with no message available.
|
||||||
Timeout,
|
Timeout,
|
||||||
/// All senders dropped with no message available — the bounded analogue
|
/// Every sender was dropped with no message available. The
|
||||||
/// of [`RecvError`].
|
/// timeout-aware counterpart of plain [`RecvError`].
|
||||||
Disconnected,
|
Disconnected,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -103,7 +205,9 @@ impl std::error::Error for RecvTimeoutError {}
|
|||||||
impl<T> Clone for Sender<T> {
|
impl<T> Clone for Sender<T> {
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
self.inner.lock().senders += 1;
|
self.inner.lock().senders += 1;
|
||||||
Sender { inner: self.inner.clone() }
|
Sender {
|
||||||
|
inner: self.inner.clone(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,8 +219,8 @@ impl<T> Drop for Sender<T> {
|
|||||||
// Wake the parked receiver on the last sender drop regardless of
|
// Wake the parked receiver on the last sender drop regardless of
|
||||||
// whether the queue is empty. A plain `recv` only ever parks on an
|
// whether the queue is empty. A plain `recv` only ever parks on an
|
||||||
// empty queue (so this is unchanged for it), but a selective
|
// empty queue (so this is unchanged for it), but a selective
|
||||||
// `recv_match` may be parked on a *non-empty* queue holding only
|
// `recv_match` may be parked on a non-empty queue holding only
|
||||||
// non-matching messages — it must wake to observe closure and
|
// non-matching messages. It must wake to observe closure and
|
||||||
// return Err rather than sleep forever.
|
// return Err rather than sleep forever.
|
||||||
if g.senders == 0 {
|
if g.senders == 0 {
|
||||||
g.parked_receiver.take()
|
g.parked_receiver.take()
|
||||||
@@ -125,26 +229,54 @@ impl<T> Drop for Sender<T> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
if let Some((pid, epoch)) = unpark {
|
if let Some((pid, epoch)) = unpark {
|
||||||
crate::scheduler::unpark_at(pid, epoch);
|
crate::scheduler::unpark_at_via(pid, epoch, || self.inner.lock().rt.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Drop for Receiver<T> {
|
impl<T> Drop for Receiver<T> {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
self.inner.lock().receiver_alive = false;
|
// The only consumer is gone: queued messages can never be delivered.
|
||||||
|
// Drop them now instead of leaving them queued until the last Sender
|
||||||
|
// happens to go away, which can be long after this receiver's owner
|
||||||
|
// has exited if some other part of the runtime is still holding a
|
||||||
|
// clone of the Sender. Draining runs each queued message's own drop
|
||||||
|
// glue, which matters for a gen_server call: dropping a queued call
|
||||||
|
// envelope drops its reply channel too, which wakes the caller with
|
||||||
|
// an error instead of leaving it parked forever. Drain under the
|
||||||
|
// lock, then run the drops after releasing it, since a message's
|
||||||
|
// drop glue may itself touch a different channel or the scheduler.
|
||||||
|
let drained = {
|
||||||
|
let mut g = self.inner.lock();
|
||||||
|
g.receiver_alive = false;
|
||||||
|
std::mem::take(&mut g.queue)
|
||||||
|
};
|
||||||
|
drop(drained);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Sender<T> {
|
impl<T> Sender<T> {
|
||||||
/// Number of messages currently queued behind this channel. Introspection
|
/// Number of messages currently queued and not yet received. For
|
||||||
/// only (RFC 016 mailbox depth); takes the channel lock, so callers reach
|
/// introspection and monitoring; takes the channel's internal lock, so
|
||||||
/// it under the registry Leaf (Leaf → Channel) via the erased probe in
|
/// avoid calling it from a hot path.
|
||||||
/// `registry.rs`, never on a hot path.
|
|
||||||
pub(crate) fn queued_len(&self) -> usize {
|
pub(crate) fn queued_len(&self) -> usize {
|
||||||
self.inner.lock().queue.len()
|
self.inner.lock().queue.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Whether the [`Receiver`] is still alive (a send would be accepted).
|
||||||
|
pub(crate) fn receiver_alive(&self) -> bool {
|
||||||
|
self.inner.lock().receiver_alive
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Whether `other` is a sender of this very channel (a clone).
|
||||||
|
pub(crate) fn same_channel(&self, other: &Sender<T>) -> bool {
|
||||||
|
Arc::ptr_eq(&self.inner, &other.inner)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Push `value` onto the channel. Succeeds unconditionally as long as
|
||||||
|
/// the [`Receiver`] is still alive: the queue has no capacity limit, so
|
||||||
|
/// this never blocks and never fails except when the channel is closed,
|
||||||
|
/// in which case `value` comes back in [`SendError`].
|
||||||
pub fn send(&self, value: T) -> Result<(), SendError<T>> {
|
pub fn send(&self, value: T) -> Result<(), SendError<T>> {
|
||||||
let unpark = {
|
let unpark = {
|
||||||
let mut g = self.inner.lock();
|
let mut g = self.inner.lock();
|
||||||
@@ -155,16 +287,28 @@ impl<T> Sender<T> {
|
|||||||
g.parked_receiver.take()
|
g.parked_receiver.take()
|
||||||
};
|
};
|
||||||
if let Some((pid, epoch)) = unpark {
|
if let Some((pid, epoch)) = unpark {
|
||||||
crate::te!(crate::trace::Event::Send { sender: crate::actor::current_pid().unwrap_or(crate::pid::Pid::new(u32::MAX, u32::MAX)), receiver: Some(pid) });
|
crate::te!(crate::trace::Event::Send {
|
||||||
crate::scheduler::unpark_at(pid, epoch);
|
sender: crate::actor::current_pid()
|
||||||
|
.unwrap_or(crate::pid::Pid::new(u32::MAX, u32::MAX)),
|
||||||
|
receiver: Some(pid)
|
||||||
|
});
|
||||||
|
crate::scheduler::unpark_at_via(pid, epoch, || self.inner.lock().rt.clone());
|
||||||
} else {
|
} else {
|
||||||
crate::te!(crate::trace::Event::Send { sender: crate::actor::current_pid().unwrap_or(crate::pid::Pid::new(u32::MAX, u32::MAX)), receiver: None });
|
crate::te!(crate::trace::Event::Send {
|
||||||
|
sender: crate::actor::current_pid()
|
||||||
|
.unwrap_or(crate::pid::Pid::new(u32::MAX, u32::MAX)),
|
||||||
|
receiver: None
|
||||||
|
});
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Receiver<T> {
|
impl<T> Receiver<T> {
|
||||||
|
/// Block until a message is available and return it. Messages come back
|
||||||
|
/// in the order they were sent. If the queue is empty and every
|
||||||
|
/// [`Sender`] has already been dropped, returns [`RecvError`] instead of
|
||||||
|
/// blocking forever.
|
||||||
pub fn recv(&self) -> Result<T, RecvError> {
|
pub fn recv(&self) -> Result<T, RecvError> {
|
||||||
loop {
|
loop {
|
||||||
{
|
{
|
||||||
@@ -176,54 +320,53 @@ impl<T> Receiver<T> {
|
|||||||
if g.senders == 0 {
|
if g.senders == 0 {
|
||||||
return Err(RecvError);
|
return Err(RecvError);
|
||||||
}
|
}
|
||||||
let me = crate::actor::current_pid()
|
let me = match crate::actor::current_pid() {
|
||||||
.expect("recv() called outside an actor");
|
Some(me) => me,
|
||||||
|
None => panic!("smarm: recv() called outside an actor"),
|
||||||
|
};
|
||||||
debug_assert!(
|
debug_assert!(
|
||||||
g.parked_receiver.is_none_or(|(p, _)| p == me),
|
g.parked_receiver.is_none_or(|(p, _)| p == me),
|
||||||
"channel has more than one receiver"
|
"channel has more than one receiver"
|
||||||
);
|
);
|
||||||
// begin_wait is lock-free — legal under the Channel lock;
|
// begin_wait is lock-free, so it's legal under the Channel lock;
|
||||||
// registering in the same critical section makes the epoch
|
// registering in the same critical section makes the epoch
|
||||||
// atomic with the senders' view of the registration.
|
// atomic with the senders' view of the registration.
|
||||||
|
g.note_runtime();
|
||||||
g.parked_receiver = Some((me, crate::scheduler::begin_wait()));
|
g.parked_receiver = Some((me, crate::scheduler::begin_wait()));
|
||||||
crate::te!(crate::trace::Event::RecvPark(me));
|
crate::te!(crate::trace::Event::RecvPark(me));
|
||||||
}
|
}
|
||||||
// Release the lock before parking — the unparker will need it.
|
// Release the lock before parking: the unparker will need it.
|
||||||
crate::scheduler::park_current();
|
crate::scheduler::park_current();
|
||||||
// Woken up — record it before looping to check the queue.
|
// Woken up. Record it before looping to check the queue.
|
||||||
crate::te!(crate::trace::Event::RecvWake(crate::actor::current_pid().unwrap()));
|
crate::te!(crate::trace::Event::RecvWake(
|
||||||
|
match crate::actor::current_pid() {
|
||||||
|
Some(p) => p,
|
||||||
|
None => panic!("smarm: RecvWake outside an actor (core corrupt)"),
|
||||||
|
}
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Bounded receive: like [`recv`](Self::recv), but gives up once
|
/// Like [`recv`](Self::recv), but gives up and returns
|
||||||
/// `timeout` has elapsed, returning [`RecvTimeoutError::Timeout`].
|
/// [`RecvTimeoutError::Timeout`] if no message has arrived by the time
|
||||||
|
/// `timeout` elapses.
|
||||||
///
|
///
|
||||||
/// Built on the same timer machinery as `Mutex::lock_timeout`: the wait
|
/// If a message arrives at essentially the same moment the deadline
|
||||||
/// registers a `WaitTimeout` entry stamped with the wait's park-epoch;
|
/// passes, the message wins: you get `Ok` rather than `Timeout`. If
|
||||||
/// on expiry the channel (as the
|
/// every sender is dropped before a message arrives or the deadline
|
||||||
/// [`TimerTarget`](crate::timer::TimerTarget)) checks whether *this*
|
/// passes, you get [`RecvTimeoutError::Disconnected`].
|
||||||
/// wait is still parked and, only then, cancels it. A wake that races
|
|
||||||
/// the deadline resolves message-first: if a message is available when
|
|
||||||
/// the receiver runs, it is delivered even if the timer had already
|
|
||||||
/// fired. A satisfied or abandoned wait leaves its timer entry to expire
|
|
||||||
/// as a no-op (registration gone; epoch consumed), per the
|
|
||||||
/// no-cancellation convention in `timer.rs`.
|
|
||||||
///
|
///
|
||||||
/// The wake is classified from state alone — wakes are precise (the only
|
/// `Duration::ZERO` is a valid timeout: it still gives any
|
||||||
/// stamped wakers of this wait are a send, the last-sender drop, and the
|
/// already-queued message a chance to be returned, and only then
|
||||||
/// timer; a stop wake unwinds out of `park_current` and never reaches
|
/// reports `Timeout`.
|
||||||
/// the classification), so: message queued → `Ok`; `senders == 0` →
|
|
||||||
/// `Disconnected`; neither → it was the timer → `Timeout`.
|
|
||||||
///
|
|
||||||
/// `Duration::ZERO` is a valid timeout: it parks until the immediately-
|
|
||||||
/// due timer is drained, then reports `Timeout` unless a message was
|
|
||||||
/// already queued.
|
|
||||||
pub fn recv_timeout(&self, timeout: std::time::Duration) -> Result<T, RecvTimeoutError>
|
pub fn recv_timeout(&self, timeout: std::time::Duration) -> Result<T, RecvTimeoutError>
|
||||||
where
|
where
|
||||||
T: Send + 'static,
|
T: Send + 'static,
|
||||||
{
|
{
|
||||||
let me = crate::actor::current_pid()
|
let me = match crate::actor::current_pid() {
|
||||||
.expect("recv_timeout() called outside an actor");
|
Some(me) => me,
|
||||||
|
None => panic!("smarm: recv_timeout() called outside an actor"),
|
||||||
|
};
|
||||||
|
|
||||||
// Fast path + wait registration, one critical section.
|
// Fast path + wait registration, one critical section.
|
||||||
let epoch;
|
let epoch;
|
||||||
@@ -241,20 +384,26 @@ impl<T> Receiver<T> {
|
|||||||
"channel has more than one receiver"
|
"channel has more than one receiver"
|
||||||
);
|
);
|
||||||
epoch = crate::scheduler::begin_wait();
|
epoch = crate::scheduler::begin_wait();
|
||||||
|
g.note_runtime();
|
||||||
g.parked_receiver = Some((me, epoch));
|
g.parked_receiver = Some((me, epoch));
|
||||||
crate::te!(crate::trace::Event::RecvPark(me));
|
crate::te!(crate::trace::Event::RecvPark(me));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Arm the timer after releasing the channel lock (insert takes the
|
// Arm the timer after releasing the channel lock (insert takes the
|
||||||
// timers lock; never nest under a Channel lock). A send or even the
|
// timers lock; never nest under a Channel lock). A send or even the
|
||||||
// timer itself may unpark us before we park — the RunningNotified
|
// timer itself may unpark us before we park; the runtime's wake
|
||||||
// protocol makes the park below return immediately in that case.
|
// protocol makes the park below return immediately in that case.
|
||||||
let deadline = crate::timer::deadline_from_now(timeout);
|
let deadline = crate::timer::deadline_from_now(timeout);
|
||||||
let target: std::sync::Arc<dyn crate::timer::TimerTarget> = self.inner.clone();
|
let target: std::sync::Arc<dyn crate::timer::TimerTarget> = self.inner.clone();
|
||||||
crate::scheduler::insert_wait_timer(deadline, me, target, epoch);
|
crate::scheduler::insert_wait_timer(deadline, me, target, epoch);
|
||||||
|
|
||||||
crate::scheduler::park_current();
|
crate::scheduler::park_current();
|
||||||
crate::te!(crate::trace::Event::RecvWake(crate::actor::current_pid().unwrap()));
|
crate::te!(crate::trace::Event::RecvWake(
|
||||||
|
match crate::actor::current_pid() {
|
||||||
|
Some(p) => p,
|
||||||
|
None => panic!("smarm: RecvWake outside an actor (core corrupt)"),
|
||||||
|
}
|
||||||
|
));
|
||||||
let mut g = self.inner.lock();
|
let mut g = self.inner.lock();
|
||||||
if let Some(v) = g.queue.pop_front() {
|
if let Some(v) = g.queue.pop_front() {
|
||||||
crate::preempt::note_message_received();
|
crate::preempt::note_message_received();
|
||||||
@@ -266,16 +415,23 @@ impl<T> Receiver<T> {
|
|||||||
Err(RecvTimeoutError::Timeout)
|
Err(RecvTimeoutError::Timeout)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Selective receive: remove and return the first queued message for which
|
/// Selective receive: find and return the first queued message for
|
||||||
/// `pred` holds, leaving the rest in arrival order. If no queued message
|
/// which `pred` returns `true`, leaving every other message in the
|
||||||
/// matches, parks and re-scans on every send (a selective receiver may park
|
/// queue untouched and in order. Useful when an actor's inbox mixes
|
||||||
/// on a *non-empty* queue). Returns `Err(RecvError)` only once the channel
|
/// message kinds and it wants to handle one kind out of turn, without
|
||||||
/// is closed and no queued message matches.
|
/// discarding the rest.
|
||||||
///
|
///
|
||||||
/// `pred` is run while the channel lock is held: keep it cheap and pure,
|
/// If nothing queued matches, this blocks and re-checks every time a new
|
||||||
/// and do not call back into this channel from inside it. It is modelled as
|
/// message arrives, the same way [`recv`](Self::recv) blocks on an empty
|
||||||
/// `Fn` (not `FnMut`) deliberately — it is re-run from scratch on every
|
/// queue: a selective receiver can be waiting even while the queue holds
|
||||||
/// scan, so a stateful predicate would observe surprising re-counting.
|
/// messages, just none that match yet. Returns [`RecvError`] only once
|
||||||
|
/// the channel is closed and still nothing matches.
|
||||||
|
///
|
||||||
|
/// `pred` runs while the channel is locked, so keep it cheap, side
|
||||||
|
/// effect free, and make sure it never calls back into this same
|
||||||
|
/// channel. It takes `&T` and is called fresh on every scan (not `FnMut`
|
||||||
|
/// with running state), so it should judge each message purely on its
|
||||||
|
/// own content.
|
||||||
pub fn recv_match<F>(&self, pred: F) -> Result<T, RecvError>
|
pub fn recv_match<F>(&self, pred: F) -> Result<T, RecvError>
|
||||||
where
|
where
|
||||||
F: Fn(&T) -> bool,
|
F: Fn(&T) -> bool,
|
||||||
@@ -283,42 +439,60 @@ impl<T> Receiver<T> {
|
|||||||
loop {
|
loop {
|
||||||
{
|
{
|
||||||
let mut g = self.inner.lock();
|
let mut g = self.inner.lock();
|
||||||
if let Some(i) = g.queue.iter().position(|v| pred(v)) {
|
if let Some(i) = g.queue.iter().position(&pred) {
|
||||||
// position() found it, so remove() returns Some.
|
// position() found it, so remove() returns Some.
|
||||||
crate::preempt::note_message_received();
|
crate::preempt::note_message_received();
|
||||||
return Ok(g.queue.remove(i).unwrap());
|
let v = match g.queue.remove(i) {
|
||||||
|
Some(v) => v,
|
||||||
|
None => panic!("smarm: channel queue.remove after position (logic bug)"),
|
||||||
|
};
|
||||||
|
return Ok(v);
|
||||||
}
|
}
|
||||||
if g.senders == 0 {
|
if g.senders == 0 {
|
||||||
// Closed and nothing queued can ever match.
|
// Closed and nothing queued can ever match.
|
||||||
return Err(RecvError);
|
return Err(RecvError);
|
||||||
}
|
}
|
||||||
let me = crate::actor::current_pid()
|
let me = match crate::actor::current_pid() {
|
||||||
.expect("recv_match() called outside an actor");
|
Some(me) => me,
|
||||||
|
None => panic!("smarm: recv_match() called outside an actor"),
|
||||||
|
};
|
||||||
debug_assert!(
|
debug_assert!(
|
||||||
g.parked_receiver.is_none_or(|(p, _)| p == me),
|
g.parked_receiver.is_none_or(|(p, _)| p == me),
|
||||||
"channel has more than one receiver"
|
"channel has more than one receiver"
|
||||||
);
|
);
|
||||||
|
g.note_runtime();
|
||||||
g.parked_receiver = Some((me, crate::scheduler::begin_wait()));
|
g.parked_receiver = Some((me, crate::scheduler::begin_wait()));
|
||||||
crate::te!(crate::trace::Event::RecvPark(me));
|
crate::te!(crate::trace::Event::RecvPark(me));
|
||||||
}
|
}
|
||||||
// Release the lock before parking — the unparker will need it.
|
// Release the lock before parking: the unparker will need it.
|
||||||
crate::scheduler::park_current();
|
crate::scheduler::park_current();
|
||||||
crate::te!(crate::trace::Event::RecvWake(crate::actor::current_pid().unwrap()));
|
crate::te!(crate::trace::Event::RecvWake(
|
||||||
|
match crate::actor::current_pid() {
|
||||||
|
Some(p) => p,
|
||||||
|
None => panic!("smarm: RecvWake outside an actor (core corrupt)"),
|
||||||
|
}
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Non-blocking selective receive. `Ok(Some(v))` if a queued message
|
/// The non-blocking counterpart of [`recv_match`](Self::recv_match):
|
||||||
/// matched `pred` (removed, rest left in order), `Ok(None)` if the channel
|
/// returns immediately either way. `Ok(Some(v))` if a queued message
|
||||||
/// is open but nothing matched, `Err(RecvError)` if closed and nothing
|
/// matched `pred` (removed; the rest stay queued in order), `Ok(None)`
|
||||||
/// matched. Same predicate contract as [`recv_match`](Self::recv_match).
|
/// if the channel is open but nothing currently matches, `Err(RecvError)`
|
||||||
|
/// if the channel is closed and nothing matches. Same predicate contract
|
||||||
|
/// as `recv_match`.
|
||||||
pub fn try_recv_match<F>(&self, pred: F) -> Result<Option<T>, RecvError>
|
pub fn try_recv_match<F>(&self, pred: F) -> Result<Option<T>, RecvError>
|
||||||
where
|
where
|
||||||
F: Fn(&T) -> bool,
|
F: Fn(&T) -> bool,
|
||||||
{
|
{
|
||||||
let mut g = self.inner.lock();
|
let mut g = self.inner.lock();
|
||||||
if let Some(i) = g.queue.iter().position(|v| pred(v)) {
|
if let Some(i) = g.queue.iter().position(&pred) {
|
||||||
crate::preempt::note_message_received();
|
crate::preempt::note_message_received();
|
||||||
return Ok(Some(g.queue.remove(i).unwrap()));
|
let v = match g.queue.remove(i) {
|
||||||
|
Some(v) => v,
|
||||||
|
None => panic!("smarm: channel queue.remove after position (logic bug)"),
|
||||||
|
};
|
||||||
|
return Ok(Some(v));
|
||||||
}
|
}
|
||||||
if g.senders == 0 {
|
if g.senders == 0 {
|
||||||
return Err(RecvError);
|
return Err(RecvError);
|
||||||
@@ -326,8 +500,10 @@ impl<T> Receiver<T> {
|
|||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Non-blocking. `Ok(Some(v))` if a message was available, `Ok(None)` if
|
/// The non-blocking counterpart of [`recv`](Self::recv): returns
|
||||||
/// the channel is empty but open, `Err(RecvError)` if closed and drained.
|
/// immediately either way. `Ok(Some(v))` if a message was queued,
|
||||||
|
/// `Ok(None)` if the channel is open but currently empty, `Err(RecvError)`
|
||||||
|
/// if the channel is closed and the queue is drained.
|
||||||
pub fn try_recv(&self) -> Result<Option<T>, RecvError> {
|
pub fn try_recv(&self) -> Result<Option<T>, RecvError> {
|
||||||
let mut g = self.inner.lock();
|
let mut g = self.inner.lock();
|
||||||
if let Some(v) = g.queue.pop_front() {
|
if let Some(v) = g.queue.pop_front() {
|
||||||
@@ -342,18 +518,18 @@ impl<T> Receiver<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// TimerTarget — the expiry half of recv_timeout
|
// TimerTarget: the expiry half of recv_timeout
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
impl<T: Send + 'static> crate::timer::TimerTarget for RawMutex<Inner<T>> {
|
impl<T: Send + 'static> crate::timer::TimerTarget for RawMutex<Inner<T>> {
|
||||||
fn on_timeout(&self, pid: Pid, epoch: u32) {
|
fn on_timeout(&self, pid: Pid, epoch: u32) {
|
||||||
// Cancel the wait only if THIS wait (epoch match) is still
|
// Cancel the wait only if THIS wait (epoch match) is still
|
||||||
// registered. If a sender already took `parked_receiver`, the
|
// registered. If a sender already took `parked_receiver`, the
|
||||||
// receiver is waking with a message — message wins, the timer
|
// receiver is waking with a message: message wins, the timer
|
||||||
// no-ops. If a later wait by the same receiver is registered, the
|
// no-ops. If a later wait by the same receiver is registered, the
|
||||||
// epoch mismatches — stale entry, no-op. (The unpark_at would fail
|
// epoch mismatches: stale entry, no-op. (unpark_at would fail its
|
||||||
// its word CAS in either case anyway; checking under the lock keeps
|
// internal check in either case anyway; checking under the lock
|
||||||
// the registration bookkeeping exact.)
|
// keeps the registration bookkeeping exact.)
|
||||||
let unpark = {
|
let unpark = {
|
||||||
let mut g = self.lock();
|
let mut g = self.lock();
|
||||||
if g.parked_receiver == Some((pid, epoch)) {
|
if g.parked_receiver == Some((pid, epoch)) {
|
||||||
@@ -363,7 +539,7 @@ impl<T: Send + 'static> crate::timer::TimerTarget for RawMutex<Inner<T>> {
|
|||||||
false
|
false
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
// Unpark outside the channel lock — it may take the run-queue lock;
|
// Unpark outside the channel lock: it may take the run-queue lock;
|
||||||
// legal under a Channel lock, but pointless to nest.
|
// legal under a Channel lock, but pointless to nest.
|
||||||
if unpark {
|
if unpark {
|
||||||
crate::scheduler::unpark_at(pid, epoch);
|
crate::scheduler::unpark_at(pid, epoch);
|
||||||
@@ -372,7 +548,7 @@ impl<T: Send + 'static> crate::timer::TimerTarget for RawMutex<Inner<T>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// select — ready-index wait over multiple receivers
|
// select: ready-index wait over multiple receivers
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
pub(crate) mod sealed {
|
pub(crate) mod sealed {
|
||||||
@@ -380,33 +556,34 @@ pub(crate) mod sealed {
|
|||||||
}
|
}
|
||||||
impl<T> sealed::Sealed for Receiver<T> {}
|
impl<T> sealed::Sealed for Receiver<T> {}
|
||||||
|
|
||||||
/// An arm of a [`select`]. Implemented by [`Receiver`]; sealed, because the
|
/// An arm of a [`select`]: something you can wait on alongside other arms
|
||||||
/// registration contract below is part of the runtime's wake protocol.
|
/// and be told when it becomes ready. Implemented by [`Receiver`]; sealed
|
||||||
|
/// (cannot be implemented outside this crate), since the registration
|
||||||
|
/// contract below is part of the runtime's internal wake protocol.
|
||||||
///
|
///
|
||||||
/// Contract (all under the arm's own lock): `sel_register` checks-or-
|
/// Contract (all under the arm's own lock): `sel_register` checks-or-
|
||||||
/// registers atomically — if the arm is ready it does NOT register and
|
/// registers atomically. If the arm is ready it does not register and
|
||||||
/// returns `Ok(false)`; otherwise it publishes `(pid, epoch)` where its
|
/// returns `Ok(false)`; otherwise it publishes `(pid, epoch)` where its
|
||||||
/// wakers will find it and returns `Ok(true)`. "Ready" means a receive
|
/// wakers will find it and returns `Ok(true)`. "Ready" means a receive
|
||||||
/// would not park: a message is queued, or the arm is closed. `Err` means
|
/// would not block: a message is queued, or the arm is closed. `Err` means
|
||||||
/// the arm could not register at all (only fd arms can fail; channel
|
/// the arm could not register at all (only fd arms can fail; channel
|
||||||
/// registration is infallible) — the wait must be retired and earlier
|
/// registration always succeeds), and the wait must be retired and earlier
|
||||||
/// eager-cleanup arms unregistered.
|
/// eager-cleanup arms unregistered.
|
||||||
pub trait Selectable: sealed::Sealed {
|
pub trait Selectable: sealed::Sealed {
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
fn sel_register(&self, pid: Pid, epoch: u32) -> std::io::Result<bool>;
|
fn sel_register(&self, pid: Pid, epoch: u32) -> std::io::Result<bool>;
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
fn sel_ready(&self) -> bool;
|
fn sel_ready(&self) -> bool;
|
||||||
/// Remove this arm's `(pid, epoch)` registration if — and only if — it
|
/// Remove this arm's `(pid, epoch)` registration if, and only if, it is
|
||||||
/// is still in place. Default no-op: a losing channel arm's stale
|
/// still in place. Default no-op: a losing channel arm's stale
|
||||||
/// registration is inert (its wakers die at the epoch CAS; the next
|
/// registration is harmless and self-cleans. Fd arms override this:
|
||||||
/// wait overwrites the slot). Fd arms override this: their staleness
|
/// their staleness would otherwise leave the fd unusable for future
|
||||||
/// poisons the fd (waiters entry + kernel-side ONESHOT registration)
|
/// selects, so they need an eager cleanup pass.
|
||||||
/// and needs an eager cleanup pass.
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
fn sel_unregister(&self, _pid: Pid, _epoch: u32) {}
|
fn sel_unregister(&self, _pid: Pid, _epoch: u32) {}
|
||||||
/// Whether this arm requires the eager cleanup pass at all. Gates the
|
/// Whether this arm requires the eager cleanup pass at all. Gates the
|
||||||
/// post-wake `sel_unregister` sweep so channel-only selects keep
|
/// post-wake `sel_unregister` sweep so channel-only selects keep their
|
||||||
/// today's zero-cancellation hot path.
|
/// cheap, cleanup-free path.
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
fn sel_eager_cleanup(&self) -> bool {
|
fn sel_eager_cleanup(&self) -> bool {
|
||||||
false
|
false
|
||||||
@@ -423,6 +600,7 @@ impl<T> Selectable for Receiver<T> {
|
|||||||
g.parked_receiver.is_none_or(|(p, _)| p == pid),
|
g.parked_receiver.is_none_or(|(p, _)| p == pid),
|
||||||
"channel has more than one receiver"
|
"channel has more than one receiver"
|
||||||
);
|
);
|
||||||
|
g.note_runtime();
|
||||||
g.parked_receiver = Some((pid, epoch));
|
g.parked_receiver = Some((pid, epoch));
|
||||||
Ok(true)
|
Ok(true)
|
||||||
}
|
}
|
||||||
@@ -433,66 +611,77 @@ impl<T> Selectable for Receiver<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Park on every arm at once; return the index of the first ready one.
|
/// Wait on several channels at once and return the index of the first one
|
||||||
|
/// that is ready, instead of blocking on just one with [`Receiver::recv`].
|
||||||
///
|
///
|
||||||
/// "Ready" means a receive on that arm would not park: a message is queued,
|
/// "Ready" means a receive on that arm would not block: a message is
|
||||||
/// or the arm is **closed** (so the caller's `try_recv` observes the
|
/// queued, or the arm is closed (so the caller's own `try_recv` observes
|
||||||
/// disconnect — a dead arm is an event, not a hang). The caller consumes the
|
/// the disconnect: a dead arm is something to react to, not something to
|
||||||
/// arm itself, typically via [`Receiver::try_recv`]; single-receiver
|
/// hang on). `select` only tells you which arm is ready; read the actual
|
||||||
/// channels guarantee nothing can steal the message in between.
|
/// message yourself, typically with [`Receiver::try_recv`] on that arm.
|
||||||
///
|
///
|
||||||
/// A closed arm stays ready *forever*: once its disconnect has been
|
/// A closed arm stays ready forever. Once you have observed its disconnect,
|
||||||
/// observed, drop it from the arm set — under priority order it would
|
/// drop it from the arm set you pass in next time: otherwise, under the
|
||||||
/// otherwise win every subsequent call and starve every higher-indexed arm.
|
/// priority order below, it would win every subsequent call and starve
|
||||||
|
/// every arm listed after it.
|
||||||
///
|
///
|
||||||
/// Arms are scanned **in order**: index 0 is the highest priority, both on
|
/// Arms are checked **in order**: index 0 is the highest priority, both
|
||||||
/// the immediate-ready path and after a wake. This is a documented
|
/// when checking immediately and after being woken. This is a deliberate,
|
||||||
/// guarantee (compose like BEAM receive clauses: put control channels
|
/// documented guarantee, not an accident of implementation: put a control
|
||||||
/// first), not an accident — and therefore there is NO fairness promise; a
|
/// or shutdown channel first so it is always noticed promptly. The
|
||||||
/// saturated arm 0 starves arm 1 by design.
|
/// flip side is that there is **no fairness guarantee**: a busy arm 0 can
|
||||||
|
/// starve arm 1 indefinitely by design.
|
||||||
///
|
///
|
||||||
/// One actor may select on a channel and later `recv` on it (or select on
|
/// One actor can `select` on a channel and later plain `recv` on it (or
|
||||||
/// overlapping sets) freely. What stays illegal is what was always illegal:
|
/// `select` again on an overlapping set of arms) with no restriction. What
|
||||||
/// two *different* actors receiving on one channel.
|
/// stays illegal is what was always illegal for a channel: two *different*
|
||||||
|
/// actors receiving on the same one.
|
||||||
///
|
///
|
||||||
/// Built on the consuming-wake protocol (see slot_state.rs): all arms are
|
/// Panics if `arms` is empty, if called outside an actor, or if an fd arm
|
||||||
/// registered under one wait epoch; the winning wake consumes it, so losing
|
/// fails to register (see [`try_select`] for the fallible form; a
|
||||||
/// arms' registrations are inert and need no cancellation pass — they
|
/// channel-only `select` can never fail).
|
||||||
/// self-clean at their wakers' failed CAS, or get overwritten by this
|
|
||||||
/// receiver's next wait on that channel.
|
|
||||||
///
|
|
||||||
/// Panics if `arms` is empty, when called outside an actor, or if an fd
|
|
||||||
/// arm fails to register (EBADF, EMFILE, a second waiter on one fd —
|
|
||||||
/// see [`try_select`] for the fallible form; channel-only selects cannot
|
|
||||||
/// fail).
|
|
||||||
pub fn select(arms: &[&dyn Selectable]) -> usize {
|
pub fn select(arms: &[&dyn Selectable]) -> usize {
|
||||||
try_select(arms).expect("select(): fd arm failed to register (use try_select)")
|
match try_select(arms) {
|
||||||
|
Ok(i) => i,
|
||||||
|
Err(e) => panic!("smarm: select() fd arm failed to register (use try_select): {e}"),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// [`select`], fallible: `Err` when an arm fails to register (only fd
|
/// The fallible form of [`select`]: `Err` when an arm fails to register.
|
||||||
/// arms can — EBADF, EMFILE on the epoll set, or a second waiter on an
|
/// Only fd arms can fail this way (for example, the file descriptor is
|
||||||
/// fd that already has one). On `Err` the wait is fully retired and no
|
/// invalid, or something else is already waiting on it); a channel-only
|
||||||
|
/// select can never fail. On `Err` the wait is fully retired and no
|
||||||
/// registration is left behind: every arm registered before the failing
|
/// registration is left behind: every arm registered before the failing
|
||||||
/// one has been unregistered.
|
/// one has been unregistered.
|
||||||
pub fn try_select(arms: &[&dyn Selectable]) -> std::io::Result<usize> {
|
pub fn try_select(arms: &[&dyn Selectable]) -> std::io::Result<usize> {
|
||||||
assert!(!arms.is_empty(), "select() on an empty arm list");
|
assert!(!arms.is_empty(), "select() on an empty arm list");
|
||||||
let me = crate::actor::current_pid().expect("select() called outside an actor");
|
let me = match crate::actor::current_pid() {
|
||||||
|
Some(me) => me,
|
||||||
|
None => panic!("smarm: select() called outside an actor"),
|
||||||
|
};
|
||||||
loop {
|
loop {
|
||||||
|
// Register-to-park must not be a preemption point: a Queued actor drops `unpark_at`,
|
||||||
|
// so a one-shot fd registration consumed in the window is a lost wake. As in `wait_fd`.
|
||||||
|
let _np = crate::scheduler::NoPreempt::enter();
|
||||||
let epoch = crate::scheduler::begin_wait();
|
let epoch = crate::scheduler::begin_wait();
|
||||||
if let Some(i) = register_arms(me, epoch, arms)? {
|
if let Some(i) = register_arms(me, epoch, arms)? {
|
||||||
return Ok(i);
|
return Ok(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stale fd registrations are not harmless (a losing fd arm's
|
// Stale fd registrations are not harmless (a losing fd arm's
|
||||||
// waiters entry poisons the fd with AlreadyExists and its
|
// leftover registration can make the fd unusable for the next
|
||||||
// kernel-side ONESHOT registration can fire arbitrarily late), so
|
// select until a kernel event happens to clear it), so selects
|
||||||
// selects containing fd arms run an eager cleanup pass after the
|
// containing fd arms run an eager cleanup pass after the park,
|
||||||
// park — including when a terminal stop unwinds out of it, via
|
// including when a terminal stop unwinds out of it, via the guard.
|
||||||
// the guard. Channel-only selects skip all of it: `eager` is
|
// Channel-only selects skip all of it: `eager` is false, the guard
|
||||||
// false, the guard is disarmed, and the loser-arm self-cleaning
|
// is disarmed, and the loser-arm self-cleaning story is unchanged.
|
||||||
// story is unchanged.
|
|
||||||
let eager = arms.iter().any(|a| a.sel_eager_cleanup());
|
let eager = arms.iter().any(|a| a.sel_eager_cleanup());
|
||||||
let mut guard = UnregisterGuard { arms, me, epoch, armed: eager };
|
let mut guard = UnregisterGuard {
|
||||||
|
arms,
|
||||||
|
me,
|
||||||
|
epoch,
|
||||||
|
armed: eager,
|
||||||
|
};
|
||||||
|
|
||||||
crate::scheduler::park_current();
|
crate::scheduler::park_current();
|
||||||
|
|
||||||
@@ -503,22 +692,22 @@ pub fn try_select(arms: &[&dyn Selectable]) -> std::io::Result<usize> {
|
|||||||
drop(guard);
|
drop(guard);
|
||||||
|
|
||||||
// Woken precisely: an arm's send (message) or last-sender drop
|
// Woken precisely: an arm's send (message) or last-sender drop
|
||||||
// (closure) consumed our epoch, and both leave their arm ready —
|
// (closure) is what woke us, and both leave their arm ready.
|
||||||
// return the first one, in priority order (which may be a
|
// Return the first ready one, in priority order (which may be a
|
||||||
// different, higher-priority arm than the one that woke us; its
|
// different, higher-priority arm than the one that woke us; its
|
||||||
// message stays queued and re-reports ready on the next call).
|
// message stays queued and re-reports ready on the next call).
|
||||||
// Fd arms classify by a fresh zero-timeout poll, so they too are
|
// Fd arms classify by a fresh zero-timeout poll, so they too are
|
||||||
// a pure function of state — independent of the registration the
|
// a pure function of current state, independent of the
|
||||||
// cleanup pass just removed.
|
// registration the cleanup pass just removed.
|
||||||
for (i, arm) in arms.iter().enumerate() {
|
for (i, arm) in arms.iter().enumerate() {
|
||||||
if arm.sel_ready() {
|
if arm.sel_ready() {
|
||||||
return Ok(i);
|
return Ok(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Unreachable by protocol (a stop wake unwinds out of
|
// Unreachable in practice (a stop wake unwinds out of
|
||||||
// park_current). Defensive: re-open the wait and re-register —
|
// park_current before we get here). Defensive: re-open the wait
|
||||||
// stale own-registrations are overwritten (channels) or were
|
// and re-register; stale own-registrations are overwritten
|
||||||
// removed by the cleanup pass above (fds).
|
// (channels) or were removed by the cleanup pass above (fds).
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -533,11 +722,11 @@ fn unregister_arms(arms: &[&dyn Selectable], me: Pid, epoch: u32) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Stop-unwind twin of the explicit cleanup pass: a terminal stop unwinds
|
// Stop-unwind twin of the explicit cleanup pass: a terminal stop unwinds
|
||||||
/// out of `park_current`, and a registered fd arm must not outlive its
|
// out of `park_current`, and a registered fd arm must not outlive its
|
||||||
/// actor (the generalization of `wait_fd`'s `Dereg`). Disarmed on the
|
// actor. Disarmed on the normal path after the explicit pass runs; never
|
||||||
/// normal path after the explicit pass runs; never armed when no fd arm
|
// armed when no fd arm is registered, keeping the channel-only path
|
||||||
/// registered, keeping the channel-only path guard-free in effect.
|
// guard-free in effect.
|
||||||
struct UnregisterGuard<'a> {
|
struct UnregisterGuard<'a> {
|
||||||
arms: &'a [&'a dyn Selectable],
|
arms: &'a [&'a dyn Selectable],
|
||||||
me: Pid,
|
me: Pid,
|
||||||
@@ -553,25 +742,17 @@ impl Drop for UnregisterGuard<'_> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The registration pass shared by [`select`] and [`select_timeout`]:
|
// The registration pass shared by `select` and `select_timeout`: check-or-
|
||||||
/// check-or-register each arm, in priority order, each atomically under its
|
// register each arm, in priority order, each atomically under its own lock.
|
||||||
/// own lock. Cross-arm atomicity is unnecessary: an arm becoming ready
|
// Cross-arm atomicity is unnecessary: an arm becoming ready right after its
|
||||||
/// right after its registration wakes the caller through the protocol (the
|
// registration still wakes the caller through the normal wake path.
|
||||||
/// prep-to-park window is closed by RunningNotified).
|
//
|
||||||
///
|
// `Ok(Some(i))` = arm `i` was already ready, the pass stopped, and the wait
|
||||||
/// `Ok(Some(i))` = arm `i` was ready, the pass stopped, and the wait has
|
// has been fully retired (no park may follow): earlier fd arms are
|
||||||
/// been RETIRED (no park may follow): earlier arms hold live-epoch
|
// unregistered eagerly so none are left dangling. `Err` = an arm failed to
|
||||||
/// registrations, so earlier *fd* arms are unregistered eagerly, then the
|
// register; same unwind (earlier fd arms unregistered, wait retired).
|
||||||
/// epoch is bumped, a landed notification eaten, and a pending stop
|
// `Ok(None)` = every arm registered successfully; the caller parks.
|
||||||
/// re-observed — without which a stale arm wake could fault a later
|
fn register_arms(me: Pid, epoch: u32, arms: &[&dyn Selectable]) -> std::io::Result<Option<usize>> {
|
||||||
/// one-shot park. `Err` = an arm failed to register; identical unwind
|
|
||||||
/// (earlier fd arms unregistered, wait retired). `Ok(None)` = every arm
|
|
||||||
/// registered; the caller parks.
|
|
||||||
fn register_arms(
|
|
||||||
me: Pid,
|
|
||||||
epoch: u32,
|
|
||||||
arms: &[&dyn Selectable],
|
|
||||||
) -> std::io::Result<Option<usize>> {
|
|
||||||
for (i, arm) in arms.iter().enumerate() {
|
for (i, arm) in arms.iter().enumerate() {
|
||||||
let registered = match arm.sel_register(me, epoch) {
|
let registered = match arm.sel_register(me, epoch) {
|
||||||
Ok(r) => r,
|
Ok(r) => r,
|
||||||
@@ -590,10 +771,10 @@ fn register_arms(
|
|||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The [`select_timeout`] timer target: stateless, because precise wakes
|
// The `select_timeout` timer target: stateless, because a wake's cause can
|
||||||
/// make classification a pure function of channel state. The entry is
|
// always be read back off plain channel state (an arm ready, or not). If
|
||||||
/// stamped with the select's epoch; if an arm already won, this unpark dies
|
// an arm already won before the deadline, this timer's fire is simply
|
||||||
/// at the word's epoch CAS (the no-cancellation convention in `timer.rs`).
|
// ignored, the way any other stale wakeup is.
|
||||||
struct SelectTimeout;
|
struct SelectTimeout;
|
||||||
impl crate::timer::TimerTarget for SelectTimeout {
|
impl crate::timer::TimerTarget for SelectTimeout {
|
||||||
fn on_timeout(&self, pid: Pid, epoch: u32) {
|
fn on_timeout(&self, pid: Pid, epoch: u32) {
|
||||||
@@ -601,62 +782,67 @@ impl crate::timer::TimerTarget for SelectTimeout {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// [`select`] with a deadline: returns `Some(index)` like `select`, or
|
/// Like [`select`], but gives up and returns `None` if no arm becomes
|
||||||
/// `None` once `timeout` elapses with no arm ready.
|
/// ready before `timeout` elapses.
|
||||||
///
|
///
|
||||||
/// All of `select`'s semantics carry over (priority order, closed arms
|
/// All of `select`'s semantics carry over: arms are still checked in
|
||||||
/// permanently ready, no fairness promise). The timeout is one more stamped
|
/// priority order, a closed arm is still permanently ready, and there is
|
||||||
/// waker on the same wait epoch — nothing is registered in any arm for it,
|
/// still no fairness guarantee across arms. A message that arrives at
|
||||||
/// so there is nothing to cancel or leak: an arm winning leaves the timer
|
/// essentially the same moment the deadline passes still wins, the same
|
||||||
/// entry to expire as a stale-epoch no-op; the timer winning leaves the
|
/// way [`Receiver::recv_timeout`] resolves that race.
|
||||||
/// arms' registrations to self-clean exactly as a `select` loser's would.
|
|
||||||
///
|
///
|
||||||
/// The wake is classified from state alone (wakes are precise): some arm
|
/// `Duration::ZERO` is a valid timeout: it still gives an already-ready arm
|
||||||
/// ready → `Some` of the first, in priority order; none ready → the timer
|
/// a chance to be reported before falling through to `None`.
|
||||||
/// was the only remaining stamped waker → `None`. A message that races the
|
|
||||||
/// deadline resolves message-first, as `recv_timeout` does.
|
|
||||||
///
|
///
|
||||||
/// `Duration::ZERO` is a valid timeout: it parks until the immediately-due
|
/// Panics if `arms` is empty, if called outside an actor, or if an fd arm
|
||||||
/// timer is drained, then reports `None` unless an arm was already ready.
|
/// fails to register (see [`try_select_timeout`] for the fallible form; a
|
||||||
///
|
/// channel-only select can never fail).
|
||||||
/// Panics if `arms` is empty, when called outside an actor, or if an fd
|
pub fn select_timeout(arms: &[&dyn Selectable], timeout: std::time::Duration) -> Option<usize> {
|
||||||
/// arm fails to register (see [`try_select_timeout`] for the fallible
|
match try_select_timeout(arms, timeout) {
|
||||||
/// form; channel-only selects cannot fail).
|
Ok(r) => r,
|
||||||
pub fn select_timeout(
|
Err(e) => panic!(
|
||||||
arms: &[&dyn Selectable],
|
"smarm: select_timeout() fd arm failed to register (use try_select_timeout): {e}"
|
||||||
timeout: std::time::Duration,
|
),
|
||||||
) -> Option<usize> {
|
}
|
||||||
try_select_timeout(arms, timeout)
|
|
||||||
.expect("select_timeout(): fd arm failed to register (use try_select_timeout)")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// [`select_timeout`], fallible: `Err` when an arm fails to register
|
/// The fallible form of [`select_timeout`]: `Err` when an arm fails to
|
||||||
/// (only fd arms can). On `Err` the wait is fully retired and no
|
/// register (only fd arms can). On `Err` the wait is fully retired and no
|
||||||
/// registration — arm-side or kernel-side — is left behind.
|
/// registration is left behind on any arm.
|
||||||
pub fn try_select_timeout(
|
pub fn try_select_timeout(
|
||||||
arms: &[&dyn Selectable],
|
arms: &[&dyn Selectable],
|
||||||
timeout: std::time::Duration,
|
timeout: std::time::Duration,
|
||||||
) -> std::io::Result<Option<usize>> {
|
) -> std::io::Result<Option<usize>> {
|
||||||
assert!(!arms.is_empty(), "select_timeout() on an empty arm list");
|
assert!(!arms.is_empty(), "select_timeout() on an empty arm list");
|
||||||
let me = crate::actor::current_pid().expect("select_timeout() called outside an actor");
|
let me = match crate::actor::current_pid() {
|
||||||
|
Some(me) => me,
|
||||||
|
None => panic!("smarm: select_timeout() called outside an actor"),
|
||||||
|
};
|
||||||
|
// Prep-to-park region (see `try_select`): the timeout target allocation and the timer
|
||||||
|
// insert below must not be preemption points while the arms are registered.
|
||||||
|
let _np = crate::scheduler::NoPreempt::enter();
|
||||||
let epoch = crate::scheduler::begin_wait();
|
let epoch = crate::scheduler::begin_wait();
|
||||||
if let Some(i) = register_arms(me, epoch, arms)? {
|
if let Some(i) = register_arms(me, epoch, arms)? {
|
||||||
return Ok(Some(i)); // ready now: the timer was never armed
|
return Ok(Some(i)); // ready now: the timer was never armed
|
||||||
}
|
}
|
||||||
|
|
||||||
// Arm the timer after the registration pass, outside every Channel
|
// Arm the timer after the registration pass, outside every channel
|
||||||
// lock (insert takes the timers lock).
|
// lock (inserting a timer takes the timers lock).
|
||||||
let deadline = crate::timer::deadline_from_now(timeout);
|
let deadline = crate::timer::deadline_from_now(timeout);
|
||||||
let target: std::sync::Arc<dyn crate::timer::TimerTarget> = std::sync::Arc::new(SelectTimeout);
|
let target: std::sync::Arc<dyn crate::timer::TimerTarget> = std::sync::Arc::new(SelectTimeout);
|
||||||
crate::scheduler::insert_wait_timer(deadline, me, target, epoch);
|
crate::scheduler::insert_wait_timer(deadline, me, target, epoch);
|
||||||
|
|
||||||
// Same eager-cleanup story as `try_select`: the timer arm needs none
|
// Same eager-cleanup story as `try_select`: a timer win in particular
|
||||||
// (stateless, stale entries die at the epoch CAS), channel arms need
|
// leaves every fd arm's registration behind, which without this pass
|
||||||
// none, fd arms do — and a timer win in particular leaves every fd
|
// would leave those fds unusable until a kernel event happened to
|
||||||
// arm's registration behind, which without this pass would poison
|
// clear them.
|
||||||
// those fds until a kernel event happened to fire.
|
|
||||||
let eager = arms.iter().any(|a| a.sel_eager_cleanup());
|
let eager = arms.iter().any(|a| a.sel_eager_cleanup());
|
||||||
let mut guard = UnregisterGuard { arms, me, epoch, armed: eager };
|
let mut guard = UnregisterGuard {
|
||||||
|
arms,
|
||||||
|
me,
|
||||||
|
epoch,
|
||||||
|
armed: eager,
|
||||||
|
};
|
||||||
|
|
||||||
crate::scheduler::park_current();
|
crate::scheduler::park_current();
|
||||||
|
|
||||||
|
|||||||
+282
@@ -0,0 +1,282 @@
|
|||||||
|
//! RFC 010 — clustering (smarm⇄smarm, explicit remote boundary).
|
||||||
|
//!
|
||||||
|
//! c1: feature flag + optional deps. c2: the owned envelope. c3: the
|
||||||
|
//! transport trait (control connection), framed codec, and the TCP +
|
||||||
|
//! loopback impls. c5: the handshake state machine. c6: the connection
|
||||||
|
//! [`manager`] (registry) and per-peer connection actors ([`conn`]), started
|
||||||
|
//! as an explicit supervision subtree, plus the handshake on the
|
||||||
|
//! accept/connect path ([`connect`]). Everything above them lands in later
|
||||||
|
//! chunks.
|
||||||
|
|
||||||
|
pub mod conn;
|
||||||
|
pub mod connect;
|
||||||
|
pub mod connector;
|
||||||
|
pub mod discovery;
|
||||||
|
pub mod envelope;
|
||||||
|
pub mod expose;
|
||||||
|
pub mod handshake;
|
||||||
|
pub mod manager;
|
||||||
|
pub mod membership;
|
||||||
|
pub mod pg;
|
||||||
|
pub mod remote;
|
||||||
|
pub mod transport;
|
||||||
|
|
||||||
|
use std::io;
|
||||||
|
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||||
|
|
||||||
|
use crate::gen_server::{self, GenServerBuilder};
|
||||||
|
use crate::monitor::monitor;
|
||||||
|
use crate::pg::Incarnation;
|
||||||
|
use crate::scheduler::{sleep, spawn, JoinHandle};
|
||||||
|
use crate::supervisor::{ChildSpec, OneForOne, Restart};
|
||||||
|
|
||||||
|
use envelope::NodeMeta;
|
||||||
|
use handshake::Local;
|
||||||
|
use transport::tcp::TcpTransport;
|
||||||
|
use transport::Transport;
|
||||||
|
|
||||||
|
pub use conn::{spawn_established, ConnHandle};
|
||||||
|
pub use connect::{dial, spawn_acceptor, AcceptorHandle};
|
||||||
|
pub use connector::{spawn_connector, ConnectorHandle};
|
||||||
|
pub use discovery::{Discovery, StaticSeeds, Strategy};
|
||||||
|
pub use envelope::RemoteDownReason;
|
||||||
|
pub use expose::{expose, expose_type, type_hash, DeliverError};
|
||||||
|
pub use manager::{Manager, MANAGER};
|
||||||
|
pub use membership::{subscribe, view, MembershipEvents, NodeEvent, NodeInfo};
|
||||||
|
pub use pg::{dispatch_any, members_all, pick_any, DispatchAnyError, GroupMember, PgMsg, PG_NAME};
|
||||||
|
pub use remote::{
|
||||||
|
demonitor_remote, monitor_remote, send_to_remote, NotConnected, RemoteDown, RemoteMonitor,
|
||||||
|
RemoteName, RemotePid, RemoteSendError, ToRemoteError,
|
||||||
|
};
|
||||||
|
|
||||||
|
/// c6d — the derived build hash for [`handshake::LocalNode::build_hash`]:
|
||||||
|
/// two builds may mesh only when this matches, and it is a pure function of
|
||||||
|
/// the compile-time inputs that define wire compatibility today — the exact
|
||||||
|
/// toolchain (`rustc -V`), the declared feature set, and
|
||||||
|
/// [`envelope::PROTO_VERSION`]. FNV-1a 64 over the build-script string, then
|
||||||
|
/// the proto version folded byte-wise, so a proto bump moves the hash even
|
||||||
|
/// on an identical toolchain. The domain is deliberately lean and
|
||||||
|
/// tightenable later without a wire change — it is just a `u64`.
|
||||||
|
pub const BUILD_HASH: u64 = fold_u32(
|
||||||
|
fnv1a64(env!("SMARM_BUILD_HASH_INPUTS").as_bytes()),
|
||||||
|
envelope::PROTO_VERSION,
|
||||||
|
);
|
||||||
|
|
||||||
|
/// FNV-1a 64 (const so [`BUILD_HASH`] is a compile-time fact).
|
||||||
|
const fn fnv1a64(bytes: &[u8]) -> u64 {
|
||||||
|
let mut h: u64 = 0xcbf2_9ce4_8422_2325;
|
||||||
|
let mut i = 0;
|
||||||
|
while i < bytes.len() {
|
||||||
|
h ^= bytes[i] as u64;
|
||||||
|
h = h.wrapping_mul(0x0000_0100_0000_01b3);
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
h
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Continue an FNV-1a state over a `u32`'s little-endian bytes.
|
||||||
|
const fn fold_u32(mut h: u64, v: u32) -> u64 {
|
||||||
|
let b = v.to_le_bytes();
|
||||||
|
let mut i = 0;
|
||||||
|
while i < b.len() {
|
||||||
|
h ^= b[i] as u64;
|
||||||
|
h = h.wrapping_mul(0x0000_0100_0000_01b3);
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
h
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The control-plane timing knobs, all with today's fixed values as
|
||||||
|
/// defaults ([`Timing::default`]). One struct threaded explicitly to the
|
||||||
|
/// acceptor, the dial path, every connection actor and the connector — no
|
||||||
|
/// ambient state, so a test can run a fast mesh without touching globals.
|
||||||
|
/// Every node in a mesh should agree on `heartbeat_interval` <
|
||||||
|
/// `liveness_timeout`; nothing enforces it.
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
pub struct Timing {
|
||||||
|
/// Idle-connection heartbeat pace. Default [`conn::HEARTBEAT_INTERVAL`].
|
||||||
|
pub heartbeat_interval: Duration,
|
||||||
|
/// Inbound silence that tears a connection down. Default
|
||||||
|
/// [`conn::LIVENESS_TIMEOUT`].
|
||||||
|
pub liveness_timeout: Duration,
|
||||||
|
/// Per-frame handshake deadline on the accept/dial path. Default
|
||||||
|
/// [`connect::HANDSHAKE_TIMEOUT`].
|
||||||
|
pub handshake_timeout: Duration,
|
||||||
|
/// Connector redial delay after the first failure. Default
|
||||||
|
/// [`connector::INITIAL_BACKOFF`].
|
||||||
|
pub initial_backoff: Duration,
|
||||||
|
/// Connector redial delay cap. Default [`connector::MAX_BACKOFF`].
|
||||||
|
pub max_backoff: Duration,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Timing {
|
||||||
|
fn default() -> Self {
|
||||||
|
Timing {
|
||||||
|
heartbeat_interval: conn::HEARTBEAT_INTERVAL,
|
||||||
|
liveness_timeout: conn::LIVENESS_TIMEOUT,
|
||||||
|
handshake_timeout: connect::HANDSHAKE_TIMEOUT,
|
||||||
|
initial_backoff: connector::INITIAL_BACKOFF,
|
||||||
|
max_backoff: connector::MAX_BACKOFF,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// How to run this node: its identity and how it finds peers.
|
||||||
|
pub struct Config {
|
||||||
|
/// This node's claimed name — the mesh-wide identity peers dial by and
|
||||||
|
/// the tie-break input. Must be unique across the mesh.
|
||||||
|
pub node_name: String,
|
||||||
|
/// Metadata offered in this node's `Hello`.
|
||||||
|
pub meta: NodeMeta,
|
||||||
|
/// The control-connection listen address (e.g. `"127.0.0.1:0"`; the
|
||||||
|
/// concrete bound address is [`Cluster::local_addr`]).
|
||||||
|
pub listen_addr: String,
|
||||||
|
/// The peer-discovery strategy — [`StaticSeeds`] until richer ones land.
|
||||||
|
pub strategy: Box<dyn Strategy>,
|
||||||
|
/// Heartbeat / liveness / handshake / backoff knobs; [`Timing::default`]
|
||||||
|
/// is the shipping configuration.
|
||||||
|
pub timing: Timing,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A running cluster node: the supervised [`Manager`], the acceptor over the
|
||||||
|
/// bound listener, and the connector driving its [`Strategy`]. Roles will
|
||||||
|
/// eventually mount this; until the role mechanism lands it is started by
|
||||||
|
/// hand (RFC 010 §7).
|
||||||
|
///
|
||||||
|
/// Dropping the handle stops the acceptor and connector loops (no new
|
||||||
|
/// connections in either direction) but detaches the manager subtree, which
|
||||||
|
/// — with every established connection — keeps running for the life of the
|
||||||
|
/// runtime, the same split as [`AcceptorHandle`] alone.
|
||||||
|
pub struct Cluster {
|
||||||
|
_sup: JoinHandle,
|
||||||
|
acceptor: AcceptorHandle,
|
||||||
|
connector: ConnectorHandle,
|
||||||
|
local: Local,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Cluster {
|
||||||
|
/// The concrete bound listen address, dialable as-is.
|
||||||
|
pub fn local_addr(&self) -> &str {
|
||||||
|
self.acceptor.local_addr()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// This node's handshake identity (name, incarnation, build hash, meta).
|
||||||
|
pub fn local(&self) -> &Local {
|
||||||
|
&self.local
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Stop accepting and dialing. Established connections stay up (they
|
||||||
|
/// belong to the manager); tear those down via the manager.
|
||||||
|
pub fn shutdown(&self) {
|
||||||
|
self.acceptor.shutdown();
|
||||||
|
self.connector.shutdown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Start a cluster node: the supervised manager (blocking until it is
|
||||||
|
/// registered and ready to answer), the acceptor bound per
|
||||||
|
/// [`Config::listen_addr`], and the connector running [`Config::strategy`].
|
||||||
|
/// The node's identity is completed here: `incarnation` is
|
||||||
|
/// [`self_incarnation`] and `build_hash` is [`BUILD_HASH`] — c7 is its first
|
||||||
|
/// consumer. Errs only if the listener cannot bind.
|
||||||
|
///
|
||||||
|
/// The manager is a supervised child (restarted on crash); per-peer
|
||||||
|
/// connection actors are dynamic and monitored by the manager rather than
|
||||||
|
/// statically supervised — a lost connection is re-established by the
|
||||||
|
/// connector's dial loop, never resurrected onto a stale socket.
|
||||||
|
pub fn start(config: Config) -> io::Result<Cluster> {
|
||||||
|
let sup = spawn(|| {
|
||||||
|
OneForOne::new()
|
||||||
|
.child(ChildSpec::new(Restart::Permanent, manager_child))
|
||||||
|
.run()
|
||||||
|
});
|
||||||
|
while gen_server::whereis_server(MANAGER).is_none() {
|
||||||
|
sleep(Duration::from_millis(1));
|
||||||
|
}
|
||||||
|
let local = Local {
|
||||||
|
node_name: config.node_name,
|
||||||
|
incarnation: self_incarnation(),
|
||||||
|
build_hash: BUILD_HASH,
|
||||||
|
meta: config.meta,
|
||||||
|
};
|
||||||
|
// The wire identity serialized pids are stamped with (c10).
|
||||||
|
remote::set_local_identity(&local.node_name, local.incarnation);
|
||||||
|
// The pg actor (Phase 5): subscribes membership, owns the "pg" name.
|
||||||
|
pg::attach_cluster();
|
||||||
|
let listener = TcpTransport.listen(&config.listen_addr)?;
|
||||||
|
let acceptor = spawn_acceptor(listener, local.clone(), config.timing);
|
||||||
|
let connector = spawn_connector(
|
||||||
|
Box::new(TcpTransport),
|
||||||
|
local.clone(),
|
||||||
|
config.strategy,
|
||||||
|
config.timing,
|
||||||
|
);
|
||||||
|
Ok(Cluster {
|
||||||
|
_sup: sup,
|
||||||
|
acceptor,
|
||||||
|
connector,
|
||||||
|
local,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// This process's incarnation epoch: milliseconds since the Unix epoch,
|
||||||
|
/// truncated to `u32`. Not a clock — its one job is separating a node from
|
||||||
|
/// its own restart (two starts of the same name land on the same value only
|
||||||
|
/// if they happen within the same millisecond modulo ~49.7 days). Seconds
|
||||||
|
/// would be too coarse: a crash-and-restart inside one second is routine
|
||||||
|
/// under supervision.
|
||||||
|
pub fn self_incarnation() -> Incarnation {
|
||||||
|
let ms = SystemTime::now()
|
||||||
|
.duration_since(UNIX_EPOCH)
|
||||||
|
.map(|d| d.as_millis())
|
||||||
|
.unwrap_or(0);
|
||||||
|
Incarnation::new(ms as u32)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The supervised manager child body. It *is* the child actor: it starts the
|
||||||
|
/// named manager, then parks on the manager's own termination so this actor's
|
||||||
|
/// lifetime tracks the manager's — the supervisor's restart accounting keys off
|
||||||
|
/// this actor exiting.
|
||||||
|
fn manager_child() {
|
||||||
|
let m = match GenServerBuilder::new(Manager::new()).named(MANAGER).start() {
|
||||||
|
Ok(m) => m,
|
||||||
|
// Name still held by a not-yet-reaped prior instance: return and let
|
||||||
|
// the supervisor retry under its restart policy.
|
||||||
|
Err(_) => return,
|
||||||
|
};
|
||||||
|
let _ = monitor(m.pid()).rx.recv();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
/// The hash core against the published FNV-1a 64 test vectors — the
|
||||||
|
/// contract is "this is FNV-1a", not "whatever the fn does".
|
||||||
|
#[test]
|
||||||
|
fn fnv1a64_known_vectors() {
|
||||||
|
assert_eq!(fnv1a64(b""), 0xcbf2_9ce4_8422_2325);
|
||||||
|
assert_eq!(fnv1a64(b"a"), 0xaf63_dc4c_8601_ec8c);
|
||||||
|
assert_eq!(fnv1a64(b"foobar"), 0x85944171f73967e8);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Folding the proto version continues the same FNV state: identical
|
||||||
|
/// inputs with a different version must land on a different hash.
|
||||||
|
#[test]
|
||||||
|
fn proto_version_moves_the_hash() {
|
||||||
|
let base = fnv1a64(b"same-toolchain;features=CLUSTER");
|
||||||
|
assert_ne!(fold_u32(base, 1), fold_u32(base, 2));
|
||||||
|
// And it equals hashing the bytes in one pass — the fold is a
|
||||||
|
// continuation, not a second construction.
|
||||||
|
let mut all = b"same-toolchain;features=CLUSTER".to_vec();
|
||||||
|
all.extend_from_slice(&1u32.to_le_bytes());
|
||||||
|
assert_eq!(fold_u32(base, 1), fnv1a64(&all));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The derived constant exists, is compile-time, and is not degenerate.
|
||||||
|
#[test]
|
||||||
|
fn build_hash_is_nonzero() {
|
||||||
|
const H: u64 = BUILD_HASH;
|
||||||
|
assert_ne!(H, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,630 @@
|
|||||||
|
//! RFC 010 c6 — the per-peer connection actor.
|
||||||
|
//!
|
||||||
|
//! One actor per established control connection. It owns the whole
|
||||||
|
//! [`FramedConn`] and, in a single [`select`](crate::select), waits on two
|
||||||
|
//! things at once: its command inbox and the connection becoming readable (the
|
||||||
|
//! [`FdArm`](crate::scheduler::FdArm) the transport hands back). That is why it
|
||||||
|
//! is a plain select-loop actor rather than a `gen_server` or `gen_statem` —
|
||||||
|
//! neither of those can fold fd-readiness into its wait, and folding it in is
|
||||||
|
//! the whole job. The single owner sends and receives on the one `FramedConn`,
|
||||||
|
//! so no read/write split is needed.
|
||||||
|
//!
|
||||||
|
//! The handshake completes *before* this actor exists (on the accept/connect
|
||||||
|
//! path — c6b) and produces the [`Peer`]; the *path* then registers the
|
||||||
|
//! connection with the [`manager`](crate::cluster::manager), which takes
|
||||||
|
//! ownership of its [`ConnHandle`] and monitors the actor, so any exit
|
||||||
|
//! deregisters the connection. The actor itself holds no authority over its
|
||||||
|
//! own lifetime: it runs until the manager drops its handle (deregistration,
|
||||||
|
//! `Disconnect`, or manager shutdown), the connection ends, or liveness
|
||||||
|
//! expires. Heartbeat send and fixed-timeout liveness are the timeout arm of
|
||||||
|
//! the same `select` (c6c): [`HEARTBEAT_INTERVAL`] paces outbound
|
||||||
|
//! [`Frame::Heartbeat`](crate::cluster::envelope::Frame::Heartbeat)s, and a
|
||||||
|
//! [`LIVENESS_TIMEOUT`] window — reset by any inbound frame — tears the
|
||||||
|
//! connection down when it empties.
|
||||||
|
//!
|
||||||
|
//! c9 adds the third arm — the connection's dedicated **outbound inbox**
|
||||||
|
//! (`Sender<Frame>` bound in the manager-maintained outbound table, D13),
|
||||||
|
//! drained onto the wire in the same loop — and inbound *interpretation*:
|
||||||
|
//! `SendNamed` goes to the one resolution seam,
|
||||||
|
//! [`remote::deliver_named`](crate::cluster::remote::deliver_named).
|
||||||
|
//! `Send` goes to the pid seam (c10). The outbound
|
||||||
|
//! sender is a separate channel from `cmd_tx` on purpose: closing it is not
|
||||||
|
//! a stop signal — lifetime authority stays with the [`ConnHandle`] (D9).
|
||||||
|
//!
|
||||||
|
//! c12 adds the monitor plane, and it lives *here* on purpose. Two tables,
|
||||||
|
//! both owned by this actor and dying with the connection:
|
||||||
|
//!
|
||||||
|
//! - **outstanding** — monitors *this* node holds on actors at the peer:
|
||||||
|
//! `monitor_id → (target, Sender<RemoteDown>)`. Fed by
|
||||||
|
//! [`MonCmd`](crate::cluster::remote::MonCmd) from `monitor_remote`; the
|
||||||
|
//! actor records the id and *then* emits the `Monitor` frame, so a `Down`
|
||||||
|
//! frame can never race an entry that isn't there yet. An inbound `Down`
|
||||||
|
//! removes the entry and delivers.
|
||||||
|
//! - **watched** — monitors the *peer* holds on actors here: `monitor_id →
|
||||||
|
//! local Monitor`. An inbound `Monitor` is admitted only for a pid that
|
||||||
|
//! was exposed or crossed the wire (`is_watchable`, D12): a corpse answers
|
||||||
|
//! with its recorded terminal reason (RFC §6), an unwatchable or unknown
|
||||||
|
//! pid with `NoProc` — indistinguishable from dead, so nothing leaks. A
|
||||||
|
//! live watchable pid gets a local monitor whose `rx` is one more arm of
|
||||||
|
//! the select; its `Down` goes back as a frame.
|
||||||
|
//!
|
||||||
|
//! Because both tables are actor state, connection loss (c13) needs no
|
||||||
|
//! second bookkeeping owner: this actor's exit is the one place that knows
|
||||||
|
//! every monitor the link was carrying. `Monitors::teardown` runs on every
|
||||||
|
//! exit path and answers each outstanding monitor with `Disconnected` —
|
||||||
|
//! the roadmap's "partition vs. death" contrast: an actor that dies sends
|
||||||
|
//! its true reason over the link, a link that dies says only that.
|
||||||
|
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
|
use crate::channel::{channel, try_select_timeout, Receiver, Selectable, Sender};
|
||||||
|
use crate::cluster::envelope::{Frame, RemoteDownReason};
|
||||||
|
use crate::cluster::handshake::Peer;
|
||||||
|
use crate::cluster::manager::{Call, Registered, Reply, MANAGER};
|
||||||
|
use crate::cluster::remote::{
|
||||||
|
deliver_named, deliver_to_pid, InboundVerdict, MonCmd, RemoteDown, RemotePid,
|
||||||
|
};
|
||||||
|
use crate::cluster::transport::FramedConn;
|
||||||
|
use crate::cluster::Timing;
|
||||||
|
use crate::gen_server;
|
||||||
|
use crate::monitor::{
|
||||||
|
demonitor, is_watchable, monitor, terminal_reason, DownReason, Monitor, MonitorId,
|
||||||
|
};
|
||||||
|
use crate::pid::{Erased, Pid};
|
||||||
|
use crate::scheduler::spawn;
|
||||||
|
|
||||||
|
/// Commands to a running connection actor.
|
||||||
|
enum Cmd {
|
||||||
|
Shutdown,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The manager's authority over one connection actor: while this handle
|
||||||
|
/// lives the connection lives, and dropping it stops the actor and closes
|
||||||
|
/// the socket. Only the [`manager`](crate::cluster::manager) holds one —
|
||||||
|
/// callers of [`spawn_established`] get a [`Pid`] and no lifetime authority,
|
||||||
|
/// so a connection can never outlive, or die with, whichever actor happened
|
||||||
|
/// to establish it.
|
||||||
|
pub struct ConnHandle {
|
||||||
|
cmd_tx: Sender<Cmd>,
|
||||||
|
/// The connection's dedicated outbound inboxes — frames and monitor
|
||||||
|
/// commands. The manager moves them into the outbound table on
|
||||||
|
/// `Register` (see [`take_outbound`](ConnHandle::take_outbound)); a
|
||||||
|
/// `Duplicate` verdict drops them with the handle.
|
||||||
|
out_tx: Option<(Sender<Frame>, Sender<MonCmd>)>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Debug for ConnHandle {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
f.write_str("ConnHandle")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ConnHandle {
|
||||||
|
/// Ask the connection to close and exit. Idempotent, and a no-op if the
|
||||||
|
/// actor has already gone. Dropping the handle does the same thing; this
|
||||||
|
/// exists for the manager's explicit `Disconnect` path.
|
||||||
|
pub fn shutdown(&self) {
|
||||||
|
let _ = self.cmd_tx.send(Cmd::Shutdown);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Manager-only: take the outbound senders to bind into the outbound
|
||||||
|
/// table. Once, at registration.
|
||||||
|
pub(crate) fn take_outbound(&mut self) -> Option<(Sender<Frame>, Sender<MonCmd>)> {
|
||||||
|
self.out_tx.take()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The name was already claimed by a live connection, so this one was
|
||||||
|
/// refused; its actor has been stopped and its socket closed.
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
pub struct RegisterRefused;
|
||||||
|
|
||||||
|
/// Spawn a connection actor for an **already-established** connection (the
|
||||||
|
/// handshake completed on the path and produced `peer`) and register it with
|
||||||
|
/// the manager, synchronously, before returning. The manager takes the
|
||||||
|
/// actor's [`ConnHandle`]; the caller gets only the [`Pid`], because
|
||||||
|
/// connection lifetime belongs to the table and not to the establishing
|
||||||
|
/// actor. A refusal has already stopped the actor and closed the socket.
|
||||||
|
pub fn spawn_established(
|
||||||
|
framed: FramedConn,
|
||||||
|
peer: Peer,
|
||||||
|
timing: Timing,
|
||||||
|
) -> Result<Pid, RegisterRefused> {
|
||||||
|
let (cmd_tx, cmd_rx) = channel();
|
||||||
|
let (out_tx, out_rx) = channel();
|
||||||
|
let (mon_tx, mon_rx) = channel();
|
||||||
|
let reg_peer = peer.clone();
|
||||||
|
let pid = spawn(move || run(framed, peer, timing, cmd_rx, out_rx, mon_rx)).pid();
|
||||||
|
match gen_server::call(
|
||||||
|
MANAGER,
|
||||||
|
Call::Register {
|
||||||
|
peer: reg_peer,
|
||||||
|
pid,
|
||||||
|
handle: ConnHandle {
|
||||||
|
cmd_tx,
|
||||||
|
out_tx: Some((out_tx, mon_tx)),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
) {
|
||||||
|
Ok(Reply::Registered(Registered::Ok)) => Ok(pid),
|
||||||
|
// Duplicate name, or the manager is unreachable. Either way the
|
||||||
|
// handle went with the call and is dropped there (or never arrived
|
||||||
|
// and dropped with it), which stops the actor and closes the socket.
|
||||||
|
_ => Err(RegisterRefused),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Default for [`Timing::heartbeat_interval`]: how often this end emits
|
||||||
|
/// [`Frame::Heartbeat`] on an idle connection. The first one goes out
|
||||||
|
/// immediately at spawn, so the peer's liveness window starts fed.
|
||||||
|
pub const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(1);
|
||||||
|
|
||||||
|
/// Default for [`Timing::liveness_timeout`]: how long the connection may go
|
||||||
|
/// without a single inbound frame before it is declared dead and torn down. Any inbound frame resets the window —
|
||||||
|
/// heartbeats keep an idle connection alive, and real traffic (c8+) counts
|
||||||
|
/// for free. Fixed by design (RFC v2 §5): this is the control connection, a
|
||||||
|
/// heartbeat can never queue behind bulk traffic, so a fixed timeout is an
|
||||||
|
/// honest detector.
|
||||||
|
pub const LIVENESS_TIMEOUT: Duration = Duration::from_secs(4);
|
||||||
|
|
||||||
|
fn run(
|
||||||
|
mut framed: FramedConn,
|
||||||
|
_peer: Peer,
|
||||||
|
timing: Timing,
|
||||||
|
cmd_rx: Receiver<Cmd>,
|
||||||
|
out_rx: Receiver<Frame>,
|
||||||
|
mon_rx: Receiver<MonCmd>,
|
||||||
|
) {
|
||||||
|
let mut mons = Monitors::default();
|
||||||
|
match framed.readable_arm() {
|
||||||
|
Some(arm) => run_live(
|
||||||
|
&mut framed,
|
||||||
|
arm,
|
||||||
|
timing,
|
||||||
|
&cmd_rx,
|
||||||
|
&out_rx,
|
||||||
|
&mon_rx,
|
||||||
|
&mut mons,
|
||||||
|
),
|
||||||
|
None => run_inert(&cmd_rx),
|
||||||
|
}
|
||||||
|
framed.close();
|
||||||
|
mons.teardown(&mon_rx);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The monitor plane's two tables (module docs). Owned by the actor.
|
||||||
|
#[derive(Default)]
|
||||||
|
struct Monitors {
|
||||||
|
/// Monitors this node holds on peer actors: id → (target, delivery).
|
||||||
|
outstanding: HashMap<MonitorId, (RemotePid<Erased>, Sender<RemoteDown>)>,
|
||||||
|
/// Monitors the peer holds on local actors: id → the local monitor.
|
||||||
|
watched: HashMap<MonitorId, Monitor>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Monitors {
|
||||||
|
/// The connection is gone, whatever the exit path (liveness expiry,
|
||||||
|
/// EOF, wire failure, commanded stop): release the peer's local
|
||||||
|
/// monitors, and answer every one of ours with `Disconnected` — nothing
|
||||||
|
/// more can be known about those actors. Commands still sitting in the
|
||||||
|
/// inbox are folded in first (a `Monitor` handed to us but never
|
||||||
|
/// processed gets its notice too; a `Demonitor` still cancels), so the
|
||||||
|
/// only registration that can miss this is one that lands after the
|
||||||
|
/// drain and before the inbox drops — the reader side backstops that
|
||||||
|
/// (`RemoteMonitor`). Entries leave the table as they are answered, and
|
||||||
|
/// this runs once per actor, so no monitor sees two notices.
|
||||||
|
fn teardown(&mut self, mon_rx: &Receiver<MonCmd>) {
|
||||||
|
for (_, m) in self.watched.drain() {
|
||||||
|
let _ = demonitor(&m);
|
||||||
|
}
|
||||||
|
while let Ok(Some(cmd)) = mon_rx.try_recv() {
|
||||||
|
match cmd {
|
||||||
|
MonCmd::Monitor { id, target, tx } => {
|
||||||
|
self.outstanding.insert(id, (target, tx));
|
||||||
|
}
|
||||||
|
MonCmd::Demonitor { id } => {
|
||||||
|
self.outstanding.remove(&id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (_, (pid, tx)) in self.outstanding.drain() {
|
||||||
|
let _ = tx.send(RemoteDown {
|
||||||
|
pid,
|
||||||
|
reason: RemoteDownReason::Disconnected,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Admit a peer's `Monitor` for local `(index, generation)`. Returns
|
||||||
|
/// the reason to answer with at once, or `None` if a live monitor was
|
||||||
|
/// installed. Corpse → recorded terminal reason (RFC §6, and only
|
||||||
|
/// watchable deaths are recorded); live watchable → monitor; anything
|
||||||
|
/// else → `NoProc`. The check-then-monitor race (dies in between) is
|
||||||
|
/// closed on the read side: a `NoProc` from a monitor we installed on a
|
||||||
|
/// live pid is upgraded through `terminal_reason` in `sweep_watched`.
|
||||||
|
fn admit(&mut self, id: MonitorId, index: u32, generation: u32) -> Option<DownReason> {
|
||||||
|
let pid = Pid::new(index, generation);
|
||||||
|
if let Some(reason) = terminal_reason(pid) {
|
||||||
|
return Some(reason);
|
||||||
|
}
|
||||||
|
if !is_watchable(pid) {
|
||||||
|
return Some(DownReason::NoProc);
|
||||||
|
}
|
||||||
|
let m = monitor(pid);
|
||||||
|
self.watched.insert(id, m);
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
fn cancel(&mut self, id: MonitorId) {
|
||||||
|
if let Some(m) = self.watched.remove(&id) {
|
||||||
|
let _ = demonitor(&m);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Collect every local `Down` that has arrived for a peer-held monitor.
|
||||||
|
fn sweep_watched(&mut self) -> Vec<(MonitorId, DownReason)> {
|
||||||
|
let mut fired = Vec::new();
|
||||||
|
for (id, m) in self.watched.iter() {
|
||||||
|
if let Ok(Some(down)) = m.rx.try_recv() {
|
||||||
|
let reason = match down.reason {
|
||||||
|
DownReason::NoProc => terminal_reason(m.target).unwrap_or(DownReason::NoProc),
|
||||||
|
r => r,
|
||||||
|
};
|
||||||
|
fired.push((*id, reason));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (id, _) in &fired {
|
||||||
|
self.watched.remove(id);
|
||||||
|
}
|
||||||
|
fired
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The peer reports a monitored actor down: deliver locally.
|
||||||
|
fn down(&mut self, id: MonitorId, reason: RemoteDownReason) {
|
||||||
|
if let Some((pid, tx)) = self.outstanding.remove(&id) {
|
||||||
|
let _ = tx.send(RemoteDown { pid, reason });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The steady-state loop over an fd-backed connection: one
|
||||||
|
/// `select_timeout` folds the command inbox, the outbound inbox, socket
|
||||||
|
/// readability, and the nearer of the two deadlines (`hb_send`,
|
||||||
|
/// `liveness`) into a single wait.
|
||||||
|
fn run_live(
|
||||||
|
framed: &mut FramedConn,
|
||||||
|
arm: crate::scheduler::FdArm,
|
||||||
|
timing: Timing,
|
||||||
|
cmd_rx: &Receiver<Cmd>,
|
||||||
|
out_rx: &Receiver<Frame>,
|
||||||
|
mon_rx: &Receiver<MonCmd>,
|
||||||
|
mons: &mut Monitors,
|
||||||
|
) {
|
||||||
|
let mut next_hb = Instant::now();
|
||||||
|
let mut live_until = Instant::now() + timing.liveness_timeout;
|
||||||
|
// The outbound senders live in the manager's table and are dropped on
|
||||||
|
// unbind; after that these arms would wake forever, so they drop out of
|
||||||
|
// the select (not a stop signal — see the module docs).
|
||||||
|
let mut out_open = true;
|
||||||
|
let mut mon_open = true;
|
||||||
|
// Which wait each select arm stands for. Built in lockstep with the
|
||||||
|
// `Selectable` vector each iteration, so a wake is decoded by name and
|
||||||
|
// never by position.
|
||||||
|
enum Arm {
|
||||||
|
Cmd,
|
||||||
|
Fd,
|
||||||
|
Out,
|
||||||
|
Mon,
|
||||||
|
/// A peer-held local monitor (any of them: firing sweeps them all).
|
||||||
|
Watched,
|
||||||
|
}
|
||||||
|
fn push<'s>(
|
||||||
|
arms: &mut Vec<&'s dyn Selectable>,
|
||||||
|
what: &mut Vec<Arm>,
|
||||||
|
s: &'s dyn Selectable,
|
||||||
|
a: Arm,
|
||||||
|
) {
|
||||||
|
arms.push(s);
|
||||||
|
what.push(a);
|
||||||
|
}
|
||||||
|
loop {
|
||||||
|
let now = Instant::now();
|
||||||
|
if now >= live_until {
|
||||||
|
break; // liveness expired: the peer is dead to us
|
||||||
|
}
|
||||||
|
if now >= next_hb {
|
||||||
|
if framed.send(&Frame::Heartbeat).is_err() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
next_hb = now + timing.heartbeat_interval;
|
||||||
|
}
|
||||||
|
let wait = next_hb.min(live_until).saturating_duration_since(now);
|
||||||
|
let mut arms: Vec<&dyn Selectable> = Vec::new();
|
||||||
|
let mut what: Vec<Arm> = Vec::new();
|
||||||
|
push(&mut arms, &mut what, cmd_rx, Arm::Cmd);
|
||||||
|
push(&mut arms, &mut what, &arm, Arm::Fd);
|
||||||
|
if out_open {
|
||||||
|
push(&mut arms, &mut what, out_rx, Arm::Out);
|
||||||
|
}
|
||||||
|
if mon_open {
|
||||||
|
push(&mut arms, &mut what, mon_rx, Arm::Mon);
|
||||||
|
}
|
||||||
|
for m in mons.watched.values() {
|
||||||
|
push(&mut arms, &mut what, &m.rx, Arm::Watched);
|
||||||
|
}
|
||||||
|
match try_select_timeout(&arms, wait).map(|i| i.map(|i| &what[i])) {
|
||||||
|
Ok(Some(Arm::Cmd)) => {
|
||||||
|
if should_stop(cmd_rx) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(Some(Arm::Fd)) => match pump_readable(framed, mons) {
|
||||||
|
Pump::Ended => break,
|
||||||
|
Pump::Frames(n) => {
|
||||||
|
if n > 0 {
|
||||||
|
live_until = Instant::now() + timing.liveness_timeout;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Ok(Some(Arm::Out)) => match pump_outbound(framed, out_rx) {
|
||||||
|
Outbound::Drained => {}
|
||||||
|
Outbound::Closed => out_open = false,
|
||||||
|
Outbound::WireFailed => break,
|
||||||
|
},
|
||||||
|
Ok(Some(Arm::Mon)) => match pump_moncmds(framed, mon_rx, mons) {
|
||||||
|
Outbound::Drained => {}
|
||||||
|
Outbound::Closed => mon_open = false,
|
||||||
|
Outbound::WireFailed => break,
|
||||||
|
},
|
||||||
|
Ok(Some(Arm::Watched)) => {
|
||||||
|
for (id, reason) in mons.sweep_watched() {
|
||||||
|
let frame = Frame::Down {
|
||||||
|
monitor_id: id.0,
|
||||||
|
reason: reason.into(),
|
||||||
|
};
|
||||||
|
if framed.send(&frame).is_err() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// A deadline passed; the top of the loop acts on whichever.
|
||||||
|
Ok(None) => {}
|
||||||
|
// The fd arm failed to register — the connection is gone.
|
||||||
|
Err(_) => break,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Drain the monitor-command inbox: record, then emit (module docs).
|
||||||
|
fn pump_moncmds(
|
||||||
|
framed: &mut FramedConn,
|
||||||
|
mon_rx: &Receiver<MonCmd>,
|
||||||
|
mons: &mut Monitors,
|
||||||
|
) -> Outbound {
|
||||||
|
loop {
|
||||||
|
match mon_rx.try_recv() {
|
||||||
|
Ok(Some(MonCmd::Monitor { id, target, tx })) => {
|
||||||
|
let frame = Frame::Monitor {
|
||||||
|
monitor_id: id.0,
|
||||||
|
index: target.index(),
|
||||||
|
generation: target.generation(),
|
||||||
|
};
|
||||||
|
mons.outstanding.insert(id, (target, tx));
|
||||||
|
if framed.send(&frame).is_err() {
|
||||||
|
return Outbound::WireFailed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(Some(MonCmd::Demonitor { id })) => {
|
||||||
|
if mons.outstanding.remove(&id).is_some()
|
||||||
|
&& framed.send(&Frame::Demonitor { monitor_id: id.0 }).is_err()
|
||||||
|
{
|
||||||
|
return Outbound::WireFailed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(None) => return Outbound::Drained,
|
||||||
|
Err(_) => return Outbound::Closed,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// What one outbound-side wake (frames or monitor commands) yielded.
|
||||||
|
enum Outbound {
|
||||||
|
/// Everything queued went onto the wire; the inbox is open and empty.
|
||||||
|
Drained,
|
||||||
|
/// The manager unbound this connection's sender; nothing more will come.
|
||||||
|
Closed,
|
||||||
|
/// The socket refused a write: the connection is gone.
|
||||||
|
WireFailed,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Drain every queued outbound frame onto the wire.
|
||||||
|
fn pump_outbound(framed: &mut FramedConn, out_rx: &Receiver<Frame>) -> Outbound {
|
||||||
|
loop {
|
||||||
|
match out_rx.try_recv() {
|
||||||
|
Ok(Some(frame)) => {
|
||||||
|
if framed.send(&frame).is_err() {
|
||||||
|
return Outbound::WireFailed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(None) => return Outbound::Drained,
|
||||||
|
Err(_) => return Outbound::Closed,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// No fd to select on (loopback): only a command can end the wait, and
|
||||||
|
/// neither heartbeats nor liveness run — a transport that can't report
|
||||||
|
/// readiness can't be timed either (same caveat as
|
||||||
|
/// [`FramedConn::recv_deadline`]). Loopback is a test transport; every real
|
||||||
|
/// connection is fd-backed.
|
||||||
|
fn run_inert(cmd_rx: &Receiver<Cmd>) {
|
||||||
|
loop {
|
||||||
|
let arms: [&dyn Selectable; 1] = [cmd_rx];
|
||||||
|
let _ = crate::channel::select(&arms);
|
||||||
|
if should_stop(cmd_rx) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Drain the command arm. Returns `true` when the actor should exit — a
|
||||||
|
/// shutdown was requested, or the last handle was dropped.
|
||||||
|
fn should_stop(cmd_rx: &Receiver<Cmd>) -> bool {
|
||||||
|
match cmd_rx.try_recv() {
|
||||||
|
Ok(Some(Cmd::Shutdown)) => true,
|
||||||
|
Ok(None) => false, // spurious wake
|
||||||
|
Err(_) => true, // all senders dropped
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// What one readable wake yielded.
|
||||||
|
enum Pump {
|
||||||
|
/// The connection has ended: EOF (clean or mid-frame) or an
|
||||||
|
/// unrecoverable stream error.
|
||||||
|
Ended,
|
||||||
|
/// Still up; this many complete frames were consumed (possibly zero, if
|
||||||
|
/// the wake delivered only part of a frame). Any nonzero count resets
|
||||||
|
/// the liveness window.
|
||||||
|
Frames(usize),
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Surface an inbound verdict: one `smarm-trace` event, nothing else — it
|
||||||
|
/// is local knowledge (RFC §3). A no-op without the feature.
|
||||||
|
fn note_verdict(verdict: InboundVerdict) {
|
||||||
|
#[cfg(feature = "smarm-trace")]
|
||||||
|
crate::te!(crate::trace::Event::ClusterInbound(verdict.label()));
|
||||||
|
#[cfg(not(feature = "smarm-trace"))]
|
||||||
|
drop(verdict);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Consume one readable wake: exactly one socket read (which cannot block
|
||||||
|
/// after a level-triggered readable indication), then drain every complete
|
||||||
|
/// frame the buffer now holds. A blocking `recv` here would park the actor
|
||||||
|
/// past its heartbeat and liveness deadlines whenever a frame arrives split.
|
||||||
|
/// Every consumed frame counts for liveness; `SendNamed` goes to the one
|
||||||
|
/// name-resolution seam and `Send` to the pid seam. Verdicts are local
|
||||||
|
/// knowledge only — nothing goes back on the wire (RFC §3) — and surface
|
||||||
|
/// as one `smarm-trace` `ClusterInbound` event each (zero cost off).
|
||||||
|
/// `Monitor`/`Demonitor`/`Down` go to the [`Monitors`] tables; a `Monitor`
|
||||||
|
/// that can be answered at once is answered inline.
|
||||||
|
fn pump_readable(framed: &mut FramedConn, mons: &mut Monitors) -> Pump {
|
||||||
|
let eof = match framed.read_once() {
|
||||||
|
Ok(n) => n == 0,
|
||||||
|
Err(_) => return Pump::Ended,
|
||||||
|
};
|
||||||
|
let mut got = 0;
|
||||||
|
loop {
|
||||||
|
match framed.next_buffered() {
|
||||||
|
Ok(Some(frame)) => {
|
||||||
|
got += 1;
|
||||||
|
match frame {
|
||||||
|
Frame::SendNamed {
|
||||||
|
name,
|
||||||
|
type_hash,
|
||||||
|
payload,
|
||||||
|
} => {
|
||||||
|
note_verdict(deliver_named(&name, type_hash, &payload));
|
||||||
|
}
|
||||||
|
Frame::Send {
|
||||||
|
index,
|
||||||
|
generation,
|
||||||
|
type_hash,
|
||||||
|
payload,
|
||||||
|
} => {
|
||||||
|
note_verdict(deliver_to_pid(index, generation, type_hash, &payload));
|
||||||
|
}
|
||||||
|
Frame::Monitor {
|
||||||
|
monitor_id,
|
||||||
|
index,
|
||||||
|
generation,
|
||||||
|
} => {
|
||||||
|
let id = MonitorId(monitor_id);
|
||||||
|
if let Some(reason) = mons.admit(id, index, generation) {
|
||||||
|
let frame = Frame::Down {
|
||||||
|
monitor_id,
|
||||||
|
reason: reason.into(),
|
||||||
|
};
|
||||||
|
if framed.send(&frame).is_err() {
|
||||||
|
return Pump::Ended;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Frame::Demonitor { monitor_id } => mons.cancel(MonitorId(monitor_id)),
|
||||||
|
Frame::Down { monitor_id, reason } => mons.down(MonitorId(monitor_id), reason),
|
||||||
|
// Heartbeat: liveness only. Handshake frames after
|
||||||
|
// establishment: ignored.
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(None) => break,
|
||||||
|
Err(_) => return Pump::Ended, // corrupt stream
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if eof {
|
||||||
|
Pump::Ended
|
||||||
|
} else {
|
||||||
|
Pump::Frames(got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
//! `Monitors::teardown` in isolation: the actor-side half of c13, pinned
|
||||||
|
//! separately because from the outside it is indistinguishable from the
|
||||||
|
//! read-side backstop in `RemoteMonitor` (both yield `Disconnected`).
|
||||||
|
use super::*;
|
||||||
|
use crate::pg::Incarnation;
|
||||||
|
|
||||||
|
fn pid(index: u32) -> RemotePid<Erased> {
|
||||||
|
RemotePid::from_parts("peer", Incarnation::new(1), index, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn teardown_answers_every_outstanding_and_unread_monitor_once() {
|
||||||
|
crate::run(|| {
|
||||||
|
let mut mons = Monitors::default();
|
||||||
|
let (mon_tx, mon_rx) = channel::<MonCmd>();
|
||||||
|
|
||||||
|
// Already registered.
|
||||||
|
let (tx1, rx1) = channel::<RemoteDown>();
|
||||||
|
mons.outstanding.insert(MonitorId(1), (pid(1), tx1));
|
||||||
|
// In the inbox, never processed.
|
||||||
|
let (tx2, rx2) = channel::<RemoteDown>();
|
||||||
|
mon_tx
|
||||||
|
.send(MonCmd::Monitor {
|
||||||
|
id: MonitorId(2),
|
||||||
|
target: pid(2),
|
||||||
|
tx: tx2,
|
||||||
|
})
|
||||||
|
.ok()
|
||||||
|
.unwrap();
|
||||||
|
// Registered, then cancelled in the inbox: silence.
|
||||||
|
let (tx3, rx3) = channel::<RemoteDown>();
|
||||||
|
mons.outstanding.insert(MonitorId(3), (pid(3), tx3));
|
||||||
|
mon_tx
|
||||||
|
.send(MonCmd::Demonitor { id: MonitorId(3) })
|
||||||
|
.ok()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
mons.teardown(&mon_rx);
|
||||||
|
|
||||||
|
let d1 = rx1.recv().unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
(d1.pid, d1.reason),
|
||||||
|
(pid(1), RemoteDownReason::Disconnected)
|
||||||
|
);
|
||||||
|
let d2 = rx2.recv().unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
(d2.pid, d2.reason),
|
||||||
|
(pid(2), RemoteDownReason::Disconnected)
|
||||||
|
);
|
||||||
|
// Cancelled: no notice was sent (its sender is dropped, channel
|
||||||
|
// closed-empty), and nobody got a second one.
|
||||||
|
assert!(rx3.try_recv().is_err());
|
||||||
|
assert!(rx1.try_recv().is_err());
|
||||||
|
assert!(rx2.try_recv().is_err());
|
||||||
|
assert!(mons.outstanding.is_empty());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,365 @@
|
|||||||
|
//! RFC 010 c6b — the handshake on the accept/connect path.
|
||||||
|
//!
|
||||||
|
//! Per D8 (re-amended): the c5 machines are driven by **straight-line code
|
||||||
|
//! on the path**, not by an actor. The dial side runs [`Initiator`]; the
|
||||||
|
//! acceptor loop runs [`Responder`]. A connection actor is spawned only
|
||||||
|
//! *after* a successful handshake ([`spawn_established`]); every reject,
|
||||||
|
//! protocol failure, timeout, and tie-break loss is resolved right here,
|
||||||
|
//! on the path, by closing — no actor ever exists for a connection that
|
||||||
|
//! didn't establish.
|
||||||
|
//!
|
||||||
|
//! Buffer trap (binding): the path reader and the steady-state actor share
|
||||||
|
//! ONE [`FramedConn`]. Its decode buffer may hold read-ahead past the
|
||||||
|
//! handshake frames, so the *whole* `FramedConn` travels into
|
||||||
|
//! [`spawn_established`] — never a fresh codec over the same socket.
|
||||||
|
//!
|
||||||
|
//! Layering: [`dial_handshake`] and [`accept_handshake`] are the bare path
|
||||||
|
//! steps — IO on a `FramedConn`, no manager, no actors — testable over the
|
||||||
|
//! loopback transport on plain threads. [`dial`] and [`spawn_acceptor`] are
|
||||||
|
//! the manager-integrated layer (actor context required): they keep the
|
||||||
|
//! [`manager`](crate::cluster::manager)'s dial-intent set honest and spawn
|
||||||
|
//! the connection actor on success.
|
||||||
|
|
||||||
|
use std::io;
|
||||||
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
|
use crate::channel::{channel, Receiver, Selectable, Sender};
|
||||||
|
use crate::cluster::conn::spawn_established;
|
||||||
|
use crate::cluster::envelope::{Frame, RejectReason};
|
||||||
|
use crate::cluster::handshake::{
|
||||||
|
Initiator, InitiatorOutcome, Local, Peer, PeerStanding, Responder, ResponderOutcome,
|
||||||
|
};
|
||||||
|
use crate::cluster::manager::{Call, Reply, MANAGER};
|
||||||
|
use crate::cluster::transport::{FramedConn, Listener, RecvError, SendError, Transport};
|
||||||
|
use crate::cluster::Timing;
|
||||||
|
use crate::gen_server;
|
||||||
|
use crate::pid::Pid;
|
||||||
|
use crate::scheduler::{self, spawn};
|
||||||
|
|
||||||
|
/// Default for [`Timing::handshake_timeout`]: how long either side waits for
|
||||||
|
/// the peer's handshake frame before giving up and closing. Enforced on the path via [`FramedConn::recv_deadline`],
|
||||||
|
/// so a peer that connects and goes silent cannot wedge the acceptor.
|
||||||
|
pub const HANDSHAKE_TIMEOUT: Duration = Duration::from_secs(5);
|
||||||
|
|
||||||
|
/// Why a handshake did not establish. In every case the connection has
|
||||||
|
/// already been closed on the path by the time this is returned.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum HandshakeError {
|
||||||
|
/// A `HelloReject` travelled — sent by us (accept side) or received by
|
||||||
|
/// us (dial side).
|
||||||
|
Rejected(RejectReason),
|
||||||
|
/// Accept side only: the inbound dial lost the simultaneous-connect
|
||||||
|
/// tie-break (D7) and was closed silently, no frame sent.
|
||||||
|
TieBreakLoss,
|
||||||
|
/// The peer spoke a valid frame that is wrong here (non-`Hello` first
|
||||||
|
/// frame; non-response to our `Hello`), or an undecodable byte stream.
|
||||||
|
Protocol,
|
||||||
|
/// EOF before the handshake resolved. On the dial side this is also
|
||||||
|
/// what losing the tie-break looks like: the peer closes silently.
|
||||||
|
Closed,
|
||||||
|
/// [`HANDSHAKE_TIMEOUT`] (or the caller's deadline) passed first.
|
||||||
|
TimedOut,
|
||||||
|
/// The transport failed mid-handshake.
|
||||||
|
Transport(io::Error),
|
||||||
|
}
|
||||||
|
|
||||||
|
fn from_send(e: SendError) -> HandshakeError {
|
||||||
|
match e {
|
||||||
|
// Handshake frames are small and self-made; an encode failure is a
|
||||||
|
// protocol-level impossibility, not a transport fault.
|
||||||
|
SendError::Encode(_) => HandshakeError::Protocol,
|
||||||
|
SendError::Io(e) => HandshakeError::Transport(e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn from_recv(e: RecvError) -> HandshakeError {
|
||||||
|
match e {
|
||||||
|
RecvError::Corrupt(_) => HandshakeError::Protocol,
|
||||||
|
RecvError::TruncatedByPeer => HandshakeError::Closed,
|
||||||
|
RecvError::Io(e) => HandshakeError::Transport(e),
|
||||||
|
RecvError::TimedOut => HandshakeError::TimedOut,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Dial-side path step: send our `Hello`, interpret the one response. On
|
||||||
|
/// `Ok` the connection is established and `framed` is live (with any
|
||||||
|
/// read-ahead intact in its buffer); on `Err` the connection is closed.
|
||||||
|
pub fn dial_handshake(
|
||||||
|
framed: &mut FramedConn,
|
||||||
|
local: &Local,
|
||||||
|
deadline: Instant,
|
||||||
|
) -> Result<Peer, HandshakeError> {
|
||||||
|
let (initiator, hello) = Initiator::new(local);
|
||||||
|
if let Err(e) = framed.send(&hello) {
|
||||||
|
framed.close();
|
||||||
|
return Err(from_send(e));
|
||||||
|
}
|
||||||
|
let outcome = match framed.recv_deadline(deadline) {
|
||||||
|
Ok(Some(frame)) => initiator.on_frame(frame),
|
||||||
|
Ok(None) => {
|
||||||
|
framed.close();
|
||||||
|
return Err(HandshakeError::Closed);
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
framed.close();
|
||||||
|
return Err(from_recv(e));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
match outcome {
|
||||||
|
InitiatorOutcome::Established(peer) => Ok(peer),
|
||||||
|
InitiatorOutcome::Rejected(reason) => {
|
||||||
|
framed.close();
|
||||||
|
Err(HandshakeError::Rejected(reason))
|
||||||
|
}
|
||||||
|
InitiatorOutcome::Failed(_) => {
|
||||||
|
framed.close();
|
||||||
|
Err(HandshakeError::Protocol)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Accept-side path step: read the first frame, judge it, answer or close.
|
||||||
|
///
|
||||||
|
/// `standing_of` supplies the [`PeerStanding`] of the *offered* name — knowledge
|
||||||
|
/// only the frame reveals, which is why it is a callback and not a value
|
||||||
|
/// (the integrated acceptor asks the manager; loopback tests fabricate).
|
||||||
|
/// It is not called when the first frame is not a `Hello`.
|
||||||
|
///
|
||||||
|
/// On `Ok` the ack has been sent and `framed` is live (read-ahead intact);
|
||||||
|
/// on `Err` any owed reject has been sent and the connection is closed.
|
||||||
|
pub fn accept_handshake(
|
||||||
|
framed: &mut FramedConn,
|
||||||
|
local: Local,
|
||||||
|
standing_of: impl FnOnce(&str) -> PeerStanding,
|
||||||
|
deadline: Instant,
|
||||||
|
) -> Result<Peer, HandshakeError> {
|
||||||
|
let frame = match framed.recv_deadline(deadline) {
|
||||||
|
Ok(Some(frame)) => frame,
|
||||||
|
Ok(None) => {
|
||||||
|
framed.close();
|
||||||
|
return Err(HandshakeError::Closed);
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
framed.close();
|
||||||
|
return Err(from_recv(e));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let standing = match &frame {
|
||||||
|
Frame::Hello { node_name, .. } => standing_of(node_name),
|
||||||
|
_ => PeerStanding::Free,
|
||||||
|
};
|
||||||
|
match Responder::new(local).on_frame(frame, standing) {
|
||||||
|
ResponderOutcome::Accepted { reply, peer } => {
|
||||||
|
if let Err(e) = framed.send(&reply) {
|
||||||
|
framed.close();
|
||||||
|
return Err(from_send(e));
|
||||||
|
}
|
||||||
|
Ok(peer)
|
||||||
|
}
|
||||||
|
ResponderOutcome::Rejected { reply, reason } => {
|
||||||
|
// Best effort: the reject is the cross-version compatibility
|
||||||
|
// anchor, but if the write fails the peer sees a bare close,
|
||||||
|
// which it must survive anyway.
|
||||||
|
let _ = framed.send(&reply);
|
||||||
|
framed.close();
|
||||||
|
Err(HandshakeError::Rejected(reason))
|
||||||
|
}
|
||||||
|
ResponderOutcome::TieBreakLoss => {
|
||||||
|
// D7: close silently — the peer computes the same verdict.
|
||||||
|
framed.close();
|
||||||
|
Err(HandshakeError::TieBreakLoss)
|
||||||
|
}
|
||||||
|
ResponderOutcome::Failed(_) => {
|
||||||
|
framed.close();
|
||||||
|
Err(HandshakeError::Protocol)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Manager-integrated layer
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
/// Why an integrated [`dial`] did not produce a connection.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum DialError {
|
||||||
|
/// Another dial to this peer name is already in flight.
|
||||||
|
AlreadyDialing,
|
||||||
|
/// The manager is not running (or answered nonsense).
|
||||||
|
ManagerUnavailable,
|
||||||
|
/// The transport could not connect.
|
||||||
|
Connect(io::Error),
|
||||||
|
/// Connected, but the handshake did not establish.
|
||||||
|
Handshake(HandshakeError),
|
||||||
|
/// The peer at `addr` established, but answered as a different name
|
||||||
|
/// than the one we dialed — the tie-break bookkeeping (keyed by the
|
||||||
|
/// dialed name) would be unsound, so the connection is closed.
|
||||||
|
PeerNameMismatch { expected: String, got: String },
|
||||||
|
/// The handshake established, but the manager refused the registration:
|
||||||
|
/// a connection to this peer already exists. The loser has been closed.
|
||||||
|
Duplicate,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DialError {
|
||||||
|
/// A short static label per kind, for the `smarm-trace` `ClusterDial`
|
||||||
|
/// event; the payload (io error, names) is not carried.
|
||||||
|
pub fn label(&self) -> &'static str {
|
||||||
|
match self {
|
||||||
|
DialError::AlreadyDialing => "already_dialing",
|
||||||
|
DialError::ManagerUnavailable => "manager_unavailable",
|
||||||
|
DialError::Connect(_) => "connect",
|
||||||
|
DialError::Handshake(_) => "handshake",
|
||||||
|
DialError::PeerNameMismatch { .. } => "peer_name_mismatch",
|
||||||
|
DialError::Duplicate => "duplicate",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Dial `peer_name` at `addr` and run the handshake, keeping the manager's
|
||||||
|
/// dial-intent set honest around it: the intent is registered *before*
|
||||||
|
/// connecting (so a crossing inbound `Hello` sees it) and cleared the
|
||||||
|
/// moment the handshake resolves, before the connection actor is spawned.
|
||||||
|
/// Must run inside an actor. Retrying is the caller's business (c7's dial
|
||||||
|
/// loop); a lost tie-break surfaces as `Handshake(Closed)` — the peer's
|
||||||
|
/// accepted connection is already on its way.
|
||||||
|
pub fn dial(
|
||||||
|
transport: &dyn Transport,
|
||||||
|
addr: &str,
|
||||||
|
peer_name: &str,
|
||||||
|
local: &Local,
|
||||||
|
timing: Timing,
|
||||||
|
) -> Result<Pid, DialError> {
|
||||||
|
let me = scheduler::self_pid();
|
||||||
|
match gen_server::call(
|
||||||
|
MANAGER,
|
||||||
|
Call::DialBegin {
|
||||||
|
name: peer_name.to_string(),
|
||||||
|
pid: me,
|
||||||
|
},
|
||||||
|
) {
|
||||||
|
Ok(Reply::DialBegan(true)) => {}
|
||||||
|
Ok(Reply::DialBegan(false)) => return Err(DialError::AlreadyDialing),
|
||||||
|
_ => return Err(DialError::ManagerUnavailable),
|
||||||
|
}
|
||||||
|
let result = connect_and_shake(transport, addr, local, timing);
|
||||||
|
// Cleared immediately on outcome — a stale intent during the established
|
||||||
|
// window would corrupt later tie-breaks. Synchronous (a call): the
|
||||||
|
// intent is provably gone before anything else happens.
|
||||||
|
let _ = gen_server::call(
|
||||||
|
MANAGER,
|
||||||
|
Call::DialEnd {
|
||||||
|
name: peer_name.to_string(),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
let (mut framed, peer) = result?;
|
||||||
|
if peer.node_name != peer_name {
|
||||||
|
framed.close();
|
||||||
|
return Err(DialError::PeerNameMismatch {
|
||||||
|
expected: peer_name.to_string(),
|
||||||
|
got: peer.node_name,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
spawn_established(framed, peer, timing).map_err(|_| DialError::Duplicate)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn connect_and_shake(
|
||||||
|
transport: &dyn Transport,
|
||||||
|
addr: &str,
|
||||||
|
local: &Local,
|
||||||
|
timing: Timing,
|
||||||
|
) -> Result<(FramedConn, Peer), DialError> {
|
||||||
|
let conn = transport.dial(addr).map_err(DialError::Connect)?;
|
||||||
|
let mut framed = FramedConn::new(conn);
|
||||||
|
let deadline = Instant::now() + timing.handshake_timeout;
|
||||||
|
let peer = dial_handshake(&mut framed, local, deadline).map_err(DialError::Handshake)?;
|
||||||
|
Ok((framed, peer))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A running acceptor. [`shutdown`](AcceptorHandle::shutdown) (or dropping
|
||||||
|
/// the last handle) stops the accept loop only: connections it established
|
||||||
|
/// belong to the [`manager`](crate::cluster::manager) and keep running, to
|
||||||
|
/// be torn down through the table (`Disconnect`, a peer close, or manager
|
||||||
|
/// shutdown).
|
||||||
|
pub struct AcceptorHandle {
|
||||||
|
cmd_tx: Sender<()>,
|
||||||
|
addr: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AcceptorHandle {
|
||||||
|
/// Ask the acceptor to stop. Idempotent; a no-op if it already has.
|
||||||
|
pub fn shutdown(&self) {
|
||||||
|
let _ = self.cmd_tx.send(());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The concrete bound address, dialable as-is.
|
||||||
|
pub fn local_addr(&self) -> &str {
|
||||||
|
&self.addr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Spawn the acceptor actor over a bound listener. Each inbound connection
|
||||||
|
/// is handshaken **inline in the loop** (a deliberate serialization: the
|
||||||
|
/// per-frame deadline bounds how long any one peer can hold the line, and
|
||||||
|
/// nothing concurrent exists to be starved before c7). The listener must be
|
||||||
|
/// fd-backed ([`Listener::readable_arm`]); the loopback listener is not,
|
||||||
|
/// and its acceptor exits immediately — loopback handshakes are driven
|
||||||
|
/// synchronously through the path fns instead, per D8.
|
||||||
|
pub fn spawn_acceptor(listener: Box<dyn Listener>, local: Local, timing: Timing) -> AcceptorHandle {
|
||||||
|
let addr = listener.local_addr();
|
||||||
|
let (cmd_tx, cmd_rx) = channel();
|
||||||
|
spawn(move || accept_loop(listener, local, timing, cmd_rx));
|
||||||
|
AcceptorHandle { cmd_tx, addr }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn accept_loop(
|
||||||
|
mut listener: Box<dyn Listener>,
|
||||||
|
local: Local,
|
||||||
|
timing: Timing,
|
||||||
|
cmd_rx: Receiver<()>,
|
||||||
|
) {
|
||||||
|
loop {
|
||||||
|
let Some(arm) = listener.readable_arm() else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
let arms: [&dyn Selectable; 2] = [&cmd_rx, &arm];
|
||||||
|
match crate::channel::try_select(&arms) {
|
||||||
|
Ok(0) => match cmd_rx.try_recv() {
|
||||||
|
Ok(Some(())) => return,
|
||||||
|
Ok(None) => continue, // spurious wake
|
||||||
|
Err(_) => return, // all handles dropped
|
||||||
|
},
|
||||||
|
Ok(_) => {
|
||||||
|
// The listener is readable: accept completes without parking.
|
||||||
|
let conn = match listener.accept() {
|
||||||
|
Ok(conn) => conn,
|
||||||
|
Err(_) => return, // listener itself is broken
|
||||||
|
};
|
||||||
|
handle_inbound(FramedConn::new(conn), &local, timing);
|
||||||
|
}
|
||||||
|
Err(_) => return, // fd arm failed to register: listener is gone
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Run the accept-side handshake for one inbound connection, asking the
|
||||||
|
/// manager for the [`PeerStanding`], and hand the established connection to the
|
||||||
|
/// manager. Every failure was already resolved on the path (reject sent /
|
||||||
|
/// closed, or the registration refused and the actor stopped), so there is
|
||||||
|
/// nothing for the acceptor to carry forward.
|
||||||
|
fn handle_inbound(mut framed: FramedConn, local: &Local, timing: Timing) {
|
||||||
|
let deadline = Instant::now() + timing.handshake_timeout;
|
||||||
|
let standing_of = |name: &str| match gen_server::call(
|
||||||
|
MANAGER,
|
||||||
|
Call::Standing {
|
||||||
|
peer_name: name.to_string(),
|
||||||
|
},
|
||||||
|
) {
|
||||||
|
Ok(Reply::Standing(s)) => s,
|
||||||
|
// Manager unreachable: nobody could register this connection anyway,
|
||||||
|
// so claim the name taken and reject rather than accept an orphan.
|
||||||
|
_ => PeerStanding::Claimed,
|
||||||
|
};
|
||||||
|
if let Ok(peer) = accept_handshake(&mut framed, local.clone(), standing_of, deadline) {
|
||||||
|
let _ = spawn_established(framed, peer, timing);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,316 @@
|
|||||||
|
//! RFC 010 c7b — the connector: the dial loop that turns discovered
|
||||||
|
//! candidates into a full mesh.
|
||||||
|
//!
|
||||||
|
//! A plain select-loop actor (the c6 shape). It spawns its [`Strategy`] as a
|
||||||
|
//! child actor and receives [`Discovery`] events from it; it tracks which
|
||||||
|
//! peers are up by **subscribing to membership like any other consumer** —
|
||||||
|
//! no privileged channel into the manager, the same snapshot-then-stream
|
||||||
|
//! surface c8 will use. One `select` folds the command inbox, the discovery
|
||||||
|
//! stream, the membership stream, and the earliest retry deadline into a
|
||||||
|
//! single wait.
|
||||||
|
//!
|
||||||
|
//! Per-candidate state: dial on arrival; on failure retry with capped
|
||||||
|
//! exponential backoff ([`INITIAL_BACKOFF`] doubling to [`MAX_BACKOFF`]);
|
||||||
|
//! on the peer's `node_up` stop dialing and reset the backoff; on its
|
||||||
|
//! `node_down` resume immediately (a fresh sequence — the reconnect case is
|
||||||
|
//! the one backoff exists to pace, but the *first* retry after a death
|
||||||
|
//! should be prompt). A candidate bearing our own name is parked permanently
|
||||||
|
//! — that seed is us; so is one whose address answers as a different name
|
||||||
|
//! (`PeerNameMismatch`: a misconfigured or stale seed — each retry would
|
||||||
|
//! only blip the peer's membership). Every other failure retries: in
|
||||||
|
//! particular a `NameTaken` reject can be our own ghost at the peer, not
|
||||||
|
//! yet reaped by its liveness timer, so it must not park. Each attempt's
|
||||||
|
//! outcome is one `smarm-trace` `ClusterDial` event. A [`Discovery::Withdrawn`]
|
||||||
|
//! drops its `(name, addr)` from the dial set — only that: a live
|
||||||
|
//! connection is membership's, and a re-announce re-adds it fresh.
|
||||||
|
//!
|
||||||
|
//! Dials run **inline in the loop** — the same deliberate serialization as
|
||||||
|
//! the acceptor (c6b): each attempt is bounded by the connect + handshake
|
||||||
|
//! deadlines, and nothing concurrent exists to be starved. A wall of slow
|
||||||
|
//! unreachable seeds would stretch the loop's latency; revisit if a real
|
||||||
|
//! deployment ever hits that shape.
|
||||||
|
|
||||||
|
use std::collections::HashSet;
|
||||||
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
|
use crate::channel::{channel, select, select_timeout, Receiver, Selectable, Sender};
|
||||||
|
use crate::cluster::connect::{dial, DialError};
|
||||||
|
use crate::cluster::discovery::{Discovery, Strategy};
|
||||||
|
use crate::cluster::handshake::Local;
|
||||||
|
use crate::cluster::membership::{subscribe, NodeEvent};
|
||||||
|
use crate::cluster::transport::Transport;
|
||||||
|
use crate::cluster::Timing;
|
||||||
|
use crate::scheduler::spawn;
|
||||||
|
|
||||||
|
/// Default for [`Timing::initial_backoff`]: first retry delay after a failed
|
||||||
|
/// dial attempt.
|
||||||
|
pub const INITIAL_BACKOFF: Duration = Duration::from_millis(250);
|
||||||
|
/// Default for [`Timing::max_backoff`]: an unreachable seed is retried this
|
||||||
|
/// often, forever.
|
||||||
|
pub const MAX_BACKOFF: Duration = Duration::from_secs(5);
|
||||||
|
|
||||||
|
enum Cmd {
|
||||||
|
Shutdown,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A running connector. `shutdown` (or dropping the last handle) stops the
|
||||||
|
/// dial loop and its strategy only — established connections belong to the
|
||||||
|
/// manager, exactly as with the acceptor.
|
||||||
|
pub struct ConnectorHandle {
|
||||||
|
cmd_tx: Sender<Cmd>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ConnectorHandle {
|
||||||
|
/// Ask the connector to stop. Idempotent; a no-op if it already has.
|
||||||
|
pub fn shutdown(&self) {
|
||||||
|
let _ = self.cmd_tx.send(Cmd::Shutdown);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// One discovered `(name, addr)` and our dial intent towards it.
|
||||||
|
struct Candidate {
|
||||||
|
name: String,
|
||||||
|
addr: String,
|
||||||
|
state: State,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The connector's *intent* for a candidate. Whether the peer is currently
|
||||||
|
/// up is a separate, name-keyed membership fact (`up` in [`run`]): a
|
||||||
|
/// candidate can arrive after its peer's `node_up` (the snapshot lands
|
||||||
|
/// before the strategy has said anything), so "up" cannot live on the
|
||||||
|
/// candidate alone — it is a filter over dialing, not a candidate state.
|
||||||
|
enum State {
|
||||||
|
/// Never dialed: this seed is the local node itself, or the address
|
||||||
|
/// answered as a *different* name than the one seeded
|
||||||
|
/// (`DialError::PeerNameMismatch` — a misconfigured or stale seed;
|
||||||
|
/// redialing would only blip the peer's membership forever). The way
|
||||||
|
/// back is the strategy's: `Withdrawn` then a fresh `Candidate`.
|
||||||
|
Parked,
|
||||||
|
/// Dial when due; on failure, back off.
|
||||||
|
Dialing {
|
||||||
|
/// Delay to apply after the *next* failure.
|
||||||
|
backoff: Duration,
|
||||||
|
next_attempt: Instant,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
impl State {
|
||||||
|
fn fresh(timing: &Timing) -> Self {
|
||||||
|
State::Dialing {
|
||||||
|
backoff: timing.initial_backoff,
|
||||||
|
next_attempt: Instant::now(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Candidate {
|
||||||
|
/// The retry deadline, if this candidate is dialing at all.
|
||||||
|
fn due(&self) -> Option<Instant> {
|
||||||
|
match self.state {
|
||||||
|
State::Parked => None,
|
||||||
|
State::Dialing { next_attempt, .. } => Some(next_attempt),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// A dial attempt was made: schedule the retry, grow the backoff.
|
||||||
|
fn attempted(&mut self, timing: &Timing) {
|
||||||
|
if let State::Dialing {
|
||||||
|
backoff,
|
||||||
|
next_attempt,
|
||||||
|
} = &mut self.state
|
||||||
|
{
|
||||||
|
*next_attempt = Instant::now() + *backoff;
|
||||||
|
*backoff = (*backoff * 2).min(timing.max_backoff);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// The peer came up: the next sequence (after a later `node_down`)
|
||||||
|
/// starts from the initial delay again.
|
||||||
|
fn peer_up(&mut self, timing: &Timing) {
|
||||||
|
if let State::Dialing { backoff, .. } = &mut self.state {
|
||||||
|
*backoff = timing.initial_backoff;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// The peer went down: redial promptly, fresh sequence.
|
||||||
|
fn peer_down(&mut self, timing: &Timing) {
|
||||||
|
if matches!(self.state, State::Dialing { .. }) {
|
||||||
|
self.state = State::fresh(timing);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Spawn the connector actor. The strategy is spawned as its child; the
|
||||||
|
/// membership subscription is taken inside the actor. Must be called from
|
||||||
|
/// inside an actor (the same requirement as `dial`).
|
||||||
|
pub fn spawn_connector(
|
||||||
|
transport: Box<dyn Transport>,
|
||||||
|
local: Local,
|
||||||
|
strategy: Box<dyn Strategy>,
|
||||||
|
timing: Timing,
|
||||||
|
) -> ConnectorHandle {
|
||||||
|
let (cmd_tx, cmd_rx) = channel();
|
||||||
|
spawn(move || run(transport, local, strategy, timing, cmd_rx));
|
||||||
|
ConnectorHandle { cmd_tx }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(
|
||||||
|
transport: Box<dyn Transport>,
|
||||||
|
local: Local,
|
||||||
|
strategy: Box<dyn Strategy>,
|
||||||
|
timing: Timing,
|
||||||
|
cmd_rx: Receiver<Cmd>,
|
||||||
|
) {
|
||||||
|
// Membership is the connector's source of truth for "who is up" — the
|
||||||
|
// snapshot seeds `up` before any candidate arrives.
|
||||||
|
let Some(events) = subscribe() else {
|
||||||
|
return; // no manager, no cluster to connect
|
||||||
|
};
|
||||||
|
let (disc_tx, disc_rx) = channel();
|
||||||
|
spawn(move || strategy.run(disc_tx));
|
||||||
|
|
||||||
|
let mut cands: Vec<Candidate> = Vec::new();
|
||||||
|
let mut up: HashSet<String> = HashSet::new();
|
||||||
|
let mut strategy_done = false;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
// Drain every input, then act. Order does not matter: acting is
|
||||||
|
// idempotent against the resulting state.
|
||||||
|
match drain_cmd(&cmd_rx) {
|
||||||
|
Drained::Stop => return,
|
||||||
|
Drained::Open => {}
|
||||||
|
}
|
||||||
|
if !strategy_done {
|
||||||
|
strategy_done = drain_discoveries(&disc_rx, &local, &timing, &mut cands);
|
||||||
|
}
|
||||||
|
match drain_events(&events.rx, &timing, &mut up, &mut cands) {
|
||||||
|
Drained::Stop => return, // manager gone: the cluster is tearing down
|
||||||
|
Drained::Open => {}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dial everything due, inline (see the module docs on serialization).
|
||||||
|
let now = Instant::now();
|
||||||
|
for c in cands
|
||||||
|
.iter_mut()
|
||||||
|
.filter(|c| !up.contains(&c.name) && c.due().is_some_and(|d| d <= now))
|
||||||
|
{
|
||||||
|
// On success the manager's node_up is on its way and lands in
|
||||||
|
// `up` (backing off meanwhile keeps a racing re-attempt from
|
||||||
|
// spinning); every failure retries — see the module docs —
|
||||||
|
// except a peer-name mismatch, which parks the candidate.
|
||||||
|
let outcome = dial(&*transport, &c.addr, &c.name, &local, timing);
|
||||||
|
note_dial(&outcome);
|
||||||
|
match outcome {
|
||||||
|
Err(DialError::PeerNameMismatch { .. }) => c.state = State::Parked,
|
||||||
|
_ => c.attempted(&timing),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait: until the earliest retry deadline among actionable
|
||||||
|
// candidates, or indefinitely if none is pending.
|
||||||
|
let deadline = cands
|
||||||
|
.iter()
|
||||||
|
.filter(|c| !up.contains(&c.name))
|
||||||
|
.filter_map(Candidate::due)
|
||||||
|
.min();
|
||||||
|
let mut arms: Vec<&dyn Selectable> = vec![&cmd_rx, &events.rx];
|
||||||
|
if !strategy_done {
|
||||||
|
arms.push(&disc_rx);
|
||||||
|
}
|
||||||
|
match deadline {
|
||||||
|
Some(d) => {
|
||||||
|
let wait = d.saturating_duration_since(Instant::now());
|
||||||
|
let _ = select_timeout(&arms, wait);
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
let _ = select(&arms);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum Drained {
|
||||||
|
Open,
|
||||||
|
Stop,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn drain_cmd(rx: &Receiver<Cmd>) -> Drained {
|
||||||
|
match rx.try_recv() {
|
||||||
|
Ok(Some(Cmd::Shutdown)) => Drained::Stop,
|
||||||
|
Ok(None) => Drained::Open,
|
||||||
|
Err(_) => Drained::Stop, // all handles dropped
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Pull every pending discovery into the candidate set (deduplicated by
|
||||||
|
/// `(name, addr)`; a candidate bearing the local name is parked; a
|
||||||
|
/// `Withdrawn` removes its pair from the dial set and nothing else — see
|
||||||
|
/// [`Discovery::Withdrawn`]). Returns `true` once the strategy's channel
|
||||||
|
/// closes — it has said all it will.
|
||||||
|
fn drain_discoveries(
|
||||||
|
rx: &Receiver<Discovery>,
|
||||||
|
local: &Local,
|
||||||
|
timing: &Timing,
|
||||||
|
cands: &mut Vec<Candidate>,
|
||||||
|
) -> bool {
|
||||||
|
loop {
|
||||||
|
match rx.try_recv() {
|
||||||
|
Ok(Some(Discovery::Withdrawn { name, addr })) => {
|
||||||
|
cands.retain(|c| !(c.name == name && c.addr == addr));
|
||||||
|
}
|
||||||
|
Ok(Some(Discovery::Candidate { name, addr })) => {
|
||||||
|
if cands.iter().any(|c| c.name == name && c.addr == addr) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let state = if name == local.node_name {
|
||||||
|
State::Parked
|
||||||
|
} else {
|
||||||
|
State::fresh(timing)
|
||||||
|
};
|
||||||
|
cands.push(Candidate { name, addr, state });
|
||||||
|
}
|
||||||
|
Ok(None) => return false,
|
||||||
|
Err(_) => return true, // strategy done; its candidates live on here
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Fold pending membership events into `up`; each is also a transition on
|
||||||
|
/// that peer's candidates (see [`Candidate::peer_up`] / [`peer_down`]).
|
||||||
|
///
|
||||||
|
/// [`peer_down`]: Candidate::peer_down
|
||||||
|
fn drain_events(
|
||||||
|
rx: &Receiver<NodeEvent>,
|
||||||
|
timing: &Timing,
|
||||||
|
up: &mut HashSet<String>,
|
||||||
|
cands: &mut [Candidate],
|
||||||
|
) -> Drained {
|
||||||
|
loop {
|
||||||
|
match rx.try_recv() {
|
||||||
|
Ok(Some(NodeEvent::NodeUp(info))) => {
|
||||||
|
cands
|
||||||
|
.iter_mut()
|
||||||
|
.filter(|c| c.name == info.name)
|
||||||
|
.for_each(|c| c.peer_up(timing));
|
||||||
|
up.insert(info.name);
|
||||||
|
}
|
||||||
|
Ok(Some(NodeEvent::NodeDown(info))) => {
|
||||||
|
up.remove(&info.name);
|
||||||
|
cands
|
||||||
|
.iter_mut()
|
||||||
|
.filter(|c| c.name == info.name)
|
||||||
|
.for_each(|c| c.peer_down(timing));
|
||||||
|
}
|
||||||
|
Ok(None) => return Drained::Open,
|
||||||
|
Err(_) => return Drained::Stop,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Surface a dial outcome: one `smarm-trace` event, nothing else. The
|
||||||
|
/// connector's bookkeeping is decided by the caller.
|
||||||
|
fn note_dial(outcome: &Result<crate::pid::Pid, DialError>) {
|
||||||
|
#[cfg(feature = "smarm-trace")]
|
||||||
|
crate::te!(crate::trace::Event::ClusterDial(
|
||||||
|
outcome.as_ref().map_or_else(DialError::label, |_| "ok")
|
||||||
|
));
|
||||||
|
#[cfg(not(feature = "smarm-trace"))]
|
||||||
|
let _ = outcome;
|
||||||
|
}
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
//! RFC 010 c7b — peer discovery: the [`Strategy`] seam and the static-seeds
|
||||||
|
//! implementation.
|
||||||
|
//!
|
||||||
|
//! A strategy is **push-based and runs as its own actor**: the
|
||||||
|
//! [`connector`](crate::cluster::connector) spawns it with the sending end of
|
||||||
|
//! a channel, and the strategy emits [`Discovery`] events whenever it learns
|
||||||
|
//! something — once at startup for a static list, continuously for a future
|
||||||
|
//! mDNS/DNS strategy — for as long as it cares to run. Returning ends the
|
||||||
|
//! strategy actor; the candidates it pushed live on in the connector (the
|
||||||
|
//! connector owns all retry/backoff state, so a strategy never re-announces).
|
||||||
|
//!
|
||||||
|
//! A candidate is a **`(node_name, addr)` pair**, not a bare address: the
|
||||||
|
//! dial path and the D7 tie-break are keyed by peer *name* (the dial intent
|
||||||
|
//! must be registered before connecting so a crossing inbound `Hello` sees
|
||||||
|
//! it), so an anonymous dial would reintroduce exactly the
|
||||||
|
//! simultaneous-connect flap D7 exists to prevent. Discovery mechanisms know
|
||||||
|
//! names — that is what they discover.
|
||||||
|
|
||||||
|
use crate::channel::Sender;
|
||||||
|
|
||||||
|
/// A discovery event, as pushed by a [`Strategy`].
|
||||||
|
///
|
||||||
|
/// `Candidate` announces, `Withdrawn` retracts — the primitive pair. A
|
||||||
|
/// strategy that wants TTL semantics builds them on top (track its own
|
||||||
|
/// last-seen times, emit `Withdrawn` on expiry); the connector deliberately
|
||||||
|
/// has no clock of its own for candidates (D11: strategies never
|
||||||
|
/// re-announce, the connector owns retry). `#[non_exhaustive]` so more can
|
||||||
|
/// land without breaking strategies.
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
#[non_exhaustive]
|
||||||
|
pub enum Discovery {
|
||||||
|
/// A peer worth dialing: its claimed node name and a dialable address.
|
||||||
|
Candidate { name: String, addr: String },
|
||||||
|
/// Stop dialing this `(name, addr)`. Dial-set only: a connection that
|
||||||
|
/// is already up is membership's business and is left alone; an
|
||||||
|
/// attempt in flight completes on its own; a later `Candidate` for the
|
||||||
|
/// same pair re-adds it with fresh backoff. Unknown pairs are ignored.
|
||||||
|
Withdrawn { name: String, addr: String },
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A source of peers to dial. Implementations are spawned as actors by the
|
||||||
|
/// connector — see the module docs for the contract.
|
||||||
|
pub trait Strategy: Send + 'static {
|
||||||
|
/// Run the strategy: push [`Discovery`] events into `out` as they are
|
||||||
|
/// learned; return when done discovering (or when `out` reports closed —
|
||||||
|
/// the connector is gone). Runs inside an actor, so blocking
|
||||||
|
/// cooperatively is fine.
|
||||||
|
fn run(self: Box<Self>, out: Sender<Discovery>);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The static-seeds strategy: a fixed `(name, addr)` list, announced once.
|
||||||
|
#[derive(Debug, Clone, Default)]
|
||||||
|
pub struct StaticSeeds {
|
||||||
|
seeds: Vec<(String, String)>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl StaticSeeds {
|
||||||
|
pub fn new(seeds: impl IntoIterator<Item = (impl Into<String>, impl Into<String>)>) -> Self {
|
||||||
|
StaticSeeds {
|
||||||
|
seeds: seeds
|
||||||
|
.into_iter()
|
||||||
|
.map(|(n, a)| (n.into(), a.into()))
|
||||||
|
.collect(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Strategy for StaticSeeds {
|
||||||
|
fn run(self: Box<Self>, out: Sender<Discovery>) {
|
||||||
|
for (name, addr) in self.seeds {
|
||||||
|
if out.send(Discovery::Candidate { name, addr }).is_err() {
|
||||||
|
return; // connector gone; nobody to discover for
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,549 @@
|
|||||||
|
//! RFC 010 c2 — the owned wire envelope.
|
||||||
|
//!
|
||||||
|
//! Every control-plane frame is `u32` little-endian length prefix (of tag +
|
||||||
|
//! body), `u8` tag, hand-encoded body. postcard appears in exactly one place:
|
||||||
|
//! the payload blob inside `Send`/`SendNamed`, via [`encode_payload`] /
|
||||||
|
//! [`decode_payload`] — the seam where a codec swap would land (RFC 010 §2).
|
||||||
|
//! Everything else is hand-rolled and wholly owned.
|
||||||
|
//!
|
||||||
|
//! Integers are little-endian. Strings are `u16` length + UTF-8 bytes.
|
||||||
|
//! Payload blobs are `u32` length + bytes. Enum-shaped fields
|
||||||
|
//! ([`RejectReason`], [`DownReason`]) are a single tag byte.
|
||||||
|
|
||||||
|
use crate::monitor::DownReason;
|
||||||
|
use crate::pg::Incarnation;
|
||||||
|
|
||||||
|
/// Wire protocol version, checked in the handshake (c5).
|
||||||
|
pub const PROTO_VERSION: u32 = 1;
|
||||||
|
|
||||||
|
/// Hard cap on the length prefix. The control plane never carries bulk data
|
||||||
|
/// (RFC 010 §5 — that is the jarred rkyv plane), so anything larger is
|
||||||
|
/// corruption or an attack, not a legitimate frame.
|
||||||
|
pub const MAX_FRAME_LEN: usize = 16 * 1024 * 1024;
|
||||||
|
|
||||||
|
/// Per-node metadata exchanged in the handshake (RFC 010 §1: not identity).
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
pub struct NodeMeta {
|
||||||
|
pub role: String,
|
||||||
|
pub region: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Why a `Hello` was rejected.
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
pub enum RejectReason {
|
||||||
|
/// Build hashes differ — not the same binary.
|
||||||
|
HashMismatch,
|
||||||
|
/// The offered node name is already claimed by a live peer.
|
||||||
|
NameTaken,
|
||||||
|
/// Wire protocol version mismatch.
|
||||||
|
ProtoVersion,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The control-plane frame inventory (RFC 010, *Implementation details*).
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
pub enum Frame {
|
||||||
|
Hello {
|
||||||
|
proto_version: u32,
|
||||||
|
build_hash: u64,
|
||||||
|
node_name: String,
|
||||||
|
incarnation: Incarnation,
|
||||||
|
meta: NodeMeta,
|
||||||
|
},
|
||||||
|
HelloAck {
|
||||||
|
node_name: String,
|
||||||
|
incarnation: Incarnation,
|
||||||
|
meta: NodeMeta,
|
||||||
|
},
|
||||||
|
HelloReject {
|
||||||
|
reason: RejectReason,
|
||||||
|
},
|
||||||
|
Heartbeat,
|
||||||
|
Send {
|
||||||
|
/// Target slot index (node is implicit in the connection, incarnation
|
||||||
|
/// is bound at handshake — RFC 010 §3).
|
||||||
|
index: u32,
|
||||||
|
generation: u32,
|
||||||
|
type_hash: u64,
|
||||||
|
payload: Vec<u8>,
|
||||||
|
},
|
||||||
|
SendNamed {
|
||||||
|
name: String,
|
||||||
|
type_hash: u64,
|
||||||
|
payload: Vec<u8>,
|
||||||
|
},
|
||||||
|
Monitor {
|
||||||
|
monitor_id: u64,
|
||||||
|
index: u32,
|
||||||
|
generation: u32,
|
||||||
|
},
|
||||||
|
Demonitor {
|
||||||
|
monitor_id: u64,
|
||||||
|
},
|
||||||
|
Down {
|
||||||
|
monitor_id: u64,
|
||||||
|
reason: RemoteDownReason,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Why a remotely-monitored actor is reported down: either the target's own
|
||||||
|
/// terminal [`DownReason`] as its node recorded it, or the *link* to that
|
||||||
|
/// node was lost (or absent) — which says nothing about the actor itself.
|
||||||
|
///
|
||||||
|
/// This is the cluster-side widening of `DownReason` (p5): `Disconnected`
|
||||||
|
/// is a fact about a connection, never about a local actor, so it lives
|
||||||
|
/// here rather than in the core enum — a local `Down` can never carry it,
|
||||||
|
/// and matches on `DownReason` stay exhaustive over actor outcomes only.
|
||||||
|
/// On the wire `Local(r)` uses `r`'s tag and `Disconnected` is tag 5,
|
||||||
|
/// bound since c11; no peer emits it today (a lost link is synthesized
|
||||||
|
/// locally), but the codec honours it both ways.
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
pub enum RemoteDownReason {
|
||||||
|
/// The target itself terminated; the peer reported this reason.
|
||||||
|
Local(DownReason),
|
||||||
|
/// The link to the target's node was lost or was never up.
|
||||||
|
Disconnected,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RemoteDownReason {
|
||||||
|
/// The actor's own reason, if this was not a link loss.
|
||||||
|
pub fn local(self) -> Option<DownReason> {
|
||||||
|
match self {
|
||||||
|
RemoteDownReason::Local(r) => Some(r),
|
||||||
|
RemoteDownReason::Disconnected => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<DownReason> for RemoteDownReason {
|
||||||
|
fn from(r: DownReason) -> Self {
|
||||||
|
RemoteDownReason::Local(r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Frame tags. 0 is deliberately unassigned so an all-zero buffer never parses.
|
||||||
|
const TAG_HELLO: u8 = 1;
|
||||||
|
const TAG_HELLO_ACK: u8 = 2;
|
||||||
|
const TAG_HELLO_REJECT: u8 = 3;
|
||||||
|
const TAG_HEARTBEAT: u8 = 4;
|
||||||
|
const TAG_SEND: u8 = 5;
|
||||||
|
const TAG_SEND_NAMED: u8 = 6;
|
||||||
|
const TAG_MONITOR: u8 = 7;
|
||||||
|
const TAG_DEMONITOR: u8 = 8;
|
||||||
|
const TAG_DOWN: u8 = 9;
|
||||||
|
|
||||||
|
// RejectReason tags.
|
||||||
|
const REJ_HASH_MISMATCH: u8 = 1;
|
||||||
|
const REJ_NAME_TAKEN: u8 = 2;
|
||||||
|
const REJ_PROTO_VERSION: u8 = 3;
|
||||||
|
|
||||||
|
// DownReason tags. Do not reuse tags.
|
||||||
|
const DR_EXIT: u8 = 1;
|
||||||
|
const DR_PANIC: u8 = 2;
|
||||||
|
const DR_STOPPED: u8 = 3;
|
||||||
|
const DR_NOPROC: u8 = 4;
|
||||||
|
const DR_DISCONNECTED: u8 = 5;
|
||||||
|
// `Shutdown` never rides in a `Down` by contract (a target that honours the
|
||||||
|
// request exits normally) — the tag exists so the codec stays total.
|
||||||
|
const DR_SHUTDOWN: u8 = 6;
|
||||||
|
|
||||||
|
/// Frame could not be encoded. The output buffer is left exactly as it was.
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
pub enum EncodeError {
|
||||||
|
/// tag + body exceed [`MAX_FRAME_LEN`].
|
||||||
|
FrameTooLarge { len: usize },
|
||||||
|
/// A string field exceeds `u16::MAX` bytes.
|
||||||
|
StringTooLong { len: usize },
|
||||||
|
}
|
||||||
|
|
||||||
|
impl core::fmt::Display for EncodeError {
|
||||||
|
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||||
|
match self {
|
||||||
|
Self::FrameTooLarge { len } => {
|
||||||
|
write!(f, "frame body of {len} bytes exceeds MAX_FRAME_LEN")
|
||||||
|
}
|
||||||
|
Self::StringTooLong { len } => {
|
||||||
|
write!(f, "string field of {len} bytes exceeds u16::MAX")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for EncodeError {}
|
||||||
|
|
||||||
|
/// Frame could not be decoded. Everything here is *corruption* — "not enough
|
||||||
|
/// bytes yet" is the `Ok(None)` streaming case, never an error.
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
pub enum DecodeError {
|
||||||
|
/// The length prefix exceeds [`MAX_FRAME_LEN`].
|
||||||
|
FrameTooLarge { declared: usize },
|
||||||
|
/// The length prefix is zero — there is no tag byte.
|
||||||
|
EmptyFrame,
|
||||||
|
/// Unknown frame tag.
|
||||||
|
UnknownTag(u8),
|
||||||
|
/// Unknown tag for an enum-shaped field.
|
||||||
|
UnknownEnumTag { what: &'static str, tag: u8 },
|
||||||
|
/// A field ran past the declared frame end (the length prefix lied long,
|
||||||
|
/// or a length-carrying field inside the body lied).
|
||||||
|
Truncated,
|
||||||
|
/// Bytes were left over after the body (the length prefix lied short).
|
||||||
|
Trailing { extra: usize },
|
||||||
|
/// A string field was not valid UTF-8.
|
||||||
|
Utf8,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl core::fmt::Display for DecodeError {
|
||||||
|
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||||
|
match self {
|
||||||
|
Self::FrameTooLarge { declared } => {
|
||||||
|
write!(f, "declared frame length {declared} exceeds MAX_FRAME_LEN")
|
||||||
|
}
|
||||||
|
Self::EmptyFrame => write!(f, "zero-length frame (no tag byte)"),
|
||||||
|
Self::UnknownTag(t) => write!(f, "unknown frame tag {t}"),
|
||||||
|
Self::UnknownEnumTag { what, tag } => write!(f, "unknown {what} tag {tag}"),
|
||||||
|
Self::Truncated => write!(f, "frame body truncated mid-field"),
|
||||||
|
Self::Trailing { extra } => write!(f, "{extra} trailing bytes after frame body"),
|
||||||
|
Self::Utf8 => write!(f, "string field is not valid UTF-8"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for DecodeError {}
|
||||||
|
|
||||||
|
impl Frame {
|
||||||
|
/// Append this frame, length-prefixed, to `out`.
|
||||||
|
///
|
||||||
|
/// On error `out` is left untouched.
|
||||||
|
pub fn encode(&self, out: &mut Vec<u8>) -> Result<(), EncodeError> {
|
||||||
|
let start = out.len();
|
||||||
|
out.extend_from_slice(&[0u8; 4]); // length placeholder, patched below
|
||||||
|
let result = self.encode_body(out);
|
||||||
|
match result {
|
||||||
|
Ok(()) => {
|
||||||
|
let frame_len = out.len() - start - 4;
|
||||||
|
if frame_len > MAX_FRAME_LEN {
|
||||||
|
out.truncate(start);
|
||||||
|
return Err(EncodeError::FrameTooLarge { len: frame_len });
|
||||||
|
}
|
||||||
|
// Cast is lossless: MAX_FRAME_LEN < u32::MAX, checked above.
|
||||||
|
let len32 = frame_len as u32;
|
||||||
|
out[start..start + 4].copy_from_slice(&len32.to_le_bytes());
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
out.truncate(start);
|
||||||
|
Err(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn encode_body(&self, out: &mut Vec<u8>) -> Result<(), EncodeError> {
|
||||||
|
match self {
|
||||||
|
Frame::Hello {
|
||||||
|
proto_version,
|
||||||
|
build_hash,
|
||||||
|
node_name,
|
||||||
|
incarnation,
|
||||||
|
meta,
|
||||||
|
} => {
|
||||||
|
out.push(TAG_HELLO);
|
||||||
|
put_u32(out, *proto_version);
|
||||||
|
put_u64(out, *build_hash);
|
||||||
|
put_str(out, node_name)?;
|
||||||
|
put_u32(out, incarnation.get());
|
||||||
|
put_meta(out, meta)?;
|
||||||
|
}
|
||||||
|
Frame::HelloAck {
|
||||||
|
node_name,
|
||||||
|
incarnation,
|
||||||
|
meta,
|
||||||
|
} => {
|
||||||
|
out.push(TAG_HELLO_ACK);
|
||||||
|
put_str(out, node_name)?;
|
||||||
|
put_u32(out, incarnation.get());
|
||||||
|
put_meta(out, meta)?;
|
||||||
|
}
|
||||||
|
Frame::HelloReject { reason } => {
|
||||||
|
out.push(TAG_HELLO_REJECT);
|
||||||
|
out.push(match reason {
|
||||||
|
RejectReason::HashMismatch => REJ_HASH_MISMATCH,
|
||||||
|
RejectReason::NameTaken => REJ_NAME_TAKEN,
|
||||||
|
RejectReason::ProtoVersion => REJ_PROTO_VERSION,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
Frame::Heartbeat => out.push(TAG_HEARTBEAT),
|
||||||
|
Frame::Send {
|
||||||
|
index,
|
||||||
|
generation,
|
||||||
|
type_hash,
|
||||||
|
payload,
|
||||||
|
} => {
|
||||||
|
out.push(TAG_SEND);
|
||||||
|
put_u32(out, *index);
|
||||||
|
put_u32(out, *generation);
|
||||||
|
put_u64(out, *type_hash);
|
||||||
|
put_blob(out, payload)?;
|
||||||
|
}
|
||||||
|
Frame::SendNamed {
|
||||||
|
name,
|
||||||
|
type_hash,
|
||||||
|
payload,
|
||||||
|
} => {
|
||||||
|
out.push(TAG_SEND_NAMED);
|
||||||
|
put_str(out, name)?;
|
||||||
|
put_u64(out, *type_hash);
|
||||||
|
put_blob(out, payload)?;
|
||||||
|
}
|
||||||
|
Frame::Monitor {
|
||||||
|
monitor_id,
|
||||||
|
index,
|
||||||
|
generation,
|
||||||
|
} => {
|
||||||
|
out.push(TAG_MONITOR);
|
||||||
|
put_u64(out, *monitor_id);
|
||||||
|
put_u32(out, *index);
|
||||||
|
put_u32(out, *generation);
|
||||||
|
}
|
||||||
|
Frame::Demonitor { monitor_id } => {
|
||||||
|
out.push(TAG_DEMONITOR);
|
||||||
|
put_u64(out, *monitor_id);
|
||||||
|
}
|
||||||
|
Frame::Down { monitor_id, reason } => {
|
||||||
|
out.push(TAG_DOWN);
|
||||||
|
put_u64(out, *monitor_id);
|
||||||
|
out.push(match reason {
|
||||||
|
RemoteDownReason::Local(DownReason::Exit) => DR_EXIT,
|
||||||
|
RemoteDownReason::Local(DownReason::Panic) => DR_PANIC,
|
||||||
|
RemoteDownReason::Local(DownReason::Stopped) => DR_STOPPED,
|
||||||
|
RemoteDownReason::Local(DownReason::NoProc) => DR_NOPROC,
|
||||||
|
RemoteDownReason::Local(DownReason::Shutdown) => DR_SHUTDOWN,
|
||||||
|
RemoteDownReason::Disconnected => DR_DISCONNECTED,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Try to decode one frame from the start of `buf`.
|
||||||
|
///
|
||||||
|
/// `Ok(Some((frame, consumed)))` — a full frame; the caller advances by
|
||||||
|
/// `consumed`. `Ok(None)` — not enough bytes yet (streaming); read more
|
||||||
|
/// and retry. `Err(_)` — the bytes are corrupt; the connection is dead.
|
||||||
|
pub fn decode(buf: &[u8]) -> Result<Option<(Frame, usize)>, DecodeError> {
|
||||||
|
let Some(prefix) = buf.get(0..4) else {
|
||||||
|
return Ok(None);
|
||||||
|
};
|
||||||
|
let mut len4 = [0u8; 4];
|
||||||
|
len4.copy_from_slice(prefix);
|
||||||
|
let declared = u32::from_le_bytes(len4) as usize;
|
||||||
|
if declared > MAX_FRAME_LEN {
|
||||||
|
return Err(DecodeError::FrameTooLarge { declared });
|
||||||
|
}
|
||||||
|
if declared == 0 {
|
||||||
|
return Err(DecodeError::EmptyFrame);
|
||||||
|
}
|
||||||
|
let Some(body) = buf.get(4..4 + declared) else {
|
||||||
|
return Ok(None);
|
||||||
|
};
|
||||||
|
let mut r = Reader { buf: body, pos: 0 };
|
||||||
|
let frame = Self::decode_body(&mut r)?;
|
||||||
|
if r.pos != body.len() {
|
||||||
|
return Err(DecodeError::Trailing {
|
||||||
|
extra: body.len() - r.pos,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
Ok(Some((frame, 4 + declared)))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn decode_body(r: &mut Reader<'_>) -> Result<Frame, DecodeError> {
|
||||||
|
let tag = r.u8()?;
|
||||||
|
let frame = match tag {
|
||||||
|
TAG_HELLO => Frame::Hello {
|
||||||
|
proto_version: r.u32()?,
|
||||||
|
build_hash: r.u64()?,
|
||||||
|
node_name: r.string()?,
|
||||||
|
incarnation: Incarnation::new(r.u32()?),
|
||||||
|
meta: r.meta()?,
|
||||||
|
},
|
||||||
|
TAG_HELLO_ACK => Frame::HelloAck {
|
||||||
|
node_name: r.string()?,
|
||||||
|
incarnation: Incarnation::new(r.u32()?),
|
||||||
|
meta: r.meta()?,
|
||||||
|
},
|
||||||
|
TAG_HELLO_REJECT => Frame::HelloReject {
|
||||||
|
reason: match r.u8()? {
|
||||||
|
REJ_HASH_MISMATCH => RejectReason::HashMismatch,
|
||||||
|
REJ_NAME_TAKEN => RejectReason::NameTaken,
|
||||||
|
REJ_PROTO_VERSION => RejectReason::ProtoVersion,
|
||||||
|
t => {
|
||||||
|
return Err(DecodeError::UnknownEnumTag {
|
||||||
|
what: "RejectReason",
|
||||||
|
tag: t,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
TAG_HEARTBEAT => Frame::Heartbeat,
|
||||||
|
TAG_SEND => Frame::Send {
|
||||||
|
index: r.u32()?,
|
||||||
|
generation: r.u32()?,
|
||||||
|
type_hash: r.u64()?,
|
||||||
|
payload: r.blob()?,
|
||||||
|
},
|
||||||
|
TAG_SEND_NAMED => Frame::SendNamed {
|
||||||
|
name: r.string()?,
|
||||||
|
type_hash: r.u64()?,
|
||||||
|
payload: r.blob()?,
|
||||||
|
},
|
||||||
|
TAG_MONITOR => Frame::Monitor {
|
||||||
|
monitor_id: r.u64()?,
|
||||||
|
index: r.u32()?,
|
||||||
|
generation: r.u32()?,
|
||||||
|
},
|
||||||
|
TAG_DEMONITOR => Frame::Demonitor {
|
||||||
|
monitor_id: r.u64()?,
|
||||||
|
},
|
||||||
|
TAG_DOWN => Frame::Down {
|
||||||
|
monitor_id: r.u64()?,
|
||||||
|
reason: match r.u8()? {
|
||||||
|
DR_EXIT => RemoteDownReason::Local(DownReason::Exit),
|
||||||
|
DR_PANIC => RemoteDownReason::Local(DownReason::Panic),
|
||||||
|
DR_STOPPED => RemoteDownReason::Local(DownReason::Stopped),
|
||||||
|
DR_NOPROC => RemoteDownReason::Local(DownReason::NoProc),
|
||||||
|
DR_SHUTDOWN => RemoteDownReason::Local(DownReason::Shutdown),
|
||||||
|
DR_DISCONNECTED => RemoteDownReason::Disconnected,
|
||||||
|
t => {
|
||||||
|
return Err(DecodeError::UnknownEnumTag {
|
||||||
|
what: "RemoteDownReason",
|
||||||
|
tag: t,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
t => return Err(DecodeError::UnknownTag(t)),
|
||||||
|
};
|
||||||
|
Ok(frame)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Body writers
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
fn put_u32(out: &mut Vec<u8>, v: u32) {
|
||||||
|
out.extend_from_slice(&v.to_le_bytes());
|
||||||
|
}
|
||||||
|
|
||||||
|
fn put_u64(out: &mut Vec<u8>, v: u64) {
|
||||||
|
out.extend_from_slice(&v.to_le_bytes());
|
||||||
|
}
|
||||||
|
|
||||||
|
fn put_str(out: &mut Vec<u8>, s: &str) -> Result<(), EncodeError> {
|
||||||
|
let Ok(len) = u16::try_from(s.len()) else {
|
||||||
|
return Err(EncodeError::StringTooLong { len: s.len() });
|
||||||
|
};
|
||||||
|
out.extend_from_slice(&len.to_le_bytes());
|
||||||
|
out.extend_from_slice(s.as_bytes());
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn put_blob(out: &mut Vec<u8>, b: &[u8]) -> Result<(), EncodeError> {
|
||||||
|
let Ok(len) = u32::try_from(b.len()) else {
|
||||||
|
return Err(EncodeError::FrameTooLarge { len: b.len() });
|
||||||
|
};
|
||||||
|
out.extend_from_slice(&len.to_le_bytes());
|
||||||
|
out.extend_from_slice(b);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn put_meta(out: &mut Vec<u8>, m: &NodeMeta) -> Result<(), EncodeError> {
|
||||||
|
put_str(out, &m.role)?;
|
||||||
|
put_str(out, &m.region)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Body reader
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
struct Reader<'a> {
|
||||||
|
buf: &'a [u8],
|
||||||
|
pos: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Reader<'_> {
|
||||||
|
fn take(&mut self, n: usize) -> Result<&[u8], DecodeError> {
|
||||||
|
let end = self.pos.checked_add(n).ok_or(DecodeError::Truncated)?;
|
||||||
|
let s = self.buf.get(self.pos..end).ok_or(DecodeError::Truncated)?;
|
||||||
|
self.pos = end;
|
||||||
|
Ok(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn u8(&mut self) -> Result<u8, DecodeError> {
|
||||||
|
Ok(self.take(1)?[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
fn u16(&mut self) -> Result<u16, DecodeError> {
|
||||||
|
let mut b = [0u8; 2];
|
||||||
|
b.copy_from_slice(self.take(2)?);
|
||||||
|
Ok(u16::from_le_bytes(b))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn u32(&mut self) -> Result<u32, DecodeError> {
|
||||||
|
let mut b = [0u8; 4];
|
||||||
|
b.copy_from_slice(self.take(4)?);
|
||||||
|
Ok(u32::from_le_bytes(b))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn u64(&mut self) -> Result<u64, DecodeError> {
|
||||||
|
let mut b = [0u8; 8];
|
||||||
|
b.copy_from_slice(self.take(8)?);
|
||||||
|
Ok(u64::from_le_bytes(b))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn string(&mut self) -> Result<String, DecodeError> {
|
||||||
|
let len = self.u16()? as usize;
|
||||||
|
let bytes = self.take(len)?;
|
||||||
|
match core::str::from_utf8(bytes) {
|
||||||
|
Ok(s) => Ok(s.to_owned()),
|
||||||
|
Err(_) => Err(DecodeError::Utf8),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn blob(&mut self) -> Result<Vec<u8>, DecodeError> {
|
||||||
|
let len = self.u32()? as usize;
|
||||||
|
Ok(self.take(len)?.to_vec())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn meta(&mut self) -> Result<NodeMeta, DecodeError> {
|
||||||
|
Ok(NodeMeta {
|
||||||
|
role: self.string()?,
|
||||||
|
region: self.string()?,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// The postcard seam (RFC 010 §2) — the ONLY place payload bytes are produced
|
||||||
|
// or consumed. A codec swap lands here and nowhere else.
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
/// Payload (de)serialization failed at the codec seam.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct PayloadError(String);
|
||||||
|
|
||||||
|
impl core::fmt::Display for PayloadError {
|
||||||
|
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||||
|
write!(f, "payload codec: {}", self.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for PayloadError {}
|
||||||
|
|
||||||
|
/// Serialize a payload value to the wire blob.
|
||||||
|
pub fn encode_payload<T: serde::Serialize + ?Sized>(value: &T) -> Result<Vec<u8>, PayloadError> {
|
||||||
|
postcard::to_allocvec(value).map_err(|e| PayloadError(e.to_string()))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Deserialize a payload value from the wire blob.
|
||||||
|
pub fn decode_payload<T: serde::de::DeserializeOwned>(bytes: &[u8]) -> Result<T, PayloadError> {
|
||||||
|
postcard::from_bytes(bytes).map_err(|e| PayloadError(e.to_string()))
|
||||||
|
}
|
||||||
@@ -0,0 +1,238 @@
|
|||||||
|
//! RFC 010 c8 — explicit exposure: the node's remote surface, and the
|
||||||
|
//! fixed-seed type hash.
|
||||||
|
//!
|
||||||
|
//! Nothing local is remotely reachable by default (RFC §4 — "a gun needs a
|
||||||
|
//! safety"). [`expose`] marks a registered name remotely addressable and
|
||||||
|
//! registers `M`'s decoder under [`type_hash::<M>()`](type_hash);
|
||||||
|
//! [`expose_type`] registers only the decoder (the reply-to path: a
|
||||||
|
//! `RemotePid<A>` received in a message is sendable only if `A::Msg`'s
|
||||||
|
//! decoder was explicitly registered). The exposed set is the node's
|
||||||
|
//! visible, auditable remote surface ([`exposed_names`]).
|
||||||
|
//!
|
||||||
|
//! ## Where the state lives
|
||||||
|
//!
|
||||||
|
//! On `RuntimeInner`, the [`pg`](crate::pg) pattern: a leaf-locked table,
|
||||||
|
//! cfg-gated behind the `cluster` feature (zero-cost-when-off, per c1).
|
||||||
|
//! Chosen over manager-held state because c9's inbound decode consults it
|
||||||
|
//! per frame — a hot path that must not serialize every remote delivery
|
||||||
|
//! through one gen_server. The state resets with the runtime, like every
|
||||||
|
//! registry.
|
||||||
|
//!
|
||||||
|
//! ## The watchable fold (D3), against the code as it stands
|
||||||
|
//!
|
||||||
|
//! RFC §4: the exposed set is not a new registry — it folds into the
|
||||||
|
//! existing `watchable` machinery, one set, two set-sites (a pid crossing
|
||||||
|
//! the membrane, and expose). Reading the code: `register` **already
|
||||||
|
//! stamps every named holder watchable** ("no successfully-registered actor
|
||||||
|
//! can die unflagged", registry.rs), so an exposed *name*'s holder needs no
|
||||||
|
//! extra mark here — the guarantee holds by registration, and re-registration
|
||||||
|
//! after a holder's death re-stamps the new holder for free (a per-tenancy
|
||||||
|
//! mark taken at expose time could not do that). The cluster's own
|
||||||
|
//! `mark_watchable` set-site is therefore the **pid crossing the wire** —
|
||||||
|
//! serialization of a pid into a frame, c10 — the exact analog of the
|
||||||
|
//! membrane crossing. What lives here is only the name/type-level state
|
||||||
|
//! neither the registry nor the slot bits can carry: which names are
|
||||||
|
//! exposed, and how to decode each type hash.
|
||||||
|
//!
|
||||||
|
//! ## The hash
|
||||||
|
//!
|
||||||
|
//! [`type_hash`] is FNV-1a 64 (fixed seed: the FNV offset basis) over
|
||||||
|
//! `TypeId`, so it is a constant of the binary: stable across runs of the
|
||||||
|
//! same build — exactly the scope the build-hash handshake reduces the mesh
|
||||||
|
//! to — and deliberately *not* stable across builds (scope guard: no
|
||||||
|
//! cross-version wire compatibility). A collision between two exposed types
|
||||||
|
//! degrades to a decode error or a refused channel, never a misroute — the
|
||||||
|
//! local `SendError::NoChannel` guarantee survives the network (RFC §3).
|
||||||
|
//!
|
||||||
|
//! ## The decoder contract
|
||||||
|
//!
|
||||||
|
//! A decoder is **decode-and-deliver-to-pid**: it captures `M` (the one
|
||||||
|
//! typed site), decodes the payload, and hands the value to the target's
|
||||||
|
//! published channel via the registry's own dynamic send. Wire-name →
|
||||||
|
//! local-pid resolution deliberately stays *outside* — that is c9's single
|
||||||
|
//! resolution seam, and it calls [`decode_deliver`].
|
||||||
|
|
||||||
|
use std::any::TypeId;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::hash::{Hash, Hasher};
|
||||||
|
|
||||||
|
use crate::cluster::envelope::{decode_payload, PayloadError};
|
||||||
|
use crate::pid::{Name, Pid};
|
||||||
|
use crate::registry::{send_dyn, SendError};
|
||||||
|
use crate::scheduler::with_runtime;
|
||||||
|
|
||||||
|
/// The fixed-seed `TypeId` → `u64` hash: FNV-1a 64 over the `TypeId`'s hash
|
||||||
|
/// bytes, seeded with the FNV offset basis. A constant of the binary — see
|
||||||
|
/// the module docs for scope.
|
||||||
|
pub fn type_hash<M: 'static>() -> u64 {
|
||||||
|
let mut h = Fnv1a64::new();
|
||||||
|
TypeId::of::<M>().hash(&mut h);
|
||||||
|
h.finish()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// FNV-1a 64 as a `Hasher`, so `TypeId` (opaque, `Hash`-only) can feed it.
|
||||||
|
/// Same constants as the const fns in [`crate::cluster`] (BUILD_HASH).
|
||||||
|
struct Fnv1a64(u64);
|
||||||
|
|
||||||
|
impl Fnv1a64 {
|
||||||
|
fn new() -> Self {
|
||||||
|
Fnv1a64(0xcbf2_9ce4_8422_2325)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Hasher for Fnv1a64 {
|
||||||
|
fn write(&mut self, bytes: &[u8]) {
|
||||||
|
for &b in bytes {
|
||||||
|
self.0 ^= b as u64;
|
||||||
|
self.0 = self.0.wrapping_mul(0x0000_0100_0000_01b3);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn finish(&self) -> u64 {
|
||||||
|
self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Why a [`decode_deliver`] did not deliver. Payload-free mirror of the
|
||||||
|
/// registry's `SendError` where relevant — the caller (c9's inbound path)
|
||||||
|
/// has only bytes to give back, not a typed message.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum DeliverError {
|
||||||
|
/// No decoder is registered under this hash — the type was never
|
||||||
|
/// exposed here.
|
||||||
|
UnknownType,
|
||||||
|
/// The bytes did not decode as the registered type.
|
||||||
|
Decode(PayloadError),
|
||||||
|
/// The target actor is dead (or was never alive).
|
||||||
|
Dead,
|
||||||
|
/// The target is live but has no channel for this message type, or that
|
||||||
|
/// channel is closed — the `NoChannel` guarantee: a decoded value is
|
||||||
|
/// refused, never misrouted.
|
||||||
|
WrongChannel,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A registered decoder: decode `bytes` as the captured type and deliver to
|
||||||
|
/// `pid`'s published channel. `Arc`, so [`decode_deliver`] can clone it out
|
||||||
|
/// from under the exposure lock and call it lock-free — the decoder's
|
||||||
|
/// `send_dyn` takes the registry lock, and the two are mutual Leaves that
|
||||||
|
/// must never nest.
|
||||||
|
type Decoder = std::sync::Arc<dyn Fn(Pid, &[u8]) -> Result<(), DeliverError> + Send + Sync>;
|
||||||
|
|
||||||
|
/// The exposure state, one per runtime (a `RuntimeInner` field, pg-style).
|
||||||
|
pub(crate) struct ExposureState {
|
||||||
|
/// The exposed names: registry key → the type hash it expects.
|
||||||
|
exposed: HashMap<&'static str, u64>,
|
||||||
|
/// The decoders: type hash → decode-and-deliver.
|
||||||
|
decoders: HashMap<u64, Decoder>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ExposureState {
|
||||||
|
pub(crate) fn new() -> Self {
|
||||||
|
ExposureState {
|
||||||
|
exposed: HashMap::new(),
|
||||||
|
decoders: HashMap::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Mark `name` remotely addressable and register `M`'s decoder under its
|
||||||
|
/// type hash (so both name-sends and pid-sends of `M` work — RFC §4).
|
||||||
|
/// Returns the hash.
|
||||||
|
///
|
||||||
|
/// Exposure is a **name-level fact**, independent of who currently holds the
|
||||||
|
/// name (names late-bind: the registry re-resolves on every send, and c9's
|
||||||
|
/// seam resolves per delivery). Exposing an unregistered name is therefore
|
||||||
|
/// valid — deliveries fail with "unresolved" until someone registers it.
|
||||||
|
/// Idempotent. Must run inside [`run`](crate::run).
|
||||||
|
pub fn expose<M>(name: Name<M>) -> u64
|
||||||
|
where
|
||||||
|
M: serde::de::DeserializeOwned + Send + 'static,
|
||||||
|
{
|
||||||
|
let h = ensure_decoder::<M>();
|
||||||
|
with_runtime(|inner| {
|
||||||
|
inner.exposure.lock().exposed.insert(name.as_str(), h);
|
||||||
|
});
|
||||||
|
h
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Register only `M`'s decoder (no name): the reply-to path. Returns the
|
||||||
|
/// hash. Idempotent. Must run inside [`run`](crate::run).
|
||||||
|
pub fn expose_type<M>() -> u64
|
||||||
|
where
|
||||||
|
M: serde::de::DeserializeOwned + Send + 'static,
|
||||||
|
{
|
||||||
|
ensure_decoder::<M>()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ensure_decoder<M>() -> u64
|
||||||
|
where
|
||||||
|
M: serde::de::DeserializeOwned + Send + 'static,
|
||||||
|
{
|
||||||
|
let h = type_hash::<M>();
|
||||||
|
with_runtime(|inner| {
|
||||||
|
inner
|
||||||
|
.exposure
|
||||||
|
.lock()
|
||||||
|
.decoders
|
||||||
|
.entry(h)
|
||||||
|
.or_insert_with(decoder::<M>);
|
||||||
|
});
|
||||||
|
h
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The one typed site: decode as `M`, deliver via the registry's dynamic
|
||||||
|
/// send. See the module docs for the error mapping.
|
||||||
|
fn decoder<M>() -> Decoder
|
||||||
|
where
|
||||||
|
M: serde::de::DeserializeOwned + Send + 'static,
|
||||||
|
{
|
||||||
|
std::sync::Arc::new(|pid, bytes| {
|
||||||
|
let m: M = decode_payload(bytes).map_err(DeliverError::Decode)?;
|
||||||
|
send_dyn(pid, m).map_err(|e| match e {
|
||||||
|
SendError::Dead(_) | SendError::Unresolved(_) | SendError::NoMember(_) => {
|
||||||
|
DeliverError::Dead
|
||||||
|
}
|
||||||
|
SendError::NoChannel(_) | SendError::Closed(_) => DeliverError::WrongChannel,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The type hash `name` was exposed with, or `None` if it is not exposed.
|
||||||
|
/// Must run inside [`run`](crate::run).
|
||||||
|
pub fn exposed_hash(name: &str) -> Option<u64> {
|
||||||
|
with_runtime(|inner| inner.exposure.lock().exposed.get(name).copied())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Whether a decoder is registered under `hash`. Must run inside
|
||||||
|
/// [`run`](crate::run).
|
||||||
|
pub fn decoder_registered(hash: u64) -> bool {
|
||||||
|
with_runtime(|inner| inner.exposure.lock().decoders.contains_key(&hash))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Decode `bytes` under `hash`'s registered decoder and deliver to `pid`.
|
||||||
|
/// This is the delivery half c9's single resolution seam calls after it has
|
||||||
|
/// resolved a wire name to a local pid. Must run inside [`run`](crate::run).
|
||||||
|
pub fn decode_deliver(hash: u64, to: Pid, bytes: &[u8]) -> Result<(), DeliverError> {
|
||||||
|
// Clone the Arc under the lock, call outside it: the decoder's
|
||||||
|
// `send_dyn` takes the registry lock — a mutual Leaf with the exposure
|
||||||
|
// lock (the runtime asserts if Leaves nest). This also keeps unrelated
|
||||||
|
// deliveries uncoupled from a slow decode.
|
||||||
|
let d = with_runtime(|inner| inner.exposure.lock().decoders.get(&hash).cloned());
|
||||||
|
match d {
|
||||||
|
Some(d) => d(to, bytes),
|
||||||
|
None => Err(DeliverError::UnknownType),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The auditable remote surface: every exposed name and its type hash,
|
||||||
|
/// unordered. Must run inside [`run`](crate::run).
|
||||||
|
pub fn exposed_names() -> Vec<(&'static str, u64)> {
|
||||||
|
with_runtime(|inner| {
|
||||||
|
inner
|
||||||
|
.exposure
|
||||||
|
.lock()
|
||||||
|
.exposed
|
||||||
|
.iter()
|
||||||
|
.map(|(&n, &h)| (n, h))
|
||||||
|
.collect()
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -0,0 +1,178 @@
|
|||||||
|
//! RFC 010 c5 — the handshake as a pure state machine.
|
||||||
|
//!
|
||||||
|
//! Frames in, actions out — no IO, no clocks, no actors. The c6 connection
|
||||||
|
//! actor drives these machines and executes their actions; everything
|
||||||
|
//! time-shaped (handshake deadline, heartbeats) lives there.
|
||||||
|
|
||||||
|
use crate::cluster::envelope::{Frame, NodeMeta, RejectReason, PROTO_VERSION};
|
||||||
|
use crate::pg::Incarnation;
|
||||||
|
|
||||||
|
/// This node's identity and metadata, as offered in (or checked against) a
|
||||||
|
/// `Hello`.
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct Local {
|
||||||
|
pub node_name: String,
|
||||||
|
pub incarnation: Incarnation,
|
||||||
|
pub build_hash: u64,
|
||||||
|
pub meta: NodeMeta,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The peer identity a successful handshake yields (what c7 feeds `node_up`).
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
pub struct Peer {
|
||||||
|
pub node_name: String,
|
||||||
|
pub incarnation: Incarnation,
|
||||||
|
pub meta: NodeMeta,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Driver-supplied standing of the *offered* name at this node — knowledge
|
||||||
|
/// the pure machine cannot have (c6 owns the connection table and dial
|
||||||
|
/// set). One answer, in the responder's own precedence: an established
|
||||||
|
/// peer under that name outranks an in-flight dial to it.
|
||||||
|
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
|
||||||
|
pub enum PeerStanding {
|
||||||
|
/// Neither connected to nor dialing that name.
|
||||||
|
#[default]
|
||||||
|
Free,
|
||||||
|
/// An established peer already holds that name.
|
||||||
|
Claimed,
|
||||||
|
/// We have our own dial in flight to that name.
|
||||||
|
Dialing,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Simultaneous-connect tie-break: does the connection dialed by
|
||||||
|
/// `dialer_name` survive against the reverse dial?
|
||||||
|
/// The rule (ratified 2026-08-14, a wire-protocol fact): the connection
|
||||||
|
/// dialed by the lexicographically **smaller** name survives. Both ends know
|
||||||
|
/// both names, so both compute the same verdict — which is why the losing
|
||||||
|
/// side may close silently instead of sending a reject.
|
||||||
|
pub fn dial_wins(dialer_name: &str, acceptor_name: &str) -> bool {
|
||||||
|
dialer_name < acceptor_name
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Dial side: emits `Hello` at construction, interprets the single response.
|
||||||
|
#[must_use]
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Initiator(());
|
||||||
|
|
||||||
|
/// What the dial side's response frame meant.
|
||||||
|
#[must_use]
|
||||||
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
|
pub enum InitiatorOutcome {
|
||||||
|
Established(Peer),
|
||||||
|
Rejected(RejectReason),
|
||||||
|
/// Protocol violation before the ack — close. Carries the offending frame.
|
||||||
|
Failed(Frame),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Initiator {
|
||||||
|
/// Start a dial-side handshake: the returned frame is the `Hello` to
|
||||||
|
/// send; the returned machine is the right to interpret the response.
|
||||||
|
pub fn new(local: &Local) -> (Self, Frame) {
|
||||||
|
let hello = Frame::Hello {
|
||||||
|
proto_version: PROTO_VERSION,
|
||||||
|
build_hash: local.build_hash,
|
||||||
|
node_name: local.node_name.clone(),
|
||||||
|
incarnation: local.incarnation,
|
||||||
|
meta: local.meta.clone(),
|
||||||
|
};
|
||||||
|
(Initiator(()), hello)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Interpret the response. The `HelloAck` carries no hash or version —
|
||||||
|
/// the responder already checked ours against its own, and equality is
|
||||||
|
/// symmetric, so a one-sided check is sound.
|
||||||
|
pub fn on_frame(self, frame: Frame) -> InitiatorOutcome {
|
||||||
|
match frame {
|
||||||
|
Frame::HelloAck {
|
||||||
|
node_name,
|
||||||
|
incarnation,
|
||||||
|
meta,
|
||||||
|
} => InitiatorOutcome::Established(Peer {
|
||||||
|
node_name,
|
||||||
|
incarnation,
|
||||||
|
meta,
|
||||||
|
}),
|
||||||
|
Frame::HelloReject { reason } => InitiatorOutcome::Rejected(reason),
|
||||||
|
other => InitiatorOutcome::Failed(other),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Accept side: awaits exactly one `Hello`, answers or closes.
|
||||||
|
#[must_use]
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Responder {
|
||||||
|
local: Local,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// What to do with an inbound connection's first frame.
|
||||||
|
#[must_use]
|
||||||
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
|
pub enum ResponderOutcome {
|
||||||
|
/// Send the ack; the connection is established.
|
||||||
|
Accepted { reply: Frame, peer: Peer },
|
||||||
|
/// Send the reject, then close.
|
||||||
|
Rejected { reply: Frame, reason: RejectReason },
|
||||||
|
/// Lost the simultaneous-connect tie-break: close silently, no frame.
|
||||||
|
TieBreakLoss,
|
||||||
|
/// Protocol violation before Hello — close, no reply. Carries the frame.
|
||||||
|
Failed(Frame),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Responder {
|
||||||
|
pub fn new(local: Local) -> Self {
|
||||||
|
Responder { local }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Judge the connection's first frame. Check order is proto → hash →
|
||||||
|
/// name → tie-break: validity before identity. `HelloReject` is the
|
||||||
|
/// cross-version compatibility anchor, so a version-mismatched peer
|
||||||
|
/// still gets one.
|
||||||
|
pub fn on_frame(self, frame: Frame, standing: PeerStanding) -> ResponderOutcome {
|
||||||
|
let Frame::Hello {
|
||||||
|
proto_version,
|
||||||
|
build_hash,
|
||||||
|
node_name,
|
||||||
|
incarnation,
|
||||||
|
meta,
|
||||||
|
} = frame
|
||||||
|
else {
|
||||||
|
return ResponderOutcome::Failed(frame);
|
||||||
|
};
|
||||||
|
|
||||||
|
let reject = |reason| ResponderOutcome::Rejected {
|
||||||
|
reply: Frame::HelloReject { reason },
|
||||||
|
reason,
|
||||||
|
};
|
||||||
|
|
||||||
|
if proto_version != PROTO_VERSION {
|
||||||
|
return reject(RejectReason::ProtoVersion);
|
||||||
|
}
|
||||||
|
if build_hash != self.local.build_hash {
|
||||||
|
return reject(RejectReason::HashMismatch);
|
||||||
|
}
|
||||||
|
if node_name == self.local.node_name || standing == PeerStanding::Claimed {
|
||||||
|
return reject(RejectReason::NameTaken);
|
||||||
|
}
|
||||||
|
// Simultaneous connect: the inbound frame is the peer's dial. If our
|
||||||
|
// own in-flight dial wins instead, drop this one silently — the peer
|
||||||
|
// computes the same verdict (see `dial_wins`).
|
||||||
|
if standing == PeerStanding::Dialing && !dial_wins(&node_name, &self.local.node_name) {
|
||||||
|
return ResponderOutcome::TieBreakLoss;
|
||||||
|
}
|
||||||
|
|
||||||
|
ResponderOutcome::Accepted {
|
||||||
|
reply: Frame::HelloAck {
|
||||||
|
node_name: self.local.node_name,
|
||||||
|
incarnation: self.local.incarnation,
|
||||||
|
meta: self.local.meta,
|
||||||
|
},
|
||||||
|
peer: Peer {
|
||||||
|
node_name,
|
||||||
|
incarnation,
|
||||||
|
meta,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,295 @@
|
|||||||
|
//! RFC 010 c6 — the cluster connection manager.
|
||||||
|
//!
|
||||||
|
//! One manager per runtime: the single registry of live peer connections and
|
||||||
|
//! the source of truth for whether a peer name is already claimed. The
|
||||||
|
//! accept/connect path registers each established connection here, handing
|
||||||
|
//! over its [`ConnHandle`] — **the manager owns connection lifetime**. A
|
||||||
|
//! connection lives as long as its table entry, so it neither outlives nor
|
||||||
|
//! dies with whichever actor happened to establish it. Registered actors are
|
||||||
|
//! also *monitored*, so the table self-heals on any exit path — a connection
|
||||||
|
//! that panics, is cancelled, or closes cleanly is removed without
|
||||||
|
//! cooperation from the dying actor.
|
||||||
|
//!
|
||||||
|
//! The manager also holds the **membership state** (c7a): `node_up` fires on
|
||||||
|
//! a successful registration and `node_down` on removal — they are derived
|
||||||
|
//! facts of the exact events this table already owns, so holding the view
|
||||||
|
//! here means no cross-actor race between "connection exists" and "node is
|
||||||
|
//! up". The consumer surface (event types, [`subscribe`], [`view`],
|
||||||
|
//! semantics) is [`membership`](crate::cluster::membership); no consumer
|
||||||
|
//! ever touches the table itself.
|
||||||
|
//!
|
||||||
|
//! The connector dial loop is c7b, built on top of both.
|
||||||
|
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use crate::channel::Sender;
|
||||||
|
use crate::cluster::conn::ConnHandle;
|
||||||
|
use crate::cluster::handshake::{Peer, PeerStanding};
|
||||||
|
use crate::cluster::membership::{NodeEvent, NodeInfo};
|
||||||
|
use crate::cluster::remote::{bind_outbound, unbind_outbound};
|
||||||
|
use crate::gen_server::{GenServer, GenServerCtx, GenServerName, Watcher};
|
||||||
|
use crate::monitor::{monitor, Down};
|
||||||
|
use crate::pg::NodeId;
|
||||||
|
use crate::pid::Pid;
|
||||||
|
|
||||||
|
/// Well-known name of the singleton manager within a runtime. Connection
|
||||||
|
/// actors reach it by name rather than by a passed-around ref, so a restarted
|
||||||
|
/// manager is always found at the same key.
|
||||||
|
pub const MANAGER: GenServerName<Manager> = GenServerName::new("smarm.cluster.manager");
|
||||||
|
|
||||||
|
/// One live connection's entry: the actor running it, the handle whose
|
||||||
|
/// lifetime *is* the connection's (see the module docs), and the peer's
|
||||||
|
/// membership identity (what `node_up` announced and `node_down` will name).
|
||||||
|
struct ConnEntry {
|
||||||
|
pid: Pid,
|
||||||
|
info: NodeInfo,
|
||||||
|
_handle: ConnHandle,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The connection registry: peer name → the connection actor that owns that
|
||||||
|
/// peer's control connection. Plus the membership state layered on it (c7a):
|
||||||
|
/// subscribers, and the `(name, incarnation)` → [`NodeId`] memo.
|
||||||
|
pub struct Manager {
|
||||||
|
conns: HashMap<String, ConnEntry>,
|
||||||
|
/// In-flight dial intents: peer name -> the actor performing the dial.
|
||||||
|
/// Registered *before* connecting so a crossing inbound `Hello` sees it
|
||||||
|
/// ([`PeerStanding::Dialing`]); cleared the moment
|
||||||
|
/// the dial resolves, and — because the dialer is monitored — on the
|
||||||
|
/// dialer's death, so a panicking dial can never wedge the tie-break.
|
||||||
|
dials: HashMap<String, Pid>,
|
||||||
|
/// Membership subscribers; a closed channel is pruned on the next emit.
|
||||||
|
subscribers: Vec<Sender<NodeEvent>>,
|
||||||
|
/// The [`NodeId`] memo: a reconnect at the same incarnation keeps its id,
|
||||||
|
/// a restart (new incarnation) allocates a fresh one. Grows one entry per
|
||||||
|
/// distinct `(name, incarnation)` ever seen — unbounded in principle,
|
||||||
|
/// bounded in practice by restarts actually happening.
|
||||||
|
ids: HashMap<(String, u32), NodeId>,
|
||||||
|
/// Next id to allocate. Starts at 1: id 0 is
|
||||||
|
/// [`DEFAULT_NODE_ID`](crate::pg::DEFAULT_NODE_ID), the local node.
|
||||||
|
next_id: u32,
|
||||||
|
watcher: Option<Watcher<Manager>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Manager {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Manager {
|
||||||
|
conns: HashMap::new(),
|
||||||
|
dials: HashMap::new(),
|
||||||
|
subscribers: Vec::new(),
|
||||||
|
ids: HashMap::new(),
|
||||||
|
next_id: 1,
|
||||||
|
watcher: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The memoized id for `(name, incarnation)` — see the field docs.
|
||||||
|
fn node_id(&mut self, name: &str, incarnation: u32) -> NodeId {
|
||||||
|
*self
|
||||||
|
.ids
|
||||||
|
.entry((name.to_string(), incarnation))
|
||||||
|
.or_insert_with(|| {
|
||||||
|
let id = NodeId::new(self.next_id);
|
||||||
|
self.next_id += 1;
|
||||||
|
id
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Deliver `event` to every live subscriber, pruning the dead: a closed
|
||||||
|
/// channel means the subscriber dropped its [`MembershipEvents`]
|
||||||
|
/// (crate::cluster::membership::MembershipEvents).
|
||||||
|
fn emit(&mut self, event: &NodeEvent) {
|
||||||
|
self.subscribers.retain(|tx| tx.send(event.clone()).is_ok());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Manager {
|
||||||
|
fn default() -> Self {
|
||||||
|
Manager::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Outcome of a [`Call::Register`].
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
pub enum Registered {
|
||||||
|
/// The name was free; this connection is now the peer of record.
|
||||||
|
Ok,
|
||||||
|
/// Another live connection already holds this name — the caller lost the
|
||||||
|
/// race (or is a duplicate) and must not run.
|
||||||
|
Duplicate,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Requests to the manager.
|
||||||
|
pub enum Call {
|
||||||
|
/// The path claims its peer's name for a freshly-established connection,
|
||||||
|
/// handing the manager the actor's [`ConnHandle`] and the handshake's
|
||||||
|
/// [`Peer`] (the membership identity `node_up` announces). The manager
|
||||||
|
/// monitors `pid` and holds the handle for as long as the entry lives; a
|
||||||
|
/// [`Registered::Duplicate`] verdict drops the handle here, which stops
|
||||||
|
/// the refused actor.
|
||||||
|
Register {
|
||||||
|
peer: Peer,
|
||||||
|
pid: Pid,
|
||||||
|
handle: ConnHandle,
|
||||||
|
},
|
||||||
|
/// Tear down the connection to `name`: the manager drops its handle, the
|
||||||
|
/// actor stops, and the monitor removes the entry. A no-op if no such
|
||||||
|
/// connection is live.
|
||||||
|
Disconnect { name: String },
|
||||||
|
/// The current peer names, sorted. For observation and tests.
|
||||||
|
Peers,
|
||||||
|
/// A dialer declares an in-flight dial to `name` before connecting. The
|
||||||
|
/// pid is the dialing actor, monitored so the intent dies with it.
|
||||||
|
DialBegin { name: String, pid: Pid },
|
||||||
|
/// The dial to `name` resolved (either way): drop the intent. A call,
|
||||||
|
/// not a cast, so the intent is provably gone before the dialer moves on.
|
||||||
|
DialEnd { name: String },
|
||||||
|
/// The [`PeerStanding`] of an inbound `Hello` offering `peer_name` — the
|
||||||
|
/// accept path asks this between reading the frame and judging it.
|
||||||
|
Standing { peer_name: String },
|
||||||
|
/// Subscribe `tx` to membership events, snapshot-then-stream: one
|
||||||
|
/// [`NodeEvent::NodeUp`] per live peer is queued into `tx` before this
|
||||||
|
/// call answers, so the stream is exact from its first event (handlers
|
||||||
|
/// are serialized — nothing interleaves with the snapshot). Use
|
||||||
|
/// [`subscribe`](crate::cluster::membership::subscribe).
|
||||||
|
Subscribe { tx: Sender<NodeEvent> },
|
||||||
|
/// The current view: every live peer's [`NodeInfo`], unordered. Use
|
||||||
|
/// [`view`](crate::cluster::membership::view).
|
||||||
|
View,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Replies from the manager.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum Reply {
|
||||||
|
Registered(Registered),
|
||||||
|
Disconnected,
|
||||||
|
Peers(Vec<String>),
|
||||||
|
/// `false`: another dial to this name is already in flight — do not dial.
|
||||||
|
DialBegan(bool),
|
||||||
|
DialEnded,
|
||||||
|
Standing(PeerStanding),
|
||||||
|
Subscribed,
|
||||||
|
View(Vec<NodeInfo>),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl GenServer for Manager {
|
||||||
|
type Call = Call;
|
||||||
|
type Reply = Reply;
|
||||||
|
type Cast = ();
|
||||||
|
type Info = ();
|
||||||
|
type Timer = ();
|
||||||
|
|
||||||
|
fn init(&mut self, ctx: &GenServerCtx<Self>) {
|
||||||
|
self.watcher = Some(ctx.watcher());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Manager shutdown drops every entry (and with it every ConnHandle);
|
||||||
|
/// the outbound table must not outlive the connections it names.
|
||||||
|
fn terminate(&mut self) {
|
||||||
|
for name in self.conns.keys() {
|
||||||
|
unbind_outbound(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_call(&mut self, request: Call) -> Reply {
|
||||||
|
match request {
|
||||||
|
Call::Register {
|
||||||
|
peer,
|
||||||
|
pid,
|
||||||
|
mut handle,
|
||||||
|
} => {
|
||||||
|
if self.conns.contains_key(&peer.node_name) {
|
||||||
|
// `handle` drops here: the refused actor stops itself.
|
||||||
|
return Reply::Registered(Registered::Duplicate);
|
||||||
|
}
|
||||||
|
if let Some(w) = &self.watcher {
|
||||||
|
w.watch(monitor(pid));
|
||||||
|
}
|
||||||
|
// The outbound table (c9) is maintained here, inside the same
|
||||||
|
// serialized handlers that own the connection's lifetime.
|
||||||
|
if let Some((frames, monitors)) = handle.take_outbound() {
|
||||||
|
bind_outbound(&peer.node_name, peer.incarnation, frames, monitors);
|
||||||
|
}
|
||||||
|
let info = NodeInfo {
|
||||||
|
node: self.node_id(&peer.node_name, peer.incarnation.get()),
|
||||||
|
name: peer.node_name.clone(),
|
||||||
|
incarnation: peer.incarnation,
|
||||||
|
meta: peer.meta,
|
||||||
|
};
|
||||||
|
self.conns.insert(
|
||||||
|
peer.node_name,
|
||||||
|
ConnEntry {
|
||||||
|
pid,
|
||||||
|
info: info.clone(),
|
||||||
|
_handle: handle,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
self.emit(&NodeEvent::NodeUp(info));
|
||||||
|
Reply::Registered(Registered::Ok)
|
||||||
|
}
|
||||||
|
Call::Disconnect { name } => {
|
||||||
|
// Dropping the entry drops the handle, which stops the actor.
|
||||||
|
if let Some(entry) = self.conns.remove(&name) {
|
||||||
|
unbind_outbound(&name);
|
||||||
|
self.emit(&NodeEvent::NodeDown(entry.info));
|
||||||
|
}
|
||||||
|
Reply::Disconnected
|
||||||
|
}
|
||||||
|
Call::Peers => {
|
||||||
|
let mut names: Vec<String> = self.conns.keys().cloned().collect();
|
||||||
|
names.sort();
|
||||||
|
Reply::Peers(names)
|
||||||
|
}
|
||||||
|
Call::DialBegin { name, pid } => {
|
||||||
|
if self.dials.contains_key(&name) {
|
||||||
|
return Reply::DialBegan(false);
|
||||||
|
}
|
||||||
|
if let Some(w) = &self.watcher {
|
||||||
|
w.watch(monitor(pid));
|
||||||
|
}
|
||||||
|
self.dials.insert(name, pid);
|
||||||
|
Reply::DialBegan(true)
|
||||||
|
}
|
||||||
|
Call::DialEnd { name } => {
|
||||||
|
self.dials.remove(&name);
|
||||||
|
Reply::DialEnded
|
||||||
|
}
|
||||||
|
Call::Standing { peer_name } => {
|
||||||
|
Reply::Standing(if self.conns.contains_key(&peer_name) {
|
||||||
|
PeerStanding::Claimed
|
||||||
|
} else if self.dials.contains_key(&peer_name) {
|
||||||
|
PeerStanding::Dialing
|
||||||
|
} else {
|
||||||
|
PeerStanding::Free
|
||||||
|
})
|
||||||
|
}
|
||||||
|
Call::Subscribe { tx } => {
|
||||||
|
// The snapshot: queued before `tx` joins the list, and — the
|
||||||
|
// handlers being serialized — before any later event.
|
||||||
|
for entry in self.conns.values() {
|
||||||
|
let _ = tx.send(NodeEvent::NodeUp(entry.info.clone()));
|
||||||
|
}
|
||||||
|
self.subscribers.push(tx);
|
||||||
|
Reply::Subscribed
|
||||||
|
}
|
||||||
|
Call::View => Reply::View(self.conns.values().map(|e| e.info.clone()).collect()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_cast(&mut self, _request: ()) {}
|
||||||
|
|
||||||
|
fn handle_down(&mut self, down: Down) {
|
||||||
|
let mut downs = Vec::new();
|
||||||
|
self.conns.retain(|name, entry| {
|
||||||
|
let dead = entry.pid == down.pid;
|
||||||
|
if dead {
|
||||||
|
downs.push((name.clone(), entry.info.clone()));
|
||||||
|
}
|
||||||
|
!dead
|
||||||
|
});
|
||||||
|
for (name, info) in downs {
|
||||||
|
unbind_outbound(&name);
|
||||||
|
self.emit(&NodeEvent::NodeDown(info));
|
||||||
|
}
|
||||||
|
self.dials.retain(|_, pid| *pid != down.pid);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,97 @@
|
|||||||
|
//! RFC 010 c7a — membership: `node_up`/`node_down` events and the view.
|
||||||
|
//!
|
||||||
|
//! The membership *state* lives inside the [`manager`](crate::cluster::manager)
|
||||||
|
//! — `node_up` and `node_down` are derived facts of the exact events the
|
||||||
|
//! manager already owns (a successful registration; a reap or `Disconnect`),
|
||||||
|
//! so holding the view anywhere else would only add a cross-actor ordering
|
||||||
|
//! seam. This module is the consumer surface: the event and view types, and
|
||||||
|
//! the [`subscribe`]/[`view`] entry points. No consumer ever touches the
|
||||||
|
//! connection table (roadmap-binding, enforced by module privacy: the table
|
||||||
|
//! is a private field, and nothing here exposes names→pids).
|
||||||
|
//!
|
||||||
|
//! ## Subscription semantics (ratified 2026-08-15)
|
||||||
|
//!
|
||||||
|
//! [`subscribe`] is **snapshot-then-stream**: the returned receiver first
|
||||||
|
//! yields one [`NodeEvent::NodeUp`] per currently-live peer, then live events
|
||||||
|
//! as they happen. Because the manager is a `gen_server` (handlers are
|
||||||
|
//! serialized), the snapshot is exact — no event can interleave with it, and
|
||||||
|
//! per-subscriber ordering matches the manager's processing order. There is
|
||||||
|
//! no join-race for late subscribers and no separate "get, then diff" dance;
|
||||||
|
//! [`view`] exists for observation, not for synchronization.
|
||||||
|
//!
|
||||||
|
//! A dropped subscriber is pruned on the next emission (its channel reports
|
||||||
|
//! closed) — no monitor needed, the sender itself tells us.
|
||||||
|
//!
|
||||||
|
//! ## NodeId identity
|
||||||
|
//!
|
||||||
|
//! A [`NodeId`] is a compact **local alias for the wire identity**
|
||||||
|
//! `(node_name, incarnation)`, memoized by the manager: a reconnect blip at
|
||||||
|
//! the same incarnation keeps its id (down, then up, same id), while a
|
||||||
|
//! restart — a new incarnation — gets a fresh one, so a node's ghost and its
|
||||||
|
//! successor are always distinguishable. Ids are allocated from 1;
|
||||||
|
//! [`DEFAULT_NODE_ID`](crate::pg::DEFAULT_NODE_ID) (0) remains the local
|
||||||
|
//! node, per [`pg`](crate::pg)'s framing.
|
||||||
|
|
||||||
|
use crate::channel::{channel, Receiver};
|
||||||
|
use crate::cluster::envelope::NodeMeta;
|
||||||
|
use crate::cluster::manager::{Call, Reply, MANAGER};
|
||||||
|
use crate::gen_server;
|
||||||
|
use crate::pg::{Incarnation, NodeId};
|
||||||
|
|
||||||
|
/// One live remote node, as the view and [`NodeEvent::NodeUp`] describe it.
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
pub struct NodeInfo {
|
||||||
|
/// The local alias for `(name, incarnation)` — see the module docs.
|
||||||
|
pub node: NodeId,
|
||||||
|
/// The peer's claimed node name (handshake-verified).
|
||||||
|
pub name: String,
|
||||||
|
/// The peer's incarnation epoch, as offered in its `Hello`.
|
||||||
|
pub incarnation: Incarnation,
|
||||||
|
/// The peer's `Hello` metadata.
|
||||||
|
pub meta: NodeMeta,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A membership change, as delivered to subscribers.
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
pub enum NodeEvent {
|
||||||
|
/// A peer's control connection established and registered.
|
||||||
|
NodeUp(NodeInfo),
|
||||||
|
/// That peer's connection ended — reaped, commanded down, or the manager
|
||||||
|
/// itself shut down. Carries the same [`NodeInfo`] the corresponding
|
||||||
|
/// `NodeUp` delivered, so consumers need no id→name reverse map.
|
||||||
|
NodeDown(NodeInfo),
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A live membership subscription: the receiving end of the event stream
|
||||||
|
/// (the [`Monitor`](crate::monitor::Monitor) shape — read from [`rx`], drop
|
||||||
|
/// to unsubscribe).
|
||||||
|
///
|
||||||
|
/// [rx]: MembershipEvents::rx
|
||||||
|
pub struct MembershipEvents {
|
||||||
|
/// The event stream: the snapshot's `NodeUp`s first, then live events.
|
||||||
|
/// Fold it into a `select` from a plain actor, or pipe it into a
|
||||||
|
/// `gen_server` via `with_info`.
|
||||||
|
pub rx: Receiver<NodeEvent>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Subscribe to membership events (snapshot-then-stream — see the module
|
||||||
|
/// docs). `None`: the manager is not running. Must be called from inside an
|
||||||
|
/// actor.
|
||||||
|
pub fn subscribe() -> Option<MembershipEvents> {
|
||||||
|
let (tx, rx) = channel();
|
||||||
|
match gen_server::call(MANAGER, Call::Subscribe { tx }) {
|
||||||
|
Ok(Reply::Subscribed) => Some(MembershipEvents { rx }),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The current view: every live peer's [`NodeInfo`], unordered. For
|
||||||
|
/// observation and tests; consumers that need to *track* the view should
|
||||||
|
/// [`subscribe`] instead (the snapshot makes the stream self-sufficient).
|
||||||
|
/// `None`: the manager is not running. Must be called from inside an actor.
|
||||||
|
pub fn view() -> Option<Vec<NodeInfo>> {
|
||||||
|
match gen_server::call(MANAGER, Call::View) {
|
||||||
|
Ok(Reply::View(v)) => Some(v),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,546 @@
|
|||||||
|
//! RFC 010 c15 — distributed process groups (Phase 5).
|
||||||
|
//!
|
||||||
|
//! The Erlang `pg` shape (D18): every node's group store is the union of
|
||||||
|
//! its own local members and each peer's *announced* local members. There
|
||||||
|
//! is one **pg actor** per node — the c14 reaper grown up — and it is the
|
||||||
|
//! only writer of remote entries and the only sender of announcements:
|
||||||
|
//!
|
||||||
|
//! - **Origin owns its members.** Joins are local (`pg::join`), the eager
|
||||||
|
//! reaper is the liveness authority, and the origin announces every
|
||||||
|
//! change: `Join`/`Leave` incrementally to every up node, and a full
|
||||||
|
//! `Sync` of its local groups to a peer the moment that peer comes up
|
||||||
|
//! (`NodeUp`). Nobody monitors a remote member; a peer's `NodeDown` sweeps
|
||||||
|
//! every member it announced.
|
||||||
|
//! - **Transport is a pure consumer** of Phase 3/4: the exposed name
|
||||||
|
//! [`PG_NAME`] (`"pg"`) carrying [`PgMsg`] over postcard, sent with
|
||||||
|
//! [`remote::send`]. No new frame, no manager change.
|
||||||
|
//! - **No anti-entropy.** Per-origin ordering rides the single TCP link:
|
||||||
|
//! the actor sends `Sync` to a peer *before* it can send that peer any
|
||||||
|
//! `Join`/`Leave` (both from the same loop, over the same connection), and
|
||||||
|
//! a reconnect is a fresh `NodeUp` ⇒ fresh `Sync` replacing that peer's
|
||||||
|
//! set wholesale.
|
||||||
|
//! - **Local API unchanged.** `members`/`pick`/`dispatch` stay local-only
|
||||||
|
//! (`get_local_members`); a remote entry in the store carries the peer's
|
||||||
|
//! `NodeId` and never surfaces there. Cluster-wide reads are the new,
|
||||||
|
//! additive [`members_all`] over [`GroupMember`] (c16 adds `pick_any` /
|
||||||
|
//! `dispatch_any`).
|
||||||
|
//!
|
||||||
|
//! ## Ordering inside the node
|
||||||
|
//!
|
||||||
|
//! `pg::join`/`pg::leave` mutate the store on the caller's thread and then
|
||||||
|
//! *announce* to the actor's control inbox. Because the store op precedes the
|
||||||
|
//! announcement and the actor re-reads the store before broadcasting a
|
||||||
|
//! `Joined`, an announcement that has been overtaken (the member left or died
|
||||||
|
//! before the actor got to it) is dropped rather than advertised: the wire
|
||||||
|
//! never sees a `Join` for a member the origin no longer holds. `Leave`
|
||||||
|
//! broadcasts unconditionally — a spurious `Leave` is a no-op at the peer.
|
||||||
|
//!
|
||||||
|
//! Inbound: `NodeUp` is emitted by the manager on the accept/connect path,
|
||||||
|
//! *before* the peer's connection actor exists, so it is queued on the
|
||||||
|
//! membership stream before any frame from that peer can reach this inbox.
|
||||||
|
//! The actor still drains membership before it interprets a `PgMsg` whose
|
||||||
|
//! sender it does not know, and drops the message if the sender is still not
|
||||||
|
//! up (a ghost — its next `NodeUp` brings a `Sync`).
|
||||||
|
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use crate::channel::{channel, select, Receiver, Selectable};
|
||||||
|
use crate::cluster::expose::expose;
|
||||||
|
use crate::cluster::membership::{subscribe, MembershipEvents, NodeEvent, NodeInfo};
|
||||||
|
use crate::cluster::remote::{
|
||||||
|
self, local_identity, send_to_remote, RemoteName, RemotePid, ToRemoteError,
|
||||||
|
};
|
||||||
|
use crate::monitor::Down;
|
||||||
|
use crate::pg::{
|
||||||
|
live, member_for, reaper_inboxes, sweep_local_death, Incarnation, Member, Membership, PgEvent,
|
||||||
|
};
|
||||||
|
use crate::pid::{assert_type, Addressable, Erased, Pid};
|
||||||
|
use crate::registry::{register, send_to, SendError};
|
||||||
|
use crate::scheduler::with_runtime;
|
||||||
|
use crate::Name;
|
||||||
|
|
||||||
|
/// The exposed name every node's pg actor answers under.
|
||||||
|
pub const PG_NAME: Name<PgMsg> = Name::new("pg");
|
||||||
|
|
||||||
|
/// The pg wire protocol. Every variant is origin-authored: `from` / the
|
||||||
|
/// pid's node is the node whose local members are being described.
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
pub enum PgMsg {
|
||||||
|
/// The origin's complete local membership, sent to a peer on `NodeUp`.
|
||||||
|
/// Replaces whatever the receiver held for that origin.
|
||||||
|
Sync {
|
||||||
|
from: String,
|
||||||
|
groups: Vec<(String, Vec<RemotePid<Erased>>)>,
|
||||||
|
},
|
||||||
|
/// The origin added `pid` (its own) to `group`.
|
||||||
|
Join {
|
||||||
|
group: String,
|
||||||
|
pid: RemotePid<Erased>,
|
||||||
|
},
|
||||||
|
/// The origin removed `pid` from `group` — voluntary leave or death.
|
||||||
|
Leave {
|
||||||
|
group: String,
|
||||||
|
pid: RemotePid<Erased>,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hand-rolled serde (the crate carries no serde-derive), as a 3-tuple with a
|
||||||
|
// leading tag: (0, from, groups) | (1, group, pid) | (2, group, pid).
|
||||||
|
impl serde::Serialize for PgMsg {
|
||||||
|
fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
|
||||||
|
use serde::ser::SerializeTuple;
|
||||||
|
let mut t = s.serialize_tuple(3)?;
|
||||||
|
match self {
|
||||||
|
PgMsg::Sync { from, groups } => {
|
||||||
|
t.serialize_element(&0u8)?;
|
||||||
|
t.serialize_element(from)?;
|
||||||
|
t.serialize_element(groups)?;
|
||||||
|
}
|
||||||
|
PgMsg::Join { group, pid } => {
|
||||||
|
t.serialize_element(&1u8)?;
|
||||||
|
t.serialize_element(group)?;
|
||||||
|
t.serialize_element(pid)?;
|
||||||
|
}
|
||||||
|
PgMsg::Leave { group, pid } => {
|
||||||
|
t.serialize_element(&2u8)?;
|
||||||
|
t.serialize_element(group)?;
|
||||||
|
t.serialize_element(pid)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
t.end()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'de> serde::Deserialize<'de> for PgMsg {
|
||||||
|
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
|
||||||
|
struct V;
|
||||||
|
impl<'de> serde::de::Visitor<'de> for V {
|
||||||
|
type Value = PgMsg;
|
||||||
|
fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||||
|
f.write_str("a pg message tuple")
|
||||||
|
}
|
||||||
|
fn visit_seq<A: serde::de::SeqAccess<'de>>(
|
||||||
|
self,
|
||||||
|
mut seq: A,
|
||||||
|
) -> Result<PgMsg, A::Error> {
|
||||||
|
use serde::de::Error;
|
||||||
|
let tag: u8 = seq
|
||||||
|
.next_element()?
|
||||||
|
.ok_or_else(|| A::Error::custom("pg: missing tag"))?;
|
||||||
|
let text: String = seq
|
||||||
|
.next_element()?
|
||||||
|
.ok_or_else(|| A::Error::custom("pg: missing name"))?;
|
||||||
|
match tag {
|
||||||
|
0 => {
|
||||||
|
let groups = seq
|
||||||
|
.next_element()?
|
||||||
|
.ok_or_else(|| A::Error::custom("pg: missing groups"))?;
|
||||||
|
Ok(PgMsg::Sync { from: text, groups })
|
||||||
|
}
|
||||||
|
1 | 2 => {
|
||||||
|
let pid = seq
|
||||||
|
.next_element()?
|
||||||
|
.ok_or_else(|| A::Error::custom("pg: missing pid"))?;
|
||||||
|
Ok(if tag == 1 {
|
||||||
|
PgMsg::Join { group: text, pid }
|
||||||
|
} else {
|
||||||
|
PgMsg::Leave { group: text, pid }
|
||||||
|
})
|
||||||
|
}
|
||||||
|
t => Err(A::Error::custom(format!("pg: unknown tag {t}"))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
d.deserialize_tuple(3, V)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A member of a group as the cluster sees it: on this node (a plain
|
||||||
|
/// [`Pid`], sendable locally) or on a peer (a [`RemotePid`], sendable via
|
||||||
|
/// [`send_to_remote`](remote::send_to_remote)). `Pid` cannot hold a remote
|
||||||
|
/// (D14), hence the two-variant shape.
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
pub enum GroupMember {
|
||||||
|
Local(Pid),
|
||||||
|
Remote(RemotePid<Erased>),
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Every member of `group` cluster-wide, in the store's order: local members
|
||||||
|
/// filtered by the same liveness backstop as [`members`](crate::pg::members),
|
||||||
|
/// remote members exactly as their origins last announced them. Must run
|
||||||
|
/// inside [`run`](crate::run).
|
||||||
|
pub fn members_all(group: &str) -> Vec<GroupMember> {
|
||||||
|
with_runtime(|inner| {
|
||||||
|
let me = inner.node_id;
|
||||||
|
let pg = inner.process_groups.lock();
|
||||||
|
pg.all_of(group)
|
||||||
|
.into_iter()
|
||||||
|
.filter_map(|m| {
|
||||||
|
if m.node == me {
|
||||||
|
live(inner, m.pid).then_some(GroupMember::Local(m.pid))
|
||||||
|
} else {
|
||||||
|
// A remote entry always has its node's name recorded
|
||||||
|
// (they land under the same lock); a missing one is a
|
||||||
|
// node already swept, so it hides rather than misnames.
|
||||||
|
pg.node_name(m.node).map(|name| {
|
||||||
|
GroupMember::Remote(RemotePid::from_parts(
|
||||||
|
name,
|
||||||
|
m.incarnation,
|
||||||
|
m.pid.index(),
|
||||||
|
m.pid.generation(),
|
||||||
|
))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// One member of `group` cluster-wide, or `None` if it has none: the first
|
||||||
|
/// entry in the store's order (this node's members in join order first when
|
||||||
|
/// they joined first — the same stateless first-live scan as
|
||||||
|
/// [`pick`](crate::pg::pick), extended over the peers' announced members).
|
||||||
|
/// Must run inside [`run`](crate::run).
|
||||||
|
pub fn pick_any(group: &str) -> Option<GroupMember> {
|
||||||
|
members_all(group).into_iter().next()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Why [`dispatch_any`] handed `msg` back.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum DispatchAnyError<M> {
|
||||||
|
/// The group has no member anywhere.
|
||||||
|
NoMember(M),
|
||||||
|
/// The pick was local and the local typed send failed.
|
||||||
|
Local(SendError<M>),
|
||||||
|
/// The pick was remote and the remote send failed at this node.
|
||||||
|
Remote(ToRemoteError<M>),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<M> DispatchAnyError<M> {
|
||||||
|
/// The undelivered message.
|
||||||
|
pub fn into_inner(self) -> M {
|
||||||
|
match self {
|
||||||
|
DispatchAnyError::NoMember(m) => m,
|
||||||
|
DispatchAnyError::Local(e) => e.into_inner(),
|
||||||
|
DispatchAnyError::Remote(e) => e.into_inner(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// [`pick_any`] and send in one step, returning the member reached: a local
|
||||||
|
/// pick goes through [`send_to`], a remote one through [`send_to_remote`]
|
||||||
|
/// (so `Ok` for a remote member means "handed to the connection", RFC 010
|
||||||
|
/// §3). Homogeneous pool assumed, as for [`dispatch`](crate::pg::dispatch);
|
||||||
|
/// a wrong `A` degrades to a clean error at the target, never a misroute.
|
||||||
|
/// Must run inside [`run`](crate::run).
|
||||||
|
pub fn dispatch_any<A>(group: &str, msg: A::Msg) -> Result<GroupMember, DispatchAnyError<A::Msg>>
|
||||||
|
where
|
||||||
|
A: Addressable,
|
||||||
|
A::Msg: serde::Serialize,
|
||||||
|
{
|
||||||
|
match pick_any(group) {
|
||||||
|
None => Err(DispatchAnyError::NoMember(msg)),
|
||||||
|
Some(GroupMember::Local(pid)) => send_to(assert_type::<A>(pid), msg)
|
||||||
|
.map(|()| GroupMember::Local(pid))
|
||||||
|
.map_err(DispatchAnyError::Local),
|
||||||
|
Some(GroupMember::Remote(rp)) => send_to_remote(rp.clone().assert_type::<A>(), msg)
|
||||||
|
.map(|()| GroupMember::Remote(rp))
|
||||||
|
.map_err(DispatchAnyError::Remote),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Attach the pg actor to the running cluster. Called once by
|
||||||
|
/// `cluster::start` after the manager is up and the local identity is set;
|
||||||
|
/// spawns the actor if this run has not joined anything yet.
|
||||||
|
pub(crate) fn attach_cluster() {
|
||||||
|
let _ = reaper_inboxes().ctl.send(PgEvent::Attach);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The attached half of the actor's state: who is up (by name) and the
|
||||||
|
/// membership stream.
|
||||||
|
struct Attached {
|
||||||
|
events: MembershipEvents,
|
||||||
|
peers: HashMap<String, NodeInfo>,
|
||||||
|
/// This node's wire identity: `Sync`'s `from`, and the stamp on every
|
||||||
|
/// pid we ship (attach requires it, so no `None` path exists here).
|
||||||
|
me: String,
|
||||||
|
incarnation: Incarnation,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The pg actor: the c14 reaper (`deaths`), the local API's announcements
|
||||||
|
/// (`ctl`), and — once attached — the membership stream and the exposed
|
||||||
|
/// `"pg"` inbox, all in one drain-then-select loop. `deaths`/`ctl` closing
|
||||||
|
/// is the run tearing down; the membership stream closing is the manager
|
||||||
|
/// gone (detach, keep reaping).
|
||||||
|
pub(crate) fn actor(deaths: Receiver<Down>, ctl: Receiver<PgEvent>) {
|
||||||
|
let (pg_tx, pg_rx) = channel::<PgMsg>();
|
||||||
|
let mut cl: Option<Attached> = None;
|
||||||
|
loop {
|
||||||
|
loop {
|
||||||
|
match deaths.try_recv() {
|
||||||
|
Ok(Some(down)) => on_death(cl.as_ref(), down.pid),
|
||||||
|
Ok(None) => break,
|
||||||
|
Err(_) => return,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
loop {
|
||||||
|
match ctl.try_recv() {
|
||||||
|
Ok(Some(PgEvent::Attach)) => {
|
||||||
|
// Own the name BEFORE subscribing (which yields to the
|
||||||
|
// manager): a peer's first frame must find "pg" exposed
|
||||||
|
// and resolvable, or it is dropped. Idempotent for a
|
||||||
|
// re-attach: same actor, same channel (the registry
|
||||||
|
// refuses a *second* live one).
|
||||||
|
let _ = register(PG_NAME, pg_tx.clone());
|
||||||
|
expose(PG_NAME);
|
||||||
|
if let Some(a) = attach() {
|
||||||
|
cl = Some(a);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(Some(PgEvent::Joined { group, pid })) => on_joined(cl.as_ref(), &group, pid),
|
||||||
|
Ok(Some(PgEvent::Left { group, pid })) => on_left(cl.as_ref(), &group, pid),
|
||||||
|
Ok(None) => break,
|
||||||
|
Err(_) => return,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if let Some(a) = cl.as_mut() {
|
||||||
|
if !drain_events(a) {
|
||||||
|
cl = None;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
loop {
|
||||||
|
match pg_rx.try_recv() {
|
||||||
|
Ok(Some(msg)) => on_msg(a, msg),
|
||||||
|
Ok(None) => break,
|
||||||
|
Err(_) => return, // our own inbox: only on teardown
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Wait. Control first (attach/teardown must be prompt), then deaths,
|
||||||
|
// then the cluster arms.
|
||||||
|
let mut arms: Vec<&dyn Selectable> = vec![&ctl, &deaths];
|
||||||
|
if let Some(a) = cl.as_ref() {
|
||||||
|
arms.push(&a.events.rx);
|
||||||
|
arms.push(&pg_rx);
|
||||||
|
}
|
||||||
|
let _ = select(&arms);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn attach() -> Option<Attached> {
|
||||||
|
let events = subscribe()?;
|
||||||
|
let (me, incarnation) = local_identity()?;
|
||||||
|
Some(Attached {
|
||||||
|
events,
|
||||||
|
peers: HashMap::new(),
|
||||||
|
me,
|
||||||
|
incarnation,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Fold pending membership events: `NodeUp` ⇒ record + `Sync` that peer;
|
||||||
|
/// `NodeDown` ⇒ sweep every member it announced. `false` when the stream
|
||||||
|
/// has closed.
|
||||||
|
fn drain_events(a: &mut Attached) -> bool {
|
||||||
|
loop {
|
||||||
|
match a.events.rx.try_recv() {
|
||||||
|
Ok(Some(NodeEvent::NodeUp(info))) => {
|
||||||
|
let name = info.name.clone();
|
||||||
|
a.peers.insert(name.clone(), info);
|
||||||
|
// Snapshot under the store lock, then stamp wire pids
|
||||||
|
// outside it (`from_local` marks watchable under the slot's
|
||||||
|
// cold lock — Leaf-on-Leaf nesting is asserted).
|
||||||
|
let local: Vec<(String, Vec<Pid>)> =
|
||||||
|
with_runtime(|inner| inner.process_groups.lock().groups_on(inner.node_id));
|
||||||
|
let groups = local
|
||||||
|
.into_iter()
|
||||||
|
.map(|(g, pids)| (g, pids.into_iter().map(|p| wire(a, p)).collect()))
|
||||||
|
.collect();
|
||||||
|
let msg = PgMsg::Sync {
|
||||||
|
from: a.me.clone(),
|
||||||
|
groups,
|
||||||
|
};
|
||||||
|
let _ = remote::send(RemoteName::new(name, PG_NAME), msg);
|
||||||
|
}
|
||||||
|
Ok(Some(NodeEvent::NodeDown(info))) => {
|
||||||
|
a.peers.remove(&info.name);
|
||||||
|
with_runtime(|inner| {
|
||||||
|
let mut pg = inner.process_groups.lock();
|
||||||
|
pg.remove_where(|m| m.node == info.node);
|
||||||
|
pg.forget_node_name(info.node);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
Ok(None) => return true,
|
||||||
|
Err(_) => return false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Send `msg` to every up peer. `NotConnected` is ignored: that peer's
|
||||||
|
/// `NodeDown` is on its way and its next `NodeUp` gets a `Sync`.
|
||||||
|
fn broadcast(a: &Attached, msg: PgMsg) {
|
||||||
|
for name in a.peers.keys() {
|
||||||
|
let _ = remote::send(RemoteName::new(name.clone(), PG_NAME), msg.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn on_death(a: Option<&Attached>, pid: Pid) {
|
||||||
|
let evicted = sweep_local_death(pid);
|
||||||
|
if let Some(a) = a {
|
||||||
|
for (group, ms) in evicted {
|
||||||
|
broadcast(
|
||||||
|
a,
|
||||||
|
PgMsg::Leave {
|
||||||
|
group,
|
||||||
|
pid: wire(a, ms.member.pid),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn on_joined(a: Option<&Attached>, group: &str, pid: Pid) {
|
||||||
|
let Some(a) = a else { return };
|
||||||
|
// Re-check: a leave/death may have overtaken the announcement.
|
||||||
|
let still = with_runtime(|inner| {
|
||||||
|
let m = member_for(inner, pid);
|
||||||
|
inner.process_groups.lock().contains(group, &m)
|
||||||
|
});
|
||||||
|
if still {
|
||||||
|
broadcast(
|
||||||
|
a,
|
||||||
|
PgMsg::Join {
|
||||||
|
group: group.to_owned(),
|
||||||
|
pid: wire(a, pid),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn on_left(a: Option<&Attached>, group: &str, pid: Pid) {
|
||||||
|
let Some(a) = a else { return };
|
||||||
|
broadcast(
|
||||||
|
a,
|
||||||
|
PgMsg::Leave {
|
||||||
|
group: group.to_owned(),
|
||||||
|
pid: wire(a, pid),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The wire form of a local member pid, stamped with the identity the
|
||||||
|
/// actor was attached with (marks watchable, like `from_local`).
|
||||||
|
fn wire(a: &Attached, pid: Pid) -> RemotePid<Erased> {
|
||||||
|
RemotePid::from_local_at(pid, a.me.clone(), a.incarnation)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The named origin's `NodeInfo`, if it is up. A second look at the
|
||||||
|
/// membership stream covers a `NodeUp` that landed after this loop
|
||||||
|
/// iteration's drain; anything still unknown is a ghost and is dropped.
|
||||||
|
fn origin(a: &mut Attached, name: &str) -> Option<NodeInfo> {
|
||||||
|
if let Some(i) = a.peers.get(name) {
|
||||||
|
return Some(i.clone());
|
||||||
|
}
|
||||||
|
drain_events(a);
|
||||||
|
a.peers.get(name).cloned()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `origin`, additionally requiring `pid` to be stamped with the origin's
|
||||||
|
/// current incarnation — a pid from a previous life of that node is a ghost.
|
||||||
|
fn origin_of(a: &mut Attached, pid: &RemotePid<Erased>) -> Option<NodeInfo> {
|
||||||
|
origin(a, pid.node()).filter(|i| i.incarnation == pid.incarnation())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn remote_membership(origin: &NodeInfo, pid: &RemotePid<Erased>) -> Membership {
|
||||||
|
Membership {
|
||||||
|
member: Member {
|
||||||
|
node: origin.node,
|
||||||
|
incarnation: origin.incarnation,
|
||||||
|
pid: Pid::new(pid.index(), pid.generation()),
|
||||||
|
},
|
||||||
|
monitor: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn on_msg(a: &mut Attached, msg: PgMsg) {
|
||||||
|
match msg {
|
||||||
|
PgMsg::Sync { from, groups } => {
|
||||||
|
let Some(info) = origin(a, &from) else { return };
|
||||||
|
with_runtime(|inner| {
|
||||||
|
let mut pg = inner.process_groups.lock();
|
||||||
|
pg.remove_where(|m| m.node == info.node);
|
||||||
|
pg.set_node_name(info.node, info.name.clone());
|
||||||
|
for (group, pids) in &groups {
|
||||||
|
// Origin-authored: only its own current-incarnation pids.
|
||||||
|
for p in pids
|
||||||
|
.iter()
|
||||||
|
.filter(|p| p.node() == from && p.incarnation() == info.incarnation)
|
||||||
|
{
|
||||||
|
pg.join(group, remote_membership(&info, p));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
PgMsg::Join { group, pid } => {
|
||||||
|
let Some(info) = origin_of(a, &pid) else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
with_runtime(|inner| {
|
||||||
|
let mut pg = inner.process_groups.lock();
|
||||||
|
pg.set_node_name(info.node, info.name.clone());
|
||||||
|
pg.join(&group, remote_membership(&info, &pid));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
PgMsg::Leave { group, pid } => {
|
||||||
|
let Some(info) = origin_of(a, &pid) else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
with_runtime(|inner| {
|
||||||
|
let ms = remote_membership(&info, &pid);
|
||||||
|
inner.process_groups.lock().leave(&group, ms.member);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use crate::cluster::envelope::{decode_payload, encode_payload};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn pg_msg_roundtrips_every_variant() {
|
||||||
|
let p = RemotePid::<Erased>::from_parts("a", Incarnation::new(9), 3, 1);
|
||||||
|
for m in [
|
||||||
|
PgMsg::Sync {
|
||||||
|
from: "a".into(),
|
||||||
|
groups: vec![
|
||||||
|
("g".into(), vec![p.clone(), p.clone()]),
|
||||||
|
("h".into(), vec![]),
|
||||||
|
],
|
||||||
|
},
|
||||||
|
PgMsg::Sync {
|
||||||
|
from: "a".into(),
|
||||||
|
groups: vec![],
|
||||||
|
},
|
||||||
|
PgMsg::Join {
|
||||||
|
group: "g".into(),
|
||||||
|
pid: p.clone(),
|
||||||
|
},
|
||||||
|
PgMsg::Leave {
|
||||||
|
group: "g".into(),
|
||||||
|
pid: p.clone(),
|
||||||
|
},
|
||||||
|
] {
|
||||||
|
let bytes = encode_payload(&m).unwrap();
|
||||||
|
let back: PgMsg = decode_payload(&bytes).unwrap();
|
||||||
|
assert_eq!(back, m);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn pg_msg_rejects_unknown_tag() {
|
||||||
|
let bytes = encode_payload(&(7u8, "x", 0u32)).unwrap();
|
||||||
|
assert!(decode_payload::<PgMsg>(&bytes).is_err());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,835 @@
|
|||||||
|
//! RFC 010 c9 — remote `Name` sends: the outbound path and the single
|
||||||
|
//! inbound name-resolution seam.
|
||||||
|
//!
|
||||||
|
//! ## Outbound (D13, ratified 2026-08-15)
|
||||||
|
//!
|
||||||
|
//! A module-private table `node name → Sender<Frame>` — one dedicated
|
||||||
|
//! outbound channel per live connection, populated and torn down by the
|
||||||
|
//! manager inside the same serialized handlers that own the connection's
|
||||||
|
//! lifetime (Register / Disconnect / reap), living on `RuntimeInner` beside
|
||||||
|
//! the exposure state. [`send`] is one leaf-lock lookup + one channel send:
|
||||||
|
//! no gen_server on the data plane, no published channel anyone holding a
|
||||||
|
//! pid could inject raw frames into. `Ok(())` means **handed to the
|
||||||
|
//! connection's inbox** — local knowledge only, exactly the BEAM contract
|
||||||
|
//! (RFC §3): a missing entry or a closed channel is
|
||||||
|
//! [`RemoteSendError::NotConnected`]; delivery confirmation is the monitor's
|
||||||
|
//! job (c11). The entry-present/actor-dying-mid-send window is *honest*
|
||||||
|
//! under that contract, not a bug.
|
||||||
|
//!
|
||||||
|
//! The outbound sender is deliberately **separate from the conn actor's
|
||||||
|
//! command channel**: if it were a clone of `cmd_tx`, the manager dropping
|
||||||
|
//! its `ConnHandle` would no longer close that channel and connection
|
||||||
|
//! lifetime would leak to whoever holds a sender — a D9 violation.
|
||||||
|
//!
|
||||||
|
//! Buffering is unbounded toward a slow peer (the BEAM `busy_dist_port`
|
||||||
|
//! shape); backpressure is out of c9's scope and noted here rather than
|
||||||
|
//! silently absent.
|
||||||
|
//!
|
||||||
|
//! ## Inbound — the ONE resolution seam (RFC v2)
|
||||||
|
//!
|
||||||
|
//! Every wire-name → local-pid resolution goes through [`deliver_named`],
|
||||||
|
//! and nothing else: the conn actor hands it the three fields of a
|
||||||
|
//! `SendNamed` and gets back a verdict. It checks the exposed set first (an
|
||||||
|
//! unexposed name is unreachable — the gun's safety), then the type hash
|
||||||
|
//! against what the name was exposed with, then resolves the name through
|
||||||
|
//! the registry and delivers via c8's [`decode_deliver`]. When an owned-name
|
||||||
|
//! table lands beside the `&'static str` registry, it slots in here without
|
||||||
|
//! touching call sites. Module privacy enforces the funnel: the exposed and
|
||||||
|
//! outbound tables are `pub(crate)`, and no other module resolves names for
|
||||||
|
//! the wire.
|
||||||
|
//!
|
||||||
|
//! Refusals are silent to the sender by design (§3: send failure reflects
|
||||||
|
//! local knowledge only); they are observable locally as the returned
|
||||||
|
//! [`InboundVerdict`], which the conn actor may log or count.
|
||||||
|
//!
|
||||||
|
//! ## Pids (c10, D14)
|
||||||
|
//!
|
||||||
|
//! [`RemotePid<A>`] = `(node_name, incarnation, index, generation)` +
|
||||||
|
//! phantom — identity-bound, dead when that incarnation dies, never
|
||||||
|
//! redirects. The node travels as its **name** (a global identifier, so a pid
|
||||||
|
//! forwarded through a third node needs no re-mapping); NodeId is a local
|
||||||
|
//! alias and never crosses. A local `Pid<A>` serializes *as* a `RemotePid`
|
||||||
|
//! stamped from the ambient [local identity](set_local_identity); a
|
||||||
|
//! `RemotePid` deserializes into `Pid<A>` only when it names this node (the
|
||||||
|
//! collapse), else it is a decode error — fields that may hold a pid from
|
||||||
|
//! anywhere are typed `RemotePid<A>`.
|
||||||
|
//!
|
||||||
|
//! [`send_to_remote`] is the pid-targeted send. A self-node pid short-
|
||||||
|
//! circuits to the local typed send with the message object itself — no
|
||||||
|
//! encode, no frame (zero-copy-equivalent). Otherwise the outbound table
|
||||||
|
//! (widened to carry each node's **current incarnation**) does the RFC v2 §3
|
||||||
|
//! check at the send site: a pid of a dead incarnation is
|
||||||
|
//! [`ToRemoteError::DeadIncarnation`] and no frame is emitted. Inbound
|
||||||
|
//! `Send` frames are delivered by index/generation through c8's
|
||||||
|
//! [`decode_deliver`]: the target actor's published channel for the exposed
|
||||||
|
//! type is the only route (the reply-to path requires
|
||||||
|
//! [`expose_type`](crate::cluster::expose::expose_type) at the receiver).
|
||||||
|
|
||||||
|
use std::cell::Cell;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
|
use crate::channel::{channel, Receiver, RecvError, Selectable, Sender};
|
||||||
|
use crate::cluster::envelope::{encode_payload, Frame, PayloadError, RemoteDownReason};
|
||||||
|
use crate::cluster::expose::{decode_deliver, exposed_hash, type_hash, DeliverError};
|
||||||
|
use crate::monitor::{demonitor, monitor, Monitor, MonitorId};
|
||||||
|
use crate::pg::Incarnation;
|
||||||
|
use crate::pid::{Addressable, Erased, Name, Pid};
|
||||||
|
use crate::registry::{send_to, whereis, SendError};
|
||||||
|
use crate::scheduler::with_runtime;
|
||||||
|
|
||||||
|
/// A name on a specific remote node: `(node_name, Name<M>)`. Sendable via
|
||||||
|
/// [`send`]; typed, so the payload is `M` and the wire hash is
|
||||||
|
/// [`type_hash::<M>()`](type_hash).
|
||||||
|
pub struct RemoteName<M> {
|
||||||
|
node: String,
|
||||||
|
name: Name<M>,
|
||||||
|
_marker: PhantomData<fn() -> M>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<M> RemoteName<M> {
|
||||||
|
pub fn new(node: impl Into<String>, name: Name<M>) -> Self {
|
||||||
|
RemoteName {
|
||||||
|
node: node.into(),
|
||||||
|
name,
|
||||||
|
_marker: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn node(&self) -> &str {
|
||||||
|
&self.node
|
||||||
|
}
|
||||||
|
pub fn name(&self) -> Name<M> {
|
||||||
|
self.name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<M> Clone for RemoteName<M> {
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
RemoteName {
|
||||||
|
node: self.node.clone(),
|
||||||
|
name: self.name,
|
||||||
|
_marker: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<M> std::fmt::Debug for RemoteName<M> {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(f, "{}@{}", self.name.as_str(), self.node)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Why a remote send did not leave this node. Local knowledge only.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum RemoteSendError<M> {
|
||||||
|
/// No live connection to that node right now (never connected, or gone
|
||||||
|
/// and not yet re-dialed). The message is handed back.
|
||||||
|
NotConnected(M),
|
||||||
|
/// The payload did not serialize.
|
||||||
|
Encode(M, PayloadError),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<M> RemoteSendError<M> {
|
||||||
|
pub fn into_inner(self) -> M {
|
||||||
|
match self {
|
||||||
|
RemoteSendError::NotConnected(m) | RemoteSendError::Encode(m, _) => m,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The outbound table, one per runtime (a `RuntimeInner` field): per live
|
||||||
|
/// node, its current incarnation (the RFC v2 §3 send-site check) and the
|
||||||
|
/// connection's dedicated outbound sender. Plus this node's own wire
|
||||||
|
/// identity, which pid serialization stamps.
|
||||||
|
pub(crate) struct Outbound {
|
||||||
|
by_node: HashMap<String, Route>,
|
||||||
|
local: Option<(String, Incarnation)>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// One live connection as the outbound path sees it: the peer's current
|
||||||
|
/// incarnation and the two inboxes of its connection actor — frames (c9)
|
||||||
|
/// and monitor bookkeeping (c12, [`MonCmd`]).
|
||||||
|
pub(crate) struct Route {
|
||||||
|
incarnation: Incarnation,
|
||||||
|
frames: Sender<Frame>,
|
||||||
|
monitors: Sender<MonCmd>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Outbound {
|
||||||
|
pub(crate) fn new() -> Self {
|
||||||
|
Outbound {
|
||||||
|
by_node: HashMap::new(),
|
||||||
|
local: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set this node's wire identity — what serialized pids are stamped with
|
||||||
|
/// and what a `RemotePid` must name to collapse. `cluster::start` sets it;
|
||||||
|
/// exposed for local tests. Must run inside [`run`](crate::run).
|
||||||
|
pub fn set_local_identity(node: &str, incarnation: Incarnation) {
|
||||||
|
with_runtime(|inner| {
|
||||||
|
inner.outbound.lock().local = Some((node.to_string(), incarnation));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// This node's wire identity, if set. Must run inside [`run`](crate::run).
|
||||||
|
pub fn local_identity() -> Option<(String, Incarnation)> {
|
||||||
|
with_runtime(|inner| inner.outbound.lock().local.clone())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Manager-only: bind `node`'s outbound channels at `incarnation`. Called
|
||||||
|
/// inside `Register`.
|
||||||
|
pub(crate) fn bind_outbound(
|
||||||
|
node: &str,
|
||||||
|
incarnation: Incarnation,
|
||||||
|
frames: Sender<Frame>,
|
||||||
|
monitors: Sender<MonCmd>,
|
||||||
|
) {
|
||||||
|
with_runtime(|inner| {
|
||||||
|
inner.outbound.lock().by_node.insert(
|
||||||
|
node.to_string(),
|
||||||
|
Route {
|
||||||
|
incarnation,
|
||||||
|
frames,
|
||||||
|
monitors,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Test probe: bind an arbitrary sender as `node`'s outbound so a test can
|
||||||
|
/// assert what frames leave — or don't. Same table, same lookup as the real
|
||||||
|
/// path (this is how "no frame emitted" is asserted at the frame level).
|
||||||
|
/// Frames only: there is no connection actor behind a probe, so a
|
||||||
|
/// [`monitor_remote`] against a probed node reports `Disconnected`.
|
||||||
|
pub fn bind_outbound_probe(node: &str, incarnation: Incarnation, tx: Sender<Frame>) {
|
||||||
|
drop(bind_outbound_probe_with_monitors(node, incarnation, tx));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The monitor half of a probed node's inbox: opaque, held only to be
|
||||||
|
/// dropped. See [`bind_outbound_probe_with_monitors`].
|
||||||
|
pub struct MonitorInbox {
|
||||||
|
_rx: Receiver<MonCmd>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Test probe: like [`bind_outbound_probe`], but the monitor-command
|
||||||
|
/// receiver is handed back instead of dropped, so a test can stage the
|
||||||
|
/// c13 drain gap — a `Monitor` command that reached the connection's inbox
|
||||||
|
/// and dies unread when the inbox is dropped. While the inbox lives,
|
||||||
|
/// [`monitor_remote`] against the probed node is simply in flight.
|
||||||
|
pub fn bind_outbound_probe_with_monitors(
|
||||||
|
node: &str,
|
||||||
|
incarnation: Incarnation,
|
||||||
|
tx: Sender<Frame>,
|
||||||
|
) -> MonitorInbox {
|
||||||
|
let (mon_tx, mon_rx) = channel();
|
||||||
|
bind_outbound(node, incarnation, tx, mon_tx);
|
||||||
|
MonitorInbox { _rx: mon_rx }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Manager-only: unbind `node`'s outbound channel. Called on `Disconnect`,
|
||||||
|
/// reap, and manager shutdown. Dropping the sender is what closes the conn
|
||||||
|
/// actor's outbound arm — but that arm's closure is NOT a stop signal (the
|
||||||
|
/// cmd channel is, per D9); the actor simply stops selecting on it.
|
||||||
|
pub(crate) fn unbind_outbound(node: &str) {
|
||||||
|
with_runtime(|inner| {
|
||||||
|
inner.outbound.lock().by_node.remove(node);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Send `msg` to `target`. `Ok(())` = handed to the connection's inbox, and
|
||||||
|
/// nothing more — see the module docs. Must run inside
|
||||||
|
/// [`run`](crate::run).
|
||||||
|
pub fn send<M>(target: RemoteName<M>, msg: M) -> Result<(), RemoteSendError<M>>
|
||||||
|
where
|
||||||
|
M: serde::Serialize + Send + 'static,
|
||||||
|
{
|
||||||
|
let payload = match encode_payload(&msg) {
|
||||||
|
Ok(p) => p,
|
||||||
|
Err(e) => return Err(RemoteSendError::Encode(msg, e)),
|
||||||
|
};
|
||||||
|
let frame = Frame::SendNamed {
|
||||||
|
name: target.name.as_str().to_string(),
|
||||||
|
type_hash: type_hash::<M>(),
|
||||||
|
payload,
|
||||||
|
};
|
||||||
|
match hand_to_connection(&target.node, frame) {
|
||||||
|
Ok(()) => Ok(()),
|
||||||
|
Err(NotConnected) => Err(RemoteSendError::NotConnected(msg)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// No live connection to the named node — the payload-free form of
|
||||||
|
/// [`RemoteSendError::NotConnected`], for the raw path.
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
pub struct NotConnected;
|
||||||
|
|
||||||
|
/// The untyped escape hatch: send pre-encoded `payload` under an explicit
|
||||||
|
/// `type_hash`. Exists so tests (and future codecs) can put deliberately
|
||||||
|
/// wrong frames on the wire; the typed [`send`] cannot express a hash/type
|
||||||
|
/// mismatch, by design. Same `Ok` semantics as [`send`].
|
||||||
|
pub fn send_remote_raw(
|
||||||
|
node: &str,
|
||||||
|
name: &str,
|
||||||
|
type_hash: u64,
|
||||||
|
payload: &[u8],
|
||||||
|
) -> Result<(), NotConnected> {
|
||||||
|
hand_to_connection(
|
||||||
|
node,
|
||||||
|
Frame::SendNamed {
|
||||||
|
name: name.to_string(),
|
||||||
|
type_hash,
|
||||||
|
payload: payload.to_vec(),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// One lookup, one send. Clone the sender out under the lock and send
|
||||||
|
/// outside it (a channel send can unpark the conn actor).
|
||||||
|
fn hand_to_connection(node: &str, frame: Frame) -> Result<(), NotConnected> {
|
||||||
|
let tx = with_runtime(|inner| {
|
||||||
|
inner
|
||||||
|
.outbound
|
||||||
|
.lock()
|
||||||
|
.by_node
|
||||||
|
.get(node)
|
||||||
|
.map(|r| r.frames.clone())
|
||||||
|
});
|
||||||
|
match tx {
|
||||||
|
Some(tx) => tx.send(frame).map_err(|_| NotConnected),
|
||||||
|
None => Err(NotConnected),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- pids ---------------------------------------------------------------
|
||||||
|
|
||||||
|
/// A pid on some node: `(node_name, incarnation, index, generation)` plus
|
||||||
|
/// the actor type. See the module docs. Serializes as a 4-tuple.
|
||||||
|
pub struct RemotePid<A> {
|
||||||
|
node: String,
|
||||||
|
incarnation: Incarnation,
|
||||||
|
index: u32,
|
||||||
|
generation: u32,
|
||||||
|
_marker: PhantomData<fn() -> A>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<A> RemotePid<A> {
|
||||||
|
/// Build from raw parts (tests, and codecs re-hydrating a pid).
|
||||||
|
pub fn from_parts(
|
||||||
|
node: impl Into<String>,
|
||||||
|
incarnation: Incarnation,
|
||||||
|
index: u32,
|
||||||
|
generation: u32,
|
||||||
|
) -> Self {
|
||||||
|
RemotePid {
|
||||||
|
node: node.into(),
|
||||||
|
incarnation,
|
||||||
|
index,
|
||||||
|
generation,
|
||||||
|
_marker: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The wire form of a local pid, stamped with this node's identity, and
|
||||||
|
/// **marked watchable** — asking for the wire form *is* the intent to
|
||||||
|
/// ship the pid, so this is the same D12 set-site as `Pid::serialize`
|
||||||
|
/// (c12 made it explicit: a peer may monitor exactly the pids that
|
||||||
|
/// crossed, and a pid handed out via `from_local` in a hand-built reply
|
||||||
|
/// has crossed). Must run inside [`run`](crate::run).
|
||||||
|
///
|
||||||
|
/// `None` when this runtime has no wire identity (no `cluster::start`,
|
||||||
|
/// no [`set_local_identity`]): such a pid cannot name a node, and a
|
||||||
|
/// stamped `("", 0)` would be dropped by every peer with no signal.
|
||||||
|
/// The pid is not marked watchable in that case either.
|
||||||
|
pub fn from_local(pid: Pid<A>) -> Option<Self> {
|
||||||
|
let (node, incarnation) = local_identity()?;
|
||||||
|
Some(Self::from_local_at(pid, node, incarnation))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `from_local` with the identity supplied by the caller — for a holder
|
||||||
|
/// that already carries the node's identity (the pg actor) and must not
|
||||||
|
/// have a `None` path. Marks watchable like `from_local`.
|
||||||
|
pub(crate) fn from_local_at(pid: Pid<A>, node: String, incarnation: Incarnation) -> Self {
|
||||||
|
crate::monitor::mark_watchable(pid);
|
||||||
|
RemotePid::from_parts(node, incarnation, pid.index(), pid.generation())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The collapse: `Some(local pid)` iff this pid names this very node
|
||||||
|
/// (name and incarnation). Must run inside [`run`](crate::run).
|
||||||
|
pub fn local(&self) -> Option<Pid<A>> {
|
||||||
|
let (n, i) = local_identity()?;
|
||||||
|
(n == self.node && i == self.incarnation)
|
||||||
|
.then(|| crate::pid::assert_type::<A>(Pid::new(self.index, self.generation)))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Drop the actor type: the untyped `RemotePid<Erased>`, the form
|
||||||
|
/// [`RemoteDown`] and [`RemoteMonitor`] carry (mirrors [`Pid::erase`]).
|
||||||
|
pub fn erase(self) -> RemotePid<Erased> {
|
||||||
|
RemotePid::from_parts(self.node, self.incarnation, self.index, self.generation)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Re-type an erased pid as `RemotePid<B>` — the unchecked mirror of
|
||||||
|
/// `pid::assert_type`, with the same degradation: a wrong `B` means the
|
||||||
|
/// target refuses the payload's hash (never a misroute).
|
||||||
|
pub(crate) fn assert_type<B>(self) -> RemotePid<B> {
|
||||||
|
RemotePid::from_parts(self.node, self.incarnation, self.index, self.generation)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn node(&self) -> &str {
|
||||||
|
&self.node
|
||||||
|
}
|
||||||
|
pub fn incarnation(&self) -> Incarnation {
|
||||||
|
self.incarnation
|
||||||
|
}
|
||||||
|
pub fn index(&self) -> u32 {
|
||||||
|
self.index
|
||||||
|
}
|
||||||
|
pub fn generation(&self) -> u32 {
|
||||||
|
self.generation
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<A> Clone for RemotePid<A> {
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
RemotePid::from_parts(
|
||||||
|
self.node.clone(),
|
||||||
|
self.incarnation,
|
||||||
|
self.index,
|
||||||
|
self.generation,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<A> PartialEq for RemotePid<A> {
|
||||||
|
fn eq(&self, o: &Self) -> bool {
|
||||||
|
self.node == o.node
|
||||||
|
&& self.incarnation == o.incarnation
|
||||||
|
&& self.index == o.index
|
||||||
|
&& self.generation == o.generation
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<A> Eq for RemotePid<A> {}
|
||||||
|
impl<A> std::fmt::Debug for RemotePid<A> {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"<{}.{}@{}#{}>",
|
||||||
|
self.index,
|
||||||
|
self.generation,
|
||||||
|
self.node,
|
||||||
|
self.incarnation.get()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<A> serde::Serialize for RemotePid<A> {
|
||||||
|
fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
|
||||||
|
(
|
||||||
|
self.node.as_str(),
|
||||||
|
self.incarnation.get(),
|
||||||
|
self.index,
|
||||||
|
self.generation,
|
||||||
|
)
|
||||||
|
.serialize(s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<'de, A> serde::Deserialize<'de> for RemotePid<A> {
|
||||||
|
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
|
||||||
|
let (node, inc, index, generation) = <(String, u32, u32, u32)>::deserialize(d)?;
|
||||||
|
Ok(RemotePid::from_parts(
|
||||||
|
node,
|
||||||
|
Incarnation::new(inc),
|
||||||
|
index,
|
||||||
|
generation,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Why a pid-targeted send did not leave this node. Local knowledge only.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum ToRemoteError<M> {
|
||||||
|
/// No live connection to the pid's node.
|
||||||
|
NotConnected(M),
|
||||||
|
/// The pid's incarnation is not that node's current one (RFC v2 §3): the
|
||||||
|
/// actor died with its incarnation. Detected at the send site; no frame.
|
||||||
|
DeadIncarnation(M),
|
||||||
|
/// The payload did not serialize.
|
||||||
|
Encode(M, PayloadError),
|
||||||
|
/// The pid collapsed to a local one and the local typed send failed.
|
||||||
|
Local(SendError<M>),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<M> ToRemoteError<M> {
|
||||||
|
/// The undelivered message.
|
||||||
|
pub fn into_inner(self) -> M {
|
||||||
|
match self {
|
||||||
|
ToRemoteError::NotConnected(m)
|
||||||
|
| ToRemoteError::DeadIncarnation(m)
|
||||||
|
| ToRemoteError::Encode(m, _) => m,
|
||||||
|
ToRemoteError::Local(e) => e.into_inner(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Send `msg` to a pid, wherever it lives. Self-node pids short-circuit to
|
||||||
|
/// the local typed send with `msg` itself (no encode, no frame); others go
|
||||||
|
/// out as a `Send` frame after the incarnation check. `Ok(())` for a remote
|
||||||
|
/// target = handed to the connection's inbox. Must run inside
|
||||||
|
/// [`run`](crate::run).
|
||||||
|
pub fn send_to_remote<A>(target: RemotePid<A>, msg: A::Msg) -> Result<(), ToRemoteError<A::Msg>>
|
||||||
|
where
|
||||||
|
A: Addressable,
|
||||||
|
A::Msg: serde::Serialize,
|
||||||
|
{
|
||||||
|
if let Some(local) = target.local() {
|
||||||
|
return send_to(local, msg).map_err(ToRemoteError::Local);
|
||||||
|
}
|
||||||
|
let route = with_runtime(|inner| {
|
||||||
|
inner
|
||||||
|
.outbound
|
||||||
|
.lock()
|
||||||
|
.by_node
|
||||||
|
.get(&target.node)
|
||||||
|
.map(|r| (r.incarnation, r.frames.clone()))
|
||||||
|
});
|
||||||
|
let (current, tx) = match route {
|
||||||
|
Some(r) => r,
|
||||||
|
None => return Err(ToRemoteError::NotConnected(msg)),
|
||||||
|
};
|
||||||
|
if current != target.incarnation {
|
||||||
|
return Err(ToRemoteError::DeadIncarnation(msg));
|
||||||
|
}
|
||||||
|
let payload = match encode_payload(&msg) {
|
||||||
|
Ok(p) => p,
|
||||||
|
Err(e) => return Err(ToRemoteError::Encode(msg, e)),
|
||||||
|
};
|
||||||
|
let frame = Frame::Send {
|
||||||
|
index: target.index,
|
||||||
|
generation: target.generation,
|
||||||
|
type_hash: type_hash::<A::Msg>(),
|
||||||
|
payload,
|
||||||
|
};
|
||||||
|
tx.send(frame).map_err(|_| ToRemoteError::NotConnected(msg))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The inbound `Send` seam: deliver `payload` under `type_hash` to the local
|
||||||
|
/// actor `(index, generation)`. Node and incarnation are implicit in the
|
||||||
|
/// connection (bound at handshake) — the frame carries only the slot
|
||||||
|
/// identity. Delivery goes through c8's decoder table, so only types the
|
||||||
|
/// receiver has [`expose_type`](crate::cluster::expose::expose_type)d (or
|
||||||
|
/// exposed by name) can land; anything else is refused, never misrouted.
|
||||||
|
pub fn deliver_to_pid(
|
||||||
|
index: u32,
|
||||||
|
generation: u32,
|
||||||
|
type_hash: u64,
|
||||||
|
payload: &[u8],
|
||||||
|
) -> InboundVerdict {
|
||||||
|
let pid = Pid::new(index, generation);
|
||||||
|
match decode_deliver(type_hash, pid, payload) {
|
||||||
|
Ok(()) => InboundVerdict::Delivered,
|
||||||
|
Err(e) => InboundVerdict::Refused(e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// What the inbound seam did with a `SendNamed`. Local observability only;
|
||||||
|
/// nothing goes back on the wire (RFC §3).
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum InboundVerdict {
|
||||||
|
/// Decoded and handed to the name's holder.
|
||||||
|
Delivered,
|
||||||
|
/// The name is not in this node's exposed set.
|
||||||
|
NotExposed,
|
||||||
|
/// The frame's hash is not the hash the name was exposed with.
|
||||||
|
HashMismatch { expected: u64, got: u64 },
|
||||||
|
/// Exposed, but no live holder right now (unbound, or its holder died
|
||||||
|
/// and the binding is being pruned).
|
||||||
|
Unresolved,
|
||||||
|
/// Resolved, but the delivery half refused it (decode failure, or the
|
||||||
|
/// holder's channel does not accept the exposed type — a local
|
||||||
|
/// re-registration under a different type; never a misroute).
|
||||||
|
Refused(DeliverError),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl InboundVerdict {
|
||||||
|
/// A short static label for tracing/counting (`smarm-trace` records one
|
||||||
|
/// `ClusterInbound` event per frame with it).
|
||||||
|
pub fn label(&self) -> &'static str {
|
||||||
|
match self {
|
||||||
|
InboundVerdict::Delivered => "delivered",
|
||||||
|
InboundVerdict::NotExposed => "not_exposed",
|
||||||
|
InboundVerdict::HashMismatch { .. } => "hash_mismatch",
|
||||||
|
InboundVerdict::Unresolved => "unresolved",
|
||||||
|
InboundVerdict::Refused(_) => "refused",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// THE inbound resolution seam: exposed-set check → hash check → registry
|
||||||
|
/// resolution → c8 delivery. See the module docs. Must run inside
|
||||||
|
/// [`run`](crate::run) — the conn actor's context.
|
||||||
|
pub fn deliver_named(name: &str, type_hash: u64, payload: &[u8]) -> InboundVerdict {
|
||||||
|
let Some(expected) = exposed_hash(name) else {
|
||||||
|
return InboundVerdict::NotExposed;
|
||||||
|
};
|
||||||
|
if expected != type_hash {
|
||||||
|
return InboundVerdict::HashMismatch {
|
||||||
|
expected,
|
||||||
|
got: type_hash,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
let Some(pid) = whereis(name) else {
|
||||||
|
return InboundVerdict::Unresolved;
|
||||||
|
};
|
||||||
|
match decode_deliver(type_hash, pid, payload) {
|
||||||
|
Ok(()) => InboundVerdict::Delivered,
|
||||||
|
Err(e) => InboundVerdict::Refused(e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- monitors (c12) -----------------------------------------------------
|
||||||
|
|
||||||
|
/// Bookkeeping commands from [`monitor_remote`]/[`demonitor_remote`] to the
|
||||||
|
/// connection actor that owns the link to the target's node. The actor
|
||||||
|
/// records the registration and *then* emits the `Monitor` frame itself, so
|
||||||
|
/// a `Down` can never arrive at a table that does not yet know the id. It
|
||||||
|
/// lives in the actor (not on `RuntimeInner`) so the bookkeeping dies with
|
||||||
|
/// the connection — exactly what c13 needs to synthesize `Disconnected`.
|
||||||
|
pub(crate) enum MonCmd {
|
||||||
|
Monitor {
|
||||||
|
id: MonitorId,
|
||||||
|
target: RemotePid<Erased>,
|
||||||
|
tx: Sender<RemoteDown>,
|
||||||
|
},
|
||||||
|
Demonitor {
|
||||||
|
id: MonitorId,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A remotely-monitored actor's termination notice — the cluster analog of
|
||||||
|
/// [`Down`](crate::monitor::Down), with the pid in its wire form because it
|
||||||
|
/// may name any node.
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
pub struct RemoteDown {
|
||||||
|
/// The pid that was being monitored.
|
||||||
|
pub pid: RemotePid<Erased>,
|
||||||
|
/// How it went down. `Disconnected` means the *link* to its node was
|
||||||
|
/// lost (or absent) — nothing is known about the actor itself.
|
||||||
|
pub reason: RemoteDownReason,
|
||||||
|
}
|
||||||
|
|
||||||
|
enum Watch {
|
||||||
|
/// The target collapsed to this node: an ordinary local monitor,
|
||||||
|
/// translated on read.
|
||||||
|
Local(Monitor),
|
||||||
|
/// The target is elsewhere: the connection actor for its node holds the
|
||||||
|
/// registration and forwards the peer's `Down` frame here. The
|
||||||
|
/// `RemoteState` is the read-side backstop (c13): a channel that closes
|
||||||
|
/// while `Live` — the connection died with our command unread — reads
|
||||||
|
/// as `Disconnected` once; afterwards, and after a cancel, closed is
|
||||||
|
/// just closed.
|
||||||
|
Remote(Receiver<RemoteDown>, Cell<RemoteState>),
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Where a remote-watch stands from the reader's side.
|
||||||
|
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||||
|
enum RemoteState {
|
||||||
|
/// No notice yet, not cancelled: a closed channel means `Disconnected`.
|
||||||
|
Live,
|
||||||
|
/// The one notice has been read (or synthesized): nothing more is due.
|
||||||
|
Done,
|
||||||
|
/// `demonitor_remote` ran: never synthesize.
|
||||||
|
Cancelled,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A live remote monitor: read its one [`RemoteDown`] with
|
||||||
|
/// [`recv`](RemoteMonitor::recv)/[`try_recv`](RemoteMonitor::try_recv), or
|
||||||
|
/// fold it into a `select` via [`arm`](RemoteMonitor::arm). Distinct from
|
||||||
|
/// [`Monitor`] on purpose: its target is a [`RemotePid`], its notice a
|
||||||
|
/// [`RemoteDown`], and it can report `Disconnected` — none of which a local
|
||||||
|
/// monitor can express. Dropping it discards an unread notice, like the
|
||||||
|
/// local one; after [`demonitor_remote`] the channel is closed and empty, so
|
||||||
|
/// `recv` errs rather than parking — also like the local one.
|
||||||
|
///
|
||||||
|
/// Exactly one notice is guaranteed even if the connection actor dies with
|
||||||
|
/// the registration unread (the c13 drain gap): a channel that closes
|
||||||
|
/// before any notice — and before any cancel — reads as `Disconnected`,
|
||||||
|
/// once. The next read is the ordinary closed-channel `Err`.
|
||||||
|
pub struct RemoteMonitor {
|
||||||
|
/// This registration's process-unique id — minted here, echoed by the
|
||||||
|
/// peer in its `Down` frame.
|
||||||
|
pub id: MonitorId,
|
||||||
|
/// The pid being monitored.
|
||||||
|
pub target: RemotePid<Erased>,
|
||||||
|
watch: Watch,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RemoteMonitor {
|
||||||
|
/// Block (cooperatively) for the notice.
|
||||||
|
pub fn recv(&self) -> Result<RemoteDown, RecvError> {
|
||||||
|
match &self.watch {
|
||||||
|
Watch::Local(m) => m.rx.recv().map(|d| RemoteDown {
|
||||||
|
pid: self.target.clone(),
|
||||||
|
reason: d.reason.into(),
|
||||||
|
}),
|
||||||
|
Watch::Remote(rx, st) => match rx.recv() {
|
||||||
|
Ok(d) => {
|
||||||
|
st.set(RemoteState::Done);
|
||||||
|
Ok(d)
|
||||||
|
}
|
||||||
|
Err(e) => self.closed(st).ok_or(e),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The notice if it has arrived; `Ok(None)` if not yet.
|
||||||
|
pub fn try_recv(&self) -> Result<Option<RemoteDown>, RecvError> {
|
||||||
|
match &self.watch {
|
||||||
|
Watch::Local(m) => m.rx.try_recv().map(|o| {
|
||||||
|
o.map(|d| RemoteDown {
|
||||||
|
pid: self.target.clone(),
|
||||||
|
reason: d.reason.into(),
|
||||||
|
})
|
||||||
|
}),
|
||||||
|
Watch::Remote(rx, st) => match rx.try_recv() {
|
||||||
|
Ok(Some(d)) => {
|
||||||
|
st.set(RemoteState::Done);
|
||||||
|
Ok(Some(d))
|
||||||
|
}
|
||||||
|
Ok(None) => Ok(None),
|
||||||
|
Err(e) => self.closed(st).map(Some).ok_or(e),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The channel closed. While `Live` — no notice yet, no cancel — that
|
||||||
|
/// is the connection having died with our registration unread, so
|
||||||
|
/// synthesize the one `Disconnected` and mark `Done`; otherwise closed
|
||||||
|
/// is just closed.
|
||||||
|
fn closed(&self, st: &Cell<RemoteState>) -> Option<RemoteDown> {
|
||||||
|
if st.get() != RemoteState::Live {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
st.set(RemoteState::Done);
|
||||||
|
Some(RemoteDown {
|
||||||
|
pid: self.target.clone(),
|
||||||
|
reason: RemoteDownReason::Disconnected,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The selectable arm: readiness means [`try_recv`](Self::try_recv)
|
||||||
|
/// will yield the notice.
|
||||||
|
pub fn arm(&self) -> &dyn Selectable {
|
||||||
|
match &self.watch {
|
||||||
|
Watch::Local(m) => &m.rx,
|
||||||
|
Watch::Remote(rx, _) => rx,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Debug for RemoteMonitor {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
f.debug_struct("RemoteMonitor")
|
||||||
|
.field("id", &self.id)
|
||||||
|
.field("target", &self.target)
|
||||||
|
.finish_non_exhaustive()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Monitor `target`, wherever it lives. Exactly one [`RemoteDown`] arrives:
|
||||||
|
///
|
||||||
|
/// - self-node pid ⇒ an ordinary local monitor underneath (same NoProc rule);
|
||||||
|
/// - no live connection to the pid's node ⇒ `Disconnected`, queued at once
|
||||||
|
/// (the remote analog of NoProc: nothing can be known);
|
||||||
|
/// - the pid's incarnation is not the node's current one ⇒ `NoProc`, queued
|
||||||
|
/// at once — the node is *known* to have restarted, so its actor is a
|
||||||
|
/// corpse, not a partition (RFC v2 §3);
|
||||||
|
/// - otherwise the connection actor registers the id and sends `Monitor`;
|
||||||
|
/// the peer answers with the true terminal reason on exit, or immediately
|
||||||
|
/// with the recorded reason for a corpse (`terminal_reason`, RFC §6) or
|
||||||
|
/// `NoProc` for a pid it never exposed and never shipped.
|
||||||
|
///
|
||||||
|
/// The connection dropping while the monitor is outstanding delivers
|
||||||
|
/// `Disconnected` (c13): the connection actor synthesizes it on teardown,
|
||||||
|
/// and the monitor's own read path backstops the case where the actor died
|
||||||
|
/// with the registration still unread. Must run inside [`run`](crate::run).
|
||||||
|
pub fn monitor_remote<A>(target: RemotePid<A>) -> RemoteMonitor {
|
||||||
|
if let Some(local) = target.local() {
|
||||||
|
let m = monitor(local);
|
||||||
|
return RemoteMonitor {
|
||||||
|
id: m.id,
|
||||||
|
target: target.erase(),
|
||||||
|
watch: Watch::Local(m),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
let target = target.erase();
|
||||||
|
let (id, route) = with_runtime(|inner| {
|
||||||
|
let id = inner.alloc_monitor_id();
|
||||||
|
let route = inner
|
||||||
|
.outbound
|
||||||
|
.lock()
|
||||||
|
.by_node
|
||||||
|
.get(&target.node)
|
||||||
|
.map(|r| (r.incarnation, r.monitors.clone()));
|
||||||
|
(id, route)
|
||||||
|
});
|
||||||
|
let (tx, rx) = channel::<RemoteDown>();
|
||||||
|
let immediate = match route {
|
||||||
|
None => Some(RemoteDownReason::Disconnected),
|
||||||
|
Some((current, _)) if current != target.incarnation => {
|
||||||
|
Some(RemoteDownReason::Local(crate::monitor::DownReason::NoProc))
|
||||||
|
}
|
||||||
|
Some((_, mon_tx)) => {
|
||||||
|
let cmd = MonCmd::Monitor {
|
||||||
|
id,
|
||||||
|
target: target.clone(),
|
||||||
|
tx: tx.clone(),
|
||||||
|
};
|
||||||
|
match mon_tx.send(cmd) {
|
||||||
|
Ok(()) => None,
|
||||||
|
Err(_) => Some(RemoteDownReason::Disconnected), // actor already gone
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
if let Some(reason) = immediate {
|
||||||
|
let _ = tx.send(RemoteDown {
|
||||||
|
pid: target.clone(),
|
||||||
|
reason,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
RemoteMonitor {
|
||||||
|
id,
|
||||||
|
target,
|
||||||
|
watch: Watch::Remote(rx, Cell::new(RemoteState::Live)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Cancel `m`. No future notice will be *sent* for it; a notice already in
|
||||||
|
/// flight from the peer is dropped on arrival, and one already sitting in
|
||||||
|
/// `m` is discarded when `m` is dropped (same contract as
|
||||||
|
/// [`demonitor`]). Unlike the local form this returns nothing: the
|
||||||
|
/// registration is owned by the connection actor, so whether the `Down`
|
||||||
|
/// beat the cancel is not local knowledge. Must run inside
|
||||||
|
/// [`run`](crate::run).
|
||||||
|
pub fn demonitor_remote(m: &RemoteMonitor) {
|
||||||
|
match &m.watch {
|
||||||
|
Watch::Local(local) => {
|
||||||
|
let _ = demonitor(local);
|
||||||
|
}
|
||||||
|
Watch::Remote(_, st) => {
|
||||||
|
// Cancel first: a channel closing after this is closed, not a
|
||||||
|
// Disconnected notice — the caller asked for silence.
|
||||||
|
st.set(RemoteState::Cancelled);
|
||||||
|
let mon_tx = with_runtime(|inner| {
|
||||||
|
inner
|
||||||
|
.outbound
|
||||||
|
.lock()
|
||||||
|
.by_node
|
||||||
|
.get(&m.target.node)
|
||||||
|
.map(|r| r.monitors.clone())
|
||||||
|
});
|
||||||
|
if let Some(mon_tx) = mon_tx {
|
||||||
|
let _ = mon_tx.send(MonCmd::Demonitor { id: m.id });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,298 @@
|
|||||||
|
//! RFC 010 c3 — transport abstraction for the **control** connection.
|
||||||
|
//!
|
||||||
|
//! Scope, per RFC 010 v2 §5 and D2:
|
||||||
|
//!
|
||||||
|
//! - A "connection" here is the *control* connection: the one carrying this
|
||||||
|
//! RFC's frame inventory ([`crate::cluster::envelope::Frame`]), whose
|
||||||
|
//! heartbeats feed failure detection. The trait deliberately says nothing
|
||||||
|
//! about how many connections a peer pair may hold — the jarred rkyv bulk
|
||||||
|
//! plane opens **additional per-peer connections** outside this trait, and
|
||||||
|
//! nothing here may foreclose that.
|
||||||
|
//! - Homogeneous smarm⇄smarm only. The BEAM membrane is *not* a transport
|
||||||
|
//! impl and the trait does not accommodate it (D2).
|
||||||
|
//! - Addresses are opaque, **pre-resolved** strings. Name resolution is a
|
||||||
|
//! single separate seam (roadmap c9); impls reject unresolved names rather
|
||||||
|
//! than resolving them.
|
||||||
|
//!
|
||||||
|
//! Blocking model: [`Conn`] calls block the caller. The TCP impl parks the
|
||||||
|
//! calling *actor* (fd readiness via the scheduler); the loopback impl blocks
|
||||||
|
//! the calling *OS thread* and is a test transport — do not drive it from a
|
||||||
|
//! scheduler thread.
|
||||||
|
//!
|
||||||
|
//! Framing is not part of the trait: [`FramedConn`] is the single shared
|
||||||
|
//! codec that turns any byte-stream [`Conn`] into a frame pipe, feeding
|
||||||
|
//! [`Frame::decode`]'s incremental contract. Impls never re-implement
|
||||||
|
//! framing, and the conformance suite exercises the same codec over every
|
||||||
|
//! impl.
|
||||||
|
|
||||||
|
use std::io;
|
||||||
|
|
||||||
|
use crate::cluster::envelope::{DecodeError, EncodeError, Frame};
|
||||||
|
|
||||||
|
pub mod loopback;
|
||||||
|
pub mod tcp;
|
||||||
|
|
||||||
|
/// An established control connection: a bidirectional byte stream.
|
||||||
|
pub trait Conn: Send {
|
||||||
|
/// Read at least one byte, blocking the caller until data is available,
|
||||||
|
/// EOF, or error. `Ok(0)` means EOF: the peer closed and all bytes it
|
||||||
|
/// wrote before closing have been consumed.
|
||||||
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize>;
|
||||||
|
|
||||||
|
/// Write the whole buffer, blocking the caller as needed.
|
||||||
|
fn write_all(&mut self, buf: &[u8]) -> io::Result<()>;
|
||||||
|
|
||||||
|
/// Close both directions. Idempotent. Bytes already written remain
|
||||||
|
/// readable at the peer, which then observes EOF; peer writes after this
|
||||||
|
/// fail.
|
||||||
|
fn close(&mut self);
|
||||||
|
|
||||||
|
/// Diagnostic label for logs only. Mesh identity comes from the
|
||||||
|
/// handshake (`Hello`/`HelloAck`), never from the transport.
|
||||||
|
fn peer_addr(&self) -> String;
|
||||||
|
|
||||||
|
/// Readiness as a [`select`](crate::select) arm, for transports backed by
|
||||||
|
/// a file descriptor. `Some` lets a driver wait on "this connection is
|
||||||
|
/// readable" alongside an ordinary command inbox in a single `select`, so
|
||||||
|
/// one actor can interleave reading with control messages without a
|
||||||
|
/// second thread. The default is `None`: a transport with no fd (the
|
||||||
|
/// in-memory loopback) cannot be selected on and must be driven another
|
||||||
|
/// way.
|
||||||
|
fn readable_arm(&self) -> Option<crate::scheduler::FdArm> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A bound listen point producing inbound [`Conn`]s.
|
||||||
|
pub trait Listener: Send {
|
||||||
|
/// Accept the next inbound connection, blocking the caller.
|
||||||
|
fn accept(&mut self) -> io::Result<Box<dyn Conn>>;
|
||||||
|
|
||||||
|
/// The concrete bound address, dialable as-is (e.g. the real port when
|
||||||
|
/// bound with port 0).
|
||||||
|
fn local_addr(&self) -> String;
|
||||||
|
|
||||||
|
/// Readiness as a [`select`](crate::select) arm, mirroring
|
||||||
|
/// [`Conn::readable_arm`]: `Some` lets an acceptor wait on "an inbound
|
||||||
|
/// connection is pending" alongside a command inbox in one `select`, so
|
||||||
|
/// it can be told to stop without a poll loop. Default `None` (the
|
||||||
|
/// loopback listener has no fd and must be driven synchronously).
|
||||||
|
fn readable_arm(&self) -> Option<crate::scheduler::FdArm> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A way of establishing control connections. Object-safe on purpose: the
|
||||||
|
/// connector and membership layers hold `&dyn Transport` / boxed conns
|
||||||
|
/// rather than growing a generic parameter.
|
||||||
|
pub trait Transport: Send + Sync {
|
||||||
|
/// Connect to a peer's listen address. Blocks the caller until
|
||||||
|
/// established or failed.
|
||||||
|
fn dial(&self, addr: &str) -> io::Result<Box<dyn Conn>>;
|
||||||
|
|
||||||
|
/// Bind a listen point.
|
||||||
|
fn listen(&self, addr: &str) -> io::Result<Box<dyn Listener>>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Debug for dyn Conn {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(f, "Conn({})", self.peer_addr())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Debug for dyn Listener {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(f, "Listener({})", self.local_addr())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Error surface of [`FramedConn::send`].
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum SendError {
|
||||||
|
/// The frame could not be encoded (e.g. a field over its wire limit).
|
||||||
|
Encode(EncodeError),
|
||||||
|
/// The transport failed mid-write.
|
||||||
|
Io(io::Error),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Display for SendError {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
SendError::Encode(e) => write!(f, "frame encode failed: {e:?}"),
|
||||||
|
SendError::Io(e) => write!(f, "transport write failed: {e}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for SendError {}
|
||||||
|
|
||||||
|
/// Error surface of [`FramedConn::recv`].
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum RecvError {
|
||||||
|
/// The byte stream is not a valid frame stream (bad tag, lying length,
|
||||||
|
/// oversized frame, …). The connection is unusable.
|
||||||
|
Corrupt(DecodeError),
|
||||||
|
/// The peer closed mid-frame: EOF arrived with a partial frame buffered.
|
||||||
|
/// Distinct from a clean close, which is `Ok(None)`.
|
||||||
|
TruncatedByPeer,
|
||||||
|
/// The transport failed mid-read.
|
||||||
|
Io(io::Error),
|
||||||
|
/// The deadline passed before a full frame arrived
|
||||||
|
/// ([`FramedConn::recv_deadline`] only; plain [`recv`](FramedConn::recv)
|
||||||
|
/// never returns this).
|
||||||
|
TimedOut,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Display for RecvError {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
RecvError::Corrupt(e) => write!(f, "frame stream corrupt: {e:?}"),
|
||||||
|
RecvError::TruncatedByPeer => write!(f, "peer closed mid-frame"),
|
||||||
|
RecvError::Io(e) => write!(f, "transport read failed: {e}"),
|
||||||
|
RecvError::TimedOut => write!(f, "deadline passed mid-receive"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for RecvError {}
|
||||||
|
|
||||||
|
/// How many bytes each blocking read asks the transport for.
|
||||||
|
const READ_CHUNK: usize = 8 * 1024;
|
||||||
|
|
||||||
|
/// The shared framed codec: one of these per control connection, owning the
|
||||||
|
/// [`Conn`] and the reassembly buffer. Frames may arrive split or coalesced
|
||||||
|
/// arbitrarily; [`recv`](FramedConn::recv) reassembles either way.
|
||||||
|
pub struct FramedConn {
|
||||||
|
conn: Box<dyn Conn>,
|
||||||
|
rbuf: Vec<u8>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FramedConn {
|
||||||
|
pub fn new(conn: Box<dyn Conn>) -> Self {
|
||||||
|
FramedConn {
|
||||||
|
conn,
|
||||||
|
rbuf: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Encode and write one frame.
|
||||||
|
pub fn send(&mut self, frame: &Frame) -> Result<(), SendError> {
|
||||||
|
let mut out = Vec::new();
|
||||||
|
frame.encode(&mut out).map_err(SendError::Encode)?;
|
||||||
|
self.conn.write_all(&out).map_err(SendError::Io)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Receive the next frame. `Ok(None)` is a clean close: EOF at a frame
|
||||||
|
/// boundary. EOF mid-frame is [`RecvError::TruncatedByPeer`].
|
||||||
|
pub fn recv(&mut self) -> Result<Option<Frame>, RecvError> {
|
||||||
|
loop {
|
||||||
|
match Frame::decode(&self.rbuf) {
|
||||||
|
Ok(Some((frame, consumed))) => {
|
||||||
|
self.rbuf.drain(..consumed);
|
||||||
|
return Ok(Some(frame));
|
||||||
|
}
|
||||||
|
Ok(None) => {}
|
||||||
|
Err(e) => return Err(RecvError::Corrupt(e)),
|
||||||
|
}
|
||||||
|
let mut chunk = [0u8; READ_CHUNK];
|
||||||
|
let n = self.conn.read(&mut chunk).map_err(RecvError::Io)?;
|
||||||
|
if n == 0 {
|
||||||
|
return if self.rbuf.is_empty() {
|
||||||
|
Ok(None)
|
||||||
|
} else {
|
||||||
|
Err(RecvError::TruncatedByPeer)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
self.rbuf.extend_from_slice(&chunk[..n]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Like [`recv`](FramedConn::recv), but gives up with
|
||||||
|
/// [`RecvError::TimedOut`] once `deadline` passes without a full frame.
|
||||||
|
/// The deadline is enforced between reads via the connection's fd arm
|
||||||
|
/// (so the caller must be an actor); a transport with no fd (loopback)
|
||||||
|
/// cannot be timed out and this degrades to a plain blocking `recv` —
|
||||||
|
/// the same caveat as liveness.
|
||||||
|
pub fn recv_deadline(
|
||||||
|
&mut self,
|
||||||
|
deadline: std::time::Instant,
|
||||||
|
) -> Result<Option<Frame>, RecvError> {
|
||||||
|
loop {
|
||||||
|
match Frame::decode(&self.rbuf) {
|
||||||
|
Ok(Some((frame, consumed))) => {
|
||||||
|
self.rbuf.drain(..consumed);
|
||||||
|
return Ok(Some(frame));
|
||||||
|
}
|
||||||
|
Ok(None) => {}
|
||||||
|
Err(e) => return Err(RecvError::Corrupt(e)),
|
||||||
|
}
|
||||||
|
if let Some(arm) = self.conn.readable_arm() {
|
||||||
|
let left = deadline.saturating_duration_since(std::time::Instant::now());
|
||||||
|
if left.is_zero() {
|
||||||
|
return Err(RecvError::TimedOut);
|
||||||
|
}
|
||||||
|
match crate::channel::try_select_timeout(&[&arm], left) {
|
||||||
|
Ok(Some(_)) => {}
|
||||||
|
Ok(None) => return Err(RecvError::TimedOut),
|
||||||
|
Err(e) => return Err(RecvError::Io(e)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let mut chunk = [0u8; READ_CHUNK];
|
||||||
|
let n = self.conn.read(&mut chunk).map_err(RecvError::Io)?;
|
||||||
|
if n == 0 {
|
||||||
|
return if self.rbuf.is_empty() {
|
||||||
|
Ok(None)
|
||||||
|
} else {
|
||||||
|
Err(RecvError::TruncatedByPeer)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
self.rbuf.extend_from_slice(&chunk[..n]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// One socket read, appended to the reassembly buffer. Returns the byte
|
||||||
|
/// count (`0` = EOF). For select-loop callers that were just told the fd
|
||||||
|
/// is readable: under the level-triggered IO thread exactly one read per
|
||||||
|
/// readable wake never blocks and never loses data — leftover socket
|
||||||
|
/// bytes re-signal on the next select, and complete frames already
|
||||||
|
/// reassembled are drained with [`next_buffered`](FramedConn::next_buffered).
|
||||||
|
/// (A plain [`recv`](FramedConn::recv) can block into the socket while
|
||||||
|
/// the buffer holds a partial frame, which a loop with deadlines to keep
|
||||||
|
/// cannot afford.)
|
||||||
|
pub fn read_once(&mut self) -> std::io::Result<usize> {
|
||||||
|
let mut chunk = [0u8; READ_CHUNK];
|
||||||
|
let n = self.conn.read(&mut chunk)?;
|
||||||
|
self.rbuf.extend_from_slice(&chunk[..n]);
|
||||||
|
Ok(n)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Decode the next complete frame already sitting in the reassembly
|
||||||
|
/// buffer, without touching the socket. `Ok(None)` means the buffer
|
||||||
|
/// holds no complete frame (empty, or a partial awaiting more bytes).
|
||||||
|
pub fn next_buffered(&mut self) -> Result<Option<Frame>, DecodeError> {
|
||||||
|
match Frame::decode(&self.rbuf) {
|
||||||
|
Ok(Some((frame, consumed))) => {
|
||||||
|
self.rbuf.drain(..consumed);
|
||||||
|
Ok(Some(frame))
|
||||||
|
}
|
||||||
|
Ok(None) => Ok(None),
|
||||||
|
Err(e) => Err(e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Close the underlying connection (idempotent, see [`Conn::close`]).
|
||||||
|
pub fn close(&mut self) {
|
||||||
|
self.conn.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Diagnostic label of the underlying connection.
|
||||||
|
pub fn peer_addr(&self) -> String {
|
||||||
|
self.conn.peer_addr()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The underlying connection's readiness arm, if it is fd-backed (see
|
||||||
|
/// [`Conn::readable_arm`]).
|
||||||
|
pub fn readable_arm(&self) -> Option<crate::scheduler::FdArm> {
|
||||||
|
self.conn.readable_arm()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,274 @@
|
|||||||
|
//! In-memory loopback transport — a shipped **test** transport.
|
||||||
|
//!
|
||||||
|
//! Lets Phases 2–4 exercise protocol logic (connector, membership,
|
||||||
|
//! monitors) through the real transport trait and the real framed codec
|
||||||
|
//! without sockets or timing flake.
|
||||||
|
//!
|
||||||
|
//! Blocking model: calls block the **OS thread** on a condvar. That is the
|
||||||
|
//! right shape for plain `#[test]`s driving protocol state machines; it is
|
||||||
|
//! the wrong shape for scheduler threads. Do not drive a loopback conn from
|
||||||
|
//! inside an actor — use the TCP impl there.
|
||||||
|
//!
|
||||||
|
//! Semantics mirror TCP shutdown where it matters for the codec: bytes
|
||||||
|
//! written before `close` remain readable at the peer, which then sees EOF;
|
||||||
|
//! writes toward a closed peer fail with `BrokenPipe`. Write buffers are
|
||||||
|
//! unbounded, so writes never block — backpressure is not simulated.
|
||||||
|
|
||||||
|
use std::collections::{HashMap, VecDeque};
|
||||||
|
use std::io;
|
||||||
|
use std::sync::{Arc, Condvar, Mutex, MutexGuard};
|
||||||
|
|
||||||
|
use super::{Conn, Listener, Transport};
|
||||||
|
|
||||||
|
/// Poison-tolerant lock: a panicked holder in a *test* transport must not
|
||||||
|
/// cascade; the byte-queue state stays consistent under every early return.
|
||||||
|
fn lock<T>(m: &Mutex<T>) -> MutexGuard<'_, T> {
|
||||||
|
match m.lock() {
|
||||||
|
Ok(g) => g,
|
||||||
|
Err(poisoned) => poisoned.into_inner(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// One direction of a duplex: a byte queue with close flags for both ends
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
struct PipeState {
|
||||||
|
bytes: VecDeque<u8>,
|
||||||
|
/// The writing end closed: readers drain remaining bytes, then EOF.
|
||||||
|
write_closed: bool,
|
||||||
|
/// The reading end closed: writers fail with `BrokenPipe`.
|
||||||
|
read_closed: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
struct Pipe {
|
||||||
|
state: Mutex<PipeState>,
|
||||||
|
cv: Condvar,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Pipe {
|
||||||
|
fn write_all(&self, buf: &[u8]) -> io::Result<()> {
|
||||||
|
let mut st = lock(&self.state);
|
||||||
|
if st.write_closed {
|
||||||
|
return Err(io::Error::new(
|
||||||
|
io::ErrorKind::NotConnected,
|
||||||
|
"loopback conn closed locally",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
if st.read_closed {
|
||||||
|
return Err(io::Error::new(
|
||||||
|
io::ErrorKind::BrokenPipe,
|
||||||
|
"loopback peer closed",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
st.bytes.extend(buf);
|
||||||
|
self.cv.notify_all();
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
|
||||||
|
if buf.is_empty() {
|
||||||
|
return Ok(0);
|
||||||
|
}
|
||||||
|
let mut st = lock(&self.state);
|
||||||
|
loop {
|
||||||
|
if !st.bytes.is_empty() {
|
||||||
|
let n = st.bytes.len().min(buf.len());
|
||||||
|
for (slot, byte) in buf.iter_mut().zip(st.bytes.drain(..n)) {
|
||||||
|
*slot = byte;
|
||||||
|
}
|
||||||
|
return Ok(n);
|
||||||
|
}
|
||||||
|
if st.write_closed || st.read_closed {
|
||||||
|
return Ok(0); // EOF: peer closed, or our own end closed.
|
||||||
|
}
|
||||||
|
st = match self.cv.wait(st) {
|
||||||
|
Ok(g) => g,
|
||||||
|
Err(poisoned) => poisoned.into_inner(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Close from the writer side: remaining bytes stay readable, then EOF.
|
||||||
|
fn close_write(&self) {
|
||||||
|
lock(&self.state).write_closed = true;
|
||||||
|
self.cv.notify_all();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Close from the reader side: peer writes fail from now on.
|
||||||
|
fn close_read(&self) {
|
||||||
|
lock(&self.state).read_closed = true;
|
||||||
|
self.cv.notify_all();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Conn: two pipes, one per direction
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
/// One end of an established loopback connection.
|
||||||
|
pub struct LoopbackConn {
|
||||||
|
tx: Arc<Pipe>,
|
||||||
|
rx: Arc<Pipe>,
|
||||||
|
peer: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Conn for LoopbackConn {
|
||||||
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||||
|
self.rx.read(buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
|
||||||
|
self.tx.write_all(buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn close(&mut self) {
|
||||||
|
self.tx.close_write();
|
||||||
|
self.rx.close_read();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn peer_addr(&self) -> String {
|
||||||
|
self.peer.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for LoopbackConn {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
self.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn conn_pair(listen_addr: &str, conn_no: u64) -> (LoopbackConn, LoopbackConn) {
|
||||||
|
let a_to_b = Arc::new(Pipe::default());
|
||||||
|
let b_to_a = Arc::new(Pipe::default());
|
||||||
|
let dialer = LoopbackConn {
|
||||||
|
tx: a_to_b.clone(),
|
||||||
|
rx: b_to_a.clone(),
|
||||||
|
peer: listen_addr.to_string(),
|
||||||
|
};
|
||||||
|
let accepted = LoopbackConn {
|
||||||
|
tx: b_to_a,
|
||||||
|
rx: a_to_b,
|
||||||
|
peer: format!("{listen_addr}#dialer-{conn_no}"),
|
||||||
|
};
|
||||||
|
(dialer, accepted)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Listener + registry
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
struct AcceptState {
|
||||||
|
pending: VecDeque<LoopbackConn>,
|
||||||
|
closed: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
struct AcceptQueue {
|
||||||
|
state: Mutex<AcceptState>,
|
||||||
|
cv: Condvar,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A bound loopback listen point.
|
||||||
|
pub struct LoopbackListener {
|
||||||
|
addr: String,
|
||||||
|
queue: Arc<AcceptQueue>,
|
||||||
|
registry: Arc<Mutex<Registry>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Listener for LoopbackListener {
|
||||||
|
fn accept(&mut self) -> io::Result<Box<dyn Conn>> {
|
||||||
|
let mut st = lock(&self.queue.state);
|
||||||
|
loop {
|
||||||
|
if let Some(conn) = st.pending.pop_front() {
|
||||||
|
return Ok(Box::new(conn));
|
||||||
|
}
|
||||||
|
if st.closed {
|
||||||
|
return Err(io::Error::new(
|
||||||
|
io::ErrorKind::NotConnected,
|
||||||
|
"loopback listener closed",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
st = match self.queue.cv.wait(st) {
|
||||||
|
Ok(g) => g,
|
||||||
|
Err(poisoned) => poisoned.into_inner(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn local_addr(&self) -> String {
|
||||||
|
self.addr.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for LoopbackListener {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
lock(&self.registry).listeners.remove(&self.addr);
|
||||||
|
let mut st = lock(&self.queue.state);
|
||||||
|
st.closed = true;
|
||||||
|
self.queue.cv.notify_all();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
struct Registry {
|
||||||
|
listeners: HashMap<String, Arc<AcceptQueue>>,
|
||||||
|
dial_count: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The loopback transport. Addresses are arbitrary strings scoped to one
|
||||||
|
/// transport instance; distinct instances never see each other's listeners.
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct LoopbackTransport {
|
||||||
|
registry: Arc<Mutex<Registry>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Transport for LoopbackTransport {
|
||||||
|
fn dial(&self, addr: &str) -> io::Result<Box<dyn Conn>> {
|
||||||
|
let (queue, conn_no) = {
|
||||||
|
let mut reg = lock(&self.registry);
|
||||||
|
reg.dial_count += 1;
|
||||||
|
let no = reg.dial_count;
|
||||||
|
match reg.listeners.get(addr) {
|
||||||
|
Some(q) => (q.clone(), no),
|
||||||
|
None => {
|
||||||
|
return Err(io::Error::new(
|
||||||
|
io::ErrorKind::ConnectionRefused,
|
||||||
|
format!("no loopback listener at {addr:?}"),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let (dialer, accepted) = conn_pair(addr, conn_no);
|
||||||
|
let mut st = lock(&queue.state);
|
||||||
|
if st.closed {
|
||||||
|
return Err(io::Error::new(
|
||||||
|
io::ErrorKind::ConnectionRefused,
|
||||||
|
format!("loopback listener at {addr:?} closed"),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
st.pending.push_back(accepted);
|
||||||
|
queue.cv.notify_all();
|
||||||
|
Ok(Box::new(dialer))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn listen(&self, addr: &str) -> io::Result<Box<dyn Listener>> {
|
||||||
|
let queue = Arc::new(AcceptQueue::default());
|
||||||
|
let mut reg = lock(&self.registry);
|
||||||
|
if reg.listeners.contains_key(addr) {
|
||||||
|
return Err(io::Error::new(
|
||||||
|
io::ErrorKind::AddrInUse,
|
||||||
|
format!("loopback listener already bound at {addr:?}"),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
reg.listeners.insert(addr.to_string(), queue.clone());
|
||||||
|
Ok(Box::new(LoopbackListener {
|
||||||
|
addr: addr.to_string(),
|
||||||
|
queue,
|
||||||
|
registry: self.registry.clone(),
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,285 @@
|
|||||||
|
//! TCP transport — the production control-plane transport.
|
||||||
|
//!
|
||||||
|
//! Blocking model: every blocking point parks the **calling actor** on fd
|
||||||
|
//! readiness ([`crate::scheduler::wait_readable`] / `wait_writable`); the
|
||||||
|
//! scheduler thread is never blocked. All conn/listener methods must
|
||||||
|
//! therefore run inside an actor. `listen` itself only binds (no waiting)
|
||||||
|
//! and is callable anywhere.
|
||||||
|
//!
|
||||||
|
//! Addresses are pre-resolved `ip:port` strings (`SocketAddr` syntax, IPv4
|
||||||
|
//! or IPv6). Hostnames are rejected with `InvalidInput`: name resolution is
|
||||||
|
//! the single c9 seam, not something each transport does on the side.
|
||||||
|
//!
|
||||||
|
//! Writes use `send(2)` with `MSG_NOSIGNAL` — a peer reset must surface as
|
||||||
|
//! `BrokenPipe`/`ConnectionReset`, not `SIGPIPE`.
|
||||||
|
|
||||||
|
use std::io;
|
||||||
|
use std::net::{SocketAddr, TcpListener as StdListener, TcpStream};
|
||||||
|
use std::os::fd::{AsRawFd, RawFd};
|
||||||
|
|
||||||
|
use crate::scheduler::{wait_readable, wait_writable};
|
||||||
|
|
||||||
|
use super::{Conn, Listener, Transport};
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// sockaddr plumbing
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
/// A `sockaddr_in`/`sockaddr_in6` built from a parsed `SocketAddr`, plus its
|
||||||
|
/// length, ready for `connect(2)`.
|
||||||
|
union SockAddrUnion {
|
||||||
|
v4: libc::sockaddr_in,
|
||||||
|
v6: libc::sockaddr_in6,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_sockaddr(sa: &SocketAddr) -> (SockAddrUnion, libc::socklen_t) {
|
||||||
|
match sa {
|
||||||
|
SocketAddr::V4(v4) => {
|
||||||
|
let raw = libc::sockaddr_in {
|
||||||
|
sin_family: libc::AF_INET as libc::sa_family_t,
|
||||||
|
sin_port: v4.port().to_be(),
|
||||||
|
sin_addr: libc::in_addr {
|
||||||
|
s_addr: u32::from_be_bytes(v4.ip().octets()).to_be(),
|
||||||
|
},
|
||||||
|
sin_zero: [0; 8],
|
||||||
|
};
|
||||||
|
(
|
||||||
|
SockAddrUnion { v4: raw },
|
||||||
|
std::mem::size_of::<libc::sockaddr_in>() as libc::socklen_t,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
SocketAddr::V6(v6) => {
|
||||||
|
let raw = libc::sockaddr_in6 {
|
||||||
|
sin6_family: libc::AF_INET6 as libc::sa_family_t,
|
||||||
|
sin6_port: v6.port().to_be(),
|
||||||
|
sin6_flowinfo: v6.flowinfo(),
|
||||||
|
sin6_addr: libc::in6_addr {
|
||||||
|
s6_addr: v6.ip().octets(),
|
||||||
|
},
|
||||||
|
sin6_scope_id: v6.scope_id(),
|
||||||
|
};
|
||||||
|
(
|
||||||
|
SockAddrUnion { v6: raw },
|
||||||
|
std::mem::size_of::<libc::sockaddr_in6>() as libc::socklen_t,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_addr(addr: &str) -> io::Result<SocketAddr> {
|
||||||
|
addr.parse().map_err(|_| {
|
||||||
|
io::Error::new(
|
||||||
|
io::ErrorKind::InvalidInput,
|
||||||
|
format!("{addr:?} is not a resolved ip:port — resolution is the c9 seam"),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn so_error(fd: RawFd) -> io::Result<()> {
|
||||||
|
let mut err: libc::c_int = 0;
|
||||||
|
let mut len = std::mem::size_of::<libc::c_int>() as libc::socklen_t;
|
||||||
|
let rc = unsafe {
|
||||||
|
libc::getsockopt(
|
||||||
|
fd,
|
||||||
|
libc::SOL_SOCKET,
|
||||||
|
libc::SO_ERROR,
|
||||||
|
(&mut err) as *mut _ as *mut libc::c_void,
|
||||||
|
&mut len,
|
||||||
|
)
|
||||||
|
};
|
||||||
|
if rc != 0 {
|
||||||
|
return Err(io::Error::last_os_error());
|
||||||
|
}
|
||||||
|
if err != 0 {
|
||||||
|
return Err(io::Error::from_raw_os_error(err));
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Conn
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
/// One established TCP control connection. Owns the socket; drop closes it.
|
||||||
|
pub struct TcpConn {
|
||||||
|
stream: TcpStream,
|
||||||
|
closed: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TcpConn {
|
||||||
|
fn fd(&self) -> RawFd {
|
||||||
|
self.stream.as_raw_fd()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Conn for TcpConn {
|
||||||
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||||
|
if self.closed {
|
||||||
|
return Ok(0);
|
||||||
|
}
|
||||||
|
if buf.is_empty() {
|
||||||
|
return Ok(0);
|
||||||
|
}
|
||||||
|
loop {
|
||||||
|
wait_readable(self.fd())?;
|
||||||
|
let n = unsafe { libc::read(self.fd(), buf.as_mut_ptr() as *mut _, buf.len()) };
|
||||||
|
if n >= 0 {
|
||||||
|
return Ok(n as usize);
|
||||||
|
}
|
||||||
|
let e = io::Error::last_os_error();
|
||||||
|
match e.kind() {
|
||||||
|
// Spurious readiness or signal: park again.
|
||||||
|
io::ErrorKind::WouldBlock | io::ErrorKind::Interrupted => continue,
|
||||||
|
_ => return Err(e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write_all(&mut self, mut buf: &[u8]) -> io::Result<()> {
|
||||||
|
if self.closed {
|
||||||
|
return Err(io::Error::new(
|
||||||
|
io::ErrorKind::NotConnected,
|
||||||
|
"tcp conn closed locally",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
while !buf.is_empty() {
|
||||||
|
wait_writable(self.fd())?;
|
||||||
|
let n = unsafe {
|
||||||
|
libc::send(
|
||||||
|
self.fd(),
|
||||||
|
buf.as_ptr() as *const _,
|
||||||
|
buf.len(),
|
||||||
|
libc::MSG_NOSIGNAL,
|
||||||
|
)
|
||||||
|
};
|
||||||
|
if n >= 0 {
|
||||||
|
buf = &buf[n as usize..];
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let e = io::Error::last_os_error();
|
||||||
|
match e.kind() {
|
||||||
|
io::ErrorKind::WouldBlock | io::ErrorKind::Interrupted => continue,
|
||||||
|
_ => return Err(e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn close(&mut self) {
|
||||||
|
if !self.closed {
|
||||||
|
self.closed = true;
|
||||||
|
// Best-effort: the peer sees EOF after draining. The fd itself
|
||||||
|
// is released when the owning stream drops.
|
||||||
|
let _ = self.stream.shutdown(std::net::Shutdown::Both);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn peer_addr(&self) -> String {
|
||||||
|
match self.stream.peer_addr() {
|
||||||
|
Ok(sa) => sa.to_string(),
|
||||||
|
Err(_) => "<disconnected>".to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn readable_arm(&self) -> Option<crate::scheduler::FdArm> {
|
||||||
|
Some(crate::scheduler::FdArm::readable(self.fd()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Listener
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
/// A bound TCP listen point (non-blocking socket; accept parks the actor).
|
||||||
|
pub struct TcpListener {
|
||||||
|
inner: StdListener,
|
||||||
|
local: SocketAddr,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Listener for TcpListener {
|
||||||
|
fn readable_arm(&self) -> Option<crate::scheduler::FdArm> {
|
||||||
|
Some(crate::scheduler::FdArm::readable(self.inner.as_raw_fd()))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn accept(&mut self) -> io::Result<Box<dyn Conn>> {
|
||||||
|
loop {
|
||||||
|
wait_readable(self.inner.as_raw_fd())?;
|
||||||
|
match self.inner.accept() {
|
||||||
|
Ok((stream, _peer)) => {
|
||||||
|
stream.set_nonblocking(true)?;
|
||||||
|
return Ok(Box::new(TcpConn {
|
||||||
|
stream,
|
||||||
|
closed: false,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
Err(e)
|
||||||
|
if e.kind() == io::ErrorKind::WouldBlock
|
||||||
|
|| e.kind() == io::ErrorKind::Interrupted =>
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
Err(e) => return Err(e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn local_addr(&self) -> String {
|
||||||
|
self.local.to_string()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Transport
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
/// The TCP transport. Stateless; every call stands alone.
|
||||||
|
pub struct TcpTransport;
|
||||||
|
|
||||||
|
impl Transport for TcpTransport {
|
||||||
|
fn dial(&self, addr: &str) -> io::Result<Box<dyn Conn>> {
|
||||||
|
let sa = parse_addr(addr)?;
|
||||||
|
let family = match sa {
|
||||||
|
SocketAddr::V4(_) => libc::AF_INET,
|
||||||
|
SocketAddr::V6(_) => libc::AF_INET6,
|
||||||
|
};
|
||||||
|
let fd = unsafe {
|
||||||
|
libc::socket(
|
||||||
|
family,
|
||||||
|
libc::SOCK_STREAM | libc::SOCK_NONBLOCK | libc::SOCK_CLOEXEC,
|
||||||
|
0,
|
||||||
|
)
|
||||||
|
};
|
||||||
|
if fd < 0 {
|
||||||
|
return Err(io::Error::last_os_error());
|
||||||
|
}
|
||||||
|
// From here the fd is owned by `stream`; any early return drops it.
|
||||||
|
let stream = unsafe {
|
||||||
|
use std::os::fd::FromRawFd;
|
||||||
|
TcpStream::from_raw_fd(fd)
|
||||||
|
};
|
||||||
|
let (raw, len) = to_sockaddr(&sa);
|
||||||
|
let rc = unsafe { libc::connect(fd, (&raw) as *const _ as *const libc::sockaddr, len) };
|
||||||
|
if rc != 0 {
|
||||||
|
let e = io::Error::last_os_error();
|
||||||
|
if e.raw_os_error() != Some(libc::EINPROGRESS) {
|
||||||
|
return Err(e);
|
||||||
|
}
|
||||||
|
// Connect in flight: park until the socket is writable, then the
|
||||||
|
// verdict is in SO_ERROR.
|
||||||
|
wait_writable(fd)?;
|
||||||
|
so_error(fd)?;
|
||||||
|
}
|
||||||
|
Ok(Box::new(TcpConn {
|
||||||
|
stream,
|
||||||
|
closed: false,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn listen(&self, addr: &str) -> io::Result<Box<dyn Listener>> {
|
||||||
|
let sa = parse_addr(addr)?;
|
||||||
|
let inner = StdListener::bind(sa)?;
|
||||||
|
inner.set_nonblocking(true)?;
|
||||||
|
let local = inner.local_addr()?;
|
||||||
|
Ok(Box::new(TcpListener { inner, local }))
|
||||||
|
}
|
||||||
|
}
|
||||||
+115
-34
@@ -4,22 +4,66 @@
|
|||||||
//! actor running on its own mmap'd stack. The compiler cannot do this; the
|
//! actor running on its own mmap'd stack. The compiler cannot do this; the
|
||||||
//! whole point of `#[unsafe(naked)]` is that we control every instruction.
|
//! whole point of `#[unsafe(naked)]` is that we control every instruction.
|
||||||
//!
|
//!
|
||||||
//! `SCHEDULER_SP` and `ACTOR_SP` are thread-locals holding each side's saved
|
//! The actor's stack pointer travels in registers: `switch_to_actor` takes
|
||||||
//! stack pointer. `init_actor_stack` builds the initial stack so that the
|
//! the target sp as its argument and returns the sp the actor saved when it
|
||||||
//! first `switch_to_actor` lands inside the entry function with `rsp % 16 == 8`
|
//! next yielded (handed back in `rax` by `switch_to_scheduler`'s shim). Only
|
||||||
//! (the x86-64 ABI requirement at function entry).
|
//! the *scheduler* sp lives in a thread-local — the yielding actor sits at
|
||||||
|
//! arbitrary call depth with no argument channel back to the scheduler, so
|
||||||
|
//! TLS is the one place it can find the way home. `init_actor_stack` builds
|
||||||
|
//! the initial stack so that the first `switch_to_actor` lands inside the
|
||||||
|
//! entry function with `rsp % 16 == 8` (the x86-64 ABI requirement at
|
||||||
|
//! function entry).
|
||||||
|
//!
|
||||||
|
//! # Thread-locals and migration (read before touching any `thread_local!`)
|
||||||
|
//!
|
||||||
|
//! An actor may park on scheduler thread A and be resumed on thread B. LLVM
|
||||||
|
//! treats the address of a thread-local as a loop-invariant, side-effect-free
|
||||||
|
//! value: it computes `%fs:0 + offset` once per function and happily keeps it
|
||||||
|
//! in a callee-saved register across calls — including across
|
||||||
|
//! `switch_to_scheduler`. Any function that touches a scheduler thread-local
|
||||||
|
//! both before and after a switch (or that gets *inlined* into one that does)
|
||||||
|
//! therefore reads and writes the *old thread's* TLS after migration. Nothing
|
||||||
|
//! at the switch can prevent this: it is not a memory clobber problem, the
|
||||||
|
//! address is not memory-derived in LLVM's model. Under thin LTO the code
|
||||||
|
//! happened to use the local-exec model (`%fs:imm` operands, nothing to
|
||||||
|
//! cache) so it worked by luck; a plain `cargo build --release` of a
|
||||||
|
//! downstream crate broke multi-thread runs (`ACTOR_DONE` written to the wrong
|
||||||
|
//! thread → "scheduler resumed a done actor").
|
||||||
|
//!
|
||||||
|
//! Rule: every function that touches a thread-local and can execute on an
|
||||||
|
//! actor stack must be `#[inline(never)]` and call [`tls_fence`] first, so the
|
||||||
|
//! TLS base is recomputed inside a callee that cannot be inlined into a frame
|
||||||
|
//! spanning a switch, and so LLVM cannot infer the accessor is pure and merge
|
||||||
|
//! two calls to it. Scheduler-side code (`schedule_loop` and what it calls
|
||||||
|
//! before/after `switch_to_actor`) never migrates and is exempt. Do not return
|
||||||
|
//! `&Cell`/pointers into TLS from these accessors; return values.
|
||||||
|
//! `cargo test --profile reltest` (no LTO) is the regression oracle.
|
||||||
|
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
|
|
||||||
thread_local! {
|
/// Compiler barrier for TLS accessors, see the module docs. Emits no code; a
|
||||||
static SCHEDULER_SP: Cell<usize> = const { Cell::new(0) };
|
/// side-effecting empty asm keeps LLVM from marking the enclosing
|
||||||
static ACTOR_SP: Cell<usize> = const { Cell::new(0) };
|
/// `#[inline(never)]` accessor `memory(none)` and merging calls to it.
|
||||||
|
#[inline(always)]
|
||||||
|
pub(crate) fn tls_fence() {
|
||||||
|
// SAFETY: empty asm, no operands, no stack, no flags.
|
||||||
|
unsafe { core::arch::asm!("", options(nostack, preserves_flags)) }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_scheduler_sp() -> usize { SCHEDULER_SP.with(|c| c.get()) }
|
thread_local! {
|
||||||
fn set_scheduler_sp(v: usize) { SCHEDULER_SP.with(|c| c.set(v)) }
|
static SCHEDULER_SP: Cell<usize> = const { Cell::new(0) };
|
||||||
pub fn get_actor_sp() -> usize { ACTOR_SP.with(|c| c.get()) }
|
}
|
||||||
pub fn set_actor_sp(v: usize) { ACTOR_SP.with(|c| c.set(v)) }
|
|
||||||
|
#[inline(never)]
|
||||||
|
fn get_scheduler_sp() -> usize {
|
||||||
|
tls_fence();
|
||||||
|
SCHEDULER_SP.with(|c| c.get())
|
||||||
|
}
|
||||||
|
#[inline(never)]
|
||||||
|
fn set_scheduler_sp(v: usize) {
|
||||||
|
tls_fence();
|
||||||
|
SCHEDULER_SP.with(|c| c.set(v))
|
||||||
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// Initial stack layout
|
// Initial stack layout
|
||||||
@@ -49,13 +93,20 @@ pub fn set_actor_sp(v: usize) { ACTOR_SP.with(|c| c.set(v)) }
|
|||||||
pub fn init_actor_stack(top: *mut u8, entry: extern "C-unwind" fn()) -> usize {
|
pub fn init_actor_stack(top: *mut u8, entry: extern "C-unwind" fn()) -> usize {
|
||||||
unsafe {
|
unsafe {
|
||||||
let mut sp = (top as usize & !15) - 8;
|
let mut sp = (top as usize & !15) - 8;
|
||||||
sp -= 8; (sp as *mut usize).write(entry as usize); // ret target
|
sp -= 8;
|
||||||
sp -= 8; (sp as *mut usize).write(0); // rbx
|
(sp as *mut usize).write(entry as usize); // ret target
|
||||||
sp -= 8; (sp as *mut usize).write(0); // rbp
|
sp -= 8;
|
||||||
sp -= 8; (sp as *mut usize).write(0); // r12
|
(sp as *mut usize).write(0); // rbx
|
||||||
sp -= 8; (sp as *mut usize).write(0); // r13
|
sp -= 8;
|
||||||
sp -= 8; (sp as *mut usize).write(0); // r14
|
(sp as *mut usize).write(0); // rbp
|
||||||
sp -= 8; (sp as *mut usize).write(0); // r15
|
sp -= 8;
|
||||||
|
(sp as *mut usize).write(0); // r12
|
||||||
|
sp -= 8;
|
||||||
|
(sp as *mut usize).write(0); // r13
|
||||||
|
sp -= 8;
|
||||||
|
(sp as *mut usize).write(0); // r14
|
||||||
|
sp -= 8;
|
||||||
|
(sp as *mut usize).write(0); // r15
|
||||||
sp
|
sp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -63,12 +114,24 @@ pub fn init_actor_stack(top: *mut u8, entry: extern "C-unwind" fn()) -> usize {
|
|||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// Context switch shims
|
// Context switch shims
|
||||||
//
|
//
|
||||||
// Each shim:
|
// switch_to_actor_asm (rdi = target actor sp, returns rax = the sp the actor
|
||||||
// 1. Pushes the six callee-saved integer registers.
|
// saved when it next yielded):
|
||||||
// 2. Snaps rsp into rdi and calls the Rust helper that stores it.
|
// 1. Pushes the six callee-saved integer registers (scheduler side).
|
||||||
// 3. Calls the Rust helper that returns the *other* side's saved rsp.
|
// 2. Stashes the target sp in rbx — free scratch: the register's live
|
||||||
// 4. Moves that into rsp.
|
// value is on the stack we just pushed to, and the pops below load the
|
||||||
// 5. Pops the six registers and rets.
|
// *other* side's values anyway — then snaps rsp into rdi and calls the
|
||||||
|
// Rust helper that stores it in SCHEDULER_SP.
|
||||||
|
// 3. Installs the target sp and pops the actor's registers; `ret` lands
|
||||||
|
// where the actor yielded (or in `entry` on first resume).
|
||||||
|
//
|
||||||
|
// switch_to_scheduler_asm (no args; its "return value" materialises on the
|
||||||
|
// OTHER stack, as switch_to_actor's rax):
|
||||||
|
// 1. Pushes the six callee-saved integer registers (actor side).
|
||||||
|
// 2. Stashes its own rsp in rbx (same free-scratch argument), asks the
|
||||||
|
// Rust helper for SCHEDULER_SP.
|
||||||
|
// 3. Installs the scheduler sp, moves the saved actor sp into rax, pops
|
||||||
|
// the scheduler's registers and rets — completing the scheduler's
|
||||||
|
// `switch_to_actor(sp)` call with the actor's new sp as its result.
|
||||||
//
|
//
|
||||||
// XMM registers are NOT saved here. We rely on every yield happening through
|
// XMM registers are NOT saved here. We rely on every yield happening through
|
||||||
// a Rust call site, which means the compiler has spilled any live XMM state
|
// a Rust call site, which means the compiler has spilled any live XMM state
|
||||||
@@ -79,36 +142,54 @@ pub fn init_actor_stack(top: *mut u8, entry: extern "C-unwind" fn()) -> usize {
|
|||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
#[unsafe(naked)]
|
#[unsafe(naked)]
|
||||||
unsafe extern "C" fn switch_to_actor_asm() {
|
unsafe extern "C" fn switch_to_actor_asm(actor_sp: usize) -> usize {
|
||||||
core::arch::naked_asm!(
|
core::arch::naked_asm!(
|
||||||
"push rbx", "push rbp", "push r12", "push r13", "push r14", "push r15",
|
"push rbx", "push rbp", "push r12", "push r13", "push r14", "push r15",
|
||||||
|
"mov rbx, rdi",
|
||||||
"mov rdi, rsp",
|
"mov rdi, rsp",
|
||||||
"call {set_sched_sp}",
|
"call {set_sched_sp}",
|
||||||
"call {get_actor_sp}",
|
"mov rsp, rbx",
|
||||||
"mov rsp, rax",
|
|
||||||
"pop r15", "pop r14", "pop r13", "pop r12", "pop rbp", "pop rbx",
|
"pop r15", "pop r14", "pop r13", "pop r12", "pop rbp", "pop rbx",
|
||||||
"ret",
|
"ret",
|
||||||
set_sched_sp = sym set_scheduler_sp,
|
set_sched_sp = sym set_scheduler_sp,
|
||||||
get_actor_sp = sym get_actor_sp,
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Resume the actor whose sp is in `ACTOR_SP`. Returns when the actor yields.
|
/// Resume the actor whose saved stack pointer is `actor_sp`. Returns when the
|
||||||
pub unsafe fn switch_to_actor() {
|
/// actor yields, with the stack pointer the actor saved as it did — store it
|
||||||
unsafe { switch_to_actor_asm() };
|
/// back into the slot for the next resume.
|
||||||
|
///
|
||||||
|
/// # Safety
|
||||||
|
///
|
||||||
|
/// `actor_sp` must be a valid saved actor stack pointer — either from
|
||||||
|
/// `init_actor_stack` (first resume) or the value a prior `switch_to_actor`
|
||||||
|
/// returned for this actor (subsequent resumes). Resuming with a stale or
|
||||||
|
/// forged sp transfers control to an arbitrary address. Must not be called
|
||||||
|
/// from within an actor (only the scheduler side may resume).
|
||||||
|
pub unsafe fn switch_to_actor(actor_sp: usize) -> usize {
|
||||||
|
unsafe { switch_to_actor_asm(actor_sp) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Yield from the running actor back to its scheduler thread. Returns when the
|
||||||
|
/// actor is next resumed via [`switch_to_actor`].
|
||||||
|
///
|
||||||
|
/// # Safety
|
||||||
|
///
|
||||||
|
/// The caller must be running on an actor stack that was entered through
|
||||||
|
/// [`switch_to_actor`], so that `SCHEDULER_SP` holds the live saved stack
|
||||||
|
/// pointer of the scheduler side. Calling this from the scheduler thread, or
|
||||||
|
/// before any actor has been resumed, transfers control to an arbitrary
|
||||||
|
/// address.
|
||||||
#[unsafe(naked)]
|
#[unsafe(naked)]
|
||||||
pub unsafe extern "C" fn switch_to_scheduler() {
|
pub unsafe extern "C" fn switch_to_scheduler() {
|
||||||
core::arch::naked_asm!(
|
core::arch::naked_asm!(
|
||||||
"push rbx", "push rbp", "push r12", "push r13", "push r14", "push r15",
|
"push rbx", "push rbp", "push r12", "push r13", "push r14", "push r15",
|
||||||
"mov rdi, rsp",
|
"mov rbx, rsp",
|
||||||
"call {set_actor_sp}",
|
|
||||||
"call {get_sched_sp}",
|
"call {get_sched_sp}",
|
||||||
"mov rsp, rax",
|
"mov rsp, rax",
|
||||||
|
"mov rax, rbx",
|
||||||
"pop r15", "pop r14", "pop r13", "pop r12", "pop rbp", "pop rbx",
|
"pop r15", "pop r14", "pop r13", "pop r12", "pop rbp", "pop rbx",
|
||||||
"ret",
|
"ret",
|
||||||
set_actor_sp = sym set_actor_sp,
|
|
||||||
get_sched_sp = sym get_scheduler_sp,
|
get_sched_sp = sym get_scheduler_sp,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
+679
-297
File diff suppressed because it is too large
Load Diff
+1455
File diff suppressed because it is too large
Load Diff
+251
-87
@@ -1,26 +1,85 @@
|
|||||||
//! RFC 016 — runtime introspection (Chunk 1: the read primitive).
|
//! Inspect what is running right now: which actors exist, what state each one
|
||||||
|
//! is in, and how they are related.
|
||||||
//!
|
//!
|
||||||
//! A synchronous, internal read of the slab that returns *owned* data. This is
|
//! This is the tool for questions like "is my server still alive", "how many
|
||||||
//! the mechanism the whole RFC hangs off: tests, the future observer
|
//! actors are currently parked waiting on something", or "what does the spawn
|
||||||
//! gen_server (Chunk 4), and a later control plane (RFC 003) are all consumers
|
//! tree look like". It is meant for debugging, test assertions, a health check
|
||||||
//! of [`snapshot`] / [`actor_info`], never of the runtime internals directly.
|
//! endpoint, or a monitoring dashboard: anywhere you want to look at the
|
||||||
|
//! runtime from the outside without stopping it or coupling your code to its
|
||||||
|
//! internals.
|
||||||
//!
|
//!
|
||||||
//! ## Consistency (DECISION D2 — per-slot tearing, `ps` semantics)
|
//! Three entry points, in order of scope:
|
||||||
//!
|
//!
|
||||||
//! [`snapshot`] is point-in-time and mildly racy *across* actors: each slot's
|
//! - [`snapshot`] returns every actor that currently exists, as a plain
|
||||||
//! scheduling state is a lock-free word load, so an actor reported `Running`
|
//! owned `Vec`, so you can filter, count, or search it however you like.
|
||||||
//! may already be `Parked`, and an actor can die mid-scan. This is the cheap,
|
//! - [`actor_info`] returns a coherent view of exactly one actor, by pid.
|
||||||
//! useful model (a coherent stop-the-world cut is expensive and rarely wanted).
|
//! Cheaper than filtering a whole snapshot down to one entry, and more
|
||||||
//! [`actor_info`] is coherent for the single actor it names.
|
//! precise (see "Consistency" below).
|
||||||
|
//! - [`tree`] returns the same actors as [`snapshot`], folded into a
|
||||||
|
//! parent/child forest that mirrors who spawned whom.
|
||||||
//!
|
//!
|
||||||
//! ## Locking
|
//! ```
|
||||||
|
//! use smarm::{actor_info, channel, run, snapshot, spawn, ActorState};
|
||||||
//!
|
//!
|
||||||
//! The lock order is **Leaf → Channel, at most one of each** (`raw_mutex.rs`);
|
//! run(|| {
|
||||||
//! cold locks, the registry, and the free list are all Leaves, so we may never
|
//! let (ready_tx, ready_rx) = channel::<()>();
|
||||||
//! hold two at once. The read is therefore phased: first a single registry-leaf
|
//! let (gate_tx, gate_rx) = channel::<()>();
|
||||||
//! pass for names and mailbox depth (the per-channel length read is a Channel
|
//!
|
||||||
//! lock taken under that Leaf — legal), released before the slab scan takes any
|
//! let worker = spawn(move || {
|
||||||
//! per-slot cold Leaf.
|
//! ready_tx.send(()).unwrap();
|
||||||
|
//! gate_rx.recv().unwrap(); // blocks here until released
|
||||||
|
//! });
|
||||||
|
//! ready_rx.recv().unwrap();
|
||||||
|
//!
|
||||||
|
//! // `snapshot` sees every actor, including this one and the worker.
|
||||||
|
//! let snap = snapshot();
|
||||||
|
//! assert!(snap.actors.len() >= 2);
|
||||||
|
//!
|
||||||
|
//! // `actor_info` gives a coherent view of just the worker. It is
|
||||||
|
//! // blocked on the gate channel, so it must be Parked.
|
||||||
|
//! let pid = worker.pid();
|
||||||
|
//! let info = actor_info(pid).expect("worker is still alive");
|
||||||
|
//! assert_eq!(info.state, ActorState::Parked);
|
||||||
|
//!
|
||||||
|
//! gate_tx.send(()).unwrap();
|
||||||
|
//! worker.join().unwrap();
|
||||||
|
//!
|
||||||
|
//! // Once joined, the pid no longer names a live actor.
|
||||||
|
//! assert!(actor_info(pid).is_none());
|
||||||
|
//! });
|
||||||
|
//! ```
|
||||||
|
//!
|
||||||
|
//! ## Consistency
|
||||||
|
//!
|
||||||
|
//! [`snapshot`] is not a single atomic pause-the-world freeze: it walks every
|
||||||
|
//! actor's state one after another, so it is a series of independent,
|
||||||
|
//! cheap, lock-free reads rather than one coherent moment in time. Between
|
||||||
|
//! reading actor A and actor B, either one can change state, and an actor can
|
||||||
|
//! even finish and disappear mid-scan. In practice this is exactly what you
|
||||||
|
//! want: a coherent stop-the-world snapshot would mean pausing every actor in
|
||||||
|
//! the runtime just to look at it, which is expensive and rarely necessary
|
||||||
|
//! for a dashboard, a test assertion, or a debugging session.
|
||||||
|
//!
|
||||||
|
//! [`actor_info`], in contrast, is coherent for the one actor it names: all of
|
||||||
|
//! its fields describe the same instant for that actor, because a single
|
||||||
|
//! actor's data cannot tear the way a scan across many actors can.
|
||||||
|
//!
|
||||||
|
//! ## Implementation notes
|
||||||
|
//!
|
||||||
|
//! These details matter if you are working on smarm itself; they are not part
|
||||||
|
//! of the public contract.
|
||||||
|
//!
|
||||||
|
//! The read never stops the scheduler and never holds a lock across the whole
|
||||||
|
//! scan. Each actor's scheduling state is a single lock-free word load
|
||||||
|
//! (hence the possible tearing described above). Reading the rest of an
|
||||||
|
//! actor's cold data (its supervisor, monitors, links, and so on) takes a
|
||||||
|
//! brief per-actor lock, just long enough to copy those fields out; nothing
|
||||||
|
//! is held across actors. Locking follows the crate-wide rule that at most
|
||||||
|
//! one "leaf" lock (a per-actor lock, the registry lock, or the free list
|
||||||
|
//! lock) is held at a time, with no leaf lock held while acquiring another.
|
||||||
|
//! The read is phased accordingly: first one pass over the registry to
|
||||||
|
//! collect every actor's registered names and mailbox depth, released before
|
||||||
|
//! the per-actor scan begins.
|
||||||
|
|
||||||
use crate::pid::Pid;
|
use crate::pid::Pid;
|
||||||
use crate::registry::MailboxInfo;
|
use crate::registry::MailboxInfo;
|
||||||
@@ -31,15 +90,28 @@ use crate::slot_state::{
|
|||||||
};
|
};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
/// Snapshot wire-format version (DECISION D1). [`RuntimeSnapshot`] is treated as
|
/// The format version carried by every [`RuntimeSnapshot`] and
|
||||||
/// a stable type from day one: it becomes the observer protocol (Chunk 4) and
|
/// [`RuntimeTree`], as [`RuntimeSnapshot::format_version`] /
|
||||||
/// crosses a version boundary the moment a remote observer attaches (RFC 011),
|
/// [`RuntimeTree::format_version`]. If you serialize a snapshot (for example
|
||||||
/// so the version travels with the data from the start.
|
/// to send it somewhere else, or to compare snapshots taken with different
|
||||||
|
/// versions of smarm) check this field: a change in its value means the shape
|
||||||
|
/// of [`ActorInfo`] or its neighbors has changed and old and new snapshots
|
||||||
|
/// should not be assumed compatible. If you only ever read a snapshot
|
||||||
|
/// in-process in the same version of smarm that produced it, you can ignore
|
||||||
|
/// this field.
|
||||||
pub const SNAPSHOT_FORMAT_VERSION: u16 = 1;
|
pub const SNAPSHOT_FORMAT_VERSION: u16 = 1;
|
||||||
|
|
||||||
/// Fine-grained scheduling state, mapped from the packed slot word with no new
|
/// What an actor is doing right now, from the scheduler's point of view.
|
||||||
/// storage. `RunningNotified` collapses into `Notified` — a wake landed while
|
///
|
||||||
/// the actor was on-CPU and it will re-queue when it yields.
|
/// - `Queued`: runnable, waiting for a scheduler thread to pick it up.
|
||||||
|
/// - `Running`: currently executing on a scheduler thread.
|
||||||
|
/// - `Notified`: was running and got woken up (for example, a message
|
||||||
|
/// arrived) before it had a chance to yield or park; it will be re-queued
|
||||||
|
/// as soon as it does.
|
||||||
|
/// - `Parked`: blocked, waiting on something such as a channel receive, a
|
||||||
|
/// mutex, a timer, or an IO event.
|
||||||
|
/// - `Done`: has finished (returned or panicked) but its slot has not been
|
||||||
|
/// reclaimed for reuse yet, so it is still visible to introspection.
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
pub enum ActorState {
|
pub enum ActorState {
|
||||||
Queued,
|
Queued,
|
||||||
@@ -49,8 +121,8 @@ pub enum ActorState {
|
|||||||
Done,
|
Done,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Classify a packed state word. `None` for a Vacant slot (skipped by the scan)
|
/// Classify a packed state word. `None` for a Vacant slot (skipped by the
|
||||||
/// — the only state that is not an actor.
|
/// scan): a vacant slot holds no actor at all, live or done.
|
||||||
fn classify(w: u64) -> Option<ActorState> {
|
fn classify(w: u64) -> Option<ActorState> {
|
||||||
Some(match word_state(w) {
|
Some(match word_state(w) {
|
||||||
ST_QUEUED => ActorState::Queued,
|
ST_QUEUED => ActorState::Queued,
|
||||||
@@ -62,61 +134,104 @@ fn classify(w: u64) -> Option<ActorState> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Owned, point-in-time view of one actor — no borrows of runtime internals, so
|
/// An owned, self-contained view of one actor at (approximately) one moment.
|
||||||
/// it is safe to hand to any consumer.
|
/// It borrows nothing from the runtime, so you can keep it, send it
|
||||||
|
/// elsewhere, or print it long after the actor it describes has changed
|
||||||
|
/// state or even exited.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct ActorInfo {
|
pub struct ActorInfo {
|
||||||
pub pid: Pid,
|
pub pid: Pid,
|
||||||
/// Registered names, inverted from the registry (usually 0 or 1).
|
/// Names this actor is currently registered under (see the
|
||||||
|
/// [`registry`](crate::registry) module). Usually empty or one name;
|
||||||
|
/// an actor can have more if it registered several.
|
||||||
pub names: Vec<&'static str>,
|
pub names: Vec<&'static str>,
|
||||||
pub state: ActorState,
|
pub state: ActorState,
|
||||||
/// Spawn-time parent edge (DECISION D9): `spawn_under` sets it to the
|
/// The actor that spawned this one: whoever called `spawn` or
|
||||||
/// supervisor, plain `spawn` to the spawning actor — so it is parentage,
|
/// `spawn_under` to create it. This is a parentage record, not
|
||||||
/// not necessarily a supervision relationship. `ROOT_PID` for the run's
|
/// necessarily a supervision relationship: `spawn_under` records the
|
||||||
/// root actor and for `Done` tombstones (whose `Actor` is already gone).
|
/// supervisor you asked for, while plain `spawn` records the spawning
|
||||||
|
/// actor itself, whether or not it supervises anything. It is the
|
||||||
|
/// runtime's root pid for the run's own root actor, and for a `Done`
|
||||||
|
/// actor whose bookkeeping has already been cleared.
|
||||||
pub supervisor: Pid,
|
pub supervisor: Pid,
|
||||||
pub trap_exit: bool,
|
pub trap_exit: bool,
|
||||||
pub monitors: u32,
|
pub monitors: u32,
|
||||||
pub links: u32,
|
pub links: u32,
|
||||||
pub joiners: u32,
|
pub joiners: u32,
|
||||||
/// Queued messages summed over the actor's *published* channels (register /
|
/// Messages currently queued and not yet delivered, summed across every
|
||||||
/// install / spawn_addr / gen_server). 0 for an actor that holds only a
|
/// channel this actor has published (via `register`, `install`,
|
||||||
/// private `channel()` receiver — those are invisible to the registry.
|
/// `spawn_addr`, or starting a gen_server). This is 0 for an actor that
|
||||||
|
/// only holds a private, unpublished `channel()` receiver, since nothing
|
||||||
|
/// outside the actor can see that channel exists.
|
||||||
pub mailbox_depth: u32,
|
pub mailbox_depth: u32,
|
||||||
/// Timeslice overruns tallied for this incarnation (RFC 016 Chunk 2): how
|
/// How many times this actor has been preempted for running past its
|
||||||
/// many times the actor was preempted for exceeding its slice. Resets on
|
/// scheduling timeslice. Counts only since the actor's current start (a
|
||||||
/// restart (per-incarnation, D7).
|
/// supervisor restart begins a fresh count).
|
||||||
pub overruns: u64,
|
pub overruns: u64,
|
||||||
/// Messages this actor has received (dequeued) this incarnation (RFC 016
|
/// How many messages this actor has received (taken off its inbox), since
|
||||||
/// Chunk 2) — answers "is this actor a hotspot / draining slower than its
|
/// its current start. Useful for spotting an actor whose mailbox is
|
||||||
/// mailbox fills." Counts received, not sent (D4). Per-incarnation (D7).
|
/// filling up faster than it can drain it: compare this against
|
||||||
|
/// `mailbox_depth` over time.
|
||||||
pub messages_received: u64,
|
pub messages_received: u64,
|
||||||
/// Approximate on-CPU cycles this incarnation has consumed (RFC 016 Chunk 2)
|
/// Approximate CPU cycles this actor has spent running, since its current
|
||||||
/// — a reductions-like work metric for relative comparison. Always 0 unless
|
/// start. A relative measure for comparing actors against each other, not
|
||||||
/// the `budget-accounting` feature is enabled (it costs an RDTSC per resume,
|
/// an absolute or wall-clock figure. Always 0 unless the crate's
|
||||||
/// D6). Per-incarnation (D7).
|
/// `budget-accounting` feature is enabled, since measuring it costs a
|
||||||
|
/// timestamp read on every resume.
|
||||||
pub budget_cycles: u64,
|
pub budget_cycles: u64,
|
||||||
|
/// RFC 019 §8 — this actor's stack, as the runtime sees it. All fields
|
||||||
|
/// are lock-free atomic reads, coherent for this incarnation via the
|
||||||
|
/// same generation check as the counters above. Exact RSS is
|
||||||
|
/// deliberately absent: `mincore` is debug tooling, never a runtime
|
||||||
|
/// path.
|
||||||
|
pub stack: StackInfo,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A whole-runtime snapshot. See the module docs for the D2 tearing model.
|
/// RFC 019 §8 — per-actor stack introspection. Sizes are page-rounded, as
|
||||||
|
/// [`Stack::new`](crate::stack::Stack::new) rounds them.
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
pub struct StackInfo {
|
||||||
|
/// Usable stack size ([`SpawnOpts::stack_reserve`](crate::SpawnOpts::stack_reserve)
|
||||||
|
/// or the Config/default).
|
||||||
|
pub reserve: usize,
|
||||||
|
/// PROT_NONE guard below the usable region.
|
||||||
|
pub guard: usize,
|
||||||
|
/// Sampled high-water depth in bytes: `top − lowest saved sp`. Sampled,
|
||||||
|
/// not exact — the context save at yields/parks/preemptions is the
|
||||||
|
/// sampler (RFC 019 §2), so a spike the actor never yielded inside is
|
||||||
|
/// invisible. 0 depth means "never descheduled at any depth", not
|
||||||
|
/// "never ran".
|
||||||
|
pub depth_high_water: usize,
|
||||||
|
/// Parks on this incarnation since its last shrink (or since install if
|
||||||
|
/// it has never shrunk) — the §3 cooldown counter, live.
|
||||||
|
pub parks_since_shrink: u32,
|
||||||
|
/// §3 shrinks performed on this incarnation.
|
||||||
|
pub shrinks: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A snapshot of every actor in the runtime at (approximately) one moment.
|
||||||
|
/// See the module docs' "Consistency" section for what "approximately" means
|
||||||
|
/// here.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct RuntimeSnapshot {
|
pub struct RuntimeSnapshot {
|
||||||
pub format_version: u16,
|
pub format_version: u16,
|
||||||
pub actors: Vec<ActorInfo>,
|
pub actors: Vec<ActorInfo>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Snapshot every live (and `Done`-but-not-yet-reclaimed) actor on the slab.
|
/// Every actor that currently exists: running, queued, parked, or finished
|
||||||
/// O(n) over the slot table, running with preemption disabled (like every
|
/// but not yet cleaned up. Cheap and lock-free per actor; see the module
|
||||||
/// runtime primitive) but holding no lock across the scan. Panics outside
|
/// docs for what "approximately one moment" means for the result as a whole.
|
||||||
/// `Runtime::run()`; callable from actor code and the run thread.
|
/// Panics if called outside [`run`](crate::run).
|
||||||
pub fn snapshot() -> RuntimeSnapshot {
|
pub fn snapshot() -> RuntimeSnapshot {
|
||||||
with_runtime(|inner| {
|
with_runtime(|inner| {
|
||||||
// Phase A: one registry-leaf pass for names + mailbox depth, released
|
// First pass: one registry lock to collect names + mailbox depth for
|
||||||
// before any cold leaf (no two Leaves at once).
|
// every actor, released before touching any per-actor lock below.
|
||||||
let mail = inner.registry.lock().introspect_map();
|
let mail = inner.registry.lock().introspect_map();
|
||||||
|
|
||||||
// Phase B: lock-free slab scan; per-slot cold leaf only to copy cold
|
// Second pass: walk the actor table. Each actor's scheduling state is
|
||||||
// fields. Tearing across slots is intentional (D2).
|
// a lock-free word load; only copying its other fields takes a brief
|
||||||
|
// per-actor lock. Tearing across actors is expected here (see the
|
||||||
|
// module docs' "Consistency" section).
|
||||||
let mut actors = Vec::new();
|
let mut actors = Vec::new();
|
||||||
for (idx, slot) in inner.slots.iter().enumerate() {
|
for (idx, slot) in inner.slots.iter().enumerate() {
|
||||||
let idx = idx as u32;
|
let idx = idx as u32;
|
||||||
@@ -124,12 +239,34 @@ pub fn snapshot() -> RuntimeSnapshot {
|
|||||||
actors.push(info);
|
actors.push(info);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
RuntimeSnapshot { format_version: SNAPSHOT_FORMAT_VERSION, actors }
|
RuntimeSnapshot {
|
||||||
|
format_version: SNAPSHOT_FORMAT_VERSION,
|
||||||
|
actors,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A coherent view of exactly one actor, or `None` if `pid` does not name a
|
||||||
|
/// currently-live entry: it is stale (that actor has already exited and its
|
||||||
|
/// slot was reused by another), out of range, or was never a real pid at
|
||||||
|
/// all. Unlike [`snapshot`], every field of the result describes the same
|
||||||
|
/// instant, since there is only one actor to read.
|
||||||
|
/// The stack shape `(reserve, guard)` of a live actor, page-rounded — the
|
||||||
|
/// RFC 019 introspection surface's first field (depth sampling and shrink
|
||||||
|
/// counters land with the shrink machinery). `None` if `pid` no longer names
|
||||||
|
/// a live actor. Takes the actor's cold lock briefly; debugging/assertion
|
||||||
|
/// use, not a hot-path call.
|
||||||
|
pub fn stack_shape(pid: Pid) -> Option<(usize, usize)> {
|
||||||
|
with_runtime(|inner| {
|
||||||
|
let slot = inner.slot_at(pid)?;
|
||||||
|
let cold = slot.cold.lock();
|
||||||
|
if slot.generation() != pid.generation() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
cold.actor.as_ref().map(|a| a.stack.shape())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Coherent view of a single actor, or `None` if the pid is stale, out of
|
|
||||||
/// range, or names a Vacant slot.
|
|
||||||
pub fn actor_info(pid: Pid) -> Option<ActorInfo> {
|
pub fn actor_info(pid: Pid) -> Option<ActorInfo> {
|
||||||
with_runtime(|inner| {
|
with_runtime(|inner| {
|
||||||
let slot = inner.slot_at(pid)?;
|
let slot = inner.slot_at(pid)?;
|
||||||
@@ -141,11 +278,12 @@ pub fn actor_info(pid: Pid) -> Option<ActorInfo> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Build one `ActorInfo` for slot `idx`, or `None` if Vacant or
|
/// Build one `ActorInfo` for slot `idx`, or `None` if the slot is empty or
|
||||||
/// racing-reclaimed. State is classified from a lock-free word load (the torn
|
/// was reclaimed while this read was in progress. The scheduling state comes
|
||||||
/// read); the cold lock then pins the generation (reclaim bumps it under that
|
/// from a lock-free word load (the source of the tearing described in the
|
||||||
/// same lock) so the cold fields are coherent for this incarnation. `mail` is
|
/// module docs); the per-actor lock then confirms the actor has not since
|
||||||
/// this slot's registry entry, if any.
|
/// exited and been replaced, so the rest of the fields are coherent for this
|
||||||
|
/// exact actor. `mail` is this slot's registry entry, if any.
|
||||||
fn read_slot(slot: &Slot, idx: u32, mail: Option<&MailboxInfo>) -> Option<ActorInfo> {
|
fn read_slot(slot: &Slot, idx: u32, mail: Option<&MailboxInfo>) -> Option<ActorInfo> {
|
||||||
let w = slot.state_word();
|
let w = slot.state_word();
|
||||||
let state = classify(w)?;
|
let state = classify(w)?;
|
||||||
@@ -153,10 +291,11 @@ fn read_slot(slot: &Slot, idx: u32, mail: Option<&MailboxInfo>) -> Option<ActorI
|
|||||||
let pid = Pid::new(idx, gen);
|
let pid = Pid::new(idx, gen);
|
||||||
|
|
||||||
let cold = slot.cold.lock();
|
let cold = slot.cold.lock();
|
||||||
// If the generation moved between the lock-free load and acquiring the cold
|
// If the generation moved between the lock-free load and acquiring the
|
||||||
// lock, the slot was reclaimed (and maybe reused) — drop it rather than mix
|
// per-actor lock, this actor exited (and the slot may already hold a new
|
||||||
// one incarnation's state with another's cold data. (ps semantics: a racing
|
// one). Drop it rather than mix one actor's state with another's data; a
|
||||||
// actor may simply be missed mid-scan.)
|
// racing actor may simply be missed by this scan, which is expected (see
|
||||||
|
// the module docs' "Consistency" section).
|
||||||
if word_gen(slot.state_word()) != gen {
|
if word_gen(slot.state_word()) != gen {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
@@ -172,7 +311,15 @@ fn read_slot(slot: &Slot, idx: u32, mail: Option<&MailboxInfo>) -> Option<ActorI
|
|||||||
let joiners = cold.waiters.len() as u32;
|
let joiners = cold.waiters.len() as u32;
|
||||||
drop(cold);
|
drop(cold);
|
||||||
|
|
||||||
// Counters are hot-region atomics, read lock-free (RFC 016 Chunk 2).
|
// Counters are plain atomics, read lock-free.
|
||||||
|
let (reserve, guard, top, hwm, parks_since_shrink, shrinks) = slot.stack_introspect();
|
||||||
|
let stack = StackInfo {
|
||||||
|
reserve,
|
||||||
|
guard,
|
||||||
|
depth_high_water: top.saturating_sub(hwm),
|
||||||
|
parks_since_shrink,
|
||||||
|
shrinks,
|
||||||
|
};
|
||||||
let overruns = slot.overruns();
|
let overruns = slot.overruns();
|
||||||
let messages_received = slot.messages_received();
|
let messages_received = slot.messages_received();
|
||||||
let budget_cycles = slot.budget_cycles();
|
let budget_cycles = slot.budget_cycles();
|
||||||
@@ -198,45 +345,55 @@ fn read_slot(slot: &Slot, idx: u32, mail: Option<&MailboxInfo>) -> Option<ActorI
|
|||||||
overruns,
|
overruns,
|
||||||
messages_received,
|
messages_received,
|
||||||
budget_cycles,
|
budget_cycles,
|
||||||
|
stack,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// Chunk 3 — tree view (pure derivation over a Chunk-1 snapshot)
|
// Tree view: a pure derivation over a snapshot
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
/// One node in the parentage forest. `children` are the actors whose recorded
|
/// One node in the parentage forest returned by [`tree`]. `children` are the
|
||||||
/// parent edge points at this node's pid.
|
/// actors whose recorded parent (see [`ActorInfo::supervisor`]) points at
|
||||||
|
/// this node's actor.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct TreeNode {
|
pub struct TreeNode {
|
||||||
pub info: ActorInfo,
|
pub info: ActorInfo,
|
||||||
/// The actor's recorded parent was absent from the snapshot (already
|
/// True if this actor's recorded parent was not found in the snapshot
|
||||||
/// Done/Vacant, or itself a tombstone), so it was re-rooted under the forest
|
/// (it had already exited, or was itself missing), so this node was
|
||||||
/// sentinel rather than dropped — the tree stays total (DECISION D8).
|
/// placed at the top of the forest instead of being dropped. This keeps
|
||||||
|
/// every actor in the snapshot visible somewhere in the tree, even one
|
||||||
|
/// whose parent is gone.
|
||||||
pub orphaned: bool,
|
pub orphaned: bool,
|
||||||
pub children: Vec<TreeNode>,
|
pub children: Vec<TreeNode>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The parentage forest. Roots are actors parented at `ROOT_PID` (genuine
|
/// The parentage forest: every actor from a snapshot, arranged by who spawned
|
||||||
/// roots) plus re-rooted orphans. The edge is *spawned-by / parent*, not
|
/// whom. Roots are actors with no parent in the snapshot (including the
|
||||||
/// necessarily supervision (DECISION D9) — see [`ActorInfo::supervisor`].
|
/// run's own root actor) plus any orphaned actors (see [`TreeNode::orphaned`]).
|
||||||
|
/// This mirrors spawn parentage, not necessarily a supervision tree; see
|
||||||
|
/// [`ActorInfo::supervisor`].
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct RuntimeTree {
|
pub struct RuntimeTree {
|
||||||
pub format_version: u16,
|
pub format_version: u16,
|
||||||
pub roots: Vec<TreeNode>,
|
pub roots: Vec<TreeNode>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Take a live [`snapshot`] and fold it into the parentage forest.
|
/// Take a fresh [`snapshot`] and fold it into the parentage forest.
|
||||||
pub fn tree() -> RuntimeTree {
|
pub fn tree() -> RuntimeTree {
|
||||||
tree_from(snapshot())
|
tree_from(snapshot())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Fold an existing snapshot into a forest by grouping each actor under its
|
/// Fold an existing snapshot into a parentage forest by grouping each actor
|
||||||
/// parent pid — a single O(n) pass, no new reads. Exposed separately so a
|
/// under its parent, without taking a new snapshot. Useful if you already
|
||||||
/// consumer that already holds a snapshot (or a synthetic one, in tests) can
|
/// have one (for example, one built in a test, or one you took earlier and
|
||||||
/// derive the tree without a second scan.
|
/// want to inspect again) and want the tree view of it without re-reading
|
||||||
|
/// the runtime.
|
||||||
pub fn tree_from(snap: RuntimeSnapshot) -> RuntimeTree {
|
pub fn tree_from(snap: RuntimeSnapshot) -> RuntimeTree {
|
||||||
let RuntimeSnapshot { format_version, actors } = snap;
|
let RuntimeSnapshot {
|
||||||
|
format_version,
|
||||||
|
actors,
|
||||||
|
} = snap;
|
||||||
|
|
||||||
let mut index_of: HashMap<Pid, usize> = HashMap::with_capacity(actors.len());
|
let mut index_of: HashMap<Pid, usize> = HashMap::with_capacity(actors.len());
|
||||||
for (i, a) in actors.iter().enumerate() {
|
for (i, a) in actors.iter().enumerate() {
|
||||||
@@ -254,7 +411,7 @@ pub fn tree_from(snap: RuntimeSnapshot) -> RuntimeTree {
|
|||||||
children_of.entry(parent).or_default().push(i);
|
children_of.entry(parent).or_default().push(i);
|
||||||
} else {
|
} else {
|
||||||
// Parent is the forest sentinel (genuine root) or absent from the
|
// Parent is the forest sentinel (genuine root) or absent from the
|
||||||
// snapshot (orphan, D8) — either way a root of the forest.
|
// snapshot (orphan): either way, a root of the forest.
|
||||||
orphaned[i] = parent != ROOT_PID;
|
orphaned[i] = parent != ROOT_PID;
|
||||||
roots.push(i);
|
roots.push(i);
|
||||||
}
|
}
|
||||||
@@ -267,7 +424,10 @@ pub fn tree_from(snap: RuntimeSnapshot) -> RuntimeTree {
|
|||||||
.into_iter()
|
.into_iter()
|
||||||
.filter_map(|i| build_node(i, &children_of, &orphaned, &mut slots))
|
.filter_map(|i| build_node(i, &children_of, &orphaned, &mut slots))
|
||||||
.collect();
|
.collect();
|
||||||
RuntimeTree { format_version, roots: root_nodes }
|
RuntimeTree {
|
||||||
|
format_version,
|
||||||
|
roots: root_nodes,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_node(
|
fn build_node(
|
||||||
@@ -285,5 +445,9 @@ fn build_node(
|
|||||||
.collect()
|
.collect()
|
||||||
})
|
})
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
Some(TreeNode { info, orphaned: orphaned[i], children })
|
Some(TreeNode {
|
||||||
|
info,
|
||||||
|
orphaned: orphaned[i],
|
||||||
|
children,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,44 +13,68 @@
|
|||||||
//! leaves the actor, no copying through an intermediary thread. Built on
|
//! leaves the actor, no copying through an intermediary thread. Built on
|
||||||
//! these are the conveniences `read(fd, &mut buf)` and `write(fd, &buf)`.
|
//! these are the conveniences `read(fd, &mut buf)` and `write(fd, &buf)`.
|
||||||
//!
|
//!
|
||||||
//! Architecture
|
//! Architecture (RFC 018: driver-enqueues)
|
||||||
//! ============
|
//! =======================================
|
||||||
//! Per `run()`, two OS threads:
|
//! Per `run()`, two OS threads, each a *producer* behind the runtime's
|
||||||
//! - **epoll thread**: owns the epollfd. Loops in `epoll_wait`. On a
|
//! two-call contract — make the actor runnable (`unpark_at`, whose enqueue
|
||||||
//! ready fd, pushes `Completion::FdReady { pid, fd, events }` to the
|
//! tail wakes a parked scheduler), nothing else:
|
||||||
//! shared completion queue and writes the scheduler-wake pipe. On the
|
|
||||||
//! shutdown pipe (also registered in epollfd), exits.
|
|
||||||
//! - **pool thread**: blocks on the request mpsc. Runs the closure
|
|
||||||
//! inside `catch_unwind`, pushes `Completion::Blocking { pid, result }`,
|
|
||||||
//! writes the scheduler-wake pipe.
|
|
||||||
//!
|
//!
|
||||||
//! Both threads share a single `completions: Arc<Mutex<VecDeque<Completion>>>`
|
//! - **epoll thread**: owns `epoll_wait` on the epollfd. On a ready fd it
|
||||||
//! and the same scheduler-wake pipe.
|
//! removes the parked waiter from the shared `waiters` map and DELs the
|
||||||
|
//! fd (both under the waiters lock — see below), then unparks the
|
||||||
|
//! actor directly. On the shutdown pipe (also registered in the
|
||||||
|
//! epollfd), exits.
|
||||||
|
//! - **pool thread**: blocks on the request mpsc. Runs the closure inside
|
||||||
|
//! `catch_unwind`, stashes the result in the actor's slot
|
||||||
|
//! (`pending_io_result`, under the cold lock, generation-checked),
|
||||||
|
//! decrements the runtime's `io_outstanding`, and unparks the actor.
|
||||||
//!
|
//!
|
||||||
//! `epoll_ctl` (register/unregister fd interest) is called by the
|
//! There is no shared completion queue and no wake pipe: each producer
|
||||||
//! scheduler thread *directly* on the epollfd. That's well-defined per
|
//! routes its own completion, so the whole byte-vs-completion visibility
|
||||||
//! `epoll_ctl(2)`: a thread may be calling `epoll_wait` on the epollfd
|
//! discipline of the drain era — and the stranded-completion hazards it
|
||||||
//! while another thread calls `epoll_ctl`. Avoids needing a second mpsc
|
//! defended against — is unrepresentable. Producers reach the runtime
|
||||||
//! and a second wake mechanism.
|
//! through a `Weak<RuntimeInner>`: upgraded per completion (the path is
|
||||||
|
//! syscall-bound; the refcount op is noise) and avoiding an Arc cycle
|
||||||
|
//! through `RuntimeInner::io`.
|
||||||
|
//!
|
||||||
|
//! `epoll_ctl` (register fd interest) is called by the scheduler thread
|
||||||
|
//! directly on the epollfd. That's well-defined per `epoll_ctl(2)`: a
|
||||||
|
//! thread may be calling `epoll_wait` on the epollfd while another thread
|
||||||
|
//! calls `epoll_ctl`.
|
||||||
//!
|
//!
|
||||||
//! Epoll mode
|
//! Epoll mode
|
||||||
//! ==========
|
//! ==========
|
||||||
//! Level-triggered with EPOLLONESHOT. After a wakeup the kernel
|
//! Level-triggered with EPOLLONESHOT. After a wakeup the kernel
|
||||||
//! auto-disarms the fd, so we never get two wakeups for one
|
//! auto-disarms the fd, so we never get two wakeups for one
|
||||||
//! `wait_readable` call. The scheduler explicitly `EPOLL_CTL_DEL`s the fd
|
//! `wait_readable` call. The epoll thread explicitly `EPOLL_CTL_DEL`s the
|
||||||
//! on completion to free the slot for re-registration. Net effect: each
|
//! fd on readiness to free the slot for re-registration. Net effect: each
|
||||||
//! `wait_readable(fd)` is one ADD, one wakeup, one DEL — symmetric and
|
//! `wait_readable(fd)` is one ADD, one wakeup, one DEL — symmetric and
|
||||||
//! stateless between calls.
|
//! stateless between calls.
|
||||||
//!
|
//!
|
||||||
|
//! ## The waiters lock is the ADD/DEL serialization
|
||||||
|
//!
|
||||||
|
//! Registration (scheduler thread: check-vacant, defensive DEL, ADD,
|
||||||
|
//! insert) and readiness consumption (epoll thread: remove, DEL) each run
|
||||||
|
//! entirely under the `waiters` mutex. This is what makes the
|
||||||
|
//! oneshot-rearm race unrepresentable: a woken actor re-registering the
|
||||||
|
//! same fd cannot interleave with the epoll thread's DEL for the *previous*
|
||||||
|
//! registration — whichever takes the lock second sees a consistent
|
||||||
|
//! kernel-side state. Lock order: `io` (the runtime's outer mutex, held by
|
||||||
|
//! scheduler-side callers) → `waiters` → slot/queue leaves via `unpark_at`.
|
||||||
|
//! The epoll thread takes `waiters` without `io` — it must never take
|
||||||
|
//! `io`, both for lock-order hygiene and because teardown holds `io` while
|
||||||
|
//! joining it.
|
||||||
|
//!
|
||||||
//! Fd hygiene
|
//! Fd hygiene
|
||||||
//! ==========
|
//! ==========
|
||||||
//! An actor stopped while waiting on an fd unwinds out of `wait_fd`'s park;
|
//! An actor stopped while waiting on an fd unwinds out of `wait_fd`'s park;
|
||||||
//! a drop guard there (armed after a successful register, forgotten on a
|
//! a drop guard there (armed after a successful register, forgotten on a
|
||||||
//! normal wake) removes the `waiters` entry iff it is still that wait's
|
//! normal wake) calls [`IoThread::cancel_waiter`], which removes the
|
||||||
//! `(pid, epoch)` and only then `EPOLL_CTL_DEL`s the fd — an entry already
|
//! `waiters` entry iff it is still that wait's `(pid, epoch)` and only then
|
||||||
//! consumed by a racing `FdReady` means the fd may carry someone else's
|
//! `EPOLL_CTL_DEL`s the fd — an entry already consumed by the epoll thread
|
||||||
//! fresh registration, which must be left alone. `epoll_register` keeps a
|
//! means the fd may carry someone else's fresh registration, which must be
|
||||||
//! defensive bare DEL before ADD as belt-and-braces.
|
//! left alone. `epoll_register` keeps a defensive bare DEL before ADD as
|
||||||
|
//! belt-and-braces.
|
||||||
//!
|
//!
|
||||||
//! Buffers used with `read`/`write` should be on fds opened with
|
//! Buffers used with `read`/`write` should be on fds opened with
|
||||||
//! `O_NONBLOCK`. If they aren't, the syscall may block the scheduler
|
//! `O_NONBLOCK`. If they aren't, the syscall may block the scheduler
|
||||||
@@ -68,13 +92,14 @@
|
|||||||
//! they have no equivalent panic-propagation path.
|
//! they have no equivalent panic-propagation path.
|
||||||
|
|
||||||
use crate::pid::Pid;
|
use crate::pid::Pid;
|
||||||
|
use crate::runtime::RuntimeInner;
|
||||||
use std::any::Any;
|
use std::any::Any;
|
||||||
use std::collections::{HashMap, VecDeque};
|
use std::collections::HashMap;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::os::fd::RawFd;
|
use std::os::fd::RawFd;
|
||||||
use std::panic;
|
use std::panic;
|
||||||
use std::sync::mpsc;
|
use std::sync::atomic::Ordering;
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{mpsc, Arc, Mutex, Weak};
|
||||||
use std::thread::JoinHandle as OsJoinHandle;
|
use std::thread::JoinHandle as OsJoinHandle;
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
@@ -86,45 +111,31 @@ use std::thread::JoinHandle as OsJoinHandle;
|
|||||||
pub type IoResult = Result<Box<dyn Any + Send>, Box<dyn Any + Send>>;
|
pub type IoResult = Result<Box<dyn Any + Send>, Box<dyn Any + Send>>;
|
||||||
|
|
||||||
struct Request {
|
struct Request {
|
||||||
/// The submitter's park-epoch — carried through to the `Blocking`
|
/// The submitter's park-epoch — the eventual wake is epoch-matched.
|
||||||
/// completion so the wake is epoch-matched.
|
|
||||||
epoch: u32,
|
epoch: u32,
|
||||||
pid: Pid,
|
pid: Pid,
|
||||||
/// The work to perform. Returns the wire-form result directly.
|
/// The work to perform. Returns the wire-form result directly.
|
||||||
work: Box<dyn FnOnce() -> IoResult + Send>,
|
work: Box<dyn FnOnce() -> IoResult + Send>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Completion message from either IO thread back to the scheduler.
|
/// The parked-waiter map, shared between scheduler-side registration and
|
||||||
pub enum Completion {
|
/// the epoll thread's readiness consumption. See the module docs on why
|
||||||
/// A `block_on_io` closure has finished (Ok = return value, Err = panic
|
/// this single lock is the ADD/DEL serialization.
|
||||||
/// payload).
|
type Waiters = Arc<Mutex<HashMap<RawFd, (Pid, u32)>>>;
|
||||||
Blocking { pid: Pid, epoch: u32, result: IoResult },
|
|
||||||
/// An fd registered via `wait_readable`/`wait_writable` is ready. The
|
|
||||||
/// scheduler looks up the parked pid in `waiters`, unparks it, and
|
|
||||||
/// removes the entry. `pid` isn't in this variant because the epoll
|
|
||||||
/// thread doesn't have access to the `waiters` map; the scheduler
|
|
||||||
/// thread owns that.
|
|
||||||
FdReady { fd: RawFd, events: u32 },
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// IoThread — created per `run()`, owned by `SchedulerState`.
|
// IoThread — created per `run()`, owned by `RuntimeInner::io`.
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
pub struct IoThread {
|
pub struct IoThread {
|
||||||
// ----- Channels & queues -----
|
|
||||||
|
|
||||||
/// Submission queue into the blocking-work pool.
|
/// Submission queue into the blocking-work pool.
|
||||||
tx: mpsc::Sender<Request>,
|
tx: mpsc::Sender<Request>,
|
||||||
/// Shared completion queue, fed by both the pool and the epoll thread.
|
/// One parked actor per registered fd. Populated by `epoll_register`,
|
||||||
completions: Arc<Mutex<VecDeque<Completion>>>,
|
/// consumed by the epoll thread on readiness or `cancel_waiter` on an
|
||||||
/// Pipe the scheduler polls in its idle path. Both IO threads write to
|
/// unwound wait.
|
||||||
/// `wake_write` after pushing a completion.
|
waiters: Waiters,
|
||||||
wake_read: RawFd,
|
|
||||||
wake_write: RawFd,
|
|
||||||
|
|
||||||
// ----- Epoll machinery -----
|
// ----- Epoll machinery -----
|
||||||
|
|
||||||
/// The epollfd, owned by `IoThread`. Callable cross-thread via
|
/// The epollfd, owned by `IoThread`. Callable cross-thread via
|
||||||
/// `epoll_ctl` per the man page.
|
/// `epoll_ctl` per the man page.
|
||||||
epollfd: RawFd,
|
epollfd: RawFd,
|
||||||
@@ -133,39 +144,24 @@ pub struct IoThread {
|
|||||||
/// shutdown.
|
/// shutdown.
|
||||||
shutdown_read: RawFd,
|
shutdown_read: RawFd,
|
||||||
shutdown_write: RawFd,
|
shutdown_write: RawFd,
|
||||||
/// One parked actor per registered fd. Populated by `wait_readable` /
|
|
||||||
/// `wait_writable` and drained by the scheduler when a `FdReady`
|
|
||||||
/// completion is processed.
|
|
||||||
pub waiters: HashMap<RawFd, (Pid, u32)>,
|
|
||||||
|
|
||||||
// ----- Threads -----
|
// ----- Threads -----
|
||||||
|
|
||||||
pool_thread: Option<OsJoinHandle<()>>,
|
pool_thread: Option<OsJoinHandle<()>>,
|
||||||
epoll_thread: Option<OsJoinHandle<()>>,
|
epoll_thread: Option<OsJoinHandle<()>>,
|
||||||
|
|
||||||
/// Number of `block_on_io` requests in-flight. Used by the scheduler's
|
|
||||||
/// idle path to decide whether to wait on the pipe or exit. Fd waits
|
|
||||||
/// are not counted here; they're counted by `waiters.len()`.
|
|
||||||
pub outstanding: u32,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IoThread {
|
impl IoThread {
|
||||||
pub fn start() -> io::Result<Self> {
|
/// Start the pool and epoll threads. `rt` is the producers' route back
|
||||||
// Scheduler-facing wake pipe.
|
/// into the runtime (slot table + unpark protocol); a `Weak` so the
|
||||||
let (wake_read, wake_write) = make_pipe()?;
|
/// `RuntimeInner → IoThread → RuntimeInner` cycle never forms.
|
||||||
// Pool submission channel + shared completion queue.
|
pub(crate) fn start(rt: Weak<RuntimeInner>) -> io::Result<Self> {
|
||||||
|
// Pool submission channel.
|
||||||
let (tx, rx) = mpsc::channel::<Request>();
|
let (tx, rx) = mpsc::channel::<Request>();
|
||||||
let completions: Arc<Mutex<VecDeque<Completion>>> =
|
let waiters: Waiters = Arc::new(Mutex::new(HashMap::new()));
|
||||||
Arc::new(Mutex::new(VecDeque::new()));
|
|
||||||
|
|
||||||
// Epoll machinery.
|
// Epoll machinery.
|
||||||
let epollfd = unsafe { libc::epoll_create1(libc::EPOLL_CLOEXEC) };
|
let epollfd = unsafe { libc::epoll_create1(libc::EPOLL_CLOEXEC) };
|
||||||
if epollfd < 0 {
|
if epollfd < 0 {
|
||||||
// Best-effort fd cleanup before bailing.
|
|
||||||
unsafe {
|
|
||||||
libc::close(wake_read);
|
|
||||||
libc::close(wake_write);
|
|
||||||
}
|
|
||||||
return Err(io::Error::last_os_error());
|
return Err(io::Error::last_os_error());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -174,8 +170,6 @@ impl IoThread {
|
|||||||
Err(e) => {
|
Err(e) => {
|
||||||
unsafe {
|
unsafe {
|
||||||
libc::close(epollfd);
|
libc::close(epollfd);
|
||||||
libc::close(wake_read);
|
|
||||||
libc::close(wake_write);
|
|
||||||
}
|
}
|
||||||
return Err(e);
|
return Err(e);
|
||||||
}
|
}
|
||||||
@@ -202,79 +196,51 @@ impl IoThread {
|
|||||||
libc::close(epollfd);
|
libc::close(epollfd);
|
||||||
libc::close(shutdown_read);
|
libc::close(shutdown_read);
|
||||||
libc::close(shutdown_write);
|
libc::close(shutdown_write);
|
||||||
libc::close(wake_read);
|
|
||||||
libc::close(wake_write);
|
|
||||||
}
|
}
|
||||||
return Err(e);
|
return Err(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Spawn pool thread.
|
// Spawn pool thread.
|
||||||
let pool_comps = completions.clone();
|
let pool_rt = rt.clone();
|
||||||
let pool_thread = std::thread::Builder::new()
|
let pool_thread = std::thread::Builder::new()
|
||||||
.name("smarm-io-pool".into())
|
.name("smarm-io-pool".into())
|
||||||
.spawn(move || pool_loop(rx, pool_comps, wake_write))?;
|
.spawn(move || pool_loop(rx, pool_rt))?;
|
||||||
|
|
||||||
// Spawn epoll thread.
|
// Spawn epoll thread.
|
||||||
let epoll_comps = completions.clone();
|
let epoll_waiters = waiters.clone();
|
||||||
let epoll_thread = std::thread::Builder::new()
|
let epoll_thread = std::thread::Builder::new()
|
||||||
.name("smarm-io-epoll".into())
|
.name("smarm-io-epoll".into())
|
||||||
.spawn(move || epoll_loop(epollfd, epoll_comps, wake_write))?;
|
.spawn(move || epoll_loop(epollfd, epoll_waiters, rt))?;
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
tx,
|
tx,
|
||||||
completions,
|
waiters,
|
||||||
wake_read,
|
|
||||||
wake_write,
|
|
||||||
epollfd,
|
epollfd,
|
||||||
shutdown_read,
|
shutdown_read,
|
||||||
shutdown_write,
|
shutdown_write,
|
||||||
waiters: HashMap::new(),
|
|
||||||
pool_thread: Some(pool_thread),
|
pool_thread: Some(pool_thread),
|
||||||
epoll_thread: Some(epoll_thread),
|
epoll_thread: Some(epoll_thread),
|
||||||
outstanding: 0,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Hand a request to the pool. Increments `outstanding`.
|
/// Hand a request to the pool. The caller (scheduler.rs) increments
|
||||||
|
/// `io_outstanding` BEFORE calling — the pool decrements on completion,
|
||||||
|
/// and an increment that trailed the completion would underflow.
|
||||||
pub fn submit(&mut self, pid: Pid, epoch: u32, work: Box<dyn FnOnce() -> IoResult + Send>) {
|
pub fn submit(&mut self, pid: Pid, epoch: u32, work: Box<dyn FnOnce() -> IoResult + Send>) {
|
||||||
self.outstanding += 1;
|
|
||||||
// Send can only fail if the pool has hung up, which only happens
|
// Send can only fail if the pool has hung up, which only happens
|
||||||
// on shutdown. submit during shutdown is a bug.
|
// on shutdown. submit during shutdown is a bug.
|
||||||
self.tx
|
if self.tx.send(Request { pid, epoch, work }).is_err() {
|
||||||
.send(Request { pid, epoch, work })
|
panic!("smarm: io pool hung up unexpectedly (submit during shutdown)");
|
||||||
.expect("io pool hung up unexpectedly");
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Drain every available completion. Caller (the scheduler) routes the
|
|
||||||
/// results and updates `outstanding` / `waiters` accordingly.
|
|
||||||
pub fn drain_completions(&mut self) -> Vec<Completion> {
|
|
||||||
let mut q = self.completions.lock().unwrap();
|
|
||||||
let mut out = Vec::with_capacity(q.len());
|
|
||||||
while let Some(c) = q.pop_front() {
|
|
||||||
out.push(c);
|
|
||||||
}
|
}
|
||||||
out
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn wake_fd(&self) -> RawFd {
|
|
||||||
self.wake_read
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Write the wake pipe directly: rouse every scheduler thread blocked in
|
|
||||||
/// its idle `poll_wake`. Used by the terminal (AllDone) path — an idle
|
|
||||||
/// sibling may be blocked on a snapshot that nothing will ever refresh
|
|
||||||
/// (an orphaned timer deadline, or `io_outstanding` from a waiter that
|
|
||||||
/// was stop-cancelled and so never produces a completion).
|
|
||||||
pub fn wake(&self) {
|
|
||||||
wake_scheduler(self.wake_write);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Register interest in `fd` becoming readable/writable; record `pid`
|
/// Register interest in `fd` becoming readable/writable; record `pid`
|
||||||
/// as the parked waiter. The epoll thread will push a `FdReady`
|
/// as the parked waiter. The epoll thread unparks it on readiness.
|
||||||
/// completion when the kernel signals.
|
/// The caller increments `io_fd_waiters` BEFORE calling (mirror of
|
||||||
|
/// `submit`'s contract) and decrements it again if this errors.
|
||||||
///
|
///
|
||||||
/// EPOLLONESHOT: one wakeup per registration. The scheduler must
|
/// EPOLLONESHOT: one wakeup per registration; the epoll thread DELs on
|
||||||
/// `epoll_del` on completion to free the slot for re-registration.
|
/// readiness, `cancel_waiter` DELs on an unwound wait.
|
||||||
pub fn epoll_register(
|
pub fn epoll_register(
|
||||||
&mut self,
|
&mut self,
|
||||||
fd: RawFd,
|
fd: RawFd,
|
||||||
@@ -283,20 +249,24 @@ impl IoThread {
|
|||||||
readable: bool,
|
readable: bool,
|
||||||
writable: bool,
|
writable: bool,
|
||||||
) -> io::Result<()> {
|
) -> io::Result<()> {
|
||||||
|
let mut waiters = match self.waiters.lock() {
|
||||||
|
Ok(g) => g,
|
||||||
|
Err(e) => panic!("smarm: io waiters lock poisoned (core corrupt): {e}"),
|
||||||
|
};
|
||||||
// Two actors waiting on the same fd would be a misuse: the kernel
|
// Two actors waiting on the same fd would be a misuse: the kernel
|
||||||
// delivers exactly one EPOLLONESHOT wakeup, so the second waiter
|
// delivers exactly one EPOLLONESHOT wakeup, so the second waiter
|
||||||
// would hang. Reject up front.
|
// would hang. Reject up front.
|
||||||
if self.waiters.contains_key(&fd) {
|
if waiters.contains_key(&fd) {
|
||||||
return Err(io::Error::new(
|
return Err(io::Error::new(
|
||||||
io::ErrorKind::AlreadyExists,
|
io::ErrorKind::AlreadyExists,
|
||||||
"fd already has a parked waiter",
|
"fd already has a parked waiter",
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Belt-and-braces: the unwind guard in `wait_fd` is responsible for
|
// Belt-and-braces: `cancel_waiter` is responsible for cleaning up a
|
||||||
// cleaning up a stopped waiter's registration, but a bare DEL is
|
// stopped waiter's registration, but a bare DEL is harmless if the
|
||||||
// harmless if the fd isn't registered (ENOENT) and removes any leak
|
// fd isn't registered (ENOENT) and removes any leak a path we
|
||||||
// a path we haven't thought of might leave behind.
|
// haven't thought of might leave behind.
|
||||||
unsafe {
|
unsafe {
|
||||||
libc::epoll_ctl(self.epollfd, libc::EPOLL_CTL_DEL, fd, std::ptr::null_mut());
|
libc::epoll_ctl(self.epollfd, libc::EPOLL_CTL_DEL, fd, std::ptr::null_mut());
|
||||||
}
|
}
|
||||||
@@ -312,25 +282,34 @@ impl IoThread {
|
|||||||
events,
|
events,
|
||||||
u64: fd as u64,
|
u64: fd as u64,
|
||||||
};
|
};
|
||||||
let r = unsafe {
|
let r =
|
||||||
libc::epoll_ctl(self.epollfd, libc::EPOLL_CTL_ADD, fd, &mut ev as *mut _)
|
unsafe { libc::epoll_ctl(self.epollfd, libc::EPOLL_CTL_ADD, fd, &mut ev as *mut _) };
|
||||||
};
|
|
||||||
if r < 0 {
|
if r < 0 {
|
||||||
return Err(io::Error::last_os_error());
|
return Err(io::Error::last_os_error());
|
||||||
}
|
}
|
||||||
self.waiters.insert(fd, (pid, epoch));
|
waiters.insert(fd, (pid, epoch));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Remove `fd` from the epollfd. Called by the scheduler after a
|
/// Remove `fd`'s waiter iff it is still `(pid, epoch)`, DELing the fd
|
||||||
/// `FdReady` completion, so the next `wait_readable(fd)` can ADD again.
|
/// from the epollfd in the same critical section. Returns whether the
|
||||||
///
|
/// entry was removed (the caller then decrements `io_fd_waiters`).
|
||||||
/// Does NOT touch `waiters` — that's the scheduler's bookkeeping; this
|
/// `false` means the epoll thread consumed the registration first —
|
||||||
/// is purely the kernel-side cleanup.
|
/// the fd may already carry someone else's fresh ADD; hands off.
|
||||||
pub fn epoll_deregister(&mut self, fd: RawFd) {
|
pub fn cancel_waiter(&mut self, fd: RawFd, pid: Pid, epoch: u32) -> bool {
|
||||||
// EPOLL_CTL_DEL of an already-removed fd returns ENOENT; ignore.
|
let mut waiters = match self.waiters.lock() {
|
||||||
unsafe {
|
Ok(g) => g,
|
||||||
libc::epoll_ctl(self.epollfd, libc::EPOLL_CTL_DEL, fd, std::ptr::null_mut());
|
Err(e) => panic!("smarm: io waiters lock poisoned (core corrupt): {e}"),
|
||||||
|
};
|
||||||
|
if waiters.get(&fd) == Some(&(pid, epoch)) {
|
||||||
|
waiters.remove(&fd);
|
||||||
|
// EPOLL_CTL_DEL of an already-removed fd returns ENOENT; ignore.
|
||||||
|
unsafe {
|
||||||
|
libc::epoll_ctl(self.epollfd, libc::EPOLL_CTL_DEL, fd, std::ptr::null_mut());
|
||||||
|
}
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -351,7 +330,10 @@ impl Drop for IoThread {
|
|||||||
let real_tx = std::mem::replace(&mut self.tx, dead_tx);
|
let real_tx = std::mem::replace(&mut self.tx, dead_tx);
|
||||||
drop(real_tx);
|
drop(real_tx);
|
||||||
|
|
||||||
// 3. Join both threads.
|
// 3. Join both threads. Safe even while the caller holds the
|
||||||
|
// runtime's `io` mutex: neither thread ever takes it (they reach
|
||||||
|
// the runtime through a Weak they upgrade per completion, and
|
||||||
|
// the epoll thread's only lock is `waiters`).
|
||||||
if let Some(h) = self.epoll_thread.take() {
|
if let Some(h) = self.epoll_thread.take() {
|
||||||
let _ = h.join();
|
let _ = h.join();
|
||||||
}
|
}
|
||||||
@@ -364,8 +346,6 @@ impl Drop for IoThread {
|
|||||||
libc::close(self.epollfd);
|
libc::close(self.epollfd);
|
||||||
libc::close(self.shutdown_read);
|
libc::close(self.shutdown_read);
|
||||||
libc::close(self.shutdown_write);
|
libc::close(self.shutdown_write);
|
||||||
libc::close(self.wake_read);
|
|
||||||
libc::close(self.wake_write);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -376,36 +356,38 @@ impl Drop for IoThread {
|
|||||||
const SHUTDOWN_EPOLL_TOKEN: u64 = u64::MAX;
|
const SHUTDOWN_EPOLL_TOKEN: u64 = u64::MAX;
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// Pool loop
|
// Pool loop (producer: Blocking completions)
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
fn pool_loop(
|
fn pool_loop(rx: mpsc::Receiver<Request>, rt: Weak<RuntimeInner>) {
|
||||||
rx: mpsc::Receiver<Request>,
|
|
||||||
completions: Arc<Mutex<VecDeque<Completion>>>,
|
|
||||||
wake_write: RawFd,
|
|
||||||
) {
|
|
||||||
while let Ok(Request { pid, epoch, work }) = rx.recv() {
|
while let Ok(Request { pid, epoch, work }) = rx.recv() {
|
||||||
let result: IoResult = match panic::catch_unwind(panic::AssertUnwindSafe(work)) {
|
let result: IoResult = match panic::catch_unwind(panic::AssertUnwindSafe(work)) {
|
||||||
Ok(r) => r,
|
Ok(r) => r,
|
||||||
Err(payload) => Err(payload),
|
Err(payload) => Err(payload),
|
||||||
};
|
};
|
||||||
completions
|
let Some(inner) = rt.upgrade() else { return };
|
||||||
.lock()
|
// Stash the result under the cold lock (generation-checked: an
|
||||||
.unwrap()
|
// actor stopped with the op in flight discards it), decrement the
|
||||||
.push_back(Completion::Blocking { pid, epoch, result });
|
// in-flight count, then wake through the epoch-matched unpark. The
|
||||||
wake_scheduler(wake_write);
|
// unpark's enqueue tail wakes a parked scheduler; the actor stays
|
||||||
|
// `live` until it resumes and finalizes, so the decrement's
|
||||||
|
// ordering against the termination verdict is not load-bearing.
|
||||||
|
if let Some(slot) = inner.slot_at(pid) {
|
||||||
|
let mut cold = slot.cold.lock();
|
||||||
|
if slot.generation() == pid.generation() {
|
||||||
|
cold.pending_io_result = Some(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
inner.io_outstanding.fetch_sub(1, Ordering::AcqRel);
|
||||||
|
inner.unpark_at(pid, epoch);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// Epoll loop
|
// Epoll loop (producer: FdReady completions)
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
fn epoll_loop(
|
fn epoll_loop(epollfd: RawFd, waiters: Waiters, rt: Weak<RuntimeInner>) {
|
||||||
epollfd: RawFd,
|
|
||||||
completions: Arc<Mutex<VecDeque<Completion>>>,
|
|
||||||
wake_write: RawFd,
|
|
||||||
) {
|
|
||||||
// Buffer for epoll_wait. 64 is plenty for our scale; if a real load
|
// Buffer for epoll_wait. 64 is plenty for our scale; if a real load
|
||||||
// appears that needs more, this is a one-line change.
|
// appears that needs more, this is a one-line change.
|
||||||
const MAX_EVENTS: usize = 64;
|
const MAX_EVENTS: usize = 64;
|
||||||
@@ -413,12 +395,7 @@ fn epoll_loop(
|
|||||||
|
|
||||||
loop {
|
loop {
|
||||||
let n = unsafe {
|
let n = unsafe {
|
||||||
libc::epoll_wait(
|
libc::epoll_wait(epollfd, events.as_mut_ptr(), MAX_EVENTS as libc::c_int, -1)
|
||||||
epollfd,
|
|
||||||
events.as_mut_ptr(),
|
|
||||||
MAX_EVENTS as libc::c_int,
|
|
||||||
-1,
|
|
||||||
)
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if n < 0 {
|
if n < 0 {
|
||||||
@@ -433,26 +410,36 @@ fn epoll_loop(
|
|||||||
}
|
}
|
||||||
|
|
||||||
let mut shutdown_requested = false;
|
let mut shutdown_requested = false;
|
||||||
let mut pushed_any = false;
|
for ev in events.iter().take(n as usize) {
|
||||||
{
|
if ev.u64 == SHUTDOWN_EPOLL_TOKEN {
|
||||||
let mut q = completions.lock().unwrap();
|
shutdown_requested = true;
|
||||||
for ev in events.iter().take(n as usize) {
|
continue;
|
||||||
if ev.u64 == SHUTDOWN_EPOLL_TOKEN {
|
}
|
||||||
shutdown_requested = true;
|
let fd = ev.u64 as RawFd;
|
||||||
continue;
|
// Consume the registration: remove + DEL under the waiters
|
||||||
|
// lock (the ADD/DEL serialization — see module docs). A
|
||||||
|
// vanished entry means `cancel_waiter` beat us: the wake is
|
||||||
|
// already moot.
|
||||||
|
let entry = {
|
||||||
|
let mut w = match waiters.lock() {
|
||||||
|
Ok(g) => g,
|
||||||
|
Err(e) => {
|
||||||
|
panic!("smarm: io waiters lock poisoned (core corrupt): {e}")
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let entry = w.remove(&fd);
|
||||||
|
if entry.is_some() {
|
||||||
|
unsafe {
|
||||||
|
libc::epoll_ctl(epollfd, libc::EPOLL_CTL_DEL, fd, std::ptr::null_mut());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
let fd = ev.u64 as RawFd;
|
entry
|
||||||
let evs = ev.events;
|
};
|
||||||
q.push_back(Completion::FdReady {
|
if let Some((pid, epoch)) = entry {
|
||||||
fd,
|
let Some(inner) = rt.upgrade() else { return };
|
||||||
events: evs,
|
inner.io_fd_waiters.fetch_sub(1, Ordering::AcqRel);
|
||||||
});
|
inner.unpark_at(pid, epoch);
|
||||||
pushed_any = true;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if pushed_any {
|
|
||||||
wake_scheduler(wake_write);
|
|
||||||
}
|
}
|
||||||
if shutdown_requested {
|
if shutdown_requested {
|
||||||
return;
|
return;
|
||||||
@@ -460,27 +447,8 @@ fn epoll_loop(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Write one byte to the scheduler's wake pipe. Retries on EINTR; ignores
|
|
||||||
/// EAGAIN (pipe full means there's already an outstanding wake we haven't
|
|
||||||
/// consumed yet, which is sufficient).
|
|
||||||
fn wake_scheduler(wake_write: RawFd) {
|
|
||||||
let buf: [u8; 1] = [0];
|
|
||||||
unsafe {
|
|
||||||
loop {
|
|
||||||
let n = libc::write(wake_write, buf.as_ptr() as *const _, 1);
|
|
||||||
if n < 0 {
|
|
||||||
let e = *libc::__errno_location();
|
|
||||||
if e == libc::EINTR {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// Pipe helpers (unchanged from v0.2)
|
// Pipe helper
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
fn make_pipe() -> io::Result<(RawFd, RawFd)> {
|
fn make_pipe() -> io::Result<(RawFd, RawFd)> {
|
||||||
@@ -491,46 +459,3 @@ fn make_pipe() -> io::Result<(RawFd, RawFd)> {
|
|||||||
}
|
}
|
||||||
Ok((fds[0], fds[1]))
|
Ok((fds[0], fds[1]))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Drain pending bytes from the wake pipe. The scheduler calls this after
|
|
||||||
/// a `poll` wakeup so the next idle call sees an empty pipe.
|
|
||||||
pub fn drain_wake_pipe(fd: RawFd) {
|
|
||||||
let mut buf = [0u8; 64];
|
|
||||||
loop {
|
|
||||||
let n = unsafe { libc::read(fd, buf.as_mut_ptr() as *mut _, buf.len()) };
|
|
||||||
if n <= 0 {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Block on `fd` for up to `timeout`, returning when either there's data
|
|
||||||
/// to read or the timeout elapses. `None` for `timeout` means wait forever.
|
|
||||||
pub fn poll_wake(fd: RawFd, timeout: Option<std::time::Duration>) {
|
|
||||||
let timeout_ms: libc::c_int = match timeout {
|
|
||||||
None => -1,
|
|
||||||
Some(d) => {
|
|
||||||
let ms = d.as_millis();
|
|
||||||
if ms > i32::MAX as u128 {
|
|
||||||
i32::MAX
|
|
||||||
} else {
|
|
||||||
ms as i32
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let mut pfd = libc::pollfd {
|
|
||||||
fd,
|
|
||||||
events: libc::POLLIN,
|
|
||||||
revents: 0,
|
|
||||||
};
|
|
||||||
loop {
|
|
||||||
let r = unsafe { libc::poll(&mut pfd as *mut _, 1, timeout_ms) };
|
|
||||||
if r < 0 {
|
|
||||||
let e = unsafe { *libc::__errno_location() };
|
|
||||||
if e == libc::EINTR {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
+54
-33
@@ -11,32 +11,46 @@
|
|||||||
//!
|
//!
|
||||||
//! See `LOOM.md` for the design intent and the deferred-for-later list.
|
//! See `LOOM.md` for the design intent and the deferred-for-later list.
|
||||||
|
|
||||||
pub mod stack;
|
// Docs are part of the contract: broken/private intra-doc links fail `cargo doc`,
|
||||||
pub mod context;
|
// and every doctest is compiled with `deny(warnings)` under `cargo test --doc`.
|
||||||
pub mod preempt;
|
#![deny(rustdoc::broken_intra_doc_links)]
|
||||||
pub mod pid;
|
#![deny(rustdoc::private_intra_doc_links)]
|
||||||
|
#![deny(rustdoc::redundant_explicit_links)]
|
||||||
|
#![deny(rustdoc::invalid_codeblock_attributes)]
|
||||||
|
#![deny(rustdoc::invalid_rust_codeblocks)]
|
||||||
|
#![doc(test(attr(deny(warnings))))]
|
||||||
|
|
||||||
pub mod actor;
|
pub mod actor;
|
||||||
|
pub mod causal;
|
||||||
pub mod channel;
|
pub mod channel;
|
||||||
pub mod scheduler;
|
#[cfg(feature = "cluster")]
|
||||||
pub mod supervisor;
|
pub mod cluster;
|
||||||
pub mod timer;
|
pub mod context;
|
||||||
pub mod io;
|
|
||||||
pub mod mutex;
|
|
||||||
pub mod monitor;
|
|
||||||
pub mod registry;
|
|
||||||
pub mod pg;
|
|
||||||
pub mod link;
|
|
||||||
pub mod gen_server;
|
pub mod gen_server;
|
||||||
pub mod statem;
|
pub mod gen_statem;
|
||||||
pub mod introspect;
|
pub mod introspect;
|
||||||
|
pub mod io;
|
||||||
|
pub mod link;
|
||||||
|
pub mod monitor;
|
||||||
|
pub mod mutex;
|
||||||
#[cfg(feature = "observer")]
|
#[cfg(feature = "observer")]
|
||||||
pub mod observer;
|
pub mod observer;
|
||||||
pub mod runtime;
|
pub(crate) mod park;
|
||||||
|
pub mod pg;
|
||||||
|
pub mod pid;
|
||||||
|
pub mod preempt;
|
||||||
pub(crate) mod raw_mutex;
|
pub(crate) mod raw_mutex;
|
||||||
pub(crate) mod slot_state;
|
pub mod registry;
|
||||||
pub(crate) mod sync_shim;
|
|
||||||
#[doc(hidden)] // pub only so benches/rq_micro.rs can drive the raw structures
|
#[doc(hidden)] // pub only so benches/rq_micro.rs can drive the raw structures
|
||||||
pub mod run_queue;
|
pub mod run_queue;
|
||||||
|
pub mod runtime;
|
||||||
|
pub mod scheduler;
|
||||||
|
pub(crate) mod signal;
|
||||||
|
pub(crate) mod slot_state;
|
||||||
|
pub mod stack;
|
||||||
|
pub mod supervisor;
|
||||||
|
pub(crate) mod sync_shim;
|
||||||
|
pub mod timer;
|
||||||
pub mod trace;
|
pub mod trace;
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
@@ -56,34 +70,41 @@ pub use channel::{
|
|||||||
};
|
};
|
||||||
pub use gen_server::{
|
pub use gen_server::{
|
||||||
call, cast, shutdown, whereis_server, CallError, CallTimeoutError, CastError, GenServer,
|
call, cast, shutdown, whereis_server, CallError, CallTimeoutError, CastError, GenServer,
|
||||||
NamedServerBuilder, ServerBuilder, ServerCtx, ServerName, ServerRef, TimerHandle, Watcher,
|
GenServerBuilder, GenServerCtx, GenServerName, GenServerRef, NamedGenServerBuilder,
|
||||||
|
ShutdownAction, StopHandle, TimerHandle, Watcher,
|
||||||
};
|
};
|
||||||
pub use statem::{
|
pub use gen_statem::{
|
||||||
CallError as StatemCallError, Cx, Machine, Reply, Resolution, SendError as StatemSendError,
|
CallError as GenStatemCallError, Cx, GenStatemName, GenStatemRef, Machine, Reply, Resolution,
|
||||||
StatemRef,
|
SendError as GenStatemSendError,
|
||||||
};
|
};
|
||||||
pub use introspect::{
|
pub use introspect::{
|
||||||
actor_info, snapshot, tree, tree_from, ActorInfo, ActorState, RuntimeSnapshot, RuntimeTree,
|
actor_info, snapshot, tree, tree_from, ActorInfo, ActorState, RuntimeSnapshot, RuntimeTree,
|
||||||
TreeNode, SNAPSHOT_FORMAT_VERSION,
|
StackInfo, TreeNode, SNAPSHOT_FORMAT_VERSION,
|
||||||
};
|
};
|
||||||
|
pub use link::{link, trap_exit, unlink, ExitSignal};
|
||||||
|
pub use monitor::{
|
||||||
|
demonitor, mark_watchable, monitor, terminal_reason, Down, DownReason, Monitor, MonitorId,
|
||||||
|
};
|
||||||
|
pub use mutex::{LockTimeout, Mutex, MutexGuard};
|
||||||
#[cfg(feature = "observer")]
|
#[cfg(feature = "observer")]
|
||||||
pub use observer::{ObserverReply, ObserverRequest};
|
pub use observer::{ObserverReply, ObserverRequest};
|
||||||
pub use link::{link, trap_exit, unlink, ExitSignal};
|
pub use pg::{
|
||||||
pub use monitor::{demonitor, monitor, Down, DownReason, Monitor, MonitorId};
|
dispatch, join, leave, members, members_as, pick, pick_as, Incarnation, Member, NodeId,
|
||||||
pub use mutex::{LockTimeout, Mutex, MutexGuard};
|
};
|
||||||
pub use pid::{Addressable, Erased, Name, Pid, RawPid};
|
pub use pid::{Addressable, Erased, Name, Pid, RawPid};
|
||||||
pub use pg::{dispatch, join, leave, members, members_as, pick, pick_as, Incarnation, Member, NodeId};
|
|
||||||
pub use registry::{
|
pub use registry::{
|
||||||
install, lookup_as, register, send, send_dyn, send_to, unregister, whereis, RegisterError,
|
install, lookup_as, register, resolve_name, send, send_dyn, send_to, unregister, whereis,
|
||||||
SendError,
|
NameResolution, RegisterError, SendError,
|
||||||
};
|
};
|
||||||
pub use runtime::{init, Config, Runtime};
|
pub use runtime::{init, Config, Runtime, RuntimeHandle};
|
||||||
pub use scheduler::{
|
pub use scheduler::{
|
||||||
block_on_io, cancel_timer, request_stop, run, self_pid, send_after, send_after_named, sleep,
|
block_on_io, cancel_timer, request_shutdown, request_stop, run, self_pid, send_after,
|
||||||
spawn, spawn_addr, spawn_under, wait_readable, wait_readable_timeout, wait_writable,
|
send_after_named, send_after_named_wall, send_after_wall, sleep, sleep_wall, spawn, spawn_addr,
|
||||||
wait_writable_timeout, yield_now, FdArm, JoinError, JoinHandle,
|
spawn_addr_with, spawn_monitor, spawn_monitor_with, spawn_under, spawn_under_with, spawn_with,
|
||||||
|
try_spawn, try_spawn_under_with, wait_readable, wait_readable_timeout, wait_writable,
|
||||||
|
wait_writable_timeout, yield_now, FdArm, JoinError, JoinHandle, SpawnError, SpawnOpts,
|
||||||
};
|
};
|
||||||
pub use supervisor::{ChildSpec, OneForOne, Restart, Signal, Strategy};
|
pub use supervisor::{ChildSpec, OneForOne, Restart, Shutdown, Signal, Strategy};
|
||||||
pub use timer::TimerId;
|
pub use timer::TimerId;
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|||||||
+8
-2
@@ -135,7 +135,10 @@ pub fn link<A>(target: Pid<A>) {
|
|||||||
|
|
||||||
if registered_on_target {
|
if registered_on_target {
|
||||||
with_runtime(|inner| {
|
with_runtime(|inner| {
|
||||||
let slot = inner.slot_at(me).expect("link: own slot vanished");
|
let slot = match inner.slot_at(me) {
|
||||||
|
Some(s) => s,
|
||||||
|
None => panic!("smarm: link own slot vanished (core corrupt)"),
|
||||||
|
};
|
||||||
let mut cold = slot.cold.lock();
|
let mut cold = slot.cold.lock();
|
||||||
if !cold.links.contains(&target) {
|
if !cold.links.contains(&target) {
|
||||||
cold.links.push(target);
|
cold.links.push(target);
|
||||||
@@ -154,7 +157,10 @@ pub fn link<A>(target: Pid<A>) {
|
|||||||
});
|
});
|
||||||
match my_trap {
|
match my_trap {
|
||||||
Some(tx) => {
|
Some(tx) => {
|
||||||
let _ = tx.send(ExitSignal { from: target, reason: DownReason::NoProc });
|
let _ = tx.send(ExitSignal {
|
||||||
|
from: target,
|
||||||
|
reason: DownReason::NoProc,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
None => request_stop(me),
|
None => request_stop(me),
|
||||||
}
|
}
|
||||||
|
|||||||
+230
-89
@@ -1,49 +1,85 @@
|
|||||||
//! Process monitors.
|
//! Find out when another actor dies, without it knowing or caring that you're
|
||||||
|
//! watching.
|
||||||
//!
|
//!
|
||||||
//! `monitor(target)` asks the runtime to deliver a single [`Down`] when
|
//! Say one actor manages a pool of workers and needs to know when a worker
|
||||||
//! `target` terminates, and hands back a [`Monitor`] — the [`Receiver`] to read
|
//! exits, so it can replace it. The worker does not need to know it is being
|
||||||
//! it from, plus the identity (`id`, `target`) needed to take the registration
|
//! watched, and nothing about the worker's own behavior should change because
|
||||||
//! back down with [`demonitor`]. A monitor is:
|
//! someone is watching it. That is what [`monitor`] is for: call
|
||||||
|
//! `monitor(target)` to get a [`Monitor`], and read exactly one [`Down`]
|
||||||
|
//! message off `monitor.rx` whenever `target` terminates, however it
|
||||||
|
//! terminates.
|
||||||
//!
|
//!
|
||||||
//! - **unidirectional** — the watcher learns of the target's death, but the
|
//! ```
|
||||||
//! target learns nothing of the watcher, and the watcher is unaffected by
|
//! use smarm::{monitor, run, spawn, DownReason};
|
||||||
//! the death beyond the notification (contrast a *link*, which propagates
|
|
||||||
//! failure);
|
|
||||||
//! - **one-shot** — exactly one `Down` is ever sent for a given monitor.
|
|
||||||
//! The returned channel closes afterwards, so a second `recv()` yields
|
|
||||||
//! `Err(RecvError)`.
|
|
||||||
//!
|
//!
|
||||||
//! This generalizes the older single-`supervisor_channel` mechanism: a
|
//! run(|| {
|
||||||
//! supervisor is just a hard-wired monitor that the parent installs at spawn
|
//! let worker = spawn(|| {
|
||||||
//! time. Here any actor may monitor any pid, any number of times.
|
//! // does some work, then returns
|
||||||
|
//! });
|
||||||
|
//! let pid = worker.pid();
|
||||||
//!
|
//!
|
||||||
//! ## Reasons
|
//! let m = monitor(pid);
|
||||||
|
//! let _ = worker.join();
|
||||||
//!
|
//!
|
||||||
//! [`DownReason`] is deliberately payload-free. A panicking actor's payload
|
//! let down = m.rx.recv().expect("monitor channel closed before Down");
|
||||||
//! has a single owner and is delivered to whoever `join()`s the actor (as
|
//! assert_eq!(down.pid, pid);
|
||||||
//! `JoinError`); a monitor only learns *that* it panicked, not the value.
|
//! assert_eq!(down.reason, DownReason::Exit);
|
||||||
//! Monitoring a pid that is already gone (reclaimed, or never alive) yields
|
//! });
|
||||||
//! [`DownReason::NoProc`] immediately, mirroring Erlang's `noproc`.
|
//! ```
|
||||||
//!
|
//!
|
||||||
//! ## Demonitoring
|
//! A monitor is one-directional and one-shot:
|
||||||
//!
|
//!
|
||||||
//! Each `monitor()` registration is tagged with a process-unique [`MonitorId`].
|
//! - **One-directional**: the watcher learns that the target died, but the
|
||||||
//! [`demonitor`] removes the registration named by a [`Monitor`] from its
|
//! target is completely unaffected. It never learns it was being watched,
|
||||||
//! target's slot, returning `Some(id)` if a live registration was found or
|
//! and its own behavior and lifetime do not change because of the monitor.
|
||||||
//! `None` if it had already fired (or the target is gone). Dropping the
|
//! This is the opposite of a [`link`](mod@crate::link), which is bidirectional:
|
||||||
//! [`Monitor`] afterwards discards any `Down` that the target had *already*
|
//! linking two actors means an abnormal death on either side can bring the
|
||||||
//! queued — the equivalent of Erlang's `demonitor(Ref, [flush])`.
|
//! other down too. Reach for a monitor when you just want to *know*; reach
|
||||||
|
//! for a link when a peer's crash should actually stop you.
|
||||||
|
//! - **One-shot**: you get exactly one [`Down`] per `monitor()` call, then the
|
||||||
|
//! channel closes. Calling `monitor` again on the same target (or a
|
||||||
|
//! different one) gives you an independent registration with its own
|
||||||
|
//! [`Monitor`] and its own one-shot channel; nothing stops you from
|
||||||
|
//! monitoring the same actor many times over; each call is watched and
|
||||||
|
//! fires on its own.
|
||||||
//!
|
//!
|
||||||
//! ## Races
|
//! ## Why a monitor never hands you the panic value
|
||||||
//!
|
//!
|
||||||
//! Registration (below) and `finalize_actor` (in `runtime`) both run under the
|
//! If the target panicked, [`Down`] tells you *that* it panicked
|
||||||
//! shared-state mutex, so a target that is still alive when its monitor is
|
//! ([`DownReason::Panic`]), but not the panic's payload. The payload has a
|
||||||
//! registered is guaranteed to deliver a real `Down`; there is no window in
|
//! single owner: it is handed to whichever caller `join()`s the actor's
|
||||||
//! which the death slips between the liveness check and the registration.
|
//! [`JoinHandle`](crate::JoinHandle), as a `JoinError`. A monitor only needs
|
||||||
//! `demonitor` is protected by the generation half of the pid: if the target
|
//! to know that something went wrong, not reproduce the exact value that
|
||||||
//! has died and its slot index been recycled, `slot_mut(target)` fails the
|
//! caused it, so it gets the reason and nothing else.
|
||||||
//! generation check and `demonitor` is a clean no-op — it can never strip a
|
//!
|
||||||
//! *different* actor's monitor that happens to share the slot index.
|
//! Monitoring a target that is already gone (it finished and was cleaned up,
|
||||||
|
//! or the pid never pointed at a real actor) is not an error: you get a
|
||||||
|
//! [`Down`] with [`DownReason::NoProc`] right away, instead of waiting
|
||||||
|
//! forever for something that already happened.
|
||||||
|
//!
|
||||||
|
//! ## Stopping a monitor early
|
||||||
|
//!
|
||||||
|
//! [`demonitor`] cancels a monitor before it fires. If the registration was
|
||||||
|
//! still live, it removes it and returns `Some` of the monitor's id: no
|
||||||
|
//! `Down` will arrive on that channel from here on. If the target had already
|
||||||
|
//! died and its `Down` already sent, there is nothing left to cancel and
|
||||||
|
//! `demonitor` returns `None`; the `Down` you already have (or that is
|
||||||
|
//! already sitting in the channel) is unaffected.
|
||||||
|
//!
|
||||||
|
//! If you want to cancel *and* make sure a `Down` that already arrived is
|
||||||
|
//! discarded without reading it, just drop the [`Monitor`]: dropping it closes
|
||||||
|
//! its receiver, and any queued `Down` is dropped along with it.
|
||||||
|
//!
|
||||||
|
//! ## Correctness notes for implementers
|
||||||
|
//!
|
||||||
|
//! A target that is still alive at the moment `monitor()` registers is
|
||||||
|
//! guaranteed to eventually produce a real `Down`: registration and the
|
||||||
|
//! target's own termination bookkeeping run under the same lock, so there is
|
||||||
|
//! no window in which the target could die without the just-added
|
||||||
|
//! registration seeing it. `demonitor` is similarly race-free against a target
|
||||||
|
//! that has since died and had its slot reused by a new, unrelated actor: it
|
||||||
|
//! is checked against the exact monitored incarnation, so it can never remove
|
||||||
|
//! a different actor's registration by accident, it simply reports `None`.
|
||||||
|
|
||||||
use crate::channel::{channel, Receiver, Sender};
|
use crate::channel::{channel, Receiver, Sender};
|
||||||
use crate::pid::Pid;
|
use crate::pid::Pid;
|
||||||
@@ -51,8 +87,8 @@ use crate::scheduler::with_runtime;
|
|||||||
|
|
||||||
/// Why a monitored actor went down.
|
/// Why a monitored actor went down.
|
||||||
///
|
///
|
||||||
/// `Copy` because it carries no payload — see the module docs for why the
|
/// Carries no payload: see the module docs for why a monitor never receives
|
||||||
/// panic payload is *not* included here.
|
/// the panic value itself.
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
pub enum DownReason {
|
pub enum DownReason {
|
||||||
/// The target returned normally.
|
/// The target returned normally.
|
||||||
@@ -62,6 +98,11 @@ pub enum DownReason {
|
|||||||
Panic,
|
Panic,
|
||||||
/// The target was cooperatively cancelled via `request_stop`.
|
/// The target was cooperatively cancelled via `request_stop`.
|
||||||
Stopped,
|
Stopped,
|
||||||
|
/// A graceful shutdown was requested via `request_shutdown`. Only ever
|
||||||
|
/// appears in an [`ExitSignal`](crate::link::ExitSignal) delivered to a
|
||||||
|
/// trapping actor — never in a [`Down`]: a target that honours the request
|
||||||
|
/// exits *normally*, one that does not trap is `Stopped`.
|
||||||
|
Shutdown,
|
||||||
/// The target was already gone (finished and reclaimed, or never alive)
|
/// The target was already gone (finished and reclaimed, or never alive)
|
||||||
/// at the moment `monitor()` was called.
|
/// at the moment `monitor()` was called.
|
||||||
NoProc,
|
NoProc,
|
||||||
@@ -76,21 +117,22 @@ pub struct Down {
|
|||||||
pub reason: DownReason,
|
pub reason: DownReason,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A process-unique identifier for one `monitor()` registration.
|
/// A unique identifier for one [`monitor`] registration.
|
||||||
///
|
///
|
||||||
/// Opaque and `Copy`. Allocated from a monotonic counter in shared state, so
|
/// Opaque and `Copy`. Never reused for the life of the runtime, so if you
|
||||||
/// it is never reused for the lifetime of the runtime — distinct `monitor()`
|
/// monitor the same target more than once, each call's id is distinct. This
|
||||||
/// calls on the same target get distinct ids, which is what lets [`demonitor`]
|
/// is what lets [`demonitor`] tear down exactly one of several monitors on
|
||||||
/// tear down exactly one of several monitors on a target.
|
/// the same target without disturbing the others.
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
pub struct MonitorId(pub(crate) u64);
|
pub struct MonitorId(pub(crate) u64);
|
||||||
|
|
||||||
/// A live monitor: the receiving end of the one-shot [`Down`] channel, plus the
|
/// A live monitor: the receiving end of the one-shot [`Down`] channel, plus the
|
||||||
/// identity needed to [`demonitor`] it.
|
/// identity needed to [`demonitor`] it.
|
||||||
///
|
///
|
||||||
/// Read the notification from [`Monitor::rx`]. Not `Clone` (the receiver is a
|
/// Read the notification from [`Monitor::rx`]. Not `Clone`, since only one
|
||||||
/// single consumer). Dropping it closes the receiving end; if a `Down` was
|
/// side is meant to consume it. Dropping a `Monitor` closes the receiving
|
||||||
/// already queued it is discarded with the channel.
|
/// end; if a `Down` had already arrived but was never read, it is discarded
|
||||||
|
/// along with it.
|
||||||
pub struct Monitor {
|
pub struct Monitor {
|
||||||
/// This registration's process-unique id.
|
/// This registration's process-unique id.
|
||||||
pub id: MonitorId,
|
pub id: MonitorId,
|
||||||
@@ -103,65 +145,164 @@ pub struct Monitor {
|
|||||||
/// Monitor `target`. Returns a [`Monitor`] whose `rx` receives exactly one
|
/// Monitor `target`. Returns a [`Monitor`] whose `rx` receives exactly one
|
||||||
/// [`Down`].
|
/// [`Down`].
|
||||||
///
|
///
|
||||||
|
/// To monitor a child you are spawning yourself, prefer
|
||||||
|
/// [`spawn_monitor`](crate::spawn_monitor): `spawn` followed by `monitor` on
|
||||||
|
/// the returned pid can race the child's death and observe `NoProc` instead
|
||||||
|
/// of its real reason.
|
||||||
|
///
|
||||||
/// If `target` is still live, the `Down` arrives when it terminates. If
|
/// If `target` is still live, the `Down` arrives when it terminates. If
|
||||||
/// `target` is already gone, a [`DownReason::NoProc`] `Down` is queued
|
/// `target` is already gone, a [`DownReason::NoProc`] `Down` is queued
|
||||||
/// immediately so the caller's `rx.recv()` returns without parking.
|
/// immediately so the caller's `rx.recv()` returns without parking.
|
||||||
pub fn monitor<A>(target: Pid<A>) -> Monitor {
|
pub fn monitor<A>(target: Pid<A>) -> Monitor {
|
||||||
let target = target.erase();
|
let target = target.erase();
|
||||||
let (tx, rx) = channel::<Down>();
|
let (tx, rx) = channel::<Down>();
|
||||||
|
let id = with_runtime(|inner| inner.alloc_monitor_id());
|
||||||
// Register under the target's cold lock. `tx.clone()` takes the channel's
|
if !register_monitor(target, id, &tx) {
|
||||||
// own lock — a Channel-class RawMutex, explicitly permitted *under* a Leaf
|
let _ = tx.send(Down {
|
||||||
// (cold) lock by the lock order (see raw_mutex.rs). We must still not
|
pid: target,
|
||||||
// *send* under the lock, as `Sender::send` can unpark a parked receiver,
|
reason: DownReason::NoProc,
|
||||||
// and there's no reason to nest that.
|
});
|
||||||
let (id, registered) = with_runtime(|inner| {
|
|
||||||
let id = inner.alloc_monitor_id();
|
|
||||||
let registered = match inner.slot_at(target) {
|
|
||||||
Some(slot) => {
|
|
||||||
let mut cold = slot.cold.lock();
|
|
||||||
if slot.is_live_for(target) {
|
|
||||||
cold.monitors.push((id, tx.clone()));
|
|
||||||
true
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None => false,
|
|
||||||
};
|
|
||||||
(id, registered)
|
|
||||||
});
|
|
||||||
|
|
||||||
if !registered {
|
|
||||||
let _ = tx.send(Down { pid: target, reason: DownReason::NoProc });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Monitor { id, target, rx }
|
Monitor { id, target, rx }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Cancel the monitor `m`. Returns `Some(id)` if a live registration was found
|
/// Register a monitor `id` on `target` that delivers its `Down` to `tx` — the
|
||||||
/// on the target's slot and removed, or `None` if there was nothing to remove
|
/// primitive under [`monitor`], split out so a caller can fan many monitors
|
||||||
/// — the target already fired its `Down` (the registration is drained on
|
/// into ONE channel (process groups: every membership's death lands on the
|
||||||
/// finalize), was never alive (`NoProc`), or has been reclaimed.
|
/// reaper's single inbox). Returns `false` if `target` is already gone, in
|
||||||
|
/// which case nothing is registered and the caller decides what to queue
|
||||||
|
/// (`monitor` sends `NoProc`). The caller allocates `id` up front so it can
|
||||||
|
/// record the registration *before* arming it.
|
||||||
///
|
///
|
||||||
/// This stops any *future* `Down`. To also discard a `Down` the target may have
|
/// Implementation note: registration happens under the target's cold lock.
|
||||||
/// *already* queued (the finalize-races-demonitor case), drop `m` afterwards;
|
/// `tx.clone()` takes the channel's own lock, a Channel-class RawMutex, which
|
||||||
/// dropping the [`Monitor`] closes its receiver and the queued notice goes with
|
/// is explicitly permitted under a Leaf (cold) lock by the lock order
|
||||||
/// it — the analogue of Erlang's `demonitor(Ref, [flush])`.
|
/// documented in raw_mutex.rs. We must still not *send* under the lock, since
|
||||||
pub fn demonitor(m: &Monitor) -> Option<MonitorId> {
|
/// `Sender::send` can unpark a parked receiver, and there's no reason to nest
|
||||||
// Remove the registration under the target's cold lock, but move the
|
/// that.
|
||||||
// `Sender` *out* and let it drop only after the lock is released:
|
pub(crate) fn register_monitor(target: Pid, id: MonitorId, tx: &Sender<Down>) -> bool {
|
||||||
// dropping the last sender runs `Sender::drop`, which may unpark a parked
|
with_runtime(|inner| match inner.slot_at(target) {
|
||||||
// receiver — legal under a cold lock, but pointless to nest.
|
Some(slot) => {
|
||||||
|
let mut cold = slot.cold.lock();
|
||||||
|
if slot.is_live_for(target) {
|
||||||
|
cold.monitors.push((id, tx.clone()));
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => false,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Remove registration `id` from `target` — the primitive under
|
||||||
|
/// [`demonitor`], for callers that hold only the id (see
|
||||||
|
/// [`register_monitor`]). `None` if the registration is not there: already
|
||||||
|
/// fired, already removed, or the slot has moved on to a new tenant.
|
||||||
|
///
|
||||||
|
/// The registration is removed under the target's cold lock, but the
|
||||||
|
/// `Sender` is moved *out* and dropped only after the lock is released.
|
||||||
|
/// Dropping the last sender runs `Sender::drop`, which may unpark a parked
|
||||||
|
/// receiver; legal under a cold lock, but pointless to nest.
|
||||||
|
pub(crate) fn unregister_monitor(target: Pid, id: MonitorId) -> Option<MonitorId> {
|
||||||
let removed: Option<(MonitorId, Sender<Down>)> = with_runtime(|inner| {
|
let removed: Option<(MonitorId, Sender<Down>)> = with_runtime(|inner| {
|
||||||
let slot = inner.slot_at(m.target)?;
|
let slot = inner.slot_at(target)?;
|
||||||
let mut cold = slot.cold.lock();
|
let mut cold = slot.cold.lock();
|
||||||
if slot.generation() != m.target.generation() {
|
if slot.generation() != target.generation() {
|
||||||
return None; // slot reused; the Down already fired
|
return None; // slot reused; the Down already fired
|
||||||
}
|
}
|
||||||
let pos = cold.monitors.iter().position(|(mid, _)| *mid == m.id)?;
|
let pos = cold.monitors.iter().position(|(mid, _)| *mid == id)?;
|
||||||
Some(cold.monitors.remove(pos))
|
Some(cold.monitors.remove(pos))
|
||||||
});
|
});
|
||||||
// `removed`'s sender drops here, outside the lock.
|
// `removed`'s sender drops here, outside the lock.
|
||||||
removed.map(|(id, _sender)| id)
|
removed.map(|(id, _sender)| id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Flag `target`'s tenancy as watchable: its death will stamp the slot's
|
||||||
|
/// terminal record (see [`terminal_reason`]), exactly as registering a name
|
||||||
|
/// does. The bridge calls this wherever a smarm pid is *encoded across the
|
||||||
|
/// boundary* — a contract reply, an introspection listing — because BEAM can
|
||||||
|
/// only watch pids it holds, and can only hold pids that crossed. Keeping the
|
||||||
|
/// bit rare is what keeps the record alive: anonymous never-exported churn
|
||||||
|
/// (holder threads, egress tasks) stays ineligible and cannot evict a
|
||||||
|
/// watchable tenancy's record from a LIFO-recycled slot.
|
||||||
|
///
|
||||||
|
/// Generation-checked and live-screened: marking a pid whose tenancy already
|
||||||
|
/// ended is a no-op — its record either exists (it was flagged before dying)
|
||||||
|
/// or is honestly unknowable. Same `Runtime::run()` context contract as
|
||||||
|
/// [`monitor`].
|
||||||
|
pub fn mark_watchable<A>(target: Pid<A>) {
|
||||||
|
let target = target.erase();
|
||||||
|
with_runtime(|inner| {
|
||||||
|
if let Some(slot) = inner.slot_at(target) {
|
||||||
|
// Cold lock FIRST: finalize publishes Done and checks the
|
||||||
|
// watchable bit under this same lock, so the mark either lands
|
||||||
|
// before finalize reads it (the death stamps) or observes the
|
||||||
|
// tenancy already dead (no-op). No lost-stamp window between an
|
||||||
|
// unlocked liveness read and the flag set.
|
||||||
|
let mut cold = slot.cold.lock();
|
||||||
|
if slot.is_live_for(target) {
|
||||||
|
cold.watchable = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Whether `target` is live *and* its tenancy is watchable. The cluster's
|
||||||
|
/// remote-monitor admission check (RFC 010 c12): a peer may monitor a pid only
|
||||||
|
/// if that pid was exposed or crossed the wire (the D12 set-sites), and a
|
||||||
|
/// live-but-unwatchable pid answers exactly like a dead one — no liveness leak
|
||||||
|
/// beyond what `watchable` already grants. Same context contract as
|
||||||
|
/// [`monitor`].
|
||||||
|
#[cfg(feature = "cluster")]
|
||||||
|
pub(crate) fn is_watchable<A>(target: Pid<A>) -> bool {
|
||||||
|
let target = target.erase();
|
||||||
|
with_runtime(|inner| {
|
||||||
|
inner.slot_at(target).is_some_and(|slot| {
|
||||||
|
let cold = slot.cold.lock();
|
||||||
|
slot.is_live_for(target) && cold.watchable
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The terminal [`DownReason`] of the tenancy `target` names, if that tenancy
|
||||||
|
/// ever registered a name and is the *most recent named* death of its slot:
|
||||||
|
/// finalize stamps the slot with `(generation, reason)` for once-registered
|
||||||
|
/// tenancies (anonymous green-thread churn does not stamp — nor evict), and
|
||||||
|
/// the record survives reclaim and the next tenant's install, until the next
|
||||||
|
/// *named* tenant of the slot itself dies. `None` means the pid never lived,
|
||||||
|
/// is still alive, never held a name, or its record was overwritten by a
|
||||||
|
/// later named tenancy's death — callers fall back to `NoProc` semantics.
|
||||||
|
///
|
||||||
|
/// This exists for watch-installers that raced their target's death (bridge
|
||||||
|
/// soak signature 4): a `NoProc` observed at install time can be upgraded to
|
||||||
|
/// the real reason while the record still matches, which is exactly what an
|
||||||
|
/// install that had won the race would have delivered. It does NOT change
|
||||||
|
/// [`monitor`]'s own semantics — monitoring a stale pid still queues `NoProc`,
|
||||||
|
/// the same shape Erlang gives — the upgrade is the caller's deliberate act.
|
||||||
|
/// Same context contract as [`monitor`]: must run inside `Runtime::run()`.
|
||||||
|
pub fn terminal_reason<A>(target: Pid<A>) -> Option<DownReason> {
|
||||||
|
let target = target.erase();
|
||||||
|
with_runtime(|inner| {
|
||||||
|
let slot = inner.slot_at(target)?;
|
||||||
|
let cold = slot.cold.lock();
|
||||||
|
match cold.terminal {
|
||||||
|
Some((generation, reason)) if generation == target.generation() => Some(reason),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Cancel the monitor `m`. Returns `Some(id)` if a live registration was found
|
||||||
|
/// and removed, so no `Down` will arrive on `m.rx` from here on. Returns
|
||||||
|
/// `None` if there was nothing left to remove: the target had already gone
|
||||||
|
/// down and its `Down` was already sent (or is already sitting in the
|
||||||
|
/// channel, unread).
|
||||||
|
///
|
||||||
|
/// This only stops a *future* `Down`. If you also want to discard a `Down`
|
||||||
|
/// that already arrived (or is about to, in a race with this call), drop `m`
|
||||||
|
/// instead of, or in addition to, calling this: dropping the [`Monitor`]
|
||||||
|
/// closes its receiver and any queued notice is discarded with it.
|
||||||
|
pub fn demonitor(m: &Monitor) -> Option<MonitorId> {
|
||||||
|
unregister_monitor(m.target, m.id)
|
||||||
|
}
|
||||||
|
|||||||
+236
-43
@@ -1,12 +1,89 @@
|
|||||||
//! Actor-aware mutex with mandatory timeout.
|
//! Shared mutable state across actors, when a channel is overkill.
|
||||||
//!
|
//!
|
||||||
//! `Mutex<T>` parks the calling *green* thread on contention rather than
|
//! smarm actors normally coordinate by sending messages, and for a piece of
|
||||||
//! blocking the OS thread. Every lock attempt is bounded by a timeout.
|
//! owned state the right tool is usually a `gen_server`: one actor holds the
|
||||||
|
//! data and everyone else talks to it. Sometimes that is more machinery than
|
||||||
|
//! you need, and plain shared, lockable state is simpler: [`Mutex<T>`] is
|
||||||
|
//! that escape hatch. It behaves like `std::sync::Mutex<T>`, guarding a value
|
||||||
|
//! of type `T` behind a guard that gives you `&mut T` while held, but it is
|
||||||
|
//! built for smarm's actors rather than OS threads.
|
||||||
//!
|
//!
|
||||||
//! Internals use `Arc<std::sync::Mutex<...>>` so the type is genuinely
|
//! The key difference from `std::sync::Mutex` is what happens on contention.
|
||||||
//! `Send + Sync` and can be shared across scheduler threads.
|
//! [`Mutex::lock`] parks the calling actor (a cooperatively scheduled green
|
||||||
|
//! thread) rather than blocking the underlying OS thread, so other actors on
|
||||||
|
//! the same OS thread keep running while it waits. And every lock attempt is
|
||||||
|
//! bounded by a timeout: an actor that hangs on to the lock forever (stuck in
|
||||||
|
//! a bug, or just slow) would otherwise wedge every other actor waiting on
|
||||||
|
//! it, so smarm makes the wait bounded by default instead of leaving it up
|
||||||
|
//! to you to remember.
|
||||||
//!
|
//!
|
||||||
//! Fairness: FIFO. Poisoning: none. Reentrance: deadlock (caller bug).
|
//! ## A first lock
|
||||||
|
//!
|
||||||
|
//! ```
|
||||||
|
//! use smarm::{run, spawn, Mutex};
|
||||||
|
//!
|
||||||
|
//! run(|| {
|
||||||
|
//! let counter = Mutex::new(0u32);
|
||||||
|
//!
|
||||||
|
//! // Mutex::clone() is cheap and hands out another handle to the SAME
|
||||||
|
//! // underlying value, much like Arc::clone: every clone shares one lock
|
||||||
|
//! // and one value, so mutations through one are visible through all.
|
||||||
|
//! let a = counter.clone();
|
||||||
|
//! let b = counter.clone();
|
||||||
|
//!
|
||||||
|
//! let h1 = spawn(move || {
|
||||||
|
//! let mut guard = a.lock().unwrap();
|
||||||
|
//! *guard += 1;
|
||||||
|
//! });
|
||||||
|
//! let h2 = spawn(move || {
|
||||||
|
//! let mut guard = b.lock().unwrap();
|
||||||
|
//! *guard += 1;
|
||||||
|
//! });
|
||||||
|
//! h1.join().unwrap();
|
||||||
|
//! h2.join().unwrap();
|
||||||
|
//!
|
||||||
|
//! assert_eq!(*counter.lock().unwrap(), 2);
|
||||||
|
//! });
|
||||||
|
//! ```
|
||||||
|
//!
|
||||||
|
//! ## Choosing a timeout
|
||||||
|
//!
|
||||||
|
//! [`Mutex::lock`] waits up to [`DEFAULT_TIMEOUT`] (30 seconds) before giving
|
||||||
|
//! up with [`LockTimeout`]. To use a different bound for one call, use
|
||||||
|
//! [`Mutex::lock_timeout`] instead; to change the default for every future
|
||||||
|
//! `lock()` call on this mutex (including through its clones), use
|
||||||
|
//! [`Mutex::set_default_timeout`]. If you never want to wait at all, use
|
||||||
|
//! [`Mutex::try_lock`], which returns immediately whether or not the lock was
|
||||||
|
//! free.
|
||||||
|
//!
|
||||||
|
//! ## Fairness and panics
|
||||||
|
//!
|
||||||
|
//! Waiters are granted the lock in the order they started waiting (FIFO), so
|
||||||
|
//! no actor can be starved by later arrivals repeatedly cutting in line.
|
||||||
|
//!
|
||||||
|
//! This mutex never poisons. `std::sync::Mutex` marks itself poisoned if a
|
||||||
|
//! thread panics while holding the lock, because a partly mutated value might
|
||||||
|
//! be left behind for the next lock holder to see. smarm's actors already
|
||||||
|
//! rely on `Drop` running during unwinding to release the lock, so if a
|
||||||
|
//! holder panics, [`MutexGuard::drop`] still runs and the next waiter is
|
||||||
|
//! granted the lock normally. It is the same tradeoff `std::sync::Mutex`
|
||||||
|
//! offers you if you choose to ignore poisoning: you may see a value left
|
||||||
|
//! mid-update by the panicking actor, so a panic inside a critical section is
|
||||||
|
//! still a bug worth fixing, just not one that also wedges every future lock
|
||||||
|
//! attempt.
|
||||||
|
//!
|
||||||
|
//! Locking a mutex you already hold (on the same actor) does not queue
|
||||||
|
//! behind yourself: it deadlocks, the same way relocking a non-reentrant
|
||||||
|
//! `std::sync::Mutex` does. Don't call `lock` while already holding a guard
|
||||||
|
//! from the same `Mutex`.
|
||||||
|
//!
|
||||||
|
//! ## Outside the runtime
|
||||||
|
//!
|
||||||
|
//! `Mutex<T>` also works when called from plain code that is not running as
|
||||||
|
//! a smarm actor (for example, in a test's setup code before calling
|
||||||
|
//! [`run`](crate::run)). There, an actor's cooperative park has no meaning,
|
||||||
|
//! so a lock attempt instead blocks the calling OS thread directly until the
|
||||||
|
//! mutex is free; there is no timeout on this path.
|
||||||
|
|
||||||
use crate::pid::Pid;
|
use crate::pid::Pid;
|
||||||
use crate::scheduler;
|
use crate::scheduler;
|
||||||
@@ -15,8 +92,14 @@ use std::collections::VecDeque;
|
|||||||
use std::sync::{Arc, Mutex as StdMutex};
|
use std::sync::{Arc, Mutex as StdMutex};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
|
/// How long [`Mutex::lock`] waits for the lock before giving up, unless
|
||||||
|
/// overridden per-mutex with [`Mutex::set_default_timeout`] or per-call with
|
||||||
|
/// [`Mutex::lock_timeout`].
|
||||||
pub const DEFAULT_TIMEOUT: Duration = Duration::from_secs(30);
|
pub const DEFAULT_TIMEOUT: Duration = Duration::from_secs(30);
|
||||||
|
|
||||||
|
/// Returned by [`Mutex::lock`] / [`Mutex::lock_timeout`] when the timeout
|
||||||
|
/// elapses before the lock became available. The lock attempt is abandoned;
|
||||||
|
/// nothing was acquired, and the mutex's value is unaffected.
|
||||||
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
||||||
pub struct LockTimeout;
|
pub struct LockTimeout;
|
||||||
|
|
||||||
@@ -64,20 +147,27 @@ impl MutexCore {
|
|||||||
impl TimerTarget for MutexCore {
|
impl TimerTarget for MutexCore {
|
||||||
fn on_timeout(&self, pid: Pid, epoch: u32) {
|
fn on_timeout(&self, pid: Pid, epoch: u32) {
|
||||||
let unpark = {
|
let unpark = {
|
||||||
let mut st = self.state.lock().unwrap();
|
let mut st = match self.state.lock() {
|
||||||
|
Ok(g) => g,
|
||||||
|
Err(e) => panic!("smarm: mutex state lock poisoned (core corrupt): {e}"),
|
||||||
|
};
|
||||||
// Remove from waiters only if still there with matching epoch.
|
// Remove from waiters only if still there with matching epoch.
|
||||||
// If the lock was already granted (holder == Some(pid)), the
|
// If the lock was already granted (holder == Some(pid)), the
|
||||||
// timer fired after the grant — treat as no-op; the actor
|
// timer fired after the grant: treat as no-op; the actor
|
||||||
// will see `is_holder == true` and return Ok.
|
// will see `is_holder == true` and return Ok.
|
||||||
if st.holder == Some(pid) {
|
if st.holder == Some(pid) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let pos = st.waiters.iter().position(|w| w.pid == pid && w.epoch == epoch);
|
match st
|
||||||
if pos.is_some() {
|
.waiters
|
||||||
st.waiters.remove(pos.unwrap());
|
.iter()
|
||||||
true
|
.position(|w| w.pid == pid && w.epoch == epoch)
|
||||||
} else {
|
{
|
||||||
false
|
Some(pos) => {
|
||||||
|
st.waiters.remove(pos);
|
||||||
|
true
|
||||||
|
}
|
||||||
|
None => false,
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
if unpark {
|
if unpark {
|
||||||
@@ -97,6 +187,8 @@ pub struct Mutex<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Mutex<T> {
|
impl<T> Mutex<T> {
|
||||||
|
/// Wrap `value` in a new mutex, initially unlocked, with the default
|
||||||
|
/// lock timeout ([`DEFAULT_TIMEOUT`]).
|
||||||
pub fn new(value: T) -> Self {
|
pub fn new(value: T) -> Self {
|
||||||
Self {
|
Self {
|
||||||
core: Arc::new(MutexCore::new(DEFAULT_TIMEOUT)),
|
core: Arc::new(MutexCore::new(DEFAULT_TIMEOUT)),
|
||||||
@@ -104,15 +196,36 @@ impl<T> Mutex<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Change how long future [`lock`](Self::lock) calls on this mutex wait
|
||||||
|
/// before giving up. Applies to every clone of this `Mutex` (they share
|
||||||
|
/// one underlying lock), and to `lock` calls already in progress that
|
||||||
|
/// have not yet started waiting. Does not affect [`lock_timeout`](Self::lock_timeout)
|
||||||
|
/// calls, which always use the timeout passed in.
|
||||||
pub fn set_default_timeout(&self, timeout: Duration) {
|
pub fn set_default_timeout(&self, timeout: Duration) {
|
||||||
self.core.state.lock().unwrap().default_timeout = timeout;
|
match self.core.state.lock() {
|
||||||
|
Ok(mut st) => st.default_timeout = timeout,
|
||||||
|
Err(e) => panic!("smarm: mutex state lock poisoned (core corrupt): {e}"),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Acquire the lock, waiting up to this mutex's default timeout
|
||||||
|
/// ([`DEFAULT_TIMEOUT`], or whatever [`set_default_timeout`](Self::set_default_timeout)
|
||||||
|
/// last set) if it is currently held elsewhere. Returns a [`MutexGuard`]
|
||||||
|
/// that releases the lock when dropped, or [`LockTimeout`] if the
|
||||||
|
/// deadline passes first. To use a one-off timeout instead of the
|
||||||
|
/// mutex's default, call [`lock_timeout`](Self::lock_timeout) directly.
|
||||||
pub fn lock(&self) -> Result<MutexGuard<'_, T>, LockTimeout> {
|
pub fn lock(&self) -> Result<MutexGuard<'_, T>, LockTimeout> {
|
||||||
let timeout = self.core.state.lock().unwrap().default_timeout;
|
let timeout = match self.core.state.lock() {
|
||||||
|
Ok(st) => st.default_timeout,
|
||||||
|
Err(e) => panic!("smarm: mutex state lock poisoned (core corrupt): {e}"),
|
||||||
|
};
|
||||||
self.lock_timeout(timeout)
|
self.lock_timeout(timeout)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Acquire the lock, waiting up to `timeout` (ignoring this mutex's
|
||||||
|
/// default) if it is currently held elsewhere. Returns a [`MutexGuard`]
|
||||||
|
/// that releases the lock when dropped, or [`LockTimeout`] if `timeout`
|
||||||
|
/// elapses first with the lock still unavailable.
|
||||||
pub fn lock_timeout(&self, timeout: Duration) -> Result<MutexGuard<'_, T>, LockTimeout> {
|
pub fn lock_timeout(&self, timeout: Duration) -> Result<MutexGuard<'_, T>, LockTimeout> {
|
||||||
// Outside the runtime (e.g. in tests, after run() returns) there is no
|
// Outside the runtime (e.g. in tests, after run() returns) there is no
|
||||||
// current actor PID. Fall back to a blocking std::sync::Mutex acquire.
|
// current actor PID. Fall back to a blocking std::sync::Mutex acquire.
|
||||||
@@ -122,21 +235,36 @@ impl<T> Mutex<T> {
|
|||||||
|
|
||||||
// Fast path: nobody holds it.
|
// Fast path: nobody holds it.
|
||||||
{
|
{
|
||||||
let mut st = self.core.state.lock().unwrap();
|
let mut st = match self.core.state.lock() {
|
||||||
|
Ok(g) => g,
|
||||||
|
Err(e) => panic!("smarm: mutex state lock poisoned (core corrupt): {e}"),
|
||||||
|
};
|
||||||
if st.holder.is_none() {
|
if st.holder.is_none() {
|
||||||
st.holder = Some(me);
|
st.holder = Some(me);
|
||||||
drop(st);
|
drop(st);
|
||||||
let value = self.value.lock().unwrap().take()
|
let taken = match self.value.lock() {
|
||||||
.expect("Mutex: value missing on free fast path");
|
Ok(mut g) => g.take(),
|
||||||
return Ok(MutexGuard { mutex: self, value: Some(value) });
|
Err(e) => panic!("smarm: mutex value lock poisoned (core corrupt): {e}"),
|
||||||
|
};
|
||||||
|
let value = match taken {
|
||||||
|
Some(v) => v,
|
||||||
|
None => panic!("smarm: Mutex value missing on free fast path (core corrupt)"),
|
||||||
|
};
|
||||||
|
return Ok(MutexGuard {
|
||||||
|
mutex: self,
|
||||||
|
value: Some(value),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Slow path: register as a waiter, set timeout, park.
|
// Slow path: register as a waiter, set timeout, park.
|
||||||
let _np = scheduler::NoPreempt::enter();
|
let _np = scheduler::NoPreempt::enter();
|
||||||
let epoch = {
|
let epoch = {
|
||||||
let mut st = self.core.state.lock().unwrap();
|
let mut st = match self.core.state.lock() {
|
||||||
// begin_wait is lock-free — legal under the state lock; this
|
Ok(g) => g,
|
||||||
|
Err(e) => panic!("smarm: mutex state lock poisoned (core corrupt): {e}"),
|
||||||
|
};
|
||||||
|
// begin_wait is lock-free (legal under the state lock); this
|
||||||
// makes the epoch atomic with the registration's visibility to
|
// makes the epoch atomic with the registration's visibility to
|
||||||
// grants and timeouts.
|
// grants and timeouts.
|
||||||
let epoch = scheduler::begin_wait();
|
let epoch = scheduler::begin_wait();
|
||||||
@@ -149,31 +277,58 @@ impl<T> Mutex<T> {
|
|||||||
scheduler::insert_wait_timer(deadline, me, target, epoch);
|
scheduler::insert_wait_timer(deadline, me, target, epoch);
|
||||||
scheduler::park_current();
|
scheduler::park_current();
|
||||||
|
|
||||||
// Resumed — precisely: only our grant or our timer can wake this
|
// Resumed, precisely: only our grant or our timer can wake this
|
||||||
// wait (both epoch-stamped; a stop wake unwinds out of
|
// wait (both epoch-stamped; a stop wake unwinds out of
|
||||||
// park_current). The one-shot interpretation below is therefore
|
// park_current). The one-shot interpretation below is therefore
|
||||||
// exhaustive. Are we the holder?
|
// exhaustive. Are we the holder?
|
||||||
let is_holder = self.core.state.lock().unwrap().holder == Some(me);
|
let is_holder = match self.core.state.lock() {
|
||||||
|
Ok(st) => st.holder == Some(me),
|
||||||
|
Err(e) => panic!("smarm: mutex state lock poisoned (core corrupt): {e}"),
|
||||||
|
};
|
||||||
if is_holder {
|
if is_holder {
|
||||||
let value = self.value.lock().unwrap().take()
|
let taken = match self.value.lock() {
|
||||||
.expect("Mutex: value missing after grant");
|
Ok(mut g) => g.take(),
|
||||||
Ok(MutexGuard { mutex: self, value: Some(value) })
|
Err(e) => panic!("smarm: mutex value lock poisoned (core corrupt): {e}"),
|
||||||
|
};
|
||||||
|
let value = match taken {
|
||||||
|
Some(v) => v,
|
||||||
|
None => panic!("smarm: Mutex value missing after grant (core corrupt)"),
|
||||||
|
};
|
||||||
|
Ok(MutexGuard {
|
||||||
|
mutex: self,
|
||||||
|
value: Some(value),
|
||||||
|
})
|
||||||
} else {
|
} else {
|
||||||
Err(LockTimeout)
|
Err(LockTimeout)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Acquire the lock only if it is immediately available: never parks and
|
||||||
|
/// never waits. Returns `Some` with a [`MutexGuard`] if the lock was
|
||||||
|
/// free, `None` if it is currently held elsewhere.
|
||||||
pub fn try_lock(&self) -> Option<MutexGuard<'_, T>> {
|
pub fn try_lock(&self) -> Option<MutexGuard<'_, T>> {
|
||||||
let me = crate::actor::current_pid()?;
|
let me = crate::actor::current_pid()?;
|
||||||
let mut st = self.core.state.lock().unwrap();
|
let mut st = match self.core.state.lock() {
|
||||||
|
Ok(g) => g,
|
||||||
|
Err(e) => panic!("smarm: mutex state lock poisoned (core corrupt): {e}"),
|
||||||
|
};
|
||||||
if st.holder.is_some() {
|
if st.holder.is_some() {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
st.holder = Some(me);
|
st.holder = Some(me);
|
||||||
drop(st);
|
drop(st);
|
||||||
let value = self.value.lock().unwrap().take()
|
let taken = match self.value.lock() {
|
||||||
.expect("Mutex: value missing on try_lock free path");
|
Ok(mut g) => g.take(),
|
||||||
Some(MutexGuard { mutex: self, value: Some(value) })
|
Err(e) => panic!("smarm: mutex value lock poisoned (core corrupt): {e}"),
|
||||||
|
};
|
||||||
|
let value = match taken {
|
||||||
|
Some(v) => v,
|
||||||
|
None => panic!("smarm: Mutex value missing on try_lock free path (core corrupt)"),
|
||||||
|
};
|
||||||
|
Some(MutexGuard {
|
||||||
|
mutex: self,
|
||||||
|
value: Some(value),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Blocking fallback used when called outside the smarm runtime.
|
/// Blocking fallback used when called outside the smarm runtime.
|
||||||
@@ -183,17 +338,32 @@ impl<T> Mutex<T> {
|
|||||||
// tracking and just grab the value mutex directly. This is safe because
|
// tracking and just grab the value mutex directly. This is safe because
|
||||||
// outside the runtime there are no green threads competing.
|
// outside the runtime there are no green threads competing.
|
||||||
let value = loop {
|
let value = loop {
|
||||||
let v = self.value.lock().unwrap().take();
|
let v = match self.value.lock() {
|
||||||
if let Some(v) = v { break v; }
|
Ok(mut g) => g.take(),
|
||||||
|
Err(e) => panic!("smarm: mutex value lock poisoned (core corrupt): {e}"),
|
||||||
|
};
|
||||||
|
if let Some(v) = v {
|
||||||
|
break v;
|
||||||
|
}
|
||||||
std::thread::yield_now();
|
std::thread::yield_now();
|
||||||
};
|
};
|
||||||
Ok(MutexGuard { mutex: self, value: Some(value) })
|
Ok(MutexGuard {
|
||||||
|
mutex: self,
|
||||||
|
value: Some(value),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Clone for Mutex<T> {
|
impl<T> Clone for Mutex<T> {
|
||||||
|
/// Cheap: hands back another handle to the same underlying lock and
|
||||||
|
/// value, the way `Arc::clone` does. All clones of a `Mutex` share one
|
||||||
|
/// lock and one protected value; locking through any clone excludes
|
||||||
|
/// every other clone.
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
Self { core: self.core.clone(), value: self.value.clone() }
|
Self {
|
||||||
|
core: self.core.clone(),
|
||||||
|
value: self.value.clone(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -205,6 +375,10 @@ unsafe impl<T: Send> Sync for Mutex<T> {}
|
|||||||
// Guard
|
// Guard
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
/// Grants access to the value inside a [`Mutex`] while the lock is held.
|
||||||
|
/// Dereferences to `&T` and `&mut T`. Dropping the guard releases the lock
|
||||||
|
/// and, if another actor is waiting, wakes the next one in arrival order.
|
||||||
|
/// Returned by [`Mutex::lock`], [`Mutex::lock_timeout`], and [`Mutex::try_lock`].
|
||||||
pub struct MutexGuard<'a, T> {
|
pub struct MutexGuard<'a, T> {
|
||||||
mutex: &'a Mutex<T>,
|
mutex: &'a Mutex<T>,
|
||||||
value: Option<T>,
|
value: Option<T>,
|
||||||
@@ -212,30 +386,49 @@ pub struct MutexGuard<'a, T> {
|
|||||||
|
|
||||||
impl<T> std::ops::Deref for MutexGuard<'_, T> {
|
impl<T> std::ops::Deref for MutexGuard<'_, T> {
|
||||||
type Target = T;
|
type Target = T;
|
||||||
fn deref(&self) -> &T { self.value.as_ref().expect("MutexGuard: value missing") }
|
fn deref(&self) -> &T {
|
||||||
|
match self.value.as_ref() {
|
||||||
|
Some(v) => v,
|
||||||
|
None => panic!("smarm: MutexGuard value missing (core corrupt)"),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> std::ops::DerefMut for MutexGuard<'_, T> {
|
impl<T> std::ops::DerefMut for MutexGuard<'_, T> {
|
||||||
fn deref_mut(&mut self) -> &mut T {
|
fn deref_mut(&mut self) -> &mut T {
|
||||||
self.value.as_mut().expect("MutexGuard: value missing")
|
match self.value.as_mut() {
|
||||||
|
Some(v) => v,
|
||||||
|
None => panic!("smarm: MutexGuard value missing (core corrupt)"),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: std::fmt::Debug> std::fmt::Debug for MutexGuard<'_, T> {
|
impl<T: std::fmt::Debug> std::fmt::Debug for MutexGuard<'_, T> {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
f.debug_tuple("MutexGuard")
|
let value = match self.value.as_ref() {
|
||||||
.field(self.value.as_ref().expect("MutexGuard: value missing"))
|
Some(v) => v,
|
||||||
.finish()
|
None => panic!("smarm: MutexGuard value missing (core corrupt)"),
|
||||||
|
};
|
||||||
|
f.debug_tuple("MutexGuard").field(value).finish()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Drop for MutexGuard<'_, T> {
|
impl<T> Drop for MutexGuard<'_, T> {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
let v = self.value.take().expect("MutexGuard: double drop");
|
let v = match self.value.take() {
|
||||||
*self.mutex.value.lock().unwrap() = Some(v);
|
Some(v) => v,
|
||||||
|
None => panic!("smarm: MutexGuard double drop (core corrupt)"),
|
||||||
|
};
|
||||||
|
match self.mutex.value.lock() {
|
||||||
|
Ok(mut g) => *g = Some(v),
|
||||||
|
Err(e) => panic!("smarm: mutex value lock poisoned (core corrupt): {e}"),
|
||||||
|
}
|
||||||
|
|
||||||
let next = {
|
let next = {
|
||||||
let mut st = self.mutex.core.state.lock().unwrap();
|
let mut st = match self.mutex.core.state.lock() {
|
||||||
|
Ok(g) => g,
|
||||||
|
Err(e) => panic!("smarm: mutex state lock poisoned (core corrupt): {e}"),
|
||||||
|
};
|
||||||
match st.waiters.pop_front() {
|
match st.waiters.pop_front() {
|
||||||
Some(w) => {
|
Some(w) => {
|
||||||
st.holder = Some(w.pid);
|
st.holder = Some(w.pid);
|
||||||
|
|||||||
+5
-5
@@ -29,7 +29,7 @@
|
|||||||
//! channel by value; that is intended, it is exactly what a remote observer
|
//! channel by value; that is intended, it is exactly what a remote observer
|
||||||
//! (RFC 011) will serialize across a node boundary.
|
//! (RFC 011) will serialize across a node boundary.
|
||||||
|
|
||||||
use crate::gen_server::{GenServer, ServerBuilder, ServerRef};
|
use crate::gen_server::{GenServer, GenServerBuilder, GenServerRef};
|
||||||
use crate::introspect::{actor_info, snapshot, tree};
|
use crate::introspect::{actor_info, snapshot, tree};
|
||||||
use crate::introspect::{ActorInfo, RuntimeSnapshot, RuntimeTree};
|
use crate::introspect::{ActorInfo, RuntimeSnapshot, RuntimeTree};
|
||||||
use crate::pid::Pid;
|
use crate::pid::Pid;
|
||||||
@@ -89,8 +89,8 @@ impl GenServer for Observer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Spawn the observer under the current actor and hand back its [`ServerRef`].
|
/// Spawn the observer under the current actor and hand back its [`GenServerRef`].
|
||||||
/// Shorthand for `ServerBuilder::new(Observer).start()`; use the builder
|
/// Shorthand for `GenServerBuilder::new(Observer).start()`; use the builder
|
||||||
/// directly (e.g. `.under(sup)`) to slot it into a supervision tree.
|
/// directly (e.g. `.under(sup)`) to slot it into a supervision tree.
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
@@ -109,6 +109,6 @@ impl GenServer for Observer {
|
|||||||
/// assert!(snap.actors.iter().any(|a| a.pid == obs.pid()));
|
/// assert!(snap.actors.iter().any(|a| a.pid == obs.pid()));
|
||||||
/// });
|
/// });
|
||||||
/// ```
|
/// ```
|
||||||
pub fn start() -> ServerRef<Observer> {
|
pub fn start() -> GenServerRef<Observer> {
|
||||||
ServerBuilder::new(Observer).start()
|
GenServerBuilder::new(Observer).start()
|
||||||
}
|
}
|
||||||
|
|||||||
+1017
File diff suppressed because it is too large
Load Diff
+56
-6
@@ -53,7 +53,7 @@ impl std::fmt::Display for RawPid {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Phantom actor type for a pid that has no single message type: raw `spawn`
|
/// Phantom actor type for a pid that has no single message type: raw `spawn`
|
||||||
/// actors, gen_servers (intrinsically multi-message, addressed via `ServerRef`),
|
/// actors, gen_servers (intrinsically multi-message, addressed via `GenServerRef`),
|
||||||
/// and every identity-only context. Deliberately **not** [`Addressable`], so a
|
/// and every identity-only context. Deliberately **not** [`Addressable`], so a
|
||||||
/// typed `send` to a `Pid<Erased>` does not compile; the runtime-checked
|
/// typed `send` to a `Pid<Erased>` does not compile; the runtime-checked
|
||||||
/// `send_dyn` escape hatch (RFC 014 §4.6) is the sanctioned bare-pid path.
|
/// `send_dyn` escape hatch (RFC 014 §4.6) is the sanctioned bare-pid path.
|
||||||
@@ -76,10 +76,13 @@ pub struct Pid<A = Erased> {
|
|||||||
|
|
||||||
impl Pid<Erased> {
|
impl Pid<Erased> {
|
||||||
/// Build an untyped pid from raw numbers. The runtime mints identities
|
/// Build an untyped pid from raw numbers. The runtime mints identities
|
||||||
/// here; typing happens at typed-actor boundaries via [`Pid::from_raw`].
|
/// here; typing happens at typed-actor boundaries via `Pid::from_raw`.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub const fn new(index: u32, generation: u32) -> Self {
|
pub const fn new(index: u32, generation: u32) -> Self {
|
||||||
Self { raw: RawPid::new(index, generation), _marker: PhantomData }
|
Self {
|
||||||
|
raw: RawPid::new(index, generation),
|
||||||
|
_marker: PhantomData,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,7 +93,10 @@ impl<A> Pid<A> {
|
|||||||
/// resolution paths.
|
/// resolution paths.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(crate) const fn from_raw(raw: RawPid) -> Self {
|
pub(crate) const fn from_raw(raw: RawPid) -> Self {
|
||||||
Self { raw, _marker: PhantomData }
|
Self {
|
||||||
|
raw,
|
||||||
|
_marker: PhantomData,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The raw identity, dropping the actor type — the key for identity-only
|
/// The raw identity, dropping the actor type — the key for identity-only
|
||||||
@@ -166,7 +172,7 @@ impl<A> std::fmt::Display for Pid<A> {
|
|||||||
/// An actor type with a single associated message type, so a [`Pid<Self>`] is a
|
/// An actor type with a single associated message type, so a [`Pid<Self>`] is a
|
||||||
/// typed address. The raw channel layer has no such trait (actors are closures
|
/// typed address. The raw channel layer has no such trait (actors are closures
|
||||||
/// over channels) and `GenServer` is intrinsically multi-message (addressed via
|
/// over channels) and `GenServer` is intrinsically multi-message (addressed via
|
||||||
/// its own `ServerRef`); this is the minimal hook that lets the single-message
|
/// its own `GenServerRef`); this is the minimal hook that lets the single-message
|
||||||
/// actors carry their message type in their pid. (RFC 014 §4.2.)
|
/// actors carry their message type in their pid. (RFC 014 §4.2.)
|
||||||
pub trait Addressable: 'static {
|
pub trait Addressable: 'static {
|
||||||
/// The message this actor receives. A `Pid<Self>` delivers `Self::Msg`.
|
/// The message this actor receives. A `Pid<Self>` delivers `Self::Msg`.
|
||||||
@@ -192,7 +198,10 @@ impl<M> Name<M> {
|
|||||||
/// associated constants at call sites.
|
/// associated constants at call sites.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub const fn new(name: &'static str) -> Self {
|
pub const fn new(name: &'static str) -> Self {
|
||||||
Self { name, _marker: PhantomData }
|
Self {
|
||||||
|
name,
|
||||||
|
_marker: PhantomData,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The underlying registry key.
|
/// The underlying registry key.
|
||||||
@@ -283,3 +292,44 @@ mod typed_pid_tests {
|
|||||||
assert_send_sync::<Name<CounterMsg>>();
|
assert_send_sync::<Name<CounterMsg>>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---- RFC 010 c10: pids auto-serialize (cluster feature) ---------------------
|
||||||
|
|
||||||
|
/// A local `Pid<A>` serializes as a
|
||||||
|
/// [`RemotePid<A>`](crate::cluster::remote::RemotePid): the wire form stamps
|
||||||
|
/// this node's name and incarnation from the ambient runtime, so a pid can
|
||||||
|
/// sit inside any message field and reply-to needs no ceremony (RFC 010 §3,
|
||||||
|
/// "sugar not a bear trap"). Serializing a pid also marks it **watchable**
|
||||||
|
/// — the wire crossing is the cluster's `mark_watchable` set-site (D12), the
|
||||||
|
/// exact analog of the membrane crossing.
|
||||||
|
///
|
||||||
|
/// Must run inside `run()` (the ambient identity lives on the runtime); a
|
||||||
|
/// runtime without a cluster identity cannot serialize a pid at all — it is
|
||||||
|
/// a serialize error, surfacing as the send's `Encode` failure — rather than
|
||||||
|
/// a `("", 0)` stamp that every peer would silently drop.
|
||||||
|
#[cfg(feature = "cluster")]
|
||||||
|
impl<A: 'static> serde::Serialize for Pid<A> {
|
||||||
|
fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
|
||||||
|
crate::cluster::remote::RemotePid::<A>::from_local(*self)
|
||||||
|
.ok_or_else(|| serde::ser::Error::custom("pid serialized with no local node identity"))?
|
||||||
|
.serialize(s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Deserializing into a `Pid<A>` is the **collapse**: it succeeds only when
|
||||||
|
/// the wire pid names this very node (name and incarnation both), and is a
|
||||||
|
/// decode error otherwise — a foreign pid cannot become a local `Pid`.
|
||||||
|
/// Fields that may hold a pid from anywhere are `RemotePid<A>`.
|
||||||
|
#[cfg(feature = "cluster")]
|
||||||
|
impl<'de, A: 'static> serde::Deserialize<'de> for Pid<A> {
|
||||||
|
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
|
||||||
|
let rp = crate::cluster::remote::RemotePid::<A>::deserialize(d)?;
|
||||||
|
rp.local().ok_or_else(|| {
|
||||||
|
serde::de::Error::custom(format!(
|
||||||
|
"pid {}@{} is not local to this node",
|
||||||
|
rp.index(),
|
||||||
|
rp.node()
|
||||||
|
))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+78
-4
@@ -98,12 +98,53 @@ pub(crate) fn clear_current_slot() {
|
|||||||
CURRENT_SLOT.with(|c| c.set(std::ptr::null()));
|
CURRENT_SLOT.with(|c| c.set(std::ptr::null()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Raw pointer to the on-CPU actor's slot, null on the scheduler's own
|
||||||
|
/// stack. Same lifetime argument as `note_overrun`: the slot is never
|
||||||
|
/// reclaimed while its actor is on-CPU. Consumers: the `smarm-causal`
|
||||||
|
/// profiler (RFC 007) and — unconditionally — the SIGSEGV classifier
|
||||||
|
/// (RFC 019 §7), which additionally relies on this being a plain load of a
|
||||||
|
/// const-initialized TLS Cell (no lazy init, no allocation, no dtor): safe
|
||||||
|
/// from a signal handler.
|
||||||
|
#[inline(never)]
|
||||||
|
pub(crate) fn current_slot_ptr() -> *const crate::runtime::Slot {
|
||||||
|
crate::context::tls_fence();
|
||||||
|
CURRENT_SLOT.with(|c| c.get())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Swap the preemption gate, returning the previous value. The one accessor
|
||||||
|
/// for `PREEMPTION_ENABLED` from actor context (`NoPreempt`, `RawMutex`,
|
||||||
|
/// `with_runtime`, trace): `#[inline(never)]` + fence, see `context` docs.
|
||||||
|
#[inline(never)]
|
||||||
|
pub(crate) fn preemption_swap(enabled: bool) -> bool {
|
||||||
|
crate::context::tls_fence();
|
||||||
|
PREEMPTION_ENABLED.with(|c| c.replace(enabled))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Read the preemption gate (debug assertions on the queue paths).
|
||||||
|
#[inline(never)]
|
||||||
|
pub(crate) fn preemption_enabled() -> bool {
|
||||||
|
crate::context::tls_fence();
|
||||||
|
PREEMPTION_ENABLED.with(|c| c.get())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// RFC 007 (`smarm-causal`) — push the slice start forward by `cycles`, so
|
||||||
|
/// virtually-injected delay spun inside `maybe_preempt` does not count against
|
||||||
|
/// the actor's timeslice (the clock-correction half of the RFC: the runtime
|
||||||
|
/// owns this clock, so it can subtract its own perturbation).
|
||||||
|
#[cfg(feature = "smarm-causal")]
|
||||||
|
#[inline(never)]
|
||||||
|
pub(crate) fn extend_timeslice(cycles: u64) {
|
||||||
|
crate::context::tls_fence();
|
||||||
|
TIMESLICE_START.with(|c| c.set(c.get().wrapping_add(cycles)));
|
||||||
|
}
|
||||||
|
|
||||||
/// Tally a timeslice overrun against the on-CPU actor (RFC 016 Chunk 2). A
|
/// Tally a timeslice overrun against the on-CPU actor (RFC 016 Chunk 2). A
|
||||||
/// no-op if no actor is bound (the scheduler's own stack). Reached only from
|
/// no-op if no actor is bound (the scheduler's own stack). Reached only from
|
||||||
/// the slice-expiry branch, which is already the yield path, so its cost is
|
/// the slice-expiry branch, which is already the yield path, so its cost is
|
||||||
/// irrelevant.
|
/// irrelevant.
|
||||||
#[inline]
|
#[inline(never)]
|
||||||
fn note_overrun() {
|
fn note_overrun() {
|
||||||
|
crate::context::tls_fence();
|
||||||
let p = CURRENT_SLOT.with(|c| c.get());
|
let p = CURRENT_SLOT.with(|c| c.get());
|
||||||
// SAFETY: `p` is null (no actor on-CPU) or a pointer to the on-CPU actor's
|
// SAFETY: `p` is null (no actor on-CPU) or a pointer to the on-CPU actor's
|
||||||
// slot in the fixed slab. The slot is not reclaimed while the actor runs
|
// slot in the fixed slab. The slot is not reclaimed while the actor runs
|
||||||
@@ -119,8 +160,9 @@ fn note_overrun() {
|
|||||||
/// outside an actor (null slot). One TLS load + one Relaxed load/store on a
|
/// outside an actor (null slot). One TLS load + one Relaxed load/store on a
|
||||||
/// cache line the receiving thread already owns — no atomic RMW, no lock. Same
|
/// cache line the receiving thread already owns — no atomic RMW, no lock. Same
|
||||||
/// slot-lifetime safety argument as `note_overrun`.
|
/// slot-lifetime safety argument as `note_overrun`.
|
||||||
#[inline]
|
#[inline(never)]
|
||||||
pub(crate) fn note_message_received() {
|
pub(crate) fn note_message_received() {
|
||||||
|
crate::context::tls_fence();
|
||||||
let p = CURRENT_SLOT.with(|c| c.get());
|
let p = CURRENT_SLOT.with(|c| c.get());
|
||||||
if !p.is_null() {
|
if !p.is_null() {
|
||||||
unsafe { (*p).record_message() };
|
unsafe { (*p).record_message() };
|
||||||
@@ -136,8 +178,9 @@ pub(crate) fn note_message_received() {
|
|||||||
/// Called from `maybe_preempt` (amortised, the `check!()`/alloc path) and from
|
/// Called from `maybe_preempt` (amortised, the `check!()`/alloc path) and from
|
||||||
/// the wakeup side of every blocking park (`park_current`/`yield_now`), which
|
/// the wakeup side of every blocking park (`park_current`/`yield_now`), which
|
||||||
/// is past the prep-to-park window — so it can never lose a wakeup.
|
/// is past the prep-to-park window — so it can never lose a wakeup.
|
||||||
#[inline]
|
#[inline(never)]
|
||||||
pub fn check_cancelled() {
|
pub fn check_cancelled() {
|
||||||
|
crate::context::tls_fence();
|
||||||
let p = CURRENT_STOP.with(|c| c.get());
|
let p = CURRENT_STOP.with(|c| c.get());
|
||||||
// SAFETY: `p` is either null (no actor on-CPU — the scheduler clears it on
|
// SAFETY: `p` is either null (no actor on-CPU — the scheduler clears it on
|
||||||
// every return) or a pointer into the on-CPU actor's `Arc<AtomicBool>`
|
// every return) or a pointer into the on-CPU actor's `Arc<AtomicBool>`
|
||||||
@@ -176,8 +219,27 @@ pub(crate) fn elapsed_slice_cycles() -> u64 {
|
|||||||
rdtsc().saturating_sub(TIMESLICE_START.with(|c| c.get()))
|
rdtsc().saturating_sub(TIMESLICE_START.with(|c| c.get()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Read the TSC, unserialised. The core may execute this before earlier
|
||||||
|
/// instructions retire (or after later ones start), so a single stamp can
|
||||||
|
/// land tens of cycles — worst case a stalled load's worth — early or late.
|
||||||
|
/// That is negligible against every consumer in this module: the timeslice
|
||||||
|
/// arm/expiry compare against a ~10^5-cycle slice, and an early stamp only
|
||||||
|
/// makes a slice look *more* used (expires marginally sooner, never later).
|
||||||
|
/// The `lfence` this used to carry was a pipeline drain paid on every
|
||||||
|
/// resume; the one place that needs it is causal-site attribution, which
|
||||||
|
/// opts in via [`rdtsc_serialising`].
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn rdtsc() -> u64 {
|
pub fn rdtsc() -> u64 {
|
||||||
|
// SAFETY: x86-64 only (this crate is x86-64 Linux only).
|
||||||
|
unsafe { core::arch::x86_64::_rdtsc() }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Read the TSC after all prior instructions have completed locally.
|
||||||
|
/// Use where a stamp bounds an interval attributed to *code* — a speculative
|
||||||
|
/// early read would credit the tail of that code to whatever comes next.
|
||||||
|
/// Costs a pipeline drain; keep it off the per-resume path.
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn rdtsc_serialising() -> u64 {
|
||||||
unsafe {
|
unsafe {
|
||||||
// SAFETY: x86-64 only. `lfence` serialises the instruction stream so
|
// SAFETY: x86-64 only. `lfence` serialises the instruction stream so
|
||||||
// we don't measure time before prior instructions retire.
|
// we don't measure time before prior instructions retire.
|
||||||
@@ -225,8 +287,13 @@ unsafe impl GlobalAlloc for PreemptingAllocator {
|
|||||||
/// the actor would then park, and the wakeup would be lost. Library
|
/// the actor would then park, and the wakeup would be lost. Library
|
||||||
/// code that touches the parking primitives must keep its prep-to-park
|
/// code that touches the parking primitives must keep its prep-to-park
|
||||||
/// regions allocation-free and check!()-free.
|
/// regions allocation-free and check!()-free.
|
||||||
#[inline(always)]
|
///
|
||||||
|
/// `#[inline(never)]`: this touches thread-locals and can switch threads in
|
||||||
|
/// the middle; inlined into a caller's loop the TLS base would be hoisted
|
||||||
|
/// across the switch (see `context` module docs). The call is the price.
|
||||||
|
#[inline(never)]
|
||||||
pub fn maybe_preempt() {
|
pub fn maybe_preempt() {
|
||||||
|
crate::context::tls_fence();
|
||||||
ALLOC_COUNT.with(|c| {
|
ALLOC_COUNT.with(|c| {
|
||||||
let n = c.get();
|
let n = c.get();
|
||||||
if n == 0 {
|
if n == 0 {
|
||||||
@@ -247,6 +314,11 @@ pub fn maybe_preempt() {
|
|||||||
// Observe a pending stop first: if we are being cancelled
|
// Observe a pending stop first: if we are being cancelled
|
||||||
// there is no point yielding, we unwind instead.
|
// there is no point yielding, we unwind instead.
|
||||||
check_cancelled();
|
check_cancelled();
|
||||||
|
// RFC 007: causal-profiling sample/absorb point. Shares the
|
||||||
|
// amortised cadence, and the PREEMPTION_ENABLED gate — so it
|
||||||
|
// can never spin inside a prep-to-park or no-preempt region.
|
||||||
|
#[cfg(feature = "smarm-causal")]
|
||||||
|
crate::causal::check();
|
||||||
let start = TIMESLICE_START.with(|s| s.get());
|
let start = TIMESLICE_START.with(|s| s.get());
|
||||||
if rdtsc().saturating_sub(start) > CONFIGURED_TIMESLICE_CYCLES.with(|t| t.get()) {
|
if rdtsc().saturating_sub(start) > CONFIGURED_TIMESLICE_CYCLES.with(|t| t.get()) {
|
||||||
// Tally the overrun (RFC 016 Chunk 2) before handing back —
|
// Tally the overrun (RFC 016 Chunk 2) before handing back —
|
||||||
@@ -270,7 +342,9 @@ pub fn maybe_preempt() {
|
|||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
/// Force-expire the timeslice so the next RDTSC check preempts.
|
/// Force-expire the timeslice so the next RDTSC check preempts.
|
||||||
|
#[inline(never)]
|
||||||
pub fn expire_timeslice_for_test() {
|
pub fn expire_timeslice_for_test() {
|
||||||
|
crate::context::tls_fence();
|
||||||
TIMESLICE_START.with(|c| c.set(0));
|
TIMESLICE_START.with(|c| c.set(0));
|
||||||
ALLOC_COUNT.with(|c| c.set(0));
|
ALLOC_COUNT.with(|c| c.set(0));
|
||||||
}
|
}
|
||||||
|
|||||||
+16
-5
@@ -75,8 +75,13 @@ thread_local! {
|
|||||||
static CHANNELS_HELD: std::cell::Cell<u32> = const { std::cell::Cell::new(0) };
|
static CHANNELS_HELD: std::cell::Cell<u32> = const { std::cell::Cell::new(0) };
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
// Debug-only TLS bookkeeping: out of line only when it has a body (release
|
||||||
|
// builds must not pay a call for an empty function).
|
||||||
|
#[cfg_attr(debug_assertions, inline(never))]
|
||||||
|
#[cfg_attr(not(debug_assertions), inline(always))]
|
||||||
fn order_check_acquire(class: LockClass) {
|
fn order_check_acquire(class: LockClass) {
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
crate::context::tls_fence();
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
match class {
|
match class {
|
||||||
LockClass::Leaf => LEAVES_HELD.with(|l| {
|
LockClass::Leaf => LEAVES_HELD.with(|l| {
|
||||||
@@ -111,8 +116,11 @@ fn order_check_acquire(class: LockClass) {
|
|||||||
let _ = class;
|
let _ = class;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[cfg_attr(debug_assertions, inline(never))]
|
||||||
|
#[cfg_attr(not(debug_assertions), inline(always))]
|
||||||
fn order_check_release(class: LockClass) {
|
fn order_check_release(class: LockClass) {
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
crate::context::tls_fence();
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
match class {
|
match class {
|
||||||
LockClass::Leaf => LEAVES_HELD.with(|c| c.set(c.get() - 1)),
|
LockClass::Leaf => LEAVES_HELD.with(|c| c.set(c.get() - 1)),
|
||||||
@@ -157,7 +165,7 @@ impl<T> RawMutex<T> {
|
|||||||
pub(crate) fn lock(&self) -> RawMutexGuard<'_, T> {
|
pub(crate) fn lock(&self) -> RawMutexGuard<'_, T> {
|
||||||
// Enter NoPreempt *before* acquiring, so a preemption can't fire
|
// Enter NoPreempt *before* acquiring, so a preemption can't fire
|
||||||
// between acquisition and guard construction.
|
// between acquisition and guard construction.
|
||||||
let prev_preempt = crate::preempt::PREEMPTION_ENABLED.with(|c| c.replace(false));
|
let prev_preempt = crate::preempt::preemption_swap(false);
|
||||||
order_check_acquire(self.class);
|
order_check_acquire(self.class);
|
||||||
if self
|
if self
|
||||||
.state
|
.state
|
||||||
@@ -166,7 +174,10 @@ impl<T> RawMutex<T> {
|
|||||||
{
|
{
|
||||||
self.lock_slow();
|
self.lock_slow();
|
||||||
}
|
}
|
||||||
RawMutexGuard { m: self, prev_preempt }
|
RawMutexGuard {
|
||||||
|
m: self,
|
||||||
|
prev_preempt,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cold]
|
#[cold]
|
||||||
@@ -234,7 +245,7 @@ impl<T> Drop for RawMutexGuard<'_, T> {
|
|||||||
self.m.unlock();
|
self.m.unlock();
|
||||||
order_check_release(self.m.class);
|
order_check_release(self.m.class);
|
||||||
// Restore preemption only after the lock is released.
|
// Restore preemption only after the lock is released.
|
||||||
crate::preempt::PREEMPTION_ENABLED.with(|c| c.set(self.prev_preempt));
|
crate::preempt::preemption_swap(self.prev_preempt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+436
-225
@@ -1,55 +1,107 @@
|
|||||||
//! Named mailbox registry — resolve a name (or pid) to a *messageable* actor.
|
//! Give an actor a name so other actors can find it and message it.
|
||||||
//!
|
//!
|
||||||
//! ## What changed (RFC 014)
|
//! Without the registry, the only way to reach an actor is to already be
|
||||||
|
//! holding its [`Pid`], usually because you spawned it yourself or someone
|
||||||
|
//! passed it to you. That is fine for a worker you just created, but it does
|
||||||
|
//! not work for a well-known service that arbitrary parts of your program
|
||||||
|
//! need to find independently, like a logger, a config store, or a
|
||||||
|
//! connection pool. The registry solves this: an actor claims a name once,
|
||||||
|
//! and from then on any other actor can look that name up, or send to it
|
||||||
|
//! directly, without ever having been handed a `Pid`.
|
||||||
//!
|
//!
|
||||||
//! The old registry was a `name <-> pid` bimap: `whereis` handed back a `Pid`
|
//! ```
|
||||||
//! you could not send to, because a pid is just `(index, generation)` with no
|
//! use smarm::{channel, register, run, send, spawn, whereis, Name};
|
||||||
//! delivery endpoint. This rework makes resolution yield something messageable.
|
|
||||||
//!
|
//!
|
||||||
//! Two facts shape the structure:
|
//! const COUNTER: Name<u64> = Name::new("counter");
|
||||||
//!
|
//!
|
||||||
//! 1. **A name resolves to a single actor.** Many actors under one label is
|
//! run(|| {
|
||||||
//! what *process groups* (`pg`) are for; the registry is one-name-one-actor
|
//! let (ready_tx, ready_rx) = channel::<()>();
|
||||||
//! (several names *may* point at the same actor).
|
//! let (tx, rx) = channel::<u64>();
|
||||||
//! 2. **Channels are typed**, so an actor has no single untyped mailbox. An
|
|
||||||
//! actor instead owns a *set* of typed channels — one [`Sender`] per message
|
|
||||||
//! type it accepts. So the registry maps name/pid to a [`Mailbox`]: a small
|
|
||||||
//! structure holding that actor's pid plus all of its typed channels, keyed
|
|
||||||
//! by message [`TypeId`].
|
|
||||||
//!
|
//!
|
||||||
//! Resolution is therefore: `name -> pid` (single actor) `-> Mailbox -> the
|
//! let worker = spawn(move || {
|
||||||
//! channel for message type M`. A `Name<Cmd>` and a `Name<Admin>` on the *same*
|
//! // Claim the name for this actor's inbox. Any actor holding
|
||||||
//! actor select *different* channels purely by their type parameter, so
|
//! // `COUNTER` can now reach this one by name.
|
||||||
//! capability separation (RFC 014 §4.7) needs no extra machinery.
|
//! register(COUNTER, tx).unwrap();
|
||||||
|
//! ready_tx.send(()).unwrap();
|
||||||
|
//! assert_eq!(rx.recv().unwrap(), 42);
|
||||||
|
//! });
|
||||||
//!
|
//!
|
||||||
//! ## Type erasure is contained
|
//! ready_rx.recv().unwrap(); // wait for the worker to register
|
||||||
//!
|
//!
|
||||||
//! Each stored channel is a `Box<dyn Any + Send>` that is concretely a
|
//! // Look the name up, or just send to it directly.
|
||||||
//! `Sender<M>`, filed under `TypeId::of::<M>()`. A resolve for `M` looks up
|
//! assert_eq!(whereis("counter"), Some(worker.pid()));
|
||||||
//! that exact `TypeId` and downcasts to `Sender<M>` — keyed by the very type we
|
//! send(COUNTER, 42).unwrap();
|
||||||
//! downcast to, so the downcast cannot fail on correct data; a failure is a
|
|
||||||
//! smarm bug, asserted in debug. The phantom `M` on [`Name`] re-imposes the
|
|
||||||
//! type at the call site, so callers never touch the erasure.
|
|
||||||
//!
|
//!
|
||||||
//! ## Cleanup is lazy (prune-on-contact)
|
//! worker.join().unwrap();
|
||||||
//!
|
//!
|
||||||
//! As before, there is no `finalize` hook and no name field on the slot. Every
|
//! // The name dies with the actor: nobody holds it anymore.
|
||||||
//! operation that touches a binding checks the target pid's liveness via the
|
//! assert_eq!(whereis("counter"), None);
|
||||||
//! generation-checked slot word; a binding to a dead actor behaves as absent
|
//! });
|
||||||
//! and is pruned on contact (its [`Mailbox`] and every name pointing at it are
|
//! ```
|
||||||
//! dropped). The cost is a dead binding lingering until something looks at it;
|
|
||||||
//! the payoff is zero coupling to the actor lifecycle.
|
|
||||||
//!
|
//!
|
||||||
//! ## Locking
|
//! ## Names carry a message type
|
||||||
//!
|
//!
|
||||||
//! One `RawMutex` (Leaf class) in `RuntimeInner`, exactly like the old
|
//! A [`Name<M>`] is a plain string plus a type parameter `M`: the message
|
||||||
//! registry. The fold (name index *and* handles under the one lock) is what
|
//! type that name expects to receive. [`Name::new`] is `const`, so the usual
|
||||||
//! keeps a name-addressed `send` on a single Leaf — `raw_mutex` panics on a
|
//! pattern is a module-level constant like `COUNTER` above, shared by every
|
||||||
//! second Leaf acquired while one is held. The send path clones the `Sender`
|
//! caller. The type parameter means a name is only ever sent the kind of
|
||||||
//! **under** the Leaf lock (a `Sender::clone` takes a Channel lock, permitted
|
//! message it was declared for. If two different constants share the same
|
||||||
//! under a Leaf), then **releases** the Leaf and only *then* sends — a send can
|
//! string but have different message types, they still address two
|
||||||
//! unpark a receiver, and wakeup-bearing work runs outside the Leaf. Order is
|
//! independent channels on the same actor: registering both just gives that
|
||||||
//! **Leaf -> Channel**, as `pg`/`finalize`.
|
//! actor two ways to be reached, one per message type. This is how you give
|
||||||
|
//! one actor a "public" channel and a separate, differently-typed "admin"
|
||||||
|
//! channel under related names, without inventing an enum to merge them.
|
||||||
|
//!
|
||||||
|
//! ## One actor per name, looked up fresh every time
|
||||||
|
//!
|
||||||
|
//! A name always points at exactly one actor at a time (contrast a *process
|
||||||
|
//! group*, from the [`pg`](crate::pg) module, which is one name mapping to
|
||||||
|
//! many actors). Unlike a plain [`Pid`], which names one specific actor
|
||||||
|
//! forever and stops working the moment that actor dies, a name is
|
||||||
|
//! re-resolved on every [`send`]: if the actor holding it dies and a new one
|
||||||
|
//! registers under the same name, the next `send` reaches the new holder
|
||||||
|
//! automatically. Use a name for a long-lived service whose exact identity
|
||||||
|
//! you do not want to track by hand; use a `Pid` when you already have one
|
||||||
|
//! and want to talk to that exact actor.
|
||||||
|
//!
|
||||||
|
//! ## Registration ends when the actor does
|
||||||
|
//!
|
||||||
|
//! There is no separate step to clean up a name when its actor exits: dying
|
||||||
|
//! is enough. The next operation that touches a dead binding (a [`whereis`],
|
||||||
|
//! a [`send`], or another actor's [`register`] of the same name) notices the
|
||||||
|
//! actor is gone and clears the stale entry as a side effect, so the name
|
||||||
|
//! becomes free again. [`unregister`] is only for a live actor voluntarily
|
||||||
|
//! giving up a name it no longer wants; nothing has to call it on the way
|
||||||
|
//! out.
|
||||||
|
//!
|
||||||
|
//! ## Implementation notes
|
||||||
|
//!
|
||||||
|
//! These details matter if you are working on smarm itself; they are not
|
||||||
|
//! part of the public contract.
|
||||||
|
//!
|
||||||
|
//! Internally, each live actor that has published at least one channel owns
|
||||||
|
//! a `Mailbox`: its pid plus a set of typed channels, keyed by the message
|
||||||
|
//! type's `TypeId`. A stored channel is a `Box<dyn Any + Send>` that
|
||||||
|
//! is concretely a `Sender<M>`; resolving for `M` looks up that exact
|
||||||
|
//! `TypeId` and downcasts, so the downcast cannot fail on correct data (a
|
||||||
|
//! failure would be a bug in the registry itself, checked in debug builds).
|
||||||
|
//! Registering a name therefore means: find or create the actor's mailbox,
|
||||||
|
//! insert the channel under its type, and point the name at the actor's pid.
|
||||||
|
//!
|
||||||
|
//! There is no callback when an actor exits. Every operation that touches a
|
||||||
|
//! binding checks the target pid's liveness directly against the scheduler's
|
||||||
|
//! slot table (which also tracks a generation counter, so a dead actor's
|
||||||
|
//! reused slot index is never mistaken for the same actor). A binding to a
|
||||||
|
//! dead actor is treated as absent and dropped right there. This keeps the
|
||||||
|
//! registry decoupled from actor teardown, at the cost of a dead binding
|
||||||
|
//! lingering until something happens to look at it.
|
||||||
|
//!
|
||||||
|
//! The whole registry (both the name index and the per-actor mailboxes) sits
|
||||||
|
//! behind one lock, which is what lets a name-addressed [`send`] resolve and
|
||||||
|
//! clone the target's sender in a single critical section. The sender is
|
||||||
|
//! cloned while that lock is held, then the lock is released before the
|
||||||
|
//! actual send, since delivering a message can wake a parked receiver and
|
||||||
|
//! that wakeup work should not run while the registry is locked.
|
||||||
|
|
||||||
use crate::channel::Sender;
|
use crate::channel::Sender;
|
||||||
use crate::pid::{Addressable, Name, Pid};
|
use crate::pid::{Addressable, Name, Pid};
|
||||||
@@ -80,28 +132,33 @@ impl std::fmt::Display for RegisterError {
|
|||||||
|
|
||||||
impl std::error::Error for RegisterError {}
|
impl std::error::Error for RegisterError {}
|
||||||
|
|
||||||
/// Why a name-addressed [`send`] did not deliver. Carries the message back so
|
/// Why a send did not deliver. Every variant carries the undelivered message
|
||||||
/// the caller never loses it (mirrors [`crate::channel::SendError`]).
|
/// back, mirroring [`crate::channel::SendError`], so a failed send never
|
||||||
|
/// silently drops what you tried to send.
|
||||||
///
|
///
|
||||||
/// `Debug`/`Display` are hand-written so neither demands `M: Debug` — the
|
/// `Debug` and `Display` are hand-written so neither requires `M: Debug`,
|
||||||
/// payload is returned, not printed.
|
/// since the payload is handed back to you, not printed.
|
||||||
pub enum SendError<M> {
|
pub enum SendError<M> {
|
||||||
/// No live actor is currently registered under this name. Name-addressed
|
/// No live actor is currently registered under this name. Returned only
|
||||||
/// [`send`] only; the pid-addressed counterpart is [`SendError::Dead`].
|
/// by name-addressed [`send`]; the pid-addressed counterpart of "nothing
|
||||||
|
/// there" is [`SendError::Dead`].
|
||||||
Unresolved(M),
|
Unresolved(M),
|
||||||
/// The pid-addressed actor is no longer the live incarnation this pid names
|
/// The actor this pid identifies has died, even if its slot has since
|
||||||
/// — it has died, even if its slot now holds a *different* actor (a direct
|
/// been taken over by a different, live actor. A direct `Pid<A>` send
|
||||||
/// `Pid<A>` send never redirects; contrast name-addressed [`send`]). Pid
|
/// never redirects to that new occupant; contrast name-addressed
|
||||||
/// paths ([`send_to`] / [`send_dyn`]) only.
|
/// [`send`], which would reach it. Returned by the pid-addressed sends,
|
||||||
|
/// [`send_to`] and [`send_dyn`].
|
||||||
Dead(M),
|
Dead(M),
|
||||||
/// The actor is live but exposes no channel for this message type.
|
/// The actor is live but has not published a channel for this message
|
||||||
|
/// type.
|
||||||
NoChannel(M),
|
NoChannel(M),
|
||||||
/// The actor's channel for this message type is closed (its receiver is gone).
|
/// The actor's channel for this message type is closed (its receiver has
|
||||||
|
/// been dropped).
|
||||||
Closed(M),
|
Closed(M),
|
||||||
/// No live member to deliver to — a [`dispatch`](crate::dispatch) over an
|
/// No live member was available to deliver to: returned by
|
||||||
/// empty (or all-dead) process group. Group-addressed dispatch only; the
|
/// [`dispatch`](crate::dispatch) when the target process group is empty
|
||||||
/// name-addressed counterpart is [`SendError::Unresolved`]. The message is
|
/// or every member in it has died. The name-addressed counterpart of
|
||||||
/// handed back undelivered.
|
/// this case is [`SendError::Unresolved`].
|
||||||
NoMember(M),
|
NoMember(M),
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,7 +195,9 @@ impl<M> std::fmt::Display for SendError<M> {
|
|||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
SendError::Unresolved(_) => write!(f, "no live actor registered under that name"),
|
SendError::Unresolved(_) => write!(f, "no live actor registered under that name"),
|
||||||
SendError::Dead(_) => write!(f, "the addressed actor is no longer the live incarnation"),
|
SendError::Dead(_) => {
|
||||||
|
write!(f, "the addressed actor is no longer the live incarnation")
|
||||||
|
}
|
||||||
SendError::NoChannel(_) => write!(f, "actor has no channel for this message type"),
|
SendError::NoChannel(_) => write!(f, "actor has no channel for this message type"),
|
||||||
SendError::Closed(_) => write!(f, "the actor's channel for this type is closed"),
|
SendError::Closed(_) => write!(f, "the actor's channel for this type is closed"),
|
||||||
SendError::NoMember(_) => write!(f, "no live member in the process group"),
|
SendError::NoMember(_) => write!(f, "no live member in the process group"),
|
||||||
@@ -150,12 +209,13 @@ impl<M> std::error::Error for SendError<M> {}
|
|||||||
|
|
||||||
/// A registry-stored channel, type-erased over its message type. The stored
|
/// A registry-stored channel, type-erased over its message type. The stored
|
||||||
/// object must serve two readers: `clone_sender` (downcast back to the concrete
|
/// object must serve two readers: `clone_sender` (downcast back to the concrete
|
||||||
/// `Sender<M>`) and the RFC 016 snapshot (queued length without knowing `M`).
|
/// `Sender<M>`) and the runtime introspection snapshot (queued length without
|
||||||
/// A bare `Box<dyn Any>` gives the first but not the second, so we erase behind
|
/// knowing `M`). A bare `Box<dyn Any>` gives the first but not the second, so
|
||||||
/// this small trait instead.
|
/// we erase behind this small trait instead.
|
||||||
trait ErasedSender: Send {
|
trait ErasedSender: Send {
|
||||||
fn as_any(&self) -> &dyn Any;
|
fn as_any(&self) -> &dyn Any;
|
||||||
fn queued_len(&self) -> usize;
|
fn queued_len(&self) -> usize;
|
||||||
|
fn receiver_alive(&self) -> bool;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<M: Send + 'static> ErasedSender for Sender<M> {
|
impl<M: Send + 'static> ErasedSender for Sender<M> {
|
||||||
@@ -165,11 +225,14 @@ impl<M: Send + 'static> ErasedSender for Sender<M> {
|
|||||||
fn queued_len(&self) -> usize {
|
fn queued_len(&self) -> usize {
|
||||||
Sender::queued_len(self)
|
Sender::queued_len(self)
|
||||||
}
|
}
|
||||||
|
fn receiver_alive(&self) -> bool {
|
||||||
|
Sender::receiver_alive(self)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// One typed channel of an actor, type-erased. Concretely a `Sender<M>` filed
|
/// One typed channel of an actor, type-erased. Concretely a `Sender<M>` filed
|
||||||
/// under `TypeId::of::<M>()`; `msg_type` is `type_name::<M>()`, kept for
|
/// under `TypeId::of::<M>()`; `msg_type` is `type_name::<M>()`, kept for
|
||||||
/// observers (RFC 014 §4.5) and as the debug cross-check on the downcast.
|
/// observability tooling and as the debug cross-check on the downcast.
|
||||||
struct Channel {
|
struct Channel {
|
||||||
sender: Box<dyn ErasedSender>,
|
sender: Box<dyn ErasedSender>,
|
||||||
msg_type: &'static str,
|
msg_type: &'static str,
|
||||||
@@ -185,7 +248,10 @@ struct Mailbox {
|
|||||||
|
|
||||||
impl Mailbox {
|
impl Mailbox {
|
||||||
fn new(pid: Pid) -> Self {
|
fn new(pid: Pid) -> Self {
|
||||||
Self { pid, channels: HashMap::new() }
|
Self {
|
||||||
|
pid,
|
||||||
|
channels: HashMap::new(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Clone the `Sender<M>` for this actor, if it has one. Called **under the
|
/// Clone the `Sender<M>` for this actor, if it has one. Called **under the
|
||||||
@@ -193,17 +259,18 @@ impl Mailbox {
|
|||||||
/// legal under a Leaf (Leaf -> Channel).
|
/// legal under a Leaf (Leaf -> Channel).
|
||||||
fn clone_sender<M: Send + 'static>(&self) -> Option<Sender<M>> {
|
fn clone_sender<M: Send + 'static>(&self) -> Option<Sender<M>> {
|
||||||
let ch = self.channels.get(&TypeId::of::<M>())?;
|
let ch = self.channels.get(&TypeId::of::<M>())?;
|
||||||
let tx = ch
|
let tx = match ch.sender.as_any().downcast_ref::<Sender<M>>() {
|
||||||
.sender
|
Some(tx) => tx,
|
||||||
.as_any()
|
None => panic!(
|
||||||
.downcast_ref::<Sender<M>>()
|
"smarm: channel keyed by TypeId but downcast to its own type failed (core corrupt)"
|
||||||
.expect("channel keyed by TypeId but downcast to its own type failed — smarm bug");
|
),
|
||||||
|
};
|
||||||
debug_assert_eq!(ch.msg_type, type_name::<M>(), "msg_type / TypeId disagree");
|
debug_assert_eq!(ch.msg_type, type_name::<M>(), "msg_type / TypeId disagree");
|
||||||
Some(tx.clone())
|
Some(tx.clone())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Per-actor registry view handed to RFC 016 introspection: registered names
|
/// Per-actor registry view handed to runtime introspection: registered names
|
||||||
/// and summed mailbox depth, tagged with the mailbox's `pid` so a stale
|
/// and summed mailbox depth, tagged with the mailbox's `pid` so a stale
|
||||||
/// incarnation can be filtered against the slab. Covers only *published*
|
/// incarnation can be filtered against the slab. Covers only *published*
|
||||||
/// channels (`register` / `install` / `spawn_addr` / gen_server start); an
|
/// channels (`register` / `install` / `spawn_addr` / gen_server start); an
|
||||||
@@ -216,39 +283,60 @@ pub(crate) struct MailboxInfo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// The directory. Invariant (held under the registry lock): every value in
|
/// The directory. Invariant (held under the registry lock): every value in
|
||||||
/// `by_name` is the index of a [`Mailbox`] present in `by_index`, and that
|
/// `by_name` is the full [`Pid`] (index *and* generation) of an actor that
|
||||||
/// mailbox's `pid.index()` equals the key. Stale entries (dead actors) violate
|
/// published a [`Mailbox`] into `by_index` at registration time. Stale entries
|
||||||
/// nothing — they are simply pruned on contact.
|
/// (dead holders, including holders whose slot has since been re-tenanted by
|
||||||
|
/// a different actor) violate nothing: they are pruned on contact, and the
|
||||||
|
/// generation makes "dead" decidable even after slot reuse.
|
||||||
pub(crate) struct Registry {
|
pub(crate) struct Registry {
|
||||||
/// `pid.index() -> the actor's mailbox`. The handle store.
|
/// `pid.index() -> the actor's mailbox`. The handle store.
|
||||||
by_index: HashMap<u32, Mailbox>,
|
by_index: HashMap<u32, Mailbox>,
|
||||||
/// `name -> pid.index()`. Several names may map to one actor.
|
/// `name -> holder pid`. Several names may map to one actor. The full pid
|
||||||
by_name: HashMap<&'static str, u32>,
|
/// (not just the index) is load-bearing: an index alone cannot tell a dead
|
||||||
|
/// holder from the live actor now tenanting its recycled slot. Comparing
|
||||||
|
/// only the index would make such a name read as live-held (unresolvable
|
||||||
|
/// and unregisterable at once) and could misdeliver to whatever new,
|
||||||
|
/// same-typed actor now sits in that slot.
|
||||||
|
by_name: HashMap<&'static str, Pid>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Registry {
|
impl Registry {
|
||||||
pub(crate) fn new() -> Self {
|
pub(crate) fn new() -> Self {
|
||||||
Self { by_index: HashMap::new(), by_name: HashMap::new() }
|
Self {
|
||||||
|
by_index: HashMap::new(),
|
||||||
|
by_name: HashMap::new(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Drop a dead actor's mailbox and every name that pointed at it.
|
/// Drop a dead holder's artifacts: every name bound to it, and its
|
||||||
fn prune(&mut self, index: u32) {
|
/// mailbox, but only while the mailbox is still *its own*. A recycled
|
||||||
self.by_index.remove(&index);
|
/// slot's mailbox belongs to the live tenant (publish replaces it
|
||||||
self.by_name.retain(|_, idx| *idx != index);
|
/// wholesale on pid mismatch) and is left untouched.
|
||||||
|
fn prune_holder(&mut self, holder: Pid) {
|
||||||
|
self.by_name.retain(|_, p| *p != holder);
|
||||||
|
if self
|
||||||
|
.by_index
|
||||||
|
.get(&holder.index())
|
||||||
|
.is_some_and(|mb| mb.pid == holder)
|
||||||
|
{
|
||||||
|
self.by_index.remove(&holder.index());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// RFC 016 snapshot input: per-slot-index registry view — the actor's
|
/// Runtime introspection input: per-slot-index registry view, giving the
|
||||||
/// registered names (inverted from `by_name`) and its mailbox depth (queued
|
/// actor's registered names (inverted from `by_name`) and its mailbox
|
||||||
/// messages summed across every published typed channel). Built in one pass
|
/// depth (queued messages summed across every published typed channel).
|
||||||
/// under the registry Leaf; the per-channel `queued_len` takes a Channel
|
/// Carries each mailbox's full `pid` so the caller can discard a stale
|
||||||
/// lock, legal under the Leaf (Leaf → Channel). Carries each mailbox's full
|
/// incarnation's entry against the slab's live generation. Names are
|
||||||
/// `pid` so the caller can discard a stale incarnation's entry against the
|
/// matched to mailboxes by *full pid*, so a stale name (dead holder)
|
||||||
/// slab's live generation. Names that dangle (point at no mailbox) are
|
/// still annotates the corpse's own mailbox if that survives, but never a
|
||||||
/// dropped — they violate no invariant and get pruned on next contact.
|
/// recycled slot's new tenant; names that attach to no mailbox are
|
||||||
|
/// dropped, since that violates no invariant and they get pruned on next
|
||||||
|
/// contact.
|
||||||
pub(crate) fn introspect_map(&self) -> HashMap<u32, MailboxInfo> {
|
pub(crate) fn introspect_map(&self) -> HashMap<u32, MailboxInfo> {
|
||||||
let mut names: HashMap<u32, Vec<&'static str>> = HashMap::new();
|
let mut names: HashMap<Pid, Vec<&'static str>> = HashMap::new();
|
||||||
for (&name, &idx) in &self.by_name {
|
for (&name, &pid) in &self.by_name {
|
||||||
names.entry(idx).or_default().push(name);
|
names.entry(pid).or_default().push(name);
|
||||||
}
|
}
|
||||||
let mut out: HashMap<u32, MailboxInfo> = HashMap::with_capacity(self.by_index.len());
|
let mut out: HashMap<u32, MailboxInfo> = HashMap::with_capacity(self.by_index.len());
|
||||||
for (&idx, mb) in &self.by_index {
|
for (&idx, mb) in &self.by_index {
|
||||||
@@ -257,7 +345,7 @@ impl Registry {
|
|||||||
idx,
|
idx,
|
||||||
MailboxInfo {
|
MailboxInfo {
|
||||||
pid: mb.pid,
|
pid: mb.pid,
|
||||||
names: names.remove(&idx).unwrap_or_default(),
|
names: names.remove(&mb.pid).unwrap_or_default(),
|
||||||
depth: depth.min(u32::MAX as usize) as u32,
|
depth: depth.min(u32::MAX as usize) as u32,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
@@ -267,17 +355,22 @@ impl Registry {
|
|||||||
|
|
||||||
/// Single-actor form of [`introspect_map`](Self::introspect_map): the
|
/// Single-actor form of [`introspect_map`](Self::introspect_map): the
|
||||||
/// registry view for one slot index, or `None` if no mailbox is published
|
/// registry view for one slot index, or `None` if no mailbox is published
|
||||||
/// there. Used by `actor_info` so its cost stays proportional to the one
|
/// there. Used by the runtime's per-actor introspection so its cost stays
|
||||||
/// actor rather than locking every channel in the runtime.
|
/// proportional to the one actor rather than locking every channel in the
|
||||||
|
/// runtime.
|
||||||
pub(crate) fn introspect_one(&self, idx: u32) -> Option<MailboxInfo> {
|
pub(crate) fn introspect_one(&self, idx: u32) -> Option<MailboxInfo> {
|
||||||
let mb = self.by_index.get(&idx)?;
|
let mb = self.by_index.get(&idx)?;
|
||||||
let depth: usize = mb.channels.values().map(|c| c.sender.queued_len()).sum();
|
let depth: usize = mb.channels.values().map(|c| c.sender.queued_len()).sum();
|
||||||
let names = self
|
let names = self
|
||||||
.by_name
|
.by_name
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|(&n, &i)| (i == idx).then_some(n))
|
.filter_map(|(&n, &p)| (p == mb.pid).then_some(n))
|
||||||
.collect();
|
.collect();
|
||||||
Some(MailboxInfo { pid: mb.pid, names, depth: depth.min(u32::MAX as usize) as u32 })
|
Some(MailboxInfo {
|
||||||
|
pid: mb.pid,
|
||||||
|
names,
|
||||||
|
depth: depth.min(u32::MAX as usize) as u32,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -286,14 +379,18 @@ fn live(inner: &crate::runtime::RuntimeInner, pid: Pid) -> bool {
|
|||||||
inner.slot_at(pid).is_some_and(|s| s.is_live_for(pid))
|
inner.slot_at(pid).is_some_and(|s| s.is_live_for(pid))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Publish the current actor's `Sender<M>` under `name`, capturing the channel
|
/// Give the current actor's channel a name, so other actors can find and
|
||||||
/// so the name becomes messageable. Idempotent for the same `(name, type)`;
|
/// message it by that name instead of needing its [`Pid`].
|
||||||
/// registering a *second* type under the same (or another) name on the same
|
|
||||||
/// actor just adds another channel to the actor's mailbox.
|
|
||||||
///
|
///
|
||||||
/// Fails with [`RegisterError::NameTaken`] if the name is held by a *different*
|
/// Calling this again with the same `(name, type)` from the same actor is
|
||||||
/// live actor (a binding to a dead actor is pruned and the name treated as
|
/// harmless. Registering a *second* message type under the same (or a
|
||||||
/// free). Panics if called outside `Runtime::run()`.
|
/// different) name from the same actor just adds another typed channel to
|
||||||
|
/// that actor's mailbox; it does not replace the first.
|
||||||
|
///
|
||||||
|
/// Fails with [`RegisterError::NameTaken`] if the name is currently held by a
|
||||||
|
/// *different* live actor. A name held by an actor that has since died is not
|
||||||
|
/// considered taken: it is quietly reclaimed and handed to you. Panics if
|
||||||
|
/// called outside [`run`](crate::run).
|
||||||
pub fn register<M: Send + 'static>(name: Name<M>, tx: Sender<M>) -> Result<(), RegisterError> {
|
pub fn register<M: Send + 'static>(name: Name<M>, tx: Sender<M>) -> Result<(), RegisterError> {
|
||||||
register_with(self_pid(), name.as_str(), tx)
|
register_with(self_pid(), name.as_str(), tx)
|
||||||
}
|
}
|
||||||
@@ -301,8 +398,8 @@ pub fn register<M: Send + 'static>(name: Name<M>, tx: Sender<M>) -> Result<(), R
|
|||||||
/// Bind `name` to `pid`'s mailbox and publish `tx` under `M`'s [`TypeId`], for
|
/// Bind `name` to `pid`'s mailbox and publish `tx` under `M`'s [`TypeId`], for
|
||||||
/// an explicit (already-live) actor rather than `self`. The shared core of
|
/// an explicit (already-live) actor rather than `self`. The shared core of
|
||||||
/// [`register`] (which passes `self_pid()`) and the parent-side server-name
|
/// [`register`] (which passes `self_pid()`) and the parent-side server-name
|
||||||
/// bind in `gen_server` (which names a freshly spawned server before its body
|
/// bind in `gen_server`, which names a freshly spawned server before its body
|
||||||
/// has run, so the name resolves the instant `start()` returns). Same collision
|
/// has run, so the name resolves the instant `start()` returns. Same collision
|
||||||
/// rules and lock discipline as `register`.
|
/// rules and lock discipline as `register`.
|
||||||
pub(crate) fn register_with<M: Send + 'static>(
|
pub(crate) fn register_with<M: Send + 'static>(
|
||||||
me: Pid,
|
me: Pid,
|
||||||
@@ -310,25 +407,36 @@ pub(crate) fn register_with<M: Send + 'static>(
|
|||||||
tx: Sender<M>,
|
tx: Sender<M>,
|
||||||
) -> Result<(), RegisterError> {
|
) -> Result<(), RegisterError> {
|
||||||
with_runtime(|inner| {
|
with_runtime(|inner| {
|
||||||
|
// Stamp-eligibility for the terminal record (soak sig 4): flag the
|
||||||
|
// tenancy BEFORE the binding lands and outside the registry lock (no
|
||||||
|
// nesting), so no successfully-registered actor can die unflagged.
|
||||||
|
// A register that then fails leaves a harmless overshoot; a stale
|
||||||
|
// `me` is screened by the same live() the binding requires below.
|
||||||
|
if live(inner, me) {
|
||||||
|
if let Some(slot) = inner.slot_at(me) {
|
||||||
|
slot.cold.lock().watchable = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
let mut reg = inner.registry.lock();
|
let mut reg = inner.registry.lock();
|
||||||
if !live(inner, me) {
|
if !live(inner, me) {
|
||||||
return Err(RegisterError::NoProc);
|
return Err(RegisterError::NoProc);
|
||||||
}
|
}
|
||||||
if let Some(&holder_idx) = reg.by_name.get(key) {
|
if let Some(&holder) = reg.by_name.get(key) {
|
||||||
match reg.by_index.get(&holder_idx).map(|m| m.pid) {
|
if holder == me {
|
||||||
Some(holder) if holder == me => {} // same actor: just add the channel below
|
// Same actor: just add the channel below.
|
||||||
Some(holder) if live(inner, holder) => {
|
} else if live(inner, holder) {
|
||||||
return Err(RegisterError::NameTaken { holder });
|
return Err(RegisterError::NameTaken { holder });
|
||||||
}
|
} else {
|
||||||
Some(_) => reg.prune(holder_idx), // dead holder: free the name
|
// Dead holder: free the name (and its other stale artifacts).
|
||||||
None => {
|
// Liveness is judged against the *stored* pid, generation
|
||||||
reg.by_name.remove(key); // dangling name: free it
|
// included, so a recycled slot's live tenant no longer makes a
|
||||||
}
|
// dead name read as taken.
|
||||||
|
reg.prune_holder(holder);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Publish (or extend) the mailbox with this channel, then bind the name.
|
// Publish (or extend) the mailbox with this channel, then bind the name.
|
||||||
publish_channel::<M>(&mut reg, me, tx);
|
publish_channel::<M>(&mut reg, me, tx);
|
||||||
reg.by_name.insert(key, me.index());
|
reg.by_name.insert(key, me);
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -338,27 +446,66 @@ pub(crate) fn register_with<M: Send + 'static>(
|
|||||||
/// name) and [`install`] (which does not). A leftover mailbox at this slot
|
/// name) and [`install`] (which does not). A leftover mailbox at this slot
|
||||||
/// index from a dead prior incarnation (pid mismatch) is replaced wholesale.
|
/// index from a dead prior incarnation (pid mismatch) is replaced wholesale.
|
||||||
/// Caller holds the registry lock and has established that `me` is live.
|
/// Caller holds the registry lock and has established that `me` is live.
|
||||||
|
///
|
||||||
|
/// **One channel per message type per actor.** Publishing a second `M`
|
||||||
|
/// channel on the same live actor replaces the first — and if the first's
|
||||||
|
/// receiver is still alive, that replacement drops its last sender, closing
|
||||||
|
/// it, and any `recv`/`select` on it then returns "closed" immediately and
|
||||||
|
/// forever: a silent hot loop that starves the scheduler. That is never
|
||||||
|
/// intended, so it panics here (found the hard way in RFC 010 c9, where two
|
||||||
|
/// `Name<String>`s registered on one actor did exactly this). Replacing a
|
||||||
|
/// channel whose receiver is already gone is fine (an actor re-registering
|
||||||
|
/// after dropping its old inbox) and stays silent. To hold two names of the
|
||||||
|
/// same type, register them from two actors, or bind both names to one
|
||||||
|
/// cloned sender.
|
||||||
fn publish_channel<M: Send + 'static>(reg: &mut Registry, me: Pid, tx: Sender<M>) {
|
fn publish_channel<M: Send + 'static>(reg: &mut Registry, me: Pid, tx: Sender<M>) {
|
||||||
let mb = reg.by_index.entry(me.index()).or_insert_with(|| Mailbox::new(me));
|
let mb = reg
|
||||||
|
.by_index
|
||||||
|
.entry(me.index())
|
||||||
|
.or_insert_with(|| Mailbox::new(me));
|
||||||
if mb.pid != me {
|
if mb.pid != me {
|
||||||
*mb = Mailbox::new(me);
|
*mb = Mailbox::new(me);
|
||||||
}
|
}
|
||||||
|
if let Some(existing) = mb.channels.get(&TypeId::of::<M>()) {
|
||||||
|
assert!(
|
||||||
|
!existing.sender.receiver_alive() || same_channel::<M>(existing, &tx),
|
||||||
|
"smarm: actor {me:?} already publishes a live channel for message type `{}`; \
|
||||||
|
a second one would replace and CLOSE the first (its receiver would then \
|
||||||
|
read as closed forever). Register the second name from another actor, or \
|
||||||
|
bind both names to a clone of the same sender.",
|
||||||
|
type_name::<M>()
|
||||||
|
);
|
||||||
|
}
|
||||||
mb.channels.insert(
|
mb.channels.insert(
|
||||||
TypeId::of::<M>(),
|
TypeId::of::<M>(),
|
||||||
Channel { sender: Box::new(tx), msg_type: type_name::<M>() },
|
Channel {
|
||||||
|
sender: Box::new(tx),
|
||||||
|
msg_type: type_name::<M>(),
|
||||||
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// True if `existing` and `tx` are senders of the very same channel (a
|
||||||
|
/// cloned sender bound under a second name is the sanctioned way to hold two
|
||||||
|
/// names of one type on one actor).
|
||||||
|
fn same_channel<M: Send + 'static>(existing: &Channel, tx: &Sender<M>) -> bool {
|
||||||
|
existing
|
||||||
|
.sender
|
||||||
|
.as_any()
|
||||||
|
.downcast_ref::<Sender<M>>()
|
||||||
|
.is_some_and(|old| old.same_channel(tx))
|
||||||
|
}
|
||||||
|
|
||||||
/// Publish the current actor's `Sender<A::Msg>` into its mailbox **without**
|
/// Publish the current actor's `Sender<A::Msg>` into its mailbox **without**
|
||||||
/// binding a name, and hand back the typed [`Pid<A>`] that addresses this
|
/// binding a name, and hand back the typed [`Pid<A>`] that addresses this
|
||||||
/// actor directly. This is the opt-in, lazy install of RFC 014 §5: an actor
|
/// actor directly.
|
||||||
/// that wants to be reachable by a direct, identity-bound [`Pid<A>`] (rather
|
|
||||||
/// than only via a re-resolving [`Name`]) calls this once with its inbox
|
|
||||||
/// sender, then hands the returned pid out.
|
|
||||||
///
|
///
|
||||||
/// Unlike [`register`] there is no name to collide on, and `self` is always a
|
/// This is for an actor that wants to be reachable directly by its pid,
|
||||||
/// live actor inside `run()`, so this is infallible. Panics if called outside
|
/// rather than only through a re-resolving [`Name`]: call this once with your
|
||||||
/// `Runtime::run()`.
|
/// inbox sender, then hand the returned `Pid<A>` to whoever should be able to
|
||||||
|
/// message you. Unlike [`register`] there is no name to collide on, and the
|
||||||
|
/// current actor is always live while inside `run()`, so this cannot fail.
|
||||||
|
/// Panics if called outside [`run`](crate::run).
|
||||||
pub fn install<A: Addressable>(tx: Sender<A::Msg>) -> Pid<A> {
|
pub fn install<A: Addressable>(tx: Sender<A::Msg>) -> Pid<A> {
|
||||||
let me = self_pid();
|
let me = self_pid();
|
||||||
with_runtime(|inner| {
|
with_runtime(|inner| {
|
||||||
@@ -374,158 +521,217 @@ pub fn install<A: Addressable>(tx: Sender<A::Msg>) -> Pid<A> {
|
|||||||
/// Publish `tx` into `pid`'s mailbox under `M`'s [`TypeId`], for an explicit
|
/// Publish `tx` into `pid`'s mailbox under `M`'s [`TypeId`], for an explicit
|
||||||
/// (freshly minted, already-live) actor rather than `self`. The parent-side
|
/// (freshly minted, already-live) actor rather than `self`. The parent-side
|
||||||
/// half of [`spawn_addr`](crate::spawn_addr): the spawner makes the inbox and
|
/// half of [`spawn_addr`](crate::spawn_addr): the spawner makes the inbox and
|
||||||
/// publishes the sender here *before* handing back the `Pid<A>`, so an immediate
|
/// publishes the sender here *before* handing back the `Pid<A>`, so an
|
||||||
/// `send_to` on the returned pid always resolves — the address is live the
|
/// immediate `send_to` on the returned pid always resolves. The address is
|
||||||
/// instant the caller holds it, with no dependence on the body having run yet.
|
/// live the instant the caller holds it, with no dependence on the spawned
|
||||||
|
/// actor's body having run yet.
|
||||||
///
|
///
|
||||||
/// Caller guarantees `pid` is the just-installed actor (Queued, this exact
|
/// Caller guarantees `pid` is the just-installed actor (queued, this exact
|
||||||
/// incarnation); `publish_channel` replaces any stale leftover at the slot.
|
/// incarnation); `publish_channel` replaces any stale leftover at the slot.
|
||||||
pub(crate) fn install_for<M: Send + 'static>(pid: Pid, tx: Sender<M>) {
|
pub(crate) fn install_for<M: Send + 'static>(pid: Pid, tx: Sender<M>) {
|
||||||
with_runtime(|inner| {
|
with_runtime(|inner| {
|
||||||
let mut reg = inner.registry.lock();
|
let mut reg = inner.registry.lock();
|
||||||
debug_assert!(live(inner, pid), "install_for: pid must be a freshly spawned, live actor");
|
debug_assert!(
|
||||||
|
live(inner, pid),
|
||||||
|
"install_for: pid must be a freshly spawned, live actor"
|
||||||
|
);
|
||||||
publish_channel::<M>(&mut reg, pid, tx);
|
publish_channel::<M>(&mut reg, pid, tx);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The single actor currently registered under `name`, or `None` if unbound or
|
/// Look up which actor currently holds `name`, if any. Returns `None` if the
|
||||||
/// no longer live (the stale binding is pruned on the way out).
|
/// name is unbound, or if it was bound to an actor that has since died (the
|
||||||
|
/// stale binding is cleared as a side effect of this call).
|
||||||
pub fn whereis(name: &str) -> Option<Pid> {
|
pub fn whereis(name: &str) -> Option<Pid> {
|
||||||
with_runtime(|inner| {
|
with_runtime(|inner| {
|
||||||
let mut reg = inner.registry.lock();
|
let mut reg = inner.registry.lock();
|
||||||
let idx = *reg.by_name.get(name)?;
|
let pid = *reg.by_name.get(name)?;
|
||||||
match reg.by_index.get(&idx).map(|m| m.pid) {
|
if live(inner, pid) {
|
||||||
Some(pid) if live(inner, pid) => Some(pid),
|
Some(pid)
|
||||||
Some(_) => {
|
} else {
|
||||||
reg.prune(idx);
|
// Generation-checked against the stored holder: a recycled slot's
|
||||||
None
|
// live tenant reads dead here, and the stale name heals.
|
||||||
}
|
reg.prune_holder(pid);
|
||||||
None => {
|
None
|
||||||
reg.by_name.remove(name);
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Resolve `name` to a *typed* [`Pid<A>`] — the identity-bound counterpart of
|
/// What a name is bound to, three-valued (bridge soak signature 4).
|
||||||
/// [`whereis`] (RFC 014 §4.4). Recovers the compile-checked
|
|
||||||
/// [`send_to`](crate::send_to) path from a durable name: looks the name up,
|
|
||||||
/// then re-types the erased pid as `Pid<A>` via the unchecked
|
|
||||||
/// [`assert_type`](crate::pid::assert_type) primitive. A wrong `A` is not
|
|
||||||
/// unsound — it degrades to [`SendError::NoChannel`] on the next send (routing
|
|
||||||
/// is by message `TypeId`), never a misdelivery. `None` if unbound or dead.
|
|
||||||
///
|
///
|
||||||
/// Panics if called outside `Runtime::run()`.
|
/// [`Live`](NameResolution::Live) is [`whereis`]'s `Some`.
|
||||||
|
/// [`Corpse`](NameResolution::Corpse) carries the *stored* holder pid of a
|
||||||
|
/// dead-but-unpruned binding — a state Erlang cannot represent (its name
|
||||||
|
/// death unregisters atomically; smarm's prune is lazy), captured here before
|
||||||
|
/// the prune that `whereis` performs discards it, so the caller can consult
|
||||||
|
/// [`terminal_reason`](crate::monitor::terminal_reason) for the tenancy's
|
||||||
|
/// real down reason. [`Unbound`](NameResolution::Unbound) matches Erlang's
|
||||||
|
/// unregistered name.
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
pub enum NameResolution {
|
||||||
|
/// The stored holder is live (generation-checked); the binding stands.
|
||||||
|
Live(Pid),
|
||||||
|
/// The stored holder is dead. The binding was pruned on the way out —
|
||||||
|
/// the name heals exactly as `whereis` heals it; only the evidence is
|
||||||
|
/// returned instead of discarded. A second resolve is `Unbound`.
|
||||||
|
Corpse(Pid),
|
||||||
|
/// No binding stored (never registered, or already pruned by any reader).
|
||||||
|
Unbound,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Resolve `name` like [`whereis`], but keep the corpse: the dead-holder arm
|
||||||
|
/// returns the stored pid it pruned instead of a bare `None`. Same lock
|
||||||
|
/// discipline and pruning behavior as `whereis`; same `Runtime::run()`
|
||||||
|
/// context contract.
|
||||||
|
pub fn resolve_name(name: &str) -> NameResolution {
|
||||||
|
with_runtime(|inner| {
|
||||||
|
let mut reg = inner.registry.lock();
|
||||||
|
let Some(&pid) = reg.by_name.get(name) else {
|
||||||
|
return NameResolution::Unbound;
|
||||||
|
};
|
||||||
|
if live(inner, pid) {
|
||||||
|
NameResolution::Live(pid)
|
||||||
|
} else {
|
||||||
|
reg.prune_holder(pid);
|
||||||
|
NameResolution::Corpse(pid)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Like [`whereis`], but returns a *typed* [`Pid<A>`] instead of a bare
|
||||||
|
/// [`Pid`], so a follow-up [`send_to`] is compile-checked instead of needing
|
||||||
|
/// the untyped [`send_dyn`] escape hatch. `None` if the name is unbound or its
|
||||||
|
/// holder has died.
|
||||||
|
///
|
||||||
|
/// The type `A` is not checked against what the name's holder actually
|
||||||
|
/// published: if you pick the wrong `A`, this still succeeds, but the next
|
||||||
|
/// send against the returned pid degrades to [`SendError::NoChannel`] rather
|
||||||
|
/// than reaching the wrong actor or the wrong channel.
|
||||||
|
///
|
||||||
|
/// Panics if called outside [`run`](crate::run).
|
||||||
pub fn lookup_as<A: Addressable>(name: &str) -> Option<Pid<A>> {
|
pub fn lookup_as<A: Addressable>(name: &str) -> Option<Pid<A>> {
|
||||||
whereis(name).map(crate::pid::assert_type::<A>)
|
whereis(name).map(crate::pid::assert_type::<A>)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Resolve `name` to its actor's pid and a cloned `Sender<M>`, under the Leaf
|
/// Resolve `name` to its actor's pid and a cloned `Sender<M>`, all under one
|
||||||
/// lock (clone-under-lock, then release). The crate-internal building block for
|
/// lock acquisition. The crate-internal building block for `gen_server`'s
|
||||||
/// `gen_server`'s by-name addressing: a named server publishes its inbox as a
|
/// by-name addressing: a named server publishes its inbox as a
|
||||||
/// `Sender<Envelope<G>>` (via [`register_with`]), and `whereis_server` / `call`
|
/// `Sender<Envelope<G>>` (via [`register_with`]), and the server's `call` /
|
||||||
/// / `cast` recover that exact typed sender here to rebuild a `ServerRef<G>`.
|
/// `cast` / `whereis_server` recover that exact typed sender here to rebuild a
|
||||||
/// `None` if unbound, dead (pruned on the way out), or holding no `M` channel.
|
/// `GenServerRef<G>`. `None` if unbound, dead (pruned on the way out), or
|
||||||
|
/// holding no `M` channel.
|
||||||
pub(crate) fn resolve_named_sender<M: Send + 'static>(name: &str) -> Option<(Pid, Sender<M>)> {
|
pub(crate) fn resolve_named_sender<M: Send + 'static>(name: &str) -> Option<(Pid, Sender<M>)> {
|
||||||
with_runtime(|inner| {
|
with_runtime(|inner| {
|
||||||
let mut reg = inner.registry.lock();
|
let mut reg = inner.registry.lock();
|
||||||
let idx = *reg.by_name.get(name)?;
|
let pid = *reg.by_name.get(name)?;
|
||||||
let pid = match reg.by_index.get(&idx).map(|m| m.pid) {
|
if !live(inner, pid) {
|
||||||
Some(pid) if live(inner, pid) => pid,
|
// Stored-pid liveness, generation included: a name whose holder
|
||||||
Some(_) => {
|
// died is pruned (heals) even if the slot has a new tenant.
|
||||||
reg.prune(idx);
|
// Otherwise the tenant's mailbox would make the name unresolvable
|
||||||
return None;
|
// without pruning, wedging it for the tenant's lifetime.
|
||||||
}
|
reg.prune_holder(pid);
|
||||||
None => {
|
return None;
|
||||||
reg.by_name.remove(name);
|
}
|
||||||
return None;
|
// A live holder's mailbox is its own (publish replaces wholesale on
|
||||||
}
|
// pid mismatch, and one live actor per slot), so index lookup is safe.
|
||||||
};
|
let tx = reg
|
||||||
let tx = reg.by_index.get(&idx).and_then(Mailbox::clone_sender::<M>)?;
|
.by_index
|
||||||
|
.get(&pid.index())
|
||||||
|
.and_then(Mailbox::clone_sender::<M>)?;
|
||||||
Some((pid, tx))
|
Some((pid, tx))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Remove the binding for `name`, returning the actor it pointed at if still
|
/// Give up a name. Returns the actor it pointed at, if that actor was still
|
||||||
/// live. Only the *name* is freed; the actor's mailbox (and any other names for
|
/// live. Only the *name* is freed; the actor's mailbox (and any other names
|
||||||
/// it) remain. A binding to a dead actor is reported as `None`.
|
/// bound to it) are unaffected. A binding to an already-dead actor reports
|
||||||
|
/// `None`, since there was nothing live to release.
|
||||||
pub fn unregister(name: &str) -> Option<Pid> {
|
pub fn unregister(name: &str) -> Option<Pid> {
|
||||||
with_runtime(|inner| {
|
with_runtime(|inner| {
|
||||||
let mut reg = inner.registry.lock();
|
let mut reg = inner.registry.lock();
|
||||||
let idx = reg.by_name.remove(name)?;
|
let pid = reg.by_name.remove(name)?;
|
||||||
match reg.by_index.get(&idx).map(|m| m.pid) {
|
if live(inner, pid) {
|
||||||
Some(pid) if live(inner, pid) => Some(pid),
|
Some(pid)
|
||||||
_ => None,
|
} else {
|
||||||
|
None
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Resolve `name` to its actor's `Sender<M>` and deliver `msg`. The whole point
|
/// Look `name` up and deliver `msg` to whichever actor currently holds it.
|
||||||
/// of the rework: a name you can *send* to.
|
/// This is the point of naming an actor: a name you can send a message to
|
||||||
|
/// directly, without a separate lookup step.
|
||||||
///
|
///
|
||||||
/// Errors (message returned in every case): [`SendError::Unresolved`] if no
|
/// On failure the message comes back to you, wrapped in the [`SendError`]
|
||||||
/// live actor holds the name, [`SendError::NoChannel`] if that actor has no
|
/// variant that explains why: [`SendError::Unresolved`] if no live actor
|
||||||
/// channel for `M`, [`SendError::Closed`] if its `M` channel's receiver is
|
/// currently holds the name, [`SendError::NoChannel`] if the actor that holds
|
||||||
/// gone. Panics if called outside `Runtime::run()`.
|
/// it never published a channel for `M`, or [`SendError::Closed`] if it
|
||||||
|
/// published one but has since dropped the receiving end. Panics if called
|
||||||
|
/// outside [`run`](crate::run).
|
||||||
pub fn send<M: Send + 'static>(name: Name<M>, msg: M) -> Result<(), SendError<M>> {
|
pub fn send<M: Send + 'static>(name: Name<M>, msg: M) -> Result<(), SendError<M>> {
|
||||||
let key = name.as_str();
|
let key = name.as_str();
|
||||||
with_runtime(|inner| {
|
with_runtime(|inner| {
|
||||||
// Resolve + clone the sender under the Leaf lock, then drop the lock
|
// Resolve + clone the sender under the registry lock, then drop the
|
||||||
// before sending (a send can unpark a receiver).
|
// lock before sending (a send can unpark a receiver).
|
||||||
let tx = {
|
let tx = {
|
||||||
let mut reg = inner.registry.lock();
|
let mut reg = inner.registry.lock();
|
||||||
let idx = match reg.by_name.get(key) {
|
let pid = match reg.by_name.get(key) {
|
||||||
Some(&i) => i,
|
Some(&p) => p,
|
||||||
None => return Err(SendError::Unresolved(msg)),
|
None => return Err(SendError::Unresolved(msg)),
|
||||||
};
|
};
|
||||||
let pid = match reg.by_index.get(&idx).map(|m| m.pid) {
|
|
||||||
Some(pid) => pid,
|
|
||||||
None => {
|
|
||||||
reg.by_name.remove(key);
|
|
||||||
return Err(SendError::Unresolved(msg));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
if !live(inner, pid) {
|
if !live(inner, pid) {
|
||||||
reg.prune(idx);
|
// Stored-pid liveness (generation included), so a recycled
|
||||||
|
// slot's new live tenant is never mistaken for the name's
|
||||||
|
// original (now-dead) holder.
|
||||||
|
reg.prune_holder(pid);
|
||||||
return Err(SendError::Unresolved(msg));
|
return Err(SendError::Unresolved(msg));
|
||||||
}
|
}
|
||||||
match reg.by_index.get(&idx).and_then(Mailbox::clone_sender::<M>) {
|
match reg
|
||||||
|
.by_index
|
||||||
|
.get(&pid.index())
|
||||||
|
.and_then(Mailbox::clone_sender::<M>)
|
||||||
|
{
|
||||||
Some(tx) => tx,
|
Some(tx) => tx,
|
||||||
None => return Err(SendError::NoChannel(msg)),
|
None => return Err(SendError::NoChannel(msg)),
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
tx.send(msg).map_err(|crate::channel::SendError(m)| SendError::Closed(m))
|
tx.send(msg)
|
||||||
|
.map_err(|crate::channel::SendError(m)| SendError::Closed(m))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Resolve a *raw* pid to its mailbox and deliver `msg` on the channel for `M`,
|
/// Resolve a *raw* pid to its mailbox and deliver `msg` on the channel for `M`,
|
||||||
/// with **no redirect**. The stored mailbox must be this exact incarnation
|
/// with **no redirect**. The stored mailbox must be this exact incarnation
|
||||||
/// (generation included) and still live; otherwise the actor this pid named is
|
/// (generation included) and still live; otherwise the actor this pid named
|
||||||
/// gone and the result is [`SendError::Dead`] — even when the slot now holds a
|
/// is gone and the result is [`SendError::Dead`], even when the slot now
|
||||||
/// different, live actor (which we leave untouched). Shared by [`send_to`]
|
/// holds a different, live actor (which is left untouched). Shared by
|
||||||
/// (typed, `M = A::Msg`, channel guaranteed on an installed actor) and
|
/// [`send_to`] (typed, `M = A::Msg`, channel guaranteed on an installed
|
||||||
/// [`send_dyn`] (explicit `M`, where `NoChannel` is a real outcome).
|
/// actor) and [`send_dyn`] (explicit `M`, where `NoChannel` is a real
|
||||||
|
/// outcome).
|
||||||
fn send_to_pid<M: Send + 'static>(
|
fn send_to_pid<M: Send + 'static>(
|
||||||
inner: &crate::runtime::RuntimeInner,
|
inner: &crate::runtime::RuntimeInner,
|
||||||
pid: Pid,
|
pid: Pid,
|
||||||
msg: M,
|
msg: M,
|
||||||
) -> Result<(), SendError<M>> {
|
) -> Result<(), SendError<M>> {
|
||||||
// Resolve + clone the sender under the Leaf lock, then drop the lock before
|
// Resolve + clone the sender under the registry lock, then drop the lock
|
||||||
// sending (a send can unpark a receiver) — Leaf -> Channel, as name `send`.
|
// before sending (a send can unpark a receiver), same order as `send`.
|
||||||
let tx = {
|
let tx = {
|
||||||
let mut reg = inner.registry.lock();
|
let mut reg = inner.registry.lock();
|
||||||
match reg.by_index.get(&pid.index()).map(|m| m.pid) {
|
match reg.by_index.get(&pid.index()).map(|m| m.pid) {
|
||||||
// Exact incarnation, still alive: its `M` channel, or NoChannel.
|
// Exact incarnation, still alive: its `M` channel, or NoChannel.
|
||||||
Some(stored) if stored == pid && live(inner, pid) => {
|
Some(stored) if stored == pid && live(inner, pid) => {
|
||||||
match reg.by_index.get(&pid.index()).and_then(Mailbox::clone_sender::<M>) {
|
match reg
|
||||||
|
.by_index
|
||||||
|
.get(&pid.index())
|
||||||
|
.and_then(Mailbox::clone_sender::<M>)
|
||||||
|
{
|
||||||
Some(tx) => tx,
|
Some(tx) => tx,
|
||||||
None => return Err(SendError::NoChannel(msg)),
|
None => return Err(SendError::NoChannel(msg)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Our incarnation's mailbox, but the actor has died: prune + Dead.
|
// Our incarnation's mailbox, but the actor has died: prune + Dead.
|
||||||
Some(stored) if stored == pid => {
|
Some(stored) if stored == pid => {
|
||||||
reg.prune(pid.index());
|
reg.prune_holder(pid);
|
||||||
return Err(SendError::Dead(msg));
|
return Err(SendError::Dead(msg));
|
||||||
}
|
}
|
||||||
// A different incarnation (or nothing) occupies the slot: the actor
|
// A different incarnation (or nothing) occupies the slot: the actor
|
||||||
@@ -533,35 +739,40 @@ fn send_to_pid<M: Send + 'static>(
|
|||||||
_ => return Err(SendError::Dead(msg)),
|
_ => return Err(SendError::Dead(msg)),
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
tx.send(msg).map_err(|crate::channel::SendError(m)| SendError::Closed(m))
|
tx.send(msg)
|
||||||
|
.map_err(|crate::channel::SendError(m)| SendError::Closed(m))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Deliver `msg` to the exact actor named by `pid` — RFC 014 §4.2's direct,
|
/// Deliver `msg` directly to the exact actor identified by `pid`. Unlike
|
||||||
/// identity-bound addressing mode. Unlike name-addressed [`send`] there is **no
|
/// name-addressed [`send`], there is **no redirect**: if that specific actor
|
||||||
/// redirect**: if that incarnation has died the message comes back as
|
/// has died, the message comes back as [`SendError::Dead`], even if its slot
|
||||||
/// [`SendError::Dead`], even if its slot now holds a different actor.
|
/// has since been taken over by a different, live actor. Use this when you
|
||||||
|
/// already hold a `Pid<A>` and want to talk to that one actor specifically;
|
||||||
|
/// use [`send`] with a [`Name`] when you want whichever actor currently holds
|
||||||
|
/// a name.
|
||||||
///
|
///
|
||||||
/// The message type is the actor's `A::Msg`, so on a live actor that has
|
/// The message type is the actor's `A::Msg`, so on a live actor that has
|
||||||
/// installed its inbox (via [`install`] or [`register`]) the channel is always
|
/// installed its inbox (via [`install`] or [`register`]) the channel is
|
||||||
/// present; [`SendError::NoChannel`] therefore means the actor is live but
|
/// always present; [`SendError::NoChannel`] therefore means the actor is live
|
||||||
/// never published a `Pid<A>`-reachable inbox. Panics if called outside
|
/// but never published a `Pid<A>`-reachable inbox. Panics if called outside
|
||||||
/// `Runtime::run()`.
|
/// [`run`](crate::run).
|
||||||
pub fn send_to<A: Addressable>(pid: Pid<A>, msg: A::Msg) -> Result<(), SendError<A::Msg>> {
|
pub fn send_to<A: Addressable>(pid: Pid<A>, msg: A::Msg) -> Result<(), SendError<A::Msg>> {
|
||||||
with_runtime(|inner| send_to_pid::<A::Msg>(inner, pid.erase(), msg))
|
with_runtime(|inner| send_to_pid::<A::Msg>(inner, pid.erase(), msg))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The explicit bare-pid escape hatch (RFC 014 §4.6): deliver `msg` of type `M`
|
/// The escape hatch for sending to a bare, untyped [`Pid`] when the typed
|
||||||
/// to `pid` when all you hold is an untyped [`Pid`] — a pid off a [`Down`], or
|
/// [`send_to`] is unavailable, for example a pid recovered from a [`Down`]
|
||||||
/// out of a future `members()` — so the typed [`send_to`] is unavailable.
|
/// notification or a group's `members()` list, where you no longer know the
|
||||||
|
/// actor's message type at compile time.
|
||||||
///
|
///
|
||||||
/// This is the one send whose message type can genuinely be wrong: the actor
|
/// Because the message type is not checked at compile time here, this is the
|
||||||
/// may be live yet expose no channel for `M`, returning [`SendError::NoChannel`]
|
/// one send that can genuinely be live-but-wrong: the actor may be alive yet
|
||||||
/// (on the typed paths that downcast collapses to a `debug_assert`). It is
|
/// expose no channel for `M`, in which case you get [`SendError::NoChannel`]
|
||||||
/// named and documented as the fallible fallback so the typed `Pid<A>` /
|
/// back instead of a misdelivery. Liveness and redirect behavior are
|
||||||
/// `Name<M>` paths stay the obvious default and an agentic caller reaches for a
|
/// otherwise identical to [`send_to`]: identity-bound, no redirect,
|
||||||
/// present primitive instead of inventing a workaround. Liveness is identical
|
/// [`SendError::Dead`] once the addressed incarnation is gone. Prefer
|
||||||
/// to [`send_to`]: identity-bound, no redirect, [`SendError::Dead`] once the
|
/// `send_to` with a typed `Pid<A>` whenever you have one; reach for this only
|
||||||
/// addressed incarnation is gone. Panics if called outside `Runtime::run()`.
|
/// when you don't. Panics if called outside [`run`](crate::run).
|
||||||
///
|
///
|
||||||
/// [`Down`]: crate::Down
|
/// [`Down`]: crate::Down
|
||||||
pub fn send_dyn<M: Send + 'static>(pid: Pid, msg: M) -> Result<(), SendError<M>> {
|
pub fn send_dyn<M: Send + 'static>(pid: Pid, msg: M) -> Result<(), SendError<M>> {
|
||||||
|
|||||||
+217
-31
@@ -2,9 +2,9 @@
|
|||||||
//! features (no runtime dispatch — the scheduler's pop loop is the hottest
|
//! features (no runtime dispatch — the scheduler's pop loop is the hottest
|
||||||
//! code in the runtime):
|
//! code in the runtime):
|
||||||
//!
|
//!
|
||||||
//! - `rq-mutex` (default) — `Mutex<VecDeque>`. The control/baseline:
|
//! - `rq-mutex` — `Mutex<VecDeque>`. The control/baseline:
|
||||||
//! strictly FIFO, trivially correct, one global lock.
|
//! strictly FIFO, trivially correct, one global lock.
|
||||||
//! - `rq-mpmc` — a single hand-rolled Vyukov bounded MPMC ring (per-cell
|
//! - `rq-mpmc` (default) — a single hand-rolled Vyukov bounded MPMC ring (per-cell
|
||||||
//! sequence numbers). Strict FIFO, lock-free, one hot
|
//! sequence numbers). Strict FIFO, lock-free, one hot
|
||||||
//! enqueue/dequeue cache-line pair.
|
//! enqueue/dequeue cache-line pair.
|
||||||
//! - `rq-striped` — M Vyukov rings with fetch-add ticket distribution.
|
//! - `rq-striped` — M Vyukov rings with fetch-add ticket distribution.
|
||||||
@@ -18,7 +18,7 @@
|
|||||||
//!
|
//!
|
||||||
//! All variants are compiled unconditionally (so every build runs every
|
//! All variants are compiled unconditionally (so every build runs every
|
||||||
//! variant's unit tests); the feature only picks which one the runtime uses
|
//! variant's unit tests); the feature only picks which one the runtime uses
|
||||||
//! via the [`RunQueue`] alias.
|
//! via the `RunQueue` alias.
|
||||||
//!
|
//!
|
||||||
//! # Contract (shared by all variants)
|
//! # Contract (shared by all variants)
|
||||||
//!
|
//!
|
||||||
@@ -51,7 +51,7 @@
|
|||||||
//! - `len()` is approximate (stats only).
|
//! - `len()` is approximate (stats only).
|
||||||
|
|
||||||
use crate::pid::Pid;
|
use crate::pid::Pid;
|
||||||
use crate::sync_shim::{AtomicUsize, Ordering, UnsafeCell};
|
use crate::sync_shim::{fence, AtomicUsize, Ordering, UnsafeCell};
|
||||||
use std::mem::MaybeUninit;
|
use std::mem::MaybeUninit;
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
@@ -61,20 +61,20 @@ use std::mem::MaybeUninit;
|
|||||||
#[cfg(not(any(feature = "rq-mutex", feature = "rq-mpmc", feature = "rq-striped")))]
|
#[cfg(not(any(feature = "rq-mutex", feature = "rq-mpmc", feature = "rq-striped")))]
|
||||||
compile_error!(
|
compile_error!(
|
||||||
"smarm: no run queue selected. Enable exactly one of the features \
|
"smarm: no run queue selected. Enable exactly one of the features \
|
||||||
`rq-mutex` (default), `rq-mpmc`, `rq-striped`."
|
`rq-mpmc` (default), `rq-mutex`, `rq-striped`."
|
||||||
);
|
);
|
||||||
#[cfg(all(feature = "rq-mutex", feature = "rq-mpmc"))]
|
#[cfg(all(feature = "rq-mutex", feature = "rq-mpmc"))]
|
||||||
compile_error!(
|
compile_error!(
|
||||||
"smarm: features `rq-mutex` and `rq-mpmc` are mutually exclusive \
|
"smarm: features `rq-mutex` and `rq-mpmc` are mutually exclusive \
|
||||||
(use --no-default-features to drop the default `rq-mutex`)."
|
(use --no-default-features to drop the default `rq-mpmc`)."
|
||||||
);
|
);
|
||||||
#[cfg(all(feature = "rq-mutex", feature = "rq-striped"))]
|
#[cfg(all(feature = "rq-mutex", feature = "rq-striped"))]
|
||||||
compile_error!(
|
compile_error!("smarm: features `rq-mutex` and `rq-striped` are mutually exclusive.");
|
||||||
"smarm: features `rq-mutex` and `rq-striped` are mutually exclusive \
|
|
||||||
(use --no-default-features to drop the default `rq-mutex`)."
|
|
||||||
);
|
|
||||||
#[cfg(all(feature = "rq-mpmc", feature = "rq-striped"))]
|
#[cfg(all(feature = "rq-mpmc", feature = "rq-striped"))]
|
||||||
compile_error!("smarm: features `rq-mpmc` and `rq-striped` are mutually exclusive.");
|
compile_error!(
|
||||||
|
"smarm: features `rq-mpmc` and `rq-striped` are mutually exclusive \
|
||||||
|
(use --no-default-features to drop the default `rq-mpmc`)."
|
||||||
|
);
|
||||||
|
|
||||||
#[cfg(feature = "rq-mutex")]
|
#[cfg(feature = "rq-mutex")]
|
||||||
pub(crate) type RunQueue = MutexQueue;
|
pub(crate) type RunQueue = MutexQueue;
|
||||||
@@ -86,12 +86,52 @@ pub(crate) type RunQueue = StripedRing;
|
|||||||
#[inline]
|
#[inline]
|
||||||
fn assert_no_preempt() {
|
fn assert_no_preempt() {
|
||||||
debug_assert!(
|
debug_assert!(
|
||||||
!crate::preempt::PREEMPTION_ENABLED.with(|c| c.get()),
|
!crate::preempt::preemption_enabled(),
|
||||||
"run-queue op with preemption enabled — a switch mid-op stalls or \
|
"run-queue op with preemption enabled — a switch mid-op stalls or \
|
||||||
corrupts the queue; route through with_runtime or scheduler context"
|
corrupts the queue; route through with_runtime or scheduler context"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Escalating wait for transient ring stalls: a peer preempted by the OS
|
||||||
|
/// inside its ~100ns claim→publish window (finding 13). Spin first (the
|
||||||
|
/// common stall is a peer that is merely slow, gone within a few hundred
|
||||||
|
/// cycles), then donate the timeslice — under oversubscription pure
|
||||||
|
/// spinning STARVES the descheduled peer of the CPU it needs to publish
|
||||||
|
/// (measured in the finding-13 soak: 10⁶ pure spins can outlast the very
|
||||||
|
/// stall they prolong). OS-level yielding is orthogonal to
|
||||||
|
/// `assert_no_preempt`, which guards smarm signal preemption only.
|
||||||
|
struct Backoff(u32);
|
||||||
|
|
||||||
|
impl Backoff {
|
||||||
|
const SPIN_LIMIT: u32 = 6;
|
||||||
|
|
||||||
|
fn new() -> Self {
|
||||||
|
Self(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Total waits so far — lets bounded callers cap the yield phase.
|
||||||
|
fn steps(&self) -> u32 {
|
||||||
|
self.0
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn wait(&mut self) {
|
||||||
|
#[cfg(loom)]
|
||||||
|
// Loom has no notion of spinning time; every wait is a scheduling
|
||||||
|
// point so the model explores the stalled peer's progress.
|
||||||
|
loom::thread::yield_now();
|
||||||
|
#[cfg(not(loom))]
|
||||||
|
if self.0 <= Self::SPIN_LIMIT {
|
||||||
|
for _ in 0..1u32 << self.0 {
|
||||||
|
std::hint::spin_loop();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
std::thread::yield_now();
|
||||||
|
}
|
||||||
|
self.0 += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// rq-mutex — the baseline
|
// rq-mutex — the baseline
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
@@ -113,16 +153,32 @@ impl MutexQueue {
|
|||||||
|
|
||||||
pub fn push(&self, pid: Pid) {
|
pub fn push(&self, pid: Pid) {
|
||||||
assert_no_preempt();
|
assert_no_preempt();
|
||||||
self.q.lock().unwrap().push_back(pid);
|
match self.q.lock() {
|
||||||
|
Ok(mut g) => g.push_back(pid),
|
||||||
|
Err(e) => panic!("smarm: run-queue q lock poisoned (core corrupt): {e}"),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn pop(&self) -> Option<Pid> {
|
pub fn pop(&self) -> Option<Pid> {
|
||||||
assert_no_preempt();
|
assert_no_preempt();
|
||||||
self.q.lock().unwrap().pop_front()
|
match self.q.lock() {
|
||||||
|
Ok(mut g) => g.pop_front(),
|
||||||
|
Err(e) => panic!("smarm: run-queue q lock poisoned (core corrupt): {e}"),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn len(&self) -> u64 {
|
pub fn len(&self) -> u64 {
|
||||||
self.q.lock().unwrap().len() as u64
|
match self.q.lock() {
|
||||||
|
Ok(g) => g.len() as u64,
|
||||||
|
Err(e) => panic!("smarm: run-queue q lock poisoned (core corrupt): {e}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_empty(&self) -> bool {
|
||||||
|
match self.q.lock() {
|
||||||
|
Ok(g) => g.is_empty(),
|
||||||
|
Err(e) => panic!("smarm: run-queue q lock poisoned (core corrupt): {e}"),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -188,15 +244,57 @@ impl MpmcRing {
|
|||||||
|
|
||||||
pub fn push(&self, pid: Pid) {
|
pub fn push(&self, pid: Pid) {
|
||||||
assert_no_preempt();
|
assert_no_preempt();
|
||||||
assert!(
|
if self.try_push(pid) {
|
||||||
self.try_push(pid),
|
return;
|
||||||
"smarm: run queue overflow — occupancy exceeded the slab bound, \
|
}
|
||||||
which the at-most-once-enqueued invariant forbids. This is a \
|
self.push_slow(pid);
|
||||||
runtime bug (double enqueue), not a capacity tuning problem."
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// One full claim attempt; `false` only if the ring is full.
|
/// Cold path: the cell at `enqueue_pos` is still a lap behind. Two
|
||||||
|
/// worlds are indistinguishable at the cell (finding 13): a consumer
|
||||||
|
/// preempted between its `dequeue_pos` claim and its seq release while
|
||||||
|
/// the ring lapped onto that cell (transient), or a genuine occupancy
|
||||||
|
/// overflow (a runtime bug). The COUNTERS discriminate — the same shape
|
||||||
|
/// as crossbeam `ArrayQueue::push`'s fence + opposite-counter check:
|
||||||
|
/// occupancy < capacity ⇒ transient ⇒ wait for the stalled peer;
|
||||||
|
/// occupancy ≥ capacity ⇒ the at-most-once-enqueued invariant really is
|
||||||
|
/// broken ⇒ panic (a legal push starts from occupancy ≤ max_actors − 1).
|
||||||
|
#[cold]
|
||||||
|
fn push_slow(&self, pid: Pid) {
|
||||||
|
let mut backoff = Backoff::new();
|
||||||
|
loop {
|
||||||
|
// Order the counter reads after the failed cell read.
|
||||||
|
// `enqueue_pos` is loaded BEFORE `dequeue_pos`, so a pop racing
|
||||||
|
// us can only make the computed occupancy an UNDERestimate —
|
||||||
|
// conservative in the safe direction (never a spurious panic;
|
||||||
|
// a genuine violation is persistent and caught next lap).
|
||||||
|
fence(Ordering::SeqCst);
|
||||||
|
let enq = self.enqueue_pos.0.load(Ordering::SeqCst);
|
||||||
|
let deq = self.dequeue_pos.0.load(Ordering::SeqCst);
|
||||||
|
let occ = enq.wrapping_sub(deq);
|
||||||
|
assert!(
|
||||||
|
occ <= self.mask,
|
||||||
|
"smarm: run queue occupancy {} reached capacity {} — a pid \
|
||||||
|
was enqueued more than once (or more pids exist than \
|
||||||
|
max_actors); the at-most-once-enqueued invariant is broken \
|
||||||
|
(enq={} deq={})",
|
||||||
|
occ,
|
||||||
|
self.mask + 1,
|
||||||
|
enq,
|
||||||
|
deq
|
||||||
|
);
|
||||||
|
backoff.wait();
|
||||||
|
if self.try_push(pid) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// One full claim attempt; `false` when the cell at `enqueue_pos` is
|
||||||
|
/// still a lap behind — which means EITHER genuinely full OR a
|
||||||
|
/// lap-stalled consumer (finding 13). The cell cannot tell the two
|
||||||
|
/// apart; callers must disambiguate via the counters (`push_slow`) or
|
||||||
|
/// tolerate refusal (`StripedRing`'s probe).
|
||||||
fn try_push(&self, pid: Pid) -> bool {
|
fn try_push(&self, pid: Pid) -> bool {
|
||||||
let mut pos = self.enqueue_pos.0.load(Ordering::Relaxed);
|
let mut pos = self.enqueue_pos.0.load(Ordering::Relaxed);
|
||||||
loop {
|
loop {
|
||||||
@@ -206,7 +304,10 @@ impl MpmcRing {
|
|||||||
if diff == 0 {
|
if diff == 0 {
|
||||||
// Our turn: claim the position.
|
// Our turn: claim the position.
|
||||||
match self.enqueue_pos.0.compare_exchange_weak(
|
match self.enqueue_pos.0.compare_exchange_weak(
|
||||||
pos, pos + 1, Ordering::Relaxed, Ordering::Relaxed,
|
pos,
|
||||||
|
pos + 1,
|
||||||
|
Ordering::Relaxed,
|
||||||
|
Ordering::Relaxed,
|
||||||
) {
|
) {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
// SAFETY: the claim gives us exclusive write access
|
// SAFETY: the claim gives us exclusive write access
|
||||||
@@ -227,6 +328,7 @@ impl MpmcRing {
|
|||||||
|
|
||||||
pub fn pop(&self) -> Option<Pid> {
|
pub fn pop(&self) -> Option<Pid> {
|
||||||
assert_no_preempt();
|
assert_no_preempt();
|
||||||
|
let mut backoff = Backoff::new();
|
||||||
let mut pos = self.dequeue_pos.0.load(Ordering::Relaxed);
|
let mut pos = self.dequeue_pos.0.load(Ordering::Relaxed);
|
||||||
loop {
|
loop {
|
||||||
let cell = &self.buf[pos & self.mask];
|
let cell = &self.buf[pos & self.mask];
|
||||||
@@ -234,7 +336,10 @@ impl MpmcRing {
|
|||||||
let diff = seq as isize - (pos + 1) as isize;
|
let diff = seq as isize - (pos + 1) as isize;
|
||||||
if diff == 0 {
|
if diff == 0 {
|
||||||
match self.dequeue_pos.0.compare_exchange_weak(
|
match self.dequeue_pos.0.compare_exchange_weak(
|
||||||
pos, pos + 1, Ordering::Relaxed, Ordering::Relaxed,
|
pos,
|
||||||
|
pos + 1,
|
||||||
|
Ordering::Relaxed,
|
||||||
|
Ordering::Relaxed,
|
||||||
) {
|
) {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
// SAFETY: the claim gives us exclusive read access;
|
// SAFETY: the claim gives us exclusive read access;
|
||||||
@@ -248,9 +353,31 @@ impl MpmcRing {
|
|||||||
Err(actual) => pos = actual,
|
Err(actual) => pos = actual,
|
||||||
}
|
}
|
||||||
} else if diff < 0 {
|
} else if diff < 0 {
|
||||||
// Empty (or the producer at this cell hasn't published yet —
|
// The cell at `pos` is unpublished: either the queue is
|
||||||
// a snapshot miss the caller's idle-retry loop absorbs).
|
// empty, or the producer that claimed it was preempted
|
||||||
return None;
|
// inside its claim→publish window (finding 13). The cell
|
||||||
|
// cannot tell the two apart; the counters can.
|
||||||
|
fence(Ordering::SeqCst);
|
||||||
|
let deq = self.dequeue_pos.0.load(Ordering::SeqCst);
|
||||||
|
if deq != pos {
|
||||||
|
// Stale head — no verdict; re-probe at the real head.
|
||||||
|
pos = deq;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if self.enqueue_pos.0.load(Ordering::SeqCst) == pos {
|
||||||
|
return None; // counters agree: genuinely empty
|
||||||
|
}
|
||||||
|
// Producer mid-publish. Wait briefly, then report None
|
||||||
|
// anyway — a DELIBERATE bounded deviation from crossbeam's
|
||||||
|
// unbounded retry: a spurious None is correctness-benign
|
||||||
|
// here (the stalled push completes and its RFC 018
|
||||||
|
// enqueue-wake re-wakes a parked scheduler), a parked
|
||||||
|
// scheduler beats a yielding one, and StripedRing's pop
|
||||||
|
// probe must not hang on one stripe.
|
||||||
|
if backoff.steps() > Backoff::SPIN_LIMIT + 8 {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
backoff.wait();
|
||||||
} else {
|
} else {
|
||||||
pos = self.dequeue_pos.0.load(Ordering::Relaxed);
|
pos = self.dequeue_pos.0.load(Ordering::Relaxed);
|
||||||
}
|
}
|
||||||
@@ -262,6 +389,10 @@ impl MpmcRing {
|
|||||||
let d = self.dequeue_pos.0.load(Ordering::Relaxed);
|
let d = self.dequeue_pos.0.load(Ordering::Relaxed);
|
||||||
e.saturating_sub(d) as u64
|
e.saturating_sub(d) as u64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn is_empty(&self) -> bool {
|
||||||
|
self.len() == 0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
@@ -316,6 +447,7 @@ impl StripedRing {
|
|||||||
// guarantees a free stripe exists, so the outer loop terminates.
|
// guarantees a free stripe exists, so the outer loop terminates.
|
||||||
// The retry-from-home lap handles the racy case where every stripe
|
// The retry-from-home lap handles the racy case where every stripe
|
||||||
// momentarily refused us.
|
// momentarily refused us.
|
||||||
|
let mut backoff = Backoff::new();
|
||||||
loop {
|
loop {
|
||||||
for i in 0..=self.stripe_mask {
|
for i in 0..=self.stripe_mask {
|
||||||
let s = &self.stripes[(home + i) & self.stripe_mask];
|
let s = &self.stripes[(home + i) & self.stripe_mask];
|
||||||
@@ -323,7 +455,13 @@ impl StripedRing {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::hint::spin_loop();
|
// Every stripe refused this lap: either transiently full (the
|
||||||
|
// headroom argument above guarantees a genuinely free stripe
|
||||||
|
// exists) or the probes landed on lap-stalled cells (finding
|
||||||
|
// 13 — try_push cannot tell the two apart). Waiting is correct
|
||||||
|
// either way; the backoff escalates to an OS yield so stalled
|
||||||
|
// consumers get the CPU they need to release their cells.
|
||||||
|
backoff.wait();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -341,6 +479,10 @@ impl StripedRing {
|
|||||||
pub fn len(&self) -> u64 {
|
pub fn len(&self) -> u64 {
|
||||||
self.stripes.iter().map(|s| s.len()).sum()
|
self.stripes.iter().map(|s| s.len()).sum()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn is_empty(&self) -> bool {
|
||||||
|
self.stripes.iter().all(|s| s.is_empty())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
@@ -440,19 +582,36 @@ mod tests {
|
|||||||
|
|
||||||
let popped = popped.lock().unwrap();
|
let popped = popped.lock().unwrap();
|
||||||
assert_eq!(popped.len(), total, "count mismatch");
|
assert_eq!(popped.len(), total, "count mismatch");
|
||||||
let set: HashSet<u64> = popped.iter().map(|p| ((p.index() as u64) << 32) | p.generation() as u64).collect();
|
let set: HashSet<u64> = popped
|
||||||
|
.iter()
|
||||||
|
.map(|p| ((p.index() as u64) << 32) | p.generation() as u64)
|
||||||
|
.collect();
|
||||||
assert_eq!(set.len(), total, "duplicate or lost element");
|
assert_eq!(set.len(), total, "duplicate or lost element");
|
||||||
assert_eq!(pop(&q), None);
|
assert_eq!(pop(&q), None);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn mpmc_exactly_once_contended() {
|
fn mpmc_exactly_once_contended() {
|
||||||
exactly_once(MpmcRing::new(8, 4096), |q, p| q.push(p), |q| q.pop(), 4, 4, 1000);
|
exactly_once(
|
||||||
|
MpmcRing::new(8, 4096),
|
||||||
|
|q, p| q.push(p),
|
||||||
|
|q| q.pop(),
|
||||||
|
4,
|
||||||
|
4,
|
||||||
|
1000,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn striped_exactly_once_contended() {
|
fn striped_exactly_once_contended() {
|
||||||
exactly_once(StripedRing::new(8, 4096), |q, p| q.push(p), |q| q.pop(), 4, 4, 1000);
|
exactly_once(
|
||||||
|
StripedRing::new(8, 4096),
|
||||||
|
|q, p| q.push(p),
|
||||||
|
|q| q.pop(),
|
||||||
|
4,
|
||||||
|
4,
|
||||||
|
1000,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -513,6 +672,33 @@ mod loom_tests {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Finding 13 regression: a consumer stalled between its dequeue_pos
|
||||||
|
/// claim and its seq release must NOT make a lapping producer conclude
|
||||||
|
/// "full" (the old code asserted here — occupancy never exceeded the
|
||||||
|
/// bound; the cell just hadn't been recycled). Capacity 2: fill, pop
|
||||||
|
/// once on each thread, then push a third element — in the
|
||||||
|
/// interleavings where a pop's release is still pending, that push
|
||||||
|
/// laps onto the stalled cell and must wait, not die.
|
||||||
|
#[test]
|
||||||
|
fn mpmc_lap_onto_stalled_consumer_completes() {
|
||||||
|
loom::model(|| {
|
||||||
|
let q = Arc::new(MpmcRing::with_capacity(2));
|
||||||
|
q.push(pid(0));
|
||||||
|
q.push(pid(1));
|
||||||
|
let q2 = q.clone();
|
||||||
|
let h = thread::spawn(move || q2.pop().expect("ring has two elements"));
|
||||||
|
let a = q.pop().expect("ring has two elements");
|
||||||
|
// enqueue_pos = 2 → cell 0: laps onto the other thread's cell
|
||||||
|
// whenever its release is delayed.
|
||||||
|
q.push(pid(2));
|
||||||
|
let b = h.join().unwrap();
|
||||||
|
let c = q.pop().expect("the lapping push must have landed");
|
||||||
|
let mut got = vec![a.index(), b.index(), c.index()];
|
||||||
|
got.sort_unstable();
|
||||||
|
assert_eq!(got, vec![0, 1, 2]);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/// Producer races a consumer on a single element: the consumer either
|
/// Producer races a consumer on a single element: the consumer either
|
||||||
/// gets it or sees a clean None — never a torn/duplicated element.
|
/// gets it or sees a clean None — never a torn/duplicated element.
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
+1163
-266
File diff suppressed because it is too large
Load Diff
+892
-240
File diff suppressed because it is too large
Load Diff
+330
@@ -0,0 +1,330 @@
|
|||||||
|
//! RFC 019 §7 — overflow diagnostics.
|
||||||
|
//!
|
||||||
|
//! One process-global SIGSEGV handler, installed once at [`crate::runtime::init`]
|
||||||
|
//! (before any scheduler thread exists, so the PRIOR save is unracing), plus a
|
||||||
|
//! per-scheduler-thread `sigaltstack` registered at `schedule_loop` entry — a
|
||||||
|
//! guard hit means the faulting stack has no room to run anything, so the
|
||||||
|
//! altstack is not optional.
|
||||||
|
//!
|
||||||
|
//! The handler classifies `si_addr` against the *current* actor only, reached
|
||||||
|
//! through `preempt::CURRENT_SLOT` — a const-initialized `Cell<*const Slot>`
|
||||||
|
//! whose access is a plain TLS load (no lazy init, no allocation, no dtor
|
||||||
|
//! registration), and which every scheduler thread has materialized before an
|
||||||
|
//! actor can run on it. The slot's diag atomics (`diag_stack_top` & co) are
|
||||||
|
//! written in `install_actor` before the Release publish and are only consulted
|
||||||
|
//! here while the actor is on-CPU, so they cannot be stale.
|
||||||
|
//!
|
||||||
|
//! Two classification tiers:
|
||||||
|
//! - **In-guard**: definitive. Rust frames probe pages in order
|
||||||
|
//! (`__rust_probestack`), so Rust overflow always lands here; so does any C
|
||||||
|
//! built with `-fstack-clash-protection` (distro-packaged libraries), and —
|
||||||
|
//! with the 1 MiB default guard — nearly every unprobed frame too.
|
||||||
|
//! - **Overshoot**: within [`OVERSHOOT_SLOP`] *below* the guard. An unprobed
|
||||||
|
//! frame (cargo-built C via `cc` almost never enables clash protection)
|
||||||
|
//! large enough to step over the guard in one `sub rsp`. Attribution is
|
||||||
|
//! "probable": the address is in unmapped VA that nothing else owns, an
|
||||||
|
//! actor was on-CPU, and the distance fits a frame — the diagnostic says so.
|
||||||
|
//!
|
||||||
|
//! Classified faults print one line (async-signal-safe: stack buffer +
|
||||||
|
//! `write(2)`, no fmt, no alloc, no locks) and re-raise with default
|
||||||
|
//! disposition — no unwind, no resume, no fail-soft (jarred; UB-adjacent from
|
||||||
|
//! a handler). Unclassified faults reinstate the PRIOR handler and refault, so
|
||||||
|
//! std's own "thread ... has overflowed its stack" diagnostics for OS-thread
|
||||||
|
//! stacks survive our presence. Reinstating deregisters us for good, which is
|
||||||
|
//! fine: the process is dying either way.
|
||||||
|
|
||||||
|
use std::cell::Cell;
|
||||||
|
use std::mem::MaybeUninit;
|
||||||
|
use std::sync::atomic::Ordering;
|
||||||
|
use std::sync::Once;
|
||||||
|
|
||||||
|
/// Tier-2 window below the guard. Matches the guard default (and the kernel's
|
||||||
|
/// `stack_guard_gap`): a frame that out-jumps both the guard and this window
|
||||||
|
/// in one displacement is past what a diagnostic can honestly attribute.
|
||||||
|
pub(crate) const OVERSHOOT_SLOP: usize = 1024 * 1024;
|
||||||
|
|
||||||
|
/// Per-scheduler-thread signal stack. MINSIGSTKSZ is ~11 KiB on AVX-512
|
||||||
|
/// hardware; 64 KiB leaves the formatter room without mattering to anyone.
|
||||||
|
/// One per OS thread, never freed: scheduler threads live for the process in
|
||||||
|
/// practice, and repeated `run()`s on reused threads re-use the registration
|
||||||
|
/// (the TLS flag), so the leak is bounded by the OS thread count.
|
||||||
|
const ALTSTACK_SIZE: usize = 64 * 1024;
|
||||||
|
|
||||||
|
static INSTALL: Once = Once::new();
|
||||||
|
/// The handler that was installed before ours (std's, typically). Written
|
||||||
|
/// exactly once inside INSTALL — which completes in `runtime::init` before
|
||||||
|
/// any scheduler thread (and thus any classifiable fault) can exist — and
|
||||||
|
/// only read from the handler afterwards.
|
||||||
|
static mut PRIOR: MaybeUninit<libc::sigaction> = MaybeUninit::uninit();
|
||||||
|
|
||||||
|
thread_local! {
|
||||||
|
/// Whether this OS thread has registered its altstack.
|
||||||
|
static ALTSTACK_SET: Cell<bool> = const { Cell::new(false) };
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Where a fault landed relative to the current actor's stack.
|
||||||
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
|
pub(crate) enum FaultClass {
|
||||||
|
/// Inside `[top − reserve − guard, top − reserve)`: the guard region.
|
||||||
|
Guard,
|
||||||
|
/// Within `OVERSHOOT_SLOP` below the guard: stepped over it. Payload is
|
||||||
|
/// the distance below `guard_lo`.
|
||||||
|
Overshoot(usize),
|
||||||
|
/// Not ours to explain.
|
||||||
|
Foreign,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Pure classifier — all edges unit-tested below. `top` is the stack's usable
|
||||||
|
/// top, `reserve`/`guard` its shape; both page-rounded by `Stack::new`.
|
||||||
|
pub(crate) fn classify(addr: usize, top: usize, reserve: usize, guard: usize) -> FaultClass {
|
||||||
|
let guard_hi = top.wrapping_sub(reserve);
|
||||||
|
let guard_lo = guard_hi.wrapping_sub(guard);
|
||||||
|
if addr >= guard_lo && addr < guard_hi {
|
||||||
|
FaultClass::Guard
|
||||||
|
} else if addr < guard_lo && addr >= guard_lo.saturating_sub(OVERSHOOT_SLOP) {
|
||||||
|
FaultClass::Overshoot(guard_lo - addr)
|
||||||
|
} else {
|
||||||
|
FaultClass::Foreign
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Install the process-global handler. Idempotent; called from
|
||||||
|
/// `runtime::init`.
|
||||||
|
pub(crate) fn install_once() {
|
||||||
|
INSTALL.call_once(|| unsafe {
|
||||||
|
let mut sa: libc::sigaction = std::mem::zeroed();
|
||||||
|
sa.sa_sigaction = handler as *const () as usize;
|
||||||
|
sa.sa_flags = libc::SA_SIGINFO | libc::SA_ONSTACK;
|
||||||
|
libc::sigemptyset(&mut sa.sa_mask);
|
||||||
|
let prior = &mut *std::ptr::addr_of_mut!(PRIOR);
|
||||||
|
libc::sigaction(libc::SIGSEGV, &sa, prior.as_mut_ptr());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Register this OS thread's altstack (idempotent per thread). Called at
|
||||||
|
/// `schedule_loop` entry, so every thread that can run an actor has one.
|
||||||
|
pub(crate) fn register_altstack() {
|
||||||
|
ALTSTACK_SET.with(|set| {
|
||||||
|
if set.get() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
unsafe {
|
||||||
|
let sp = libc::mmap(
|
||||||
|
std::ptr::null_mut(),
|
||||||
|
ALTSTACK_SIZE,
|
||||||
|
libc::PROT_READ | libc::PROT_WRITE,
|
||||||
|
libc::MAP_PRIVATE | libc::MAP_ANONYMOUS,
|
||||||
|
-1,
|
||||||
|
0,
|
||||||
|
);
|
||||||
|
if sp == libc::MAP_FAILED {
|
||||||
|
// Degrade: no altstack means a guard hit dies without the
|
||||||
|
// message (handler can't run) — the pre-RFC behavior, never
|
||||||
|
// incorrectness.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let ss = libc::stack_t {
|
||||||
|
ss_sp: sp,
|
||||||
|
ss_flags: 0,
|
||||||
|
ss_size: ALTSTACK_SIZE,
|
||||||
|
};
|
||||||
|
libc::sigaltstack(&ss, std::ptr::null_mut());
|
||||||
|
}
|
||||||
|
set.set(true);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// The handler
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
unsafe extern "C" fn handler(
|
||||||
|
_sig: libc::c_int,
|
||||||
|
info: *mut libc::siginfo_t,
|
||||||
|
_ctx: *mut libc::c_void,
|
||||||
|
) {
|
||||||
|
let slot_ptr = crate::preempt::current_slot_ptr();
|
||||||
|
if !slot_ptr.is_null() {
|
||||||
|
let slot = &*slot_ptr;
|
||||||
|
let top = slot.diag_stack_top.load(Ordering::Relaxed);
|
||||||
|
if top != 0 {
|
||||||
|
let reserve = slot.diag_stack_reserve.load(Ordering::Relaxed);
|
||||||
|
let guard = slot.diag_stack_guard.load(Ordering::Relaxed);
|
||||||
|
let pid = slot.diag_pid.load(Ordering::Relaxed);
|
||||||
|
let addr = (*info).si_addr() as usize;
|
||||||
|
match classify(addr, top, reserve, guard) {
|
||||||
|
FaultClass::Guard => {
|
||||||
|
let mut b = Buf::new();
|
||||||
|
b.s("smarm: actor ");
|
||||||
|
b.pid(pid);
|
||||||
|
b.s(" overflowed its stack: fault in the guard region, depth-at-fault=");
|
||||||
|
b.u(top - addr);
|
||||||
|
b.s(" bytes (reserve=");
|
||||||
|
b.u(reserve);
|
||||||
|
b.s(", guard=");
|
||||||
|
b.u(guard);
|
||||||
|
b.s("). Raise stack_reserve (SpawnOpts or Config).\n");
|
||||||
|
b.emit();
|
||||||
|
die_by_default();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
FaultClass::Overshoot(below) => {
|
||||||
|
let mut b = Buf::new();
|
||||||
|
b.s("smarm: actor ");
|
||||||
|
b.pid(pid);
|
||||||
|
b.s(" probably overflowed its stack: fault ");
|
||||||
|
b.u(below);
|
||||||
|
b.s(" bytes below the guard - an unprobed (FFI?) frame stepped over it (reserve=");
|
||||||
|
b.u(reserve);
|
||||||
|
b.s(", guard=");
|
||||||
|
b.u(guard);
|
||||||
|
b.s("). Raise stack_guard or stack_reserve.\n");
|
||||||
|
b.emit();
|
||||||
|
die_by_default();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
FaultClass::Foreign => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Not ours: put back whoever was there before us and refault into them.
|
||||||
|
let prior = &*std::ptr::addr_of!(PRIOR);
|
||||||
|
libc::sigaction(libc::SIGSEGV, prior.as_ptr(), std::ptr::null_mut());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Reset SIGSEGV to default disposition; returning from the handler then
|
||||||
|
/// refaults at the same instruction and the process dies the normal death
|
||||||
|
/// (core-dumpable, correct wait status), exactly as if we were never here —
|
||||||
|
/// but with the message already on stderr.
|
||||||
|
unsafe fn die_by_default() {
|
||||||
|
let mut dfl: libc::sigaction = std::mem::zeroed();
|
||||||
|
dfl.sa_sigaction = libc::SIG_DFL;
|
||||||
|
libc::sigemptyset(&mut dfl.sa_mask);
|
||||||
|
libc::sigaction(libc::SIGSEGV, &dfl, std::ptr::null_mut());
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Async-signal-safe formatting: fixed buffer, decimal itoa, one write(2).
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
struct Buf {
|
||||||
|
b: [u8; 320],
|
||||||
|
len: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Buf {
|
||||||
|
fn new() -> Self {
|
||||||
|
Buf {
|
||||||
|
b: [0; 320],
|
||||||
|
len: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn s(&mut self, s: &str) {
|
||||||
|
for &c in s.as_bytes() {
|
||||||
|
if self.len < self.b.len() {
|
||||||
|
self.b[self.len] = c;
|
||||||
|
self.len += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn u(&mut self, mut n: usize) {
|
||||||
|
let mut tmp = [0u8; 20];
|
||||||
|
let mut i = tmp.len();
|
||||||
|
loop {
|
||||||
|
i -= 1;
|
||||||
|
tmp[i] = b'0' + (n % 10) as u8;
|
||||||
|
n /= 10;
|
||||||
|
if n == 0 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for &c in &tmp[i..] {
|
||||||
|
if self.len < self.b.len() {
|
||||||
|
self.b[self.len] = c;
|
||||||
|
self.len += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// `idx.gen`, unpacked from the install-time packing.
|
||||||
|
fn pid(&mut self, packed: u64) {
|
||||||
|
self.u((packed >> 32) as usize);
|
||||||
|
self.s(".");
|
||||||
|
self.u((packed & 0xffff_ffff) as usize);
|
||||||
|
}
|
||||||
|
fn emit(&self) {
|
||||||
|
unsafe {
|
||||||
|
libc::write(2, self.b.as_ptr() as *const libc::c_void, self.len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Classifier units — the arithmetic edges, before anything integrates.
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::{classify, FaultClass, OVERSHOOT_SLOP};
|
||||||
|
|
||||||
|
const PG: usize = 4096;
|
||||||
|
// A synthetic stack far from address-space edges: top at 1 GiB.
|
||||||
|
const TOP: usize = 1 << 30;
|
||||||
|
const RESERVE: usize = 16 * PG;
|
||||||
|
const GUARD: usize = 4 * PG;
|
||||||
|
const GUARD_HI: usize = TOP - RESERVE;
|
||||||
|
const GUARD_LO: usize = GUARD_HI - GUARD;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn inside_guard_both_edges() {
|
||||||
|
assert_eq!(classify(GUARD_LO, TOP, RESERVE, GUARD), FaultClass::Guard);
|
||||||
|
assert_eq!(
|
||||||
|
classify(GUARD_HI - 1, TOP, RESERVE, GUARD),
|
||||||
|
FaultClass::Guard
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
classify(GUARD_LO + GUARD / 2, TOP, RESERVE, GUARD),
|
||||||
|
FaultClass::Guard
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn usable_region_is_foreign() {
|
||||||
|
// A fault inside the RW stack itself isn't a guard hit and must not
|
||||||
|
// be explained as one.
|
||||||
|
assert_eq!(classify(GUARD_HI, TOP, RESERVE, GUARD), FaultClass::Foreign);
|
||||||
|
assert_eq!(classify(TOP - 1, TOP, RESERVE, GUARD), FaultClass::Foreign);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn above_top_is_foreign() {
|
||||||
|
assert_eq!(classify(TOP, TOP, RESERVE, GUARD), FaultClass::Foreign);
|
||||||
|
assert_eq!(classify(TOP + PG, TOP, RESERVE, GUARD), FaultClass::Foreign);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn overshoot_window_edges() {
|
||||||
|
assert_eq!(
|
||||||
|
classify(GUARD_LO - 1, TOP, RESERVE, GUARD),
|
||||||
|
FaultClass::Overshoot(1)
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
classify(GUARD_LO - OVERSHOOT_SLOP, TOP, RESERVE, GUARD),
|
||||||
|
FaultClass::Overshoot(OVERSHOOT_SLOP)
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
classify(GUARD_LO - OVERSHOOT_SLOP - 1, TOP, RESERVE, GUARD),
|
||||||
|
FaultClass::Foreign
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn low_address_stack_saturates_not_wraps() {
|
||||||
|
// A stack mapped so low that the slop window would underflow: the
|
||||||
|
// window clips to 0 instead of wrapping around the address space.
|
||||||
|
let top = RESERVE + GUARD + PG; // guard_lo == PG
|
||||||
|
assert_eq!(classify(0, top, RESERVE, GUARD), FaultClass::Overshoot(PG));
|
||||||
|
// Null-page fault still classified only because it IS within slop
|
||||||
|
// here; with a normal-height stack it is Foreign (covered above by
|
||||||
|
// the window-edge test at realistic addresses).
|
||||||
|
}
|
||||||
|
}
|
||||||
+9
-9
@@ -188,8 +188,7 @@ impl StateWord {
|
|||||||
loop {
|
loop {
|
||||||
let w = self.load();
|
let w = self.load();
|
||||||
debug_assert!(
|
debug_assert!(
|
||||||
matches!(word_state(w), ST_RUNNING | ST_RUNNING_NOTIFIED)
|
matches!(word_state(w), ST_RUNNING | ST_RUNNING_NOTIFIED) && word_gen(w) == gen,
|
||||||
&& word_gen(w) == gen,
|
|
||||||
"yield return from invalid word {w:#x}"
|
"yield return from invalid word {w:#x}"
|
||||||
);
|
);
|
||||||
if self
|
if self
|
||||||
@@ -247,8 +246,7 @@ impl StateWord {
|
|||||||
loop {
|
loop {
|
||||||
let w = self.load();
|
let w = self.load();
|
||||||
debug_assert!(
|
debug_assert!(
|
||||||
matches!(word_state(w), ST_RUNNING | ST_RUNNING_NOTIFIED)
|
matches!(word_state(w), ST_RUNNING | ST_RUNNING_NOTIFIED) && word_gen(w) == gen,
|
||||||
&& word_gen(w) == gen,
|
|
||||||
"begin_wait from invalid word {w:#x}"
|
"begin_wait from invalid word {w:#x}"
|
||||||
);
|
);
|
||||||
let next = word_epoch(w).wrapping_add(1) & EPOCH_MASK;
|
let next = word_epoch(w).wrapping_add(1) & EPOCH_MASK;
|
||||||
@@ -342,8 +340,7 @@ impl StateWord {
|
|||||||
loop {
|
loop {
|
||||||
let w = self.load();
|
let w = self.load();
|
||||||
debug_assert!(
|
debug_assert!(
|
||||||
matches!(word_state(w), ST_RUNNING | ST_RUNNING_NOTIFIED)
|
matches!(word_state(w), ST_RUNNING | ST_RUNNING_NOTIFIED) && word_gen(w) == gen,
|
||||||
&& word_gen(w) == gen,
|
|
||||||
"clear_notify from invalid word {w:#x}"
|
"clear_notify from invalid word {w:#x}"
|
||||||
);
|
);
|
||||||
if word_state(w) != ST_RUNNING_NOTIFIED {
|
if word_state(w) != ST_RUNNING_NOTIFIED {
|
||||||
@@ -372,8 +369,7 @@ impl StateWord {
|
|||||||
pub(crate) fn set_done(&self, gen: u32) {
|
pub(crate) fn set_done(&self, gen: u32) {
|
||||||
let prev = self.0.swap(pack(gen, 0, ST_DONE), Ordering::AcqRel);
|
let prev = self.0.swap(pack(gen, 0, ST_DONE), Ordering::AcqRel);
|
||||||
debug_assert!(
|
debug_assert!(
|
||||||
matches!(word_state(prev), ST_RUNNING | ST_RUNNING_NOTIFIED)
|
matches!(word_state(prev), ST_RUNNING | ST_RUNNING_NOTIFIED) && word_gen(prev) == gen,
|
||||||
&& word_gen(prev) == gen,
|
|
||||||
"finalize from invalid word {prev:#x}"
|
"finalize from invalid word {prev:#x}"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -538,7 +534,11 @@ mod loom_tests {
|
|||||||
// not a pending notification.
|
// not a pending notification.
|
||||||
assert!(word.try_claim(0));
|
assert!(word.try_claim(0));
|
||||||
assert_eq!(word.unpark(0, Some(epoch)), Unpark::Noop);
|
assert_eq!(word.unpark(0, Some(epoch)), Unpark::Noop);
|
||||||
assert_eq!(word_state(word.load()), ST_RUNNING, "stale epoch notified a live run");
|
assert_eq!(
|
||||||
|
word_state(word.load()),
|
||||||
|
ST_RUNNING,
|
||||||
|
"stale epoch notified a live run"
|
||||||
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+237
-17
@@ -1,32 +1,45 @@
|
|||||||
//! mmap-based growable stack with a guard page below.
|
//! mmap-based actor stack with a PROT_NONE guard region below (RFC 019).
|
||||||
//!
|
//!
|
||||||
//! Layout (low → high address):
|
//! Layout (low → high address):
|
||||||
//! [ guard page (PROT_NONE) | stack region ]
|
//! [ guard region (PROT_NONE) | stack region ]
|
||||||
//! ^ top() — initial stack pointer
|
//! ^ top() — initial stack pointer
|
||||||
//!
|
//!
|
||||||
//! Stacks grow downward. Overflow lands in the guard page → SIGSEGV.
|
//! Stacks grow downward. Overflow lands in the guard region → SIGSEGV.
|
||||||
|
//!
|
||||||
|
//! Both the usable reserve and the guard are caller-chosen (page-rounded).
|
||||||
|
//! The reserve is a *virtual* reservation: anonymous mmap is demand-paged,
|
||||||
|
//! so RSS is touched-pages, not reserve × actors. The guard costs address
|
||||||
|
//! space only. A wide guard (the runtime defaults to 64 KiB) exists for
|
||||||
|
//! unprobed FFI frames: Rust frames touch pages in order (probestack), so
|
||||||
|
//! one page catches Rust overflow, but a C frame with a large local can
|
||||||
|
//! step over a single page in one `sub rsp`.
|
||||||
|
|
||||||
use std::io;
|
use std::io;
|
||||||
|
|
||||||
pub struct Stack {
|
pub struct Stack {
|
||||||
/// Bottom of the entire mmap'd region (start of guard page).
|
/// Bottom of the entire mmap'd region (start of the guard).
|
||||||
base: *mut u8,
|
base: *mut u8,
|
||||||
/// Total mmap'd size: guard_size + stack_size.
|
/// Total mmap'd size: guard_size + stack_size.
|
||||||
total_size: usize,
|
total_size: usize,
|
||||||
/// Usable stack size (excluding guard page).
|
/// Usable stack size (excluding the guard).
|
||||||
stack_size: usize,
|
stack_size: usize,
|
||||||
|
/// PROT_NONE region below the usable stack.
|
||||||
|
guard_size: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stack owns its memory; safe to send across threads.
|
// Stack owns its memory; safe to send across threads.
|
||||||
unsafe impl Send for Stack {}
|
unsafe impl Send for Stack {}
|
||||||
|
|
||||||
impl Stack {
|
impl Stack {
|
||||||
/// Allocate a new stack. `stack_size` is the usable region; one page is
|
/// Allocate a new stack. `stack_size` is the usable region; `guard_size`
|
||||||
/// added below as a guard page. Both are rounded up to the page size.
|
/// is mapped PROT_NONE below it. Both are rounded up to the page size
|
||||||
pub fn new(stack_size: usize) -> io::Result<Self> {
|
/// and must be non-zero.
|
||||||
|
pub fn new(stack_size: usize, guard_size: usize) -> io::Result<Self> {
|
||||||
|
assert!(stack_size > 0, "stack_size must be non-zero");
|
||||||
|
assert!(guard_size > 0, "guard_size must be non-zero");
|
||||||
let page = page_size();
|
let page = page_size();
|
||||||
let stack_size = round_up(stack_size, page);
|
let stack_size = round_up(stack_size, page);
|
||||||
let guard_size = page;
|
let guard_size = round_up(guard_size, page);
|
||||||
let total_size = guard_size + stack_size;
|
let total_size = guard_size + stack_size;
|
||||||
|
|
||||||
let base = unsafe {
|
let base = unsafe {
|
||||||
@@ -44,16 +57,19 @@ impl Stack {
|
|||||||
}
|
}
|
||||||
let base = base as *mut u8;
|
let base = base as *mut u8;
|
||||||
|
|
||||||
let ret = unsafe {
|
let ret = unsafe { libc::mprotect(base as *mut libc::c_void, guard_size, libc::PROT_NONE) };
|
||||||
libc::mprotect(base as *mut libc::c_void, guard_size, libc::PROT_NONE)
|
|
||||||
};
|
|
||||||
if ret != 0 {
|
if ret != 0 {
|
||||||
let err = io::Error::last_os_error();
|
let err = io::Error::last_os_error();
|
||||||
unsafe { libc::munmap(base as *mut libc::c_void, total_size) };
|
unsafe { libc::munmap(base as *mut libc::c_void, total_size) };
|
||||||
return Err(err);
|
return Err(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Self { base, total_size, stack_size })
|
Ok(Self {
|
||||||
|
base,
|
||||||
|
total_size,
|
||||||
|
stack_size,
|
||||||
|
guard_size,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 16-byte-aligned top of the usable region.
|
/// 16-byte-aligned top of the usable region.
|
||||||
@@ -62,14 +78,54 @@ impl Stack {
|
|||||||
(raw_top & !15) as *mut u8
|
(raw_top & !15) as *mut u8
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Pointer to the bottom of the usable region (just above the guard page).
|
/// Pointer to the bottom of the usable region (just above the guard).
|
||||||
pub fn usable_base(&self) -> *mut u8 {
|
pub fn usable_base(&self) -> *mut u8 {
|
||||||
unsafe { self.base.add(page_size()) }
|
unsafe { self.base.add(self.guard_size) }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn stack_size(&self) -> usize {
|
pub fn stack_size(&self) -> usize {
|
||||||
self.stack_size
|
self.stack_size
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn guard_size(&self) -> usize {
|
||||||
|
self.guard_size
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `(stack_size, guard_size)` after page rounding. The pool rule
|
||||||
|
/// (RFC 019 §1) compares this against the runtime defaults: only
|
||||||
|
/// default-shaped stacks are pooled.
|
||||||
|
pub fn shape(&self) -> (usize, usize) {
|
||||||
|
(self.stack_size, self.guard_size)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Pool-recycle zap (RFC 019 §6): `MADV_DONTNEED` everything below the
|
||||||
|
/// retained entry end `[top − retain, top)` — the span the next actor's
|
||||||
|
/// shallow frames land in stays resident, the dead spike below it is
|
||||||
|
/// released. The stack is unowned at the call site (its actor is dead),
|
||||||
|
/// so a synchronous eager zap races nothing and the RSS drop is
|
||||||
|
/// immediate — a museum of worst-case spikes is exactly what a pool must
|
||||||
|
/// not be; DONTNEED's ~8× per-page cost vs FREE is irrelevant off the
|
||||||
|
/// hot path. Advisory like the park-path shrink: a failure degrades to
|
||||||
|
/// "the pool keeps RSS", never to incorrectness. No-op (no syscall) when
|
||||||
|
/// `retain` covers the whole usable region — i.e. always, at the 64 KiB
|
||||||
|
/// default reserve.
|
||||||
|
pub(crate) fn recycle_zap(&self, retain: usize) {
|
||||||
|
if let Some((off, len)) = retain_range(self.stack_size, retain, page_size()) {
|
||||||
|
unsafe {
|
||||||
|
libc::madvise(
|
||||||
|
self.usable_base().add(off) as *mut libc::c_void,
|
||||||
|
len,
|
||||||
|
libc::MADV_DONTNEED,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Round `n` up to whole pages — the same rounding `Stack::new` applies, so
|
||||||
|
/// runtime defaults stored pre-rounded compare exactly against [`Stack::shape`].
|
||||||
|
pub(crate) fn round_to_pages(n: usize) -> usize {
|
||||||
|
round_up(n, page_size())
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for Stack {
|
impl Drop for Stack {
|
||||||
@@ -80,10 +136,174 @@ impl Drop for Stack {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn page_size() -> usize {
|
pub(crate) fn page_size() -> usize {
|
||||||
unsafe { libc::sysconf(libc::_SC_PAGESIZE) as usize }
|
unsafe { libc::sysconf(libc::_SC_PAGESIZE) as usize }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn round_up(n: usize, align: usize) -> usize {
|
fn round_up(n: usize, align: usize) -> usize {
|
||||||
(n + align - 1) & !(align - 1)
|
(n + align - 1) & !(align - 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The whole-page span the park-path shrink may `MADV_FREE` (RFC 019 §3):
|
||||||
|
/// `[page_up(hwm), page_down(sp − redzone))`, or `None` if no full page fits.
|
||||||
|
///
|
||||||
|
/// `hwm` is the sampled high-water (deepest observed `sp`); everything in
|
||||||
|
/// `[hwm, sp)` is below the live frame and dead by definition. One page of
|
||||||
|
/// redzone stays resident under live `sp` — it covers the SysV 128-byte red
|
||||||
|
/// zone plus spill margin with room to spare. Rounding is inward on both
|
||||||
|
/// ends so the result can never touch the redzone, cross `sp`, or dip below
|
||||||
|
/// `hwm`; all arithmetic is checked so adversarial inputs (`sp < redzone`,
|
||||||
|
/// `hwm ≥ sp`, values near the address-space edges) collapse to `None`
|
||||||
|
/// rather than a wild or negative-length range.
|
||||||
|
pub(crate) fn shrink_range(hwm: usize, sp: usize, page: usize) -> Option<(usize, usize)> {
|
||||||
|
debug_assert!(page.is_power_of_two());
|
||||||
|
if hwm >= sp {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let redzone = page;
|
||||||
|
let end = sp.checked_sub(redzone)? & !(page - 1); // page_down(sp − redzone)
|
||||||
|
let start = hwm.checked_add(page - 1)? & !(page - 1); // page_up(hwm)
|
||||||
|
if end > start {
|
||||||
|
Some((start, end - start))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The `(offset_from_usable_base, len)` span the pool recycle DONTNEEDs
|
||||||
|
/// (RFC 019 §6): everything below the retained entry end. "Bottom RETAIN of
|
||||||
|
/// the stack" is read stack-wise (entry frames = highest addresses of a
|
||||||
|
/// downward stack): the retained span is `[top − page_up(retain), top)`, the
|
||||||
|
/// zapped span is the rest — retaining the low-address deep end instead
|
||||||
|
/// would keep the coldest pages and release the ones the next actor faults
|
||||||
|
/// first. `retain` rounds *up* to whole pages (retain more, zap less), so
|
||||||
|
/// with `stack_size` page-rounded by `Stack::new` the result is always
|
||||||
|
/// page-aligned. Checked math: `retain ≥ stack_size` (notably the default
|
||||||
|
/// 64 KiB reserve with the 64 KiB RETAIN) and overflow collapse to `None`.
|
||||||
|
pub(crate) fn retain_range(
|
||||||
|
stack_size: usize,
|
||||||
|
retain: usize,
|
||||||
|
page: usize,
|
||||||
|
) -> Option<(usize, usize)> {
|
||||||
|
debug_assert!(page.is_power_of_two());
|
||||||
|
let retain = retain.checked_add(page - 1)? & !(page - 1); // page_up(retain)
|
||||||
|
let len = stack_size.checked_sub(retain)?;
|
||||||
|
if len == 0 {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
Some((0, len))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::{retain_range, shrink_range};
|
||||||
|
|
||||||
|
const PG: usize = 4096;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn retain_covers_whole_stack_is_a_noop() {
|
||||||
|
// The default config: reserve == RETAIN == 64 KiB. No zap, no syscall.
|
||||||
|
assert_eq!(retain_range(16 * PG, 16 * PG, PG), None);
|
||||||
|
assert_eq!(retain_range(PG, PG, PG), None);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn retain_larger_than_stack_is_a_noop() {
|
||||||
|
assert_eq!(retain_range(16 * PG, 17 * PG, PG), None);
|
||||||
|
assert_eq!(retain_range(PG, usize::MAX, PG), None); // page_up overflows
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn retain_zero_zaps_everything() {
|
||||||
|
assert_eq!(retain_range(16 * PG, 0, PG), Some((0, 16 * PG)));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn retain_rounds_up_zapping_less() {
|
||||||
|
// 1 byte of retain keeps a whole page.
|
||||||
|
assert_eq!(retain_range(16 * PG, 1, PG), Some((0, 15 * PG)));
|
||||||
|
assert_eq!(retain_range(16 * PG, PG + 1, PG), Some((0, 14 * PG)));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn retain_one_page_short_of_stack() {
|
||||||
|
assert_eq!(retain_range(2 * PG, PG, PG), Some((0, PG)));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn retain_range_is_page_aligned() {
|
||||||
|
for size_pg in [1usize, 2, 3, 16, 1024] {
|
||||||
|
for retain in [0usize, 1, PG - 1, PG, PG + 1, 4 * PG, size_pg * PG] {
|
||||||
|
if let Some((off, len)) = retain_range(size_pg * PG, retain, PG) {
|
||||||
|
assert_eq!(off, 0);
|
||||||
|
assert_eq!(len % PG, 0);
|
||||||
|
assert!(len <= size_pg * PG);
|
||||||
|
assert!(len > 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn empty_and_inverted_spans_are_none() {
|
||||||
|
assert_eq!(shrink_range(0x8000_0000, 0x8000_0000, PG), None); // hwm == sp
|
||||||
|
assert_eq!(shrink_range(0x8000_1000, 0x8000_0000, PG), None); // hwm > sp
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn span_smaller_than_redzone_plus_page_is_none() {
|
||||||
|
let sp = 0x8000_0000;
|
||||||
|
// Everything within redzone+1 page of sp: no full page clears both
|
||||||
|
// the redzone and the page_up(hwm) rounding.
|
||||||
|
assert_eq!(shrink_range(sp - PG, sp, PG), None);
|
||||||
|
assert_eq!(shrink_range(sp - 2 * PG + 1, sp, PG), None);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn exact_two_pages_frees_one() {
|
||||||
|
let sp = 0x8000_0000;
|
||||||
|
let hwm = sp - 2 * PG;
|
||||||
|
// [hwm, hwm+PG) frees; [sp−PG, sp) is redzone.
|
||||||
|
assert_eq!(shrink_range(hwm, sp, PG), Some((hwm, PG)));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn unaligned_ends_round_inward() {
|
||||||
|
let sp = 0x8000_0123; // live sp mid-page
|
||||||
|
let hwm = 0x7f00_0abc; // high-water mid-page
|
||||||
|
let (start, len) = shrink_range(hwm, sp, PG).unwrap();
|
||||||
|
assert_eq!(start % PG, 0);
|
||||||
|
assert_eq!(len % PG, 0);
|
||||||
|
assert!(start >= hwm); // never below the sampled high-water
|
||||||
|
assert!(start + len <= (sp - PG) & !(PG - 1)); // never into the redzone
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn result_never_crosses_sp() {
|
||||||
|
// Sweep hwm across every offset of the page straddling the boundary.
|
||||||
|
let sp = 0x8000_0000 + 137;
|
||||||
|
for hwm in (sp - 4 * PG)..(sp) {
|
||||||
|
if let Some((start, len)) = shrink_range(hwm, sp, PG) {
|
||||||
|
assert!(start >= hwm);
|
||||||
|
assert!(start + len + PG <= sp + PG); // end ≤ page_down(sp − PG) < sp
|
||||||
|
assert!(len > 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn underflow_near_zero_is_none() {
|
||||||
|
assert_eq!(shrink_range(0, PG - 1, PG), None); // sp < redzone
|
||||||
|
assert_eq!(shrink_range(0, 0, PG), None);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn big_span_frees_interior() {
|
||||||
|
let sp = 0x8000_0000;
|
||||||
|
let spike = 4 * 1024 * 1024;
|
||||||
|
let hwm = sp - spike;
|
||||||
|
let (start, len) = shrink_range(hwm, sp, PG).unwrap();
|
||||||
|
assert_eq!(start, hwm); // aligned input: starts exactly at hwm
|
||||||
|
assert_eq!(len, spike - PG); // everything but the redzone page
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
-599
@@ -1,599 +0,0 @@
|
|||||||
//! gen_statem — generic finite state machine behaviour (RFC 017).
|
|
||||||
//!
|
|
||||||
//! The sibling of [`gen_server`](crate::gen_server): where a `gen_server`
|
|
||||||
//! carries one undifferentiated blob of state and a single `handle` that
|
|
||||||
//! re-derives "what mode am I in" on every message, a `gen_statem` makes the
|
|
||||||
//! state an explicit **tag**, routes event handling by it, and (in later
|
|
||||||
//! chunks) adds the machinery state machines need — state-entry callbacks, a
|
|
||||||
//! timeout taxonomy, and event postponement.
|
|
||||||
//!
|
|
||||||
//! ## What this layer is
|
|
||||||
//!
|
|
||||||
//! This module is the **runtime support** a state machine runs on, *not* the
|
|
||||||
//! authoring surface. A machine is any type implementing [`Machine`]: it owns
|
|
||||||
//! its state tag and data, and its [`handle`](Machine::handle) reduces a
|
|
||||||
//! `(state, event)` pair to a [`Resolution`]. The loop here drives it — spawn,
|
|
||||||
//! [`on_start`](Machine::on_start), then one [`handle`](Machine::handle) per
|
|
||||||
//! inbox event — mirroring `gen_server`'s spawn/teardown idioms.
|
|
||||||
//!
|
|
||||||
//! The RFC's `statem!` macro (deferred) would *generate* a `Machine` impl from
|
|
||||||
//! a declarative `transitions { … }` graph plus per-state handler blocks, and
|
|
||||||
//! add the expansion-time edge-lint. Until then a machine is hand-written
|
|
||||||
//! against these primitives; see `examples/statem_switch.rs` for the shape the
|
|
||||||
//! macro would target.
|
|
||||||
//!
|
|
||||||
//! ## The unified event
|
|
||||||
//!
|
|
||||||
//! A machine's [`Ev`](Machine::Ev) is the single payload its inbox carries.
|
|
||||||
//! By convention (and in the macro's desugaring) it folds the user's `cast`
|
|
||||||
//! and `call` enums together with the runtime's own internal events:
|
|
||||||
//!
|
|
||||||
//! ```ignore
|
|
||||||
//! enum Ev { Cast(MyCast), Call(MyCall) /* later: StateTimeout, Timeout(name) */ }
|
|
||||||
//! ```
|
|
||||||
//!
|
|
||||||
//! [`StatemRef::send`] pushes any event (a cast is just a `send`);
|
|
||||||
//! [`StatemRef::call`] builds a one-shot [`Reply`] channel, hands it to a
|
|
||||||
//! `call` variant, and parks until the machine answers — exactly the
|
|
||||||
//! `gen_server` call round-trip, but with the reply handle riding *inside* the
|
|
||||||
//! user's own event so a handler can answer (or, later, postpone) it.
|
|
||||||
//!
|
|
||||||
//! ## Chunk status (RFC 017 §Sequencing)
|
|
||||||
//!
|
|
||||||
//! This is **chunk 1**: macro-free dispatch against real time — spawn,
|
|
||||||
//! `on_start`, stay/transition, and the `enter` callback (a method on the
|
|
||||||
//! machine, run on entry to a state). [`Resolution::Postpone`] and the timeout
|
|
||||||
//! arming on [`Cx`] are part of the type surface but are not yet acted on; they
|
|
||||||
//! land in chunks 2–3.
|
|
||||||
|
|
||||||
use crate::channel::{channel, Receiver, Sender};
|
|
||||||
use crate::pid::Pid;
|
|
||||||
use crate::scheduler::spawn as spawn_actor;
|
|
||||||
use std::marker::PhantomData;
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
// Machine
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
/// A finite state machine driven by the [`statem`](crate::statem) loop.
|
|
||||||
///
|
|
||||||
/// The implementor owns its current state tag and its persistent data. It is
|
|
||||||
/// the **sole writer** of the state cell (the loop never touches it): both
|
|
||||||
/// transition legality and, later, observer reads have a single source of
|
|
||||||
/// truth. The loop only calls [`on_start`](Self::on_start) once and
|
|
||||||
/// [`handle`](Self::handle) per event.
|
|
||||||
pub trait Machine: Send + 'static {
|
|
||||||
/// The single payload this machine's inbox carries — the user's `cast` and
|
|
||||||
/// `call` enums folded together with the runtime's internal events. See the
|
|
||||||
/// module docs.
|
|
||||||
type Ev: Send + 'static;
|
|
||||||
|
|
||||||
/// Runs once inside the actor before the first event. The canonical body
|
|
||||||
/// runs the `enter` arm for the initial state.
|
|
||||||
fn on_start(&mut self, cx: &mut Cx<Self::Ev>);
|
|
||||||
|
|
||||||
/// React to one event. The body matches `(state, event)`, performs side
|
|
||||||
/// effects / replies, and ends each arm in a [`Resolution`] — typically a
|
|
||||||
/// state tag via `Tag.into()` (transition, or "stay" when it equals the
|
|
||||||
/// current tag). On a transition the body sets the state cell and runs the
|
|
||||||
/// new state's `enter`.
|
|
||||||
fn handle(&mut self, ev: Self::Ev, cx: &mut Cx<Self::Ev>);
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
// Resolution
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
/// The outcome of handling one event, before the loop/handler acts on it:
|
|
||||||
/// dispatch reduces to `(state, event) -> Resolution<State>` (RFC §Semantics).
|
|
||||||
///
|
|
||||||
/// [`From<S>`](From) is why a bare state tag works as an arm tail and why
|
|
||||||
/// "stay" needs no keyword — `Tag.into()` is `To(Tag)`, and the handler treats
|
|
||||||
/// `To(s)` with `s == current` as stay.
|
|
||||||
pub enum Resolution<S> {
|
|
||||||
/// End in state `s`. A **transition** when `s != current` (set the cell,
|
|
||||||
/// run `enter`); a **stay** when `s == current` (no `enter`).
|
|
||||||
To(S),
|
|
||||||
/// Defer the current event onto the postpone queue, to be replayed after
|
|
||||||
/// the next real transition. Produced by `cx.postpone()` (chunk 3); present
|
|
||||||
/// here for forward-compatibility but not yet generated.
|
|
||||||
Postpone,
|
|
||||||
/// No arm matched `(state, event)`: log-and-drop via
|
|
||||||
/// [`Cx::on_unhandled`], mirroring `gen_server`'s handling of unexpected
|
|
||||||
/// messages.
|
|
||||||
Unhandled,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<S> From<S> for Resolution<S> {
|
|
||||||
fn from(s: S) -> Self {
|
|
||||||
Resolution::To(s)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
// Cx — the per-handler context
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
/// The context handle injected into [`Machine::on_start`] and
|
|
||||||
/// [`Machine::handle`]. Non-state outcomes (postpone, timeout arming) live here
|
|
||||||
/// as method calls rather than keywords (RFC §"Non-state outcomes live on cx").
|
|
||||||
///
|
|
||||||
/// In chunk 1 it carries only the [`on_unhandled`](Self::on_unhandled) hook;
|
|
||||||
/// `cx.state_timeout(d)` / `cx.timeout(name, d)` (chunk 2) and `cx.postpone()`
|
|
||||||
/// (chunk 3) attach here as those chunks land. It lives only on the actor's own
|
|
||||||
/// stack and is never sent.
|
|
||||||
pub struct Cx<Ev> {
|
|
||||||
_ev: PhantomData<fn() -> Ev>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<Ev> Cx<Ev> {
|
|
||||||
fn new() -> Self {
|
|
||||||
Cx { _ev: PhantomData }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The default for an event no arm matched: **log-and-drop**. Overridable
|
|
||||||
/// hook wiring is a follow-on (RFC Open Q5); for now an unmatched event is
|
|
||||||
/// silently dropped, as `gen_server` does with unexpected messages.
|
|
||||||
pub fn on_unhandled(&mut self) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
// Reply — the move-only reply handle a `call` variant carries
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
/// The reply side of a synchronous `call`, carried *inside* the machine's own
|
|
||||||
/// `call` event variant (`GetCount(Reply<u32>)`). Move-only: answering consumes
|
|
||||||
/// it, so a handler replies at most once. Built by [`StatemRef::call`]; the
|
|
||||||
/// caller parks on the matching receiver until `reply` is invoked (or the
|
|
||||||
/// machine dies, closing the channel).
|
|
||||||
///
|
|
||||||
/// Carrying the handle in the event — rather than the loop owning a reply slot
|
|
||||||
/// — is what lets a later chunk **postpone a call**: the whole event, reply
|
|
||||||
/// handle included, moves onto the postpone queue and is answered by a later
|
|
||||||
/// state (RFC §Postpone).
|
|
||||||
pub struct Reply<T> {
|
|
||||||
tx: Sender<T>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> Reply<T> {
|
|
||||||
/// Answer the call. A dropped/abandoned caller (e.g. one that timed out)
|
|
||||||
/// makes the send fail harmlessly — the machine's reply is simply
|
|
||||||
/// discarded, exactly as `gen_server`'s reply send behaves.
|
|
||||||
pub fn reply(self, value: T) {
|
|
||||||
let _ = self.tx.send(value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
// Client handle
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
/// Returned by [`StatemRef::call`] when the machine is unreachable.
|
|
||||||
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
|
||||||
pub enum CallError {
|
|
||||||
/// The machine was already gone, or died before replying.
|
|
||||||
Down,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returned by [`StatemRef::send`] when the machine's inbox is closed.
|
|
||||||
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
|
||||||
pub enum SendError {
|
|
||||||
/// The machine is gone (its inbox is closed).
|
|
||||||
Down,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A clonable handle to a running machine. Cloning yields another sender to the
|
|
||||||
/// same inbox; the machine lives until the last `StatemRef` is dropped, at which
|
|
||||||
/// point its inbox closes and the loop exits.
|
|
||||||
pub struct StatemRef<M: Machine> {
|
|
||||||
tx: Sender<M::Ev>,
|
|
||||||
pid: Pid,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<M: Machine> Clone for StatemRef<M> {
|
|
||||||
fn clone(&self) -> Self {
|
|
||||||
StatemRef { tx: self.tx.clone(), pid: self.pid }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<M: Machine> StatemRef<M> {
|
|
||||||
/// The machine actor's pid — usable with `monitor`, `request_stop`, `link`.
|
|
||||||
pub fn pid(&self) -> Pid {
|
|
||||||
self.pid
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Push one event into the inbox and return immediately (fire-and-forget).
|
|
||||||
/// A cast is just a `send` of the cast-tagged event. [`SendError::Down`] if
|
|
||||||
/// the inbox is already closed.
|
|
||||||
pub fn send(&self, ev: M::Ev) -> Result<(), SendError> {
|
|
||||||
self.tx.send(ev).map_err(|_| SendError::Down)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Synchronous request-reply. Builds a one-shot [`Reply`] channel, hands it
|
|
||||||
/// to `make` to construct the call-tagged event, sends it, and parks until
|
|
||||||
/// the machine replies — or returns [`CallError::Down`] if the machine is or
|
|
||||||
/// becomes unreachable first (the reply sender is dropped as the machine's
|
|
||||||
/// stack unwinds, closing the channel and waking the caller).
|
|
||||||
///
|
|
||||||
/// `make` wraps the [`Reply`] all the way to `M::Ev` — typically
|
|
||||||
/// `|r| Ev::Call(MyCall::GetCount(r))`. (The deferred macro would generate a
|
|
||||||
/// thinner `call` that hides the `Ev::Call` wrap and takes the bare variant
|
|
||||||
/// constructor.)
|
|
||||||
pub fn call<T, F>(&self, make: F) -> Result<T, CallError>
|
|
||||||
where
|
|
||||||
T: Send + 'static,
|
|
||||||
F: FnOnce(Reply<T>) -> M::Ev,
|
|
||||||
{
|
|
||||||
let (tx, rx) = channel::<T>();
|
|
||||||
let ev = make(Reply { tx });
|
|
||||||
self.send(ev).map_err(|_| CallError::Down)?;
|
|
||||||
rx.recv().map_err(|_| CallError::Down)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
// Spawn + loop
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
/// Spawn `machine` as an actor and hand back its [`StatemRef`]. Shape mirrors
|
|
||||||
/// `gen_server::start`: make the inbox, spawn the loop, return the ref; the
|
|
||||||
/// backing join handle is dropped (lifetime is governed by refs, not joining).
|
|
||||||
///
|
|
||||||
/// Panics if called outside `Runtime::run()`.
|
|
||||||
pub fn spawn<M: Machine>(machine: M) -> StatemRef<M> {
|
|
||||||
let (tx, rx) = channel::<M::Ev>();
|
|
||||||
let handle = spawn_actor(move || statem_loop(rx, machine));
|
|
||||||
StatemRef { tx, pid: handle.pid() }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The machine actor body: `on_start`, then one `handle` per inbox event until
|
|
||||||
/// the inbox closes (all refs dropped → graceful shutdown). Chunk 1 parks on
|
|
||||||
/// the inbox alone — no system arm, no timers — the analogue of `gen_server`'s
|
|
||||||
/// plain-inbox park.
|
|
||||||
fn statem_loop<M: Machine>(rx: Receiver<M::Ev>, mut machine: M) {
|
|
||||||
let mut cx = Cx::new();
|
|
||||||
machine.on_start(&mut cx);
|
|
||||||
loop {
|
|
||||||
match rx.recv() {
|
|
||||||
Ok(ev) => machine.handle(ev, &mut cx),
|
|
||||||
// All StatemRefs dropped → inbox closed → shutdown.
|
|
||||||
Err(_) => break,
|
|
||||||
}
|
|
||||||
// Observation point so a machine fed a hot inbox stays preemptible and
|
|
||||||
// cancellable.
|
|
||||||
crate::check!();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
// gen_statem! — the authoring macro (RFC 017 §Surface, fused variant)
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
/// Assemble a complete [`Machine`] from hand-written types, a glanceable
|
|
||||||
/// transition table, and free handler functions.
|
|
||||||
///
|
|
||||||
/// This is the **fused** authoring surface (see `examples/statem_fused.rs` for
|
|
||||||
/// the same machine written out by hand). You keep ownership of every type that
|
|
||||||
/// carries meaning — the state enum, the data struct, the `cast`/`call` enums,
|
|
||||||
/// and any per-row successor enums — and the macro emits only the mechanical
|
|
||||||
/// scaffolding: the unified event enum, the machine struct and its private
|
|
||||||
/// state cell, `start`, the `Machine` impl, and the `enter` dispatch.
|
|
||||||
///
|
|
||||||
/// The macro is **pure sugar with no semantic analysis of its own** — it never
|
|
||||||
/// inspects your types and never validates the graph. Every safety property is
|
|
||||||
/// a property of the code it emits, enforced by the *compiler* — which is what
|
|
||||||
/// lets a plain `macro_rules!` carry almost the whole guarantee set a proc-macro
|
|
||||||
/// would (the one exception, conflicting rows across a crate boundary, is
|
|
||||||
/// spelled out below).
|
|
||||||
///
|
|
||||||
/// # The four guarantees (and exactly how strong each is)
|
|
||||||
///
|
|
||||||
/// Two of these are hard `rustc` errors that fire unconditionally. The other
|
|
||||||
/// two are *lints*, so they need a deny in force — and lints interact with
|
|
||||||
/// macros, which is called out below. Put `#![deny(dead_code)]` on the crate
|
|
||||||
/// using the macro (the dispatch already denies its own pattern lint).
|
|
||||||
///
|
|
||||||
/// 1. **Forgotten `(state, event)` pair → `E0004` (hard error, always).** The
|
|
||||||
/// dispatch is a *total* `match (state, event)` with **no macro-injected
|
|
||||||
/// catch-all**: a pair you never wrote makes the match non-exhaustive.
|
|
||||||
/// Totality is the price — refuse the events you don't handle with explicit
|
|
||||||
/// `=> unhandled` rows (OR-patterns keep that to one row per state).
|
|
||||||
/// 4. **Out-of-set branch target → `E0599` (hard error, always).** A branching
|
|
||||||
/// row returns a per-state *successor enum* (its variants are that state's
|
|
||||||
/// declared targets); naming a tag outside it is a missing variant.
|
|
||||||
/// 3. **Orphan handler → `dead_code` (lint; needs your `#![deny(dead_code)]`).**
|
|
||||||
/// Handlers are ordinary module-private `fn`s in *your* crate, reachable only
|
|
||||||
/// through the table, so this lint sees them normally.
|
|
||||||
/// 2. **Duplicated / conflicting row → `unreachable_patterns` (lint; with a
|
|
||||||
/// caveat).** Your patterns are emitted verbatim into one match, so a second
|
|
||||||
/// arm for the same pair is unreachable. The macro applies
|
|
||||||
/// `#[deny(unreachable_patterns)]` to the dispatch itself — **but** `rustc`
|
|
||||||
/// silences this lint for code expanded from a macro defined in *another*
|
|
||||||
/// crate (`in_external_macro`). So a conflicting row is a hard error when the
|
|
||||||
/// machine lives in the same crate as this macro, and is **silently dropped
|
|
||||||
/// when you call `gen_statem!` from a downstream crate** — neither a crate
|
|
||||||
/// `#![deny]` nor `#![forbid]` overrides the suppression. This is the one
|
|
||||||
/// guarantee a `macro_rules!` cannot carry across the crate boundary; the
|
|
||||||
/// RFC's proc-macro could (it would stamp the arms with call-site spans).
|
|
||||||
/// Guard against it in review, or with an in-crate test of the machine.
|
|
||||||
///
|
|
||||||
/// There is deliberately **no separate `transitions { … }` adjacency block**:
|
|
||||||
/// in this variant the `match` *is* the table and the successor enums *are* the
|
|
||||||
/// per-state target sets, so a second listing would be redundant and
|
|
||||||
/// un-cross-checkable without a proc-macro. The graph is read off the `on`
|
|
||||||
/// blocks and the successor enums at the top of the file.
|
|
||||||
///
|
|
||||||
/// # Surface
|
|
||||||
///
|
|
||||||
/// ```ignore
|
|
||||||
/// // ── your types (the macro never generates or inspects these) ───────────
|
|
||||||
/// #[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
|
||||||
/// enum Switch { Off, On }
|
|
||||||
/// struct Counts { flips: u32, enters: u32 }
|
|
||||||
/// enum SwitchCast { Flip }
|
|
||||||
/// enum SwitchCall { GetCount(Reply<u32>) }
|
|
||||||
///
|
|
||||||
/// gen_statem! {
|
|
||||||
/// machine: SwitchSm { state: Switch, data: Counts };
|
|
||||||
/// event: Ev { cast: SwitchCast, call: SwitchCall };
|
|
||||||
///
|
|
||||||
/// // Name the bindings your handler bodies use. A declarative macro can't
|
|
||||||
/// // hand you its own `self`/`cx` (hygiene), so you choose the identifiers
|
|
||||||
/// // and the macro binds them: `data` = &mut your Data, `prev` = the
|
|
||||||
/// // current state tag, `cx` = the context handle.
|
|
||||||
/// context(data, prev, cx);
|
|
||||||
///
|
|
||||||
/// // `enter` runs on entry to a state; side effects only, returns ().
|
|
||||||
/// // Match the state tag (or `_`); the arm body is statements.
|
|
||||||
/// enter {
|
|
||||||
/// _ => data.enters += 1,
|
|
||||||
/// }
|
|
||||||
///
|
|
||||||
/// // The transition table. Group rows by current state with `on <pat>`.
|
|
||||||
/// // A row is: cast|call <event-pattern> [if <guard>] => <tail> ,
|
|
||||||
/// // and the tail is one of:
|
|
||||||
/// // * a state tag `Switch::On` (transition, or "stay"
|
|
||||||
/// // if it equals current)
|
|
||||||
/// // * a block ending in one `{ data.flips += 1; Switch::On }`
|
|
||||||
/// // * a successor-enum value `unlock(key)` (branching row)
|
|
||||||
/// // * the keyword `unhandled` (explicit refusal)
|
|
||||||
/// on Switch::Off => {
|
|
||||||
/// cast SwitchCast::Flip => { data.flips += 1; Switch::On },
|
|
||||||
/// call SwitchCall::GetCount(r) => { r.reply(data.flips); prev },
|
|
||||||
/// }
|
|
||||||
/// on Switch::On => {
|
|
||||||
/// cast SwitchCast::Flip => Switch::Off,
|
|
||||||
/// call SwitchCall::GetCount(r) => { r.reply(data.flips); prev },
|
|
||||||
/// }
|
|
||||||
/// }
|
|
||||||
/// ```
|
|
||||||
///
|
|
||||||
/// # Writing the rows
|
|
||||||
///
|
|
||||||
/// * **Every row ends in a comma** (block-bodied ones too) and every `on` block
|
|
||||||
/// is `on <state-pat> => { … }`. These two are macro-grammar requirements, not
|
|
||||||
/// style: a declarative matcher can't otherwise tell where a row's tail ends.
|
|
||||||
/// * **Qualify event patterns** (`SwitchCast::Flip`) and **state tags**
|
|
||||||
/// (`Switch::On`). A declarative macro can't prepend the enum name inside an
|
|
||||||
/// opaque pattern fragment, so the `cast`/`call` keyword only selects the
|
|
||||||
/// event wrapper — it does not qualify for you. The keyword also reads as a
|
|
||||||
/// sync/async marker at a glance.
|
|
||||||
/// * **Stay** = return the current tag. The `prev` you named in `context` is
|
|
||||||
/// bound to the pre-handler state for exactly this — handy in any-state
|
|
||||||
/// (`on _`) rows where there is no single literal tag to write.
|
|
||||||
/// * **`data` and `cx`** (the names you chose) are in scope in every body:
|
|
||||||
/// mutate `data`, reply through a `Reply` bound in the pattern, and (chunks
|
|
||||||
/// 2–3) arm timeouts or postpone via `cx`. You never write `self`.
|
|
||||||
/// * **Guards** are plain match guards. A guarded arm does not count toward
|
|
||||||
/// exhaustiveness, so pair it with an unguarded fallback or you'll (correctly)
|
|
||||||
/// trip `E0004`.
|
|
||||||
/// * **Branching rows** return a successor enum that `impl`s `From<_>` for the
|
|
||||||
/// state type; the macro supplies the outer `.into()`.
|
|
||||||
///
|
|
||||||
/// # What it emits
|
|
||||||
///
|
|
||||||
/// `enum $Ev { Cast($Cast), Call($Call) }`, `struct $Sm { state, data }`,
|
|
||||||
/// `$Sm::start(init, data) -> StatemRef<$Sm>`, the `Machine` impl (`on_start`
|
|
||||||
/// running the initial `enter`; `handle` = the dispatch match + the
|
|
||||||
/// stay/transition/unhandled apply-tail, the cell's sole writer), and the
|
|
||||||
/// `enter` dispatch. Chunk 1: real time, no timers, no postpone.
|
|
||||||
///
|
|
||||||
/// # Limitation
|
|
||||||
///
|
|
||||||
/// Diagnostics point at the macro expansion, not the offending source row —
|
|
||||||
/// the cost of staying a `macro_rules!`. The errors are the standard `E0004` /
|
|
||||||
/// `E0599` / lint messages, just sited at the invocation.
|
|
||||||
#[macro_export]
|
|
||||||
macro_rules! gen_statem {
|
|
||||||
// ===== public entry =====================================================
|
|
||||||
(
|
|
||||||
machine: $sm:ident { state: $State:ty, data: $Data:ty } ;
|
|
||||||
event: $Ev:ident { cast: $Cast:ty, call: $Call:ty } ;
|
|
||||||
context ( $data:ident , $cur:ident , $cx:ident ) ;
|
|
||||||
enter { $( $est:pat => $ebody:expr ),+ $(,)? }
|
|
||||||
$( on $st:pat => { $($rows:tt)* } )+
|
|
||||||
) => {
|
|
||||||
/// Unified inbox payload: the user's `cast`/`call` enums folded together
|
|
||||||
/// (chunks 2–3 add the runtime's internal timeout variants here).
|
|
||||||
enum $Ev {
|
|
||||||
Cast($Cast),
|
|
||||||
Call($Call),
|
|
||||||
}
|
|
||||||
|
|
||||||
struct $sm {
|
|
||||||
state: $State,
|
|
||||||
data: $Data,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl $sm {
|
|
||||||
fn start(init: $State, data: $Data) -> $crate::statem::StatemRef<$sm> {
|
|
||||||
$crate::statem::spawn($sm { state: init, data })
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(unused_variables)]
|
|
||||||
#[deny(unreachable_patterns)]
|
|
||||||
fn enter(&mut self, state: $State, $cx: &mut $crate::statem::Cx<$Ev>) {
|
|
||||||
let $data = &mut self.data;
|
|
||||||
match state {
|
|
||||||
$( $est => { $ebody } ),+
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl $crate::statem::Machine for $sm {
|
|
||||||
type Ev = $Ev;
|
|
||||||
|
|
||||||
fn on_start(&mut self, $cx: &mut $crate::statem::Cx<$Ev>) {
|
|
||||||
let s = self.state;
|
|
||||||
self.enter(s, $cx);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(unused_variables)]
|
|
||||||
#[deny(unreachable_patterns)] // conflicting rows must fail even though
|
|
||||||
// this match is external-macro-expanded
|
|
||||||
fn handle(&mut self, ev: $Ev, $cx: &mut $crate::statem::Cx<$Ev>) {
|
|
||||||
// Caller-named bindings (shared call-site hygiene, so row bodies
|
|
||||||
// can see them): `$cur` = current state tag, `$data` = &mut Data.
|
|
||||||
let $cur = self.state;
|
|
||||||
let $data = &mut self.data;
|
|
||||||
let next: $crate::statem::Resolution<$State> =
|
|
||||||
$crate::gen_statem!(@arms ($Ev) ($cur, ev) [ ]
|
|
||||||
$( on $st => { $($rows)* } )+);
|
|
||||||
match next {
|
|
||||||
$crate::statem::Resolution::To(s) if s == $cur => {}
|
|
||||||
$crate::statem::Resolution::To(s) => {
|
|
||||||
self.state = s; // <- sole writer of the state cell
|
|
||||||
self.enter(s, $cx);
|
|
||||||
}
|
|
||||||
$crate::statem::Resolution::Postpone => {
|
|
||||||
unreachable!("postpone is unreachable until chunk 3")
|
|
||||||
}
|
|
||||||
$crate::statem::Resolution::Unhandled => $cx.on_unhandled(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// ===== @arms: build the dispatch match ==================================
|
|
||||||
// No more on-blocks: emit the (total, catch-all-free) match.
|
|
||||||
(@arms ($Ev:ident) ($ss:expr, $se:expr) [ $($arms:tt)* ]) => {
|
|
||||||
match ($ss, $se) { $($arms)* }
|
|
||||||
};
|
|
||||||
// Open an on-block: remember its state pat, drain its rows, then continue.
|
|
||||||
(@arms ($Ev:ident) ($ss:expr, $se:expr) [ $($arms:tt)* ]
|
|
||||||
on $st:pat => { $($rows:tt)* } $($more:tt)*
|
|
||||||
) => {
|
|
||||||
$crate::gen_statem!(@rows ($Ev) ($ss, $se) [ $($arms)* ] ($st)
|
|
||||||
{ $($rows)* } { $($more)* })
|
|
||||||
};
|
|
||||||
|
|
||||||
// ===== @rows: drain one on-block's rows, threading the global acc ========
|
|
||||||
// cast, explicit refusal
|
|
||||||
(@rows ($Ev:ident) ($ss:expr, $se:expr) [ $($arms:tt)* ] ($st:pat)
|
|
||||||
{ cast $ev:pat $(if $g:expr)? => unhandled , $($rows:tt)* } { $($more:tt)* }
|
|
||||||
) => {
|
|
||||||
$crate::gen_statem!(@rows ($Ev) ($ss, $se)
|
|
||||||
[ $($arms)* ($st, $Ev::Cast($ev)) $(if $g)? => $crate::statem::Resolution::Unhandled, ]
|
|
||||||
($st) { $($rows)* } { $($more)* })
|
|
||||||
};
|
|
||||||
// cast, transition / stay / branch
|
|
||||||
(@rows ($Ev:ident) ($ss:expr, $se:expr) [ $($arms:tt)* ] ($st:pat)
|
|
||||||
{ cast $ev:pat $(if $g:expr)? => $tail:expr , $($rows:tt)* } { $($more:tt)* }
|
|
||||||
) => {
|
|
||||||
$crate::gen_statem!(@rows ($Ev) ($ss, $se)
|
|
||||||
[ $($arms)* ($st, $Ev::Cast($ev)) $(if $g)? => $crate::statem::Resolution::To($tail.into()), ]
|
|
||||||
($st) { $($rows)* } { $($more)* })
|
|
||||||
};
|
|
||||||
// call, explicit refusal
|
|
||||||
(@rows ($Ev:ident) ($ss:expr, $se:expr) [ $($arms:tt)* ] ($st:pat)
|
|
||||||
{ call $ev:pat $(if $g:expr)? => unhandled , $($rows:tt)* } { $($more:tt)* }
|
|
||||||
) => {
|
|
||||||
$crate::gen_statem!(@rows ($Ev) ($ss, $se)
|
|
||||||
[ $($arms)* ($st, $Ev::Call($ev)) $(if $g)? => $crate::statem::Resolution::Unhandled, ]
|
|
||||||
($st) { $($rows)* } { $($more)* })
|
|
||||||
};
|
|
||||||
// call, transition / stay / branch
|
|
||||||
(@rows ($Ev:ident) ($ss:expr, $se:expr) [ $($arms:tt)* ] ($st:pat)
|
|
||||||
{ call $ev:pat $(if $g:expr)? => $tail:expr , $($rows:tt)* } { $($more:tt)* }
|
|
||||||
) => {
|
|
||||||
$crate::gen_statem!(@rows ($Ev) ($ss, $se)
|
|
||||||
[ $($arms)* ($st, $Ev::Call($ev)) $(if $g)? => $crate::statem::Resolution::To($tail.into()), ]
|
|
||||||
($st) { $($rows)* } { $($more)* })
|
|
||||||
};
|
|
||||||
// this block is drained: hand the remaining on-blocks back to @arms
|
|
||||||
(@rows ($Ev:ident) ($ss:expr, $se:expr) [ $($arms:tt)* ] ($st:pat)
|
|
||||||
{ } { $($more:tt)* }
|
|
||||||
) => {
|
|
||||||
$crate::gen_statem!(@arms ($Ev) ($ss, $se) [ $($arms)* ] $($more)*)
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod gen_statem_tests {
|
|
||||||
use crate::statem::Reply;
|
|
||||||
|
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
|
||||||
enum Switch {
|
|
||||||
Off,
|
|
||||||
On,
|
|
||||||
}
|
|
||||||
struct Counts {
|
|
||||||
flips: u32,
|
|
||||||
enters: u32,
|
|
||||||
}
|
|
||||||
enum Cast {
|
|
||||||
Flip,
|
|
||||||
Nudge,
|
|
||||||
}
|
|
||||||
enum Call {
|
|
||||||
GetFlips(Reply<u32>),
|
|
||||||
GetEnters(Reply<u32>),
|
|
||||||
}
|
|
||||||
|
|
||||||
fn on_flip(data: &mut Counts) {
|
|
||||||
data.flips += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
crate::gen_statem! {
|
|
||||||
machine: SwitchSm { state: Switch, data: Counts };
|
|
||||||
event: Ev { cast: Cast, call: Call };
|
|
||||||
context(data, prev, cx);
|
|
||||||
|
|
||||||
enter {
|
|
||||||
_ => data.enters += 1,
|
|
||||||
}
|
|
||||||
|
|
||||||
on Switch::Off => {
|
|
||||||
cast Cast::Flip => { on_flip(data); Switch::On },
|
|
||||||
cast Cast::Nudge => unhandled,
|
|
||||||
}
|
|
||||||
on Switch::On => {
|
|
||||||
cast Cast::Flip => { on_flip(data); Switch::Off },
|
|
||||||
cast Cast::Nudge => prev, // explicit stay
|
|
||||||
}
|
|
||||||
on _ => {
|
|
||||||
call Call::GetFlips(r) => { r.reply(data.flips); prev },
|
|
||||||
call Call::GetEnters(r) => { r.reply(data.enters); prev },
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// NOTE (guarantee #2): because this machine lives in the SAME crate as
|
|
||||||
// `gen_statem!`, adding a duplicate row here — e.g. a second
|
|
||||||
// `cast Cast::Nudge => unhandled,` under `on Switch::Off` — is a hard
|
|
||||||
// `unreachable pattern` error. (From a downstream crate it is silently
|
|
||||||
// suppressed by rustc's in_external_macro rule; see the macro docs.)
|
|
||||||
#[test]
|
|
||||||
fn macro_machine_drives_and_counts() {
|
|
||||||
crate::run(|| {
|
|
||||||
let sm = SwitchSm::start(Switch::Off, Counts { flips: 0, enters: 0 });
|
|
||||||
sm.send(Ev::Cast(Cast::Flip)).unwrap(); // Off -> On (flip=1, enter)
|
|
||||||
sm.send(Ev::Cast(Cast::Nudge)).unwrap(); // On: stay (no enter)
|
|
||||||
sm.send(Ev::Cast(Cast::Flip)).unwrap(); // On -> Off (flip=2, enter)
|
|
||||||
|
|
||||||
let flips = sm.call(|r| Ev::Call(Call::GetFlips(r))).unwrap();
|
|
||||||
let enters = sm.call(|r| Ev::Call(Call::GetEnters(r))).unwrap();
|
|
||||||
assert_eq!(flips, 2);
|
|
||||||
assert_eq!(enters, 3); // Off(start) + On + Off
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+306
-107
@@ -1,16 +1,114 @@
|
|||||||
//! Supervision signals.
|
//! Supervision: keep a set of actors alive.
|
||||||
//!
|
//!
|
||||||
//! Every actor has a supervisor, which is itself just an actor with a
|
//! A *supervisor* is an actor whose only job is to start a fixed set of child
|
||||||
//! `Receiver<Signal>`. When a child actor terminates, the scheduler sends
|
//! actors and react when one of them terminates — restarting it (and, depending
|
||||||
//! a `Signal` on the supervisor's channel. The supervisor decides what to
|
//! on the strategy, some of its siblings) according to a policy, or giving up
|
||||||
//! do — restart, escalate, ignore.
|
//! when failures arrive too fast. It is how you turn "an actor that might crash"
|
||||||
|
//! into "a service that stays up": a crash becomes a restart instead of a hole
|
||||||
|
//! in the process tree.
|
||||||
//!
|
//!
|
||||||
//! For v0.1 there is no built-in restart-intensity cap. That's policy and
|
//! The supervisor type is [`OneForOne`]. The name is historical — the restart
|
||||||
//! lives in user code; library is mechanism only.
|
//! *strategy* is selectable via [`OneForOne::strategy`], and
|
||||||
|
//! [`Strategy::OneForOne`] is merely the default. You declare the children up
|
||||||
|
//! front as [`ChildSpec`]s, each carrying a [`Restart`] policy, then hand the
|
||||||
|
//! supervision loop an actor of its own with [`OneForOne::run`].
|
||||||
|
//!
|
||||||
|
//! ## A child that crashes and recovers
|
||||||
|
//!
|
||||||
|
//! ```
|
||||||
|
//! use smarm::{run, spawn, ChildSpec, OneForOne, Restart};
|
||||||
|
//! use std::sync::Arc;
|
||||||
|
//! use std::sync::atomic::{AtomicUsize, Ordering};
|
||||||
|
//! use std::time::Duration;
|
||||||
|
//!
|
||||||
|
//! run(|| {
|
||||||
|
//! // A flaky child: it panics on its first two starts, then settles.
|
||||||
|
//! let starts = Arc::new(AtomicUsize::new(0));
|
||||||
|
//! let s = starts.clone();
|
||||||
|
//! let child = move || {
|
||||||
|
//! let n = s.fetch_add(1, Ordering::SeqCst) + 1;
|
||||||
|
//! if n < 3 {
|
||||||
|
//! panic!("boom {n}");
|
||||||
|
//! }
|
||||||
|
//! // The third start returns normally.
|
||||||
|
//! };
|
||||||
|
//!
|
||||||
|
//! // The supervisor runs on its own actor. `Transient` restarts a child
|
||||||
|
//! // that panics but treats a clean return as "done", so once the child
|
||||||
|
//! // finally succeeds the supervisor has nothing left to do and `run()`
|
||||||
|
//! // returns. smarm catches the child's panic and turns it into a restart;
|
||||||
|
//! // it never reaches the process as a real crash.
|
||||||
|
//! let sup = spawn(move || {
|
||||||
|
//! OneForOne::new()
|
||||||
|
//! .intensity(5, Duration::from_secs(60))
|
||||||
|
//! .child(ChildSpec::new(Restart::Transient, child))
|
||||||
|
//! .run();
|
||||||
|
//! });
|
||||||
|
//! sup.join().unwrap();
|
||||||
|
//!
|
||||||
|
//! assert_eq!(starts.load(Ordering::SeqCst), 3); // one start, two restarts
|
||||||
|
//! });
|
||||||
|
//! ```
|
||||||
|
//!
|
||||||
|
//! ## Restart policies
|
||||||
|
//!
|
||||||
|
//! Each child carries a [`Restart`] policy that decides whether *that child*
|
||||||
|
//! comes back when it terminates:
|
||||||
|
//!
|
||||||
|
//! - [`Restart::Permanent`] restarts on any termination, normal or panic —
|
||||||
|
//! for a service that should never be down.
|
||||||
|
//! - [`Restart::Transient`] restarts only on an abnormal exit (a panic or a
|
||||||
|
//! cooperative stop); a clean return means "done" — for work that runs to
|
||||||
|
//! completion but should be retried if it crashes.
|
||||||
|
//! - [`Restart::Temporary`] never restarts; the death is simply noted.
|
||||||
|
//!
|
||||||
|
//! ## Strategies: which siblings get cycled
|
||||||
|
//!
|
||||||
|
//! When a restart is due, the [`Strategy`] decides which *other* children are
|
||||||
|
//! cycled along with the one that died. The triggering child's own policy still
|
||||||
|
//! decides whether anything restarts at all.
|
||||||
|
//!
|
||||||
|
//! - [`Strategy::OneForOne`] restarts only the child that died — the default.
|
||||||
|
//! - [`Strategy::OneForAll`] restarts every child: the survivors are stopped,
|
||||||
|
//! then the whole set is restarted.
|
||||||
|
//! - [`Strategy::RestForOne`] restarts the dead child and every child started
|
||||||
|
//! after it, leaving earlier children untouched.
|
||||||
|
//!
|
||||||
|
//! ## Stopping a sibling is cooperative
|
||||||
|
//!
|
||||||
|
//! Cycling a sibling means stopping it first, and a supervisor never tears a
|
||||||
|
//! running actor down from outside: smarm actors share a heap and rely on
|
||||||
|
//! Drop/RAII, so unwinding a peer's stack from elsewhere would be unsound.
|
||||||
|
//! Instead the supervisor *requests* the stop and the child unwinds at its next
|
||||||
|
//! observation point — a `check!()`, an allocation, or a blocking call. A child
|
||||||
|
//! wedged in a tight loop with no observation point cannot be stopped, for the
|
||||||
|
//! same reason it cannot be preempted.
|
||||||
|
//!
|
||||||
|
//! ## Giving up: the restart-intensity cap
|
||||||
|
//!
|
||||||
|
//! A child that crashes the instant it starts would otherwise restart forever.
|
||||||
|
//! [`OneForOne::intensity`] bounds that: at most `max` restarts within any
|
||||||
|
//! `period`-long sliding window. One terminating child counts as a single
|
||||||
|
//! restart event even when the strategy cycles several siblings. When the cap
|
||||||
|
//! trips, the supervisor stops restarting, cooperatively stops any survivors in
|
||||||
|
//! reverse start order, and `run()` returns.
|
||||||
|
//!
|
||||||
|
//! ## Running context
|
||||||
|
//!
|
||||||
|
//! [`OneForOne::run`] takes over the calling actor as the supervision loop, so
|
||||||
|
//! a supervisor gets an actor of its own — typically
|
||||||
|
//! `spawn(|| OneForOne::new()/* … */.run())`, all from inside
|
||||||
|
//! [`run`](crate::run). Each child is spawned beneath the supervisor's pid, so
|
||||||
|
//! every child termination funnels back to it as a [`Signal`].
|
||||||
|
|
||||||
use crate::pid::Pid;
|
use crate::pid::Pid;
|
||||||
use std::any::Any;
|
use std::any::Any;
|
||||||
|
|
||||||
|
/// A child-termination notice delivered to its supervisor.
|
||||||
|
///
|
||||||
|
/// Every child a supervisor starts is spawned beneath the supervisor's pid, so
|
||||||
|
/// each child's termination funnels back to it as one of these. The variant
|
||||||
|
/// records *how* the child went — which is what its [`Restart`] policy keys off.
|
||||||
pub enum Signal {
|
pub enum Signal {
|
||||||
/// The child exited normally.
|
/// The child exited normally.
|
||||||
Exit(Pid),
|
Exit(Pid),
|
||||||
@@ -25,9 +123,9 @@ pub enum Signal {
|
|||||||
impl std::fmt::Debug for Signal {
|
impl std::fmt::Debug for Signal {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
Signal::Exit(pid) => write!(f, "Signal::Exit({:?})", pid),
|
Signal::Exit(pid) => write!(f, "Signal::Exit({:?})", pid),
|
||||||
Signal::Panic(pid, _) => write!(f, "Signal::Panic({:?}, ..)", pid),
|
Signal::Panic(pid, _) => write!(f, "Signal::Panic({:?}, ..)", pid),
|
||||||
Signal::Stopped(pid) => write!(f, "Signal::Stopped({:?})", pid),
|
Signal::Stopped(pid) => write!(f, "Signal::Stopped({:?})", pid),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -35,35 +133,15 @@ impl std::fmt::Debug for Signal {
|
|||||||
impl Signal {
|
impl Signal {
|
||||||
pub fn pid(&self) -> Pid {
|
pub fn pid(&self) -> Pid {
|
||||||
match self {
|
match self {
|
||||||
Signal::Exit(p) => *p,
|
Signal::Exit(p) => *p,
|
||||||
Signal::Panic(p, _) => *p,
|
Signal::Panic(p, _) => *p,
|
||||||
Signal::Stopped(p) => *p,
|
Signal::Stopped(p) => *p,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
use crate::channel::{channel, RecvTimeoutError};
|
||||||
// One-for-one supervisor
|
use crate::monitor::DownReason;
|
||||||
//
|
|
||||||
// A supervisor is itself an actor. It spawns each child under its own pid so
|
|
||||||
// that every child death funnels into one mailbox (the `supervisor_channel`),
|
|
||||||
// then loops: receive a Signal, decide per the child's `Restart` policy
|
|
||||||
// whether to restart, and enforce a restart-intensity cap so a child that
|
|
||||||
// crashes in a tight loop eventually gives up instead of spinning forever.
|
|
||||||
//
|
|
||||||
// What this does NOT do: *forcibly* terminate a running child. smarm actors
|
|
||||||
// share a heap and rely on Drop/RAII, so tearing down a peer's stack from
|
|
||||||
// outside is unsound. Stopping a sibling — whether for `one_for_all` /
|
|
||||||
// `rest_for_one`, for a propagated link death, or for the ordered shutdown
|
|
||||||
// below — is therefore *cooperative*: `request_stop` flags the child and it
|
|
||||||
// unwinds at its next observation point. A child with no observation points
|
|
||||||
// (a tight loop with no `check!()`, allocation, or blocking op) cannot be
|
|
||||||
// stopped, exactly as it cannot be preempted. When the intensity cap trips,
|
|
||||||
// this supervisor stops restarting and tears the remaining children down in
|
|
||||||
// reverse start order before returning.
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
use crate::channel::channel;
|
|
||||||
use std::collections::{HashMap, VecDeque};
|
use std::collections::{HashMap, VecDeque};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
@@ -88,11 +166,52 @@ pub enum Restart {
|
|||||||
pub struct ChildSpec {
|
pub struct ChildSpec {
|
||||||
start: Arc<dyn Fn() + Send + Sync + 'static>,
|
start: Arc<dyn Fn() + Send + Sync + 'static>,
|
||||||
restart: Restart,
|
restart: Restart,
|
||||||
|
shutdown: Shutdown,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ChildSpec {
|
impl ChildSpec {
|
||||||
|
/// A child with the given restart policy and the default
|
||||||
|
/// [`Shutdown::Timeout`] of 5 seconds.
|
||||||
pub fn new(restart: Restart, start: impl Fn() + Send + Sync + 'static) -> Self {
|
pub fn new(restart: Restart, start: impl Fn() + Send + Sync + 'static) -> Self {
|
||||||
Self { start: Arc::new(start), restart }
|
Self {
|
||||||
|
start: Arc::new(start),
|
||||||
|
restart,
|
||||||
|
shutdown: Shutdown::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set how the supervisor stops this child (see [`Shutdown`]). A child
|
||||||
|
/// that is itself a supervisor should use [`Shutdown::Infinity`] so its
|
||||||
|
/// own subtree gets its full grace periods.
|
||||||
|
pub fn shutdown(mut self, shutdown: Shutdown) -> Self {
|
||||||
|
self.shutdown = shutdown;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// How a supervisor stops a child it is taking down — the OTP child-spec
|
||||||
|
/// `shutdown` value. Applies to every supervisor-initiated stop: the ordered
|
||||||
|
/// shutdown of the whole set and the sibling cycling of
|
||||||
|
/// [`Strategy::OneForAll`] / [`Strategy::RestForOne`].
|
||||||
|
///
|
||||||
|
/// A graceful stop is a [`request_shutdown`](crate::request_shutdown): a child
|
||||||
|
/// that traps exits receives the request as a message and winds down in its
|
||||||
|
/// own time; one that does not is stopped outright.
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
pub enum Shutdown {
|
||||||
|
/// `request_stop` immediately; no request, no grace period.
|
||||||
|
BrutalKill,
|
||||||
|
/// `request_shutdown`, wait up to the duration for the child to exit, then
|
||||||
|
/// `request_stop` it. The default, at 5 seconds.
|
||||||
|
Timeout(Duration),
|
||||||
|
/// `request_shutdown` and wait however long the child takes. Use for a
|
||||||
|
/// child supervisor, whose subtree has its own timeouts.
|
||||||
|
Infinity,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Shutdown {
|
||||||
|
fn default() -> Self {
|
||||||
|
Shutdown::Timeout(Duration::from_secs(5))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,11 +234,10 @@ pub enum Strategy {
|
|||||||
|
|
||||||
/// A supervisor over a fixed set of children.
|
/// A supervisor over a fixed set of children.
|
||||||
///
|
///
|
||||||
/// Despite the name (kept for backwards compatibility), the restart strategy
|
/// Build it with [`new`](Self::new), add children with [`child`](Self::child),
|
||||||
/// is selectable via [`OneForOne::strategy`]; the default is
|
/// pick a [`strategy`](Self::strategy) and an [`intensity`](Self::intensity)
|
||||||
/// [`Strategy::OneForOne`]. Build with `new()`, add children with `child()`,
|
/// cap, then drive the loop with [`run`](Self::run) on an actor of its own. See
|
||||||
/// tune the cap with `intensity()`, then drive it with `run()` from inside an
|
/// the [module docs](self) for the full picture.
|
||||||
/// actor (typically `spawn(|| OneForOne::new()....run())`).
|
|
||||||
pub struct OneForOne {
|
pub struct OneForOne {
|
||||||
children: Vec<ChildSpec>,
|
children: Vec<ChildSpec>,
|
||||||
strategy: Strategy,
|
strategy: Strategy,
|
||||||
@@ -165,23 +283,35 @@ impl OneForOne {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Run the supervision loop on the current actor. Returns when every child
|
/// Run the supervision loop on the current actor. Returns when every child
|
||||||
/// has reached a terminal, non-restartable state, or when the restart
|
/// has reached a terminal, non-restartable state, when the restart
|
||||||
/// intensity cap is tripped.
|
/// intensity cap is tripped, or when the supervisor is asked to shut down
|
||||||
|
/// (a [`request_shutdown`](crate::request_shutdown) — from its own
|
||||||
|
/// supervisor, or from the app). On every one of those exits the survivors
|
||||||
|
/// are stopped in reverse start order, each per its
|
||||||
|
/// [`Shutdown`] policy, before this returns.
|
||||||
|
///
|
||||||
|
/// The supervisor traps exits for the length of the loop (that is how the
|
||||||
|
/// shutdown request reaches it as a message). Should the supervisor itself
|
||||||
|
/// be hard-stopped with [`request_stop`](crate::request_stop), it unwinds
|
||||||
|
/// without waiting for anything — but a drop guard hard-stops its live
|
||||||
|
/// children on the way out, so the subtree is not orphaned (a child
|
||||||
|
/// supervisor unwinds the same way, recursively).
|
||||||
pub fn run(self) {
|
pub fn run(self) {
|
||||||
let me = crate::scheduler::self_pid();
|
let me = crate::scheduler::self_pid();
|
||||||
let (tx, rx) = channel::<Signal>();
|
let (tx, rx) = channel::<Signal>();
|
||||||
crate::scheduler::register_supervisor_channel(me, tx);
|
crate::scheduler::register_supervisor_channel(me, tx);
|
||||||
|
let exits = crate::link::trap_exit();
|
||||||
|
|
||||||
// pid -> index into `self.children`, for the children currently alive.
|
// pid -> index into `self.children`, for the children currently alive.
|
||||||
let mut by_pid: HashMap<Pid, usize> = HashMap::new();
|
let mut live = Live::default();
|
||||||
let mut active: usize = 0;
|
let mut active: usize = 0;
|
||||||
// Sliding window of recent restart instants, for the intensity cap.
|
// Sliding window of recent restart instants, for the intensity cap.
|
||||||
let mut restarts: Vec<Instant> = Vec::new();
|
let mut restarts: Vec<Instant> = Vec::new();
|
||||||
|
|
||||||
let start_child = |idx: usize, by_pid: &mut HashMap<Pid, usize>| {
|
let start_child = |idx: usize, live: &mut Live| {
|
||||||
let start = self.children[idx].start.clone();
|
let start = self.children[idx].start.clone();
|
||||||
let h = crate::scheduler::spawn_under(me, move || (start)());
|
let h = crate::scheduler::spawn_under(me, move || (start)());
|
||||||
by_pid.insert(h.pid(), idx);
|
live.insert(h.pid(), idx);
|
||||||
// We supervise via the signal funnel, not by joining; drop the
|
// We supervise via the signal funnel, not by joining; drop the
|
||||||
// handle so the child's slot is reclaimed promptly on death (the
|
// handle so the child's slot is reclaimed promptly on death (the
|
||||||
// termination Signal is delivered before reclamation regardless).
|
// termination Signal is delivered before reclamation regardless).
|
||||||
@@ -189,28 +319,105 @@ impl OneForOne {
|
|||||||
};
|
};
|
||||||
|
|
||||||
for idx in 0..self.children.len() {
|
for idx in 0..self.children.len() {
|
||||||
start_child(idx, &mut by_pid);
|
start_child(idx, &mut live);
|
||||||
active += 1;
|
active += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// A signal that arrives while we are awaiting stop-confirmations (for a
|
// A signal that arrives while we are awaiting stop-confirmations (for a
|
||||||
// child we are *not* currently stopping) is stashed here and processed
|
// child we are *not* currently stopping) is stashed here and processed
|
||||||
// by the main loop before it blocks on `recv` again.
|
// by the main loop before it blocks again.
|
||||||
let mut pending: VecDeque<Signal> = VecDeque::new();
|
let mut pending: VecDeque<Signal> = VecDeque::new();
|
||||||
let next_signal = |pending: &mut VecDeque<Signal>| -> Option<Signal> {
|
|
||||||
if let Some(s) = pending.pop_front() {
|
// Stop one child per its policy and wait for its termination signal.
|
||||||
Some(s)
|
// Signals for other pids that arrive meanwhile are stashed. Bounded by
|
||||||
} else {
|
// construction: `request_stop` (used directly, or as the fallback once
|
||||||
rx.recv().ok()
|
// the grace period lapses) always produces a signal.
|
||||||
|
let stop_child = |pid: Pid, idx: usize, pending: &mut VecDeque<Signal>| {
|
||||||
|
let await_one = |deadline: Option<Instant>, pending: &mut VecDeque<Signal>| -> bool {
|
||||||
|
loop {
|
||||||
|
let sig = match pending.iter().position(|s| s.pid() == pid) {
|
||||||
|
Some(i) => pending.remove(i),
|
||||||
|
None => match deadline {
|
||||||
|
None => rx.recv().ok(),
|
||||||
|
Some(dl) => {
|
||||||
|
match rx.recv_timeout(dl.saturating_duration_since(Instant::now()))
|
||||||
|
{
|
||||||
|
Ok(s) => Some(s),
|
||||||
|
Err(RecvTimeoutError::Timeout) => return false,
|
||||||
|
Err(RecvTimeoutError::Disconnected) => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
match sig {
|
||||||
|
Some(s) if s.pid() == pid => return true,
|
||||||
|
Some(s) => pending.push_back(s),
|
||||||
|
None => return true, // funnel closed: nothing more can arrive
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
match self.children[idx].shutdown {
|
||||||
|
Shutdown::BrutalKill => {
|
||||||
|
crate::scheduler::request_stop(pid);
|
||||||
|
await_one(None, pending);
|
||||||
|
}
|
||||||
|
Shutdown::Timeout(grace) => {
|
||||||
|
crate::scheduler::request_shutdown(pid);
|
||||||
|
if !await_one(Some(Instant::now() + grace), pending) {
|
||||||
|
crate::scheduler::request_stop(pid);
|
||||||
|
await_one(None, pending);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Shutdown::Infinity => {
|
||||||
|
crate::scheduler::request_shutdown(pid);
|
||||||
|
await_one(None, pending);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Stop a set of children in reverse start order, one at a time.
|
||||||
|
let stop_set =
|
||||||
|
|set: &mut Vec<(Pid, usize)>, live: &mut Live, pending: &mut VecDeque<Signal>| {
|
||||||
|
set.sort_unstable_by_key(|x| std::cmp::Reverse(x.1));
|
||||||
|
for (pid, idx) in set.iter() {
|
||||||
|
live.remove(pid);
|
||||||
|
stop_child(*pid, *idx, pending);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Wait for the next event: a stashed signal, a child signal, or a
|
||||||
|
// shutdown request. `Ok(sig)`, or `Err(())` when we must wind down.
|
||||||
|
let next_event = |pending: &mut VecDeque<Signal>| -> Result<Signal, ()> {
|
||||||
|
loop {
|
||||||
|
if let Some(s) = pending.pop_front() {
|
||||||
|
return Ok(s);
|
||||||
|
}
|
||||||
|
// The trap inbox is arm 0: a shutdown request is noticed even
|
||||||
|
// under a flood of child signals.
|
||||||
|
match crate::channel::select(&[&exits, &rx]) {
|
||||||
|
0 => match exits.try_recv() {
|
||||||
|
Ok(Some(sig)) if sig.reason == DownReason::Shutdown => return Err(()),
|
||||||
|
// Any other exit signal (a linked peer's death — a
|
||||||
|
// supervisor links nothing itself, but may be linked
|
||||||
|
// to) is not ours to act on; a closed trap inbox is
|
||||||
|
// impossible while `exits` is held here.
|
||||||
|
_ => {}
|
||||||
|
},
|
||||||
|
_ => match rx.try_recv() {
|
||||||
|
Ok(Some(s)) => return Ok(s),
|
||||||
|
Ok(None) => {}
|
||||||
|
Err(_) => return Err(()), // funnel closed: nothing left to supervise
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
while active > 0 {
|
while active > 0 {
|
||||||
let sig = match next_signal(&mut pending) {
|
let sig = match next_event(&mut pending) {
|
||||||
Some(s) => s,
|
Ok(s) => s,
|
||||||
None => break, // mailbox closed: nothing left to supervise
|
Err(()) => break,
|
||||||
};
|
};
|
||||||
let idx = match by_pid.remove(&sig.pid()) {
|
let idx = match live.remove(&sig.pid()) {
|
||||||
Some(i) => i,
|
Some(i) => i,
|
||||||
None => continue, // stray/duplicate signal
|
None => continue, // stray/duplicate signal
|
||||||
};
|
};
|
||||||
@@ -242,78 +449,70 @@ impl OneForOne {
|
|||||||
restarts.push(now);
|
restarts.push(now);
|
||||||
|
|
||||||
// Which *live* siblings get cycled along with the failed child.
|
// Which *live* siblings get cycled along with the failed child.
|
||||||
// (The failed child is already gone — removed from `by_pid` above.)
|
// (The failed child is already gone — removed from `live` above.)
|
||||||
let mut to_stop: Vec<(Pid, usize)> = match self.strategy {
|
let mut to_stop: Vec<(Pid, usize)> = match self.strategy {
|
||||||
Strategy::OneForOne => Vec::new(),
|
Strategy::OneForOne => Vec::new(),
|
||||||
Strategy::OneForAll => by_pid.iter().map(|(p, i)| (*p, *i)).collect(),
|
Strategy::OneForAll => live.iter().map(|(p, i)| (*p, *i)).collect(),
|
||||||
Strategy::RestForOne => by_pid
|
Strategy::RestForOne => live
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|(_, i)| **i > idx)
|
.filter(|(_, i)| **i > idx)
|
||||||
.map(|(p, i)| (*p, *i))
|
.map(|(p, i)| (*p, *i))
|
||||||
.collect(),
|
.collect(),
|
||||||
};
|
};
|
||||||
// Stop survivors in reverse start order (highest child index first).
|
|
||||||
to_stop.sort_unstable_by(|a, b| b.1.cmp(&a.1));
|
|
||||||
|
|
||||||
// The set we will restart: the failed child plus every sibling we
|
// The set we will restart: the failed child plus every sibling we
|
||||||
// are about to stop, restarted in start (ascending index) order.
|
// are about to stop, restarted in start (ascending index) order.
|
||||||
let mut restart_set: Vec<usize> = Vec::with_capacity(to_stop.len() + 1);
|
let mut restart_set: Vec<usize> = Vec::with_capacity(to_stop.len() + 1);
|
||||||
restart_set.push(idx);
|
restart_set.push(idx);
|
||||||
|
restart_set.extend(to_stop.iter().map(|(_, i)| *i));
|
||||||
|
|
||||||
// Request stops, then await each survivor's termination signal
|
// Stop the survivors (each per its policy, reverse start order),
|
||||||
// before restarting. `request_stop` on an already-dead pid is a
|
// then restart the whole set in start order. Net effect on
|
||||||
// no-op; in that case its (already-sent) Exit signal serves as the
|
// `active`: one child died (idx), `to_stop.len()` were stopped,
|
||||||
// confirmation. Any signal for a pid we are *not* awaiting is
|
// and `restart_set.len() == 1 + to_stop.len()` are started — so
|
||||||
// stashed for the main loop.
|
|
||||||
let mut awaiting: Vec<Pid> = Vec::with_capacity(to_stop.len());
|
|
||||||
for (pid, cidx) in &to_stop {
|
|
||||||
by_pid.remove(pid);
|
|
||||||
restart_set.push(*cidx);
|
|
||||||
crate::scheduler::request_stop(*pid);
|
|
||||||
awaiting.push(*pid);
|
|
||||||
}
|
|
||||||
while !awaiting.is_empty() {
|
|
||||||
let s = match next_signal(&mut pending) {
|
|
||||||
Some(s) => s,
|
|
||||||
None => break, // mailbox closed mid-await; stop waiting
|
|
||||||
};
|
|
||||||
if let Some(pos) = awaiting.iter().position(|p| *p == s.pid()) {
|
|
||||||
awaiting.swap_remove(pos);
|
|
||||||
} else {
|
|
||||||
pending.push_back(s);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Restart the whole set in start order. Net effect on `active`:
|
|
||||||
// one child died (idx), `to_stop.len()` were stopped, and
|
|
||||||
// `restart_set.len() == 1 + to_stop.len()` are started — so
|
|
||||||
// `active` is unchanged and needs no adjustment here.
|
// `active` is unchanged and needs no adjustment here.
|
||||||
|
stop_set(&mut to_stop, &mut live, &mut pending);
|
||||||
restart_set.sort_unstable();
|
restart_set.sort_unstable();
|
||||||
for cidx in restart_set {
|
for cidx in restart_set {
|
||||||
start_child(cidx, &mut by_pid);
|
start_child(cidx, &mut live);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ordered shutdown: stop any survivors in reverse start order and await
|
// Ordered shutdown: stop any survivors in reverse start order, each per
|
||||||
// their termination. On the normal `active == 0` exit `by_pid` is empty
|
// its policy. On the normal `active == 0` exit `live` is empty and this
|
||||||
// and this is a no-op; on a cap-trip or mailbox-closed break it tears
|
// is a no-op; on a shutdown request, a cap-trip, or a closed funnel it
|
||||||
// the remaining children down deterministically instead of leaking them.
|
// tears the remaining children down deterministically.
|
||||||
let mut survivors: Vec<(Pid, usize)> = by_pid.iter().map(|(p, i)| (*p, *i)).collect();
|
let mut survivors: Vec<(Pid, usize)> = live.iter().map(|(p, i)| (*p, *i)).collect();
|
||||||
survivors.sort_unstable_by(|a, b| b.1.cmp(&a.1));
|
stop_set(&mut survivors, &mut live, &mut pending);
|
||||||
let mut awaiting: Vec<Pid> = Vec::with_capacity(survivors.len());
|
}
|
||||||
for (pid, _) in &survivors {
|
}
|
||||||
crate::scheduler::request_stop(*pid);
|
|
||||||
awaiting.push(*pid);
|
/// The live children of a supervisor, with a drop guard: if the supervisor is
|
||||||
}
|
/// unwound (a hard `request_stop`, or a panic in the loop) its children are
|
||||||
while !awaiting.is_empty() {
|
/// hard-stopped rather than orphaned. Fire-and-forget by necessity — a guard
|
||||||
let s = match next_signal(&mut pending) {
|
/// running mid-unwind cannot park to await anything.
|
||||||
Some(s) => s,
|
#[derive(Default)]
|
||||||
None => break,
|
struct Live(HashMap<Pid, usize>);
|
||||||
};
|
|
||||||
if let Some(pos) = awaiting.iter().position(|p| *p == s.pid()) {
|
impl std::ops::Deref for Live {
|
||||||
awaiting.swap_remove(pos);
|
type Target = HashMap<Pid, usize>;
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::ops::DerefMut for Live {
|
||||||
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||||
|
&mut self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for Live {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
if std::thread::panicking() {
|
||||||
|
for pid in self.0.keys() {
|
||||||
|
crate::scheduler::request_stop(*pid);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+11
-2
@@ -6,10 +6,19 @@
|
|||||||
//! Build the loom models with: `RUSTFLAGS="--cfg loom" cargo test --lib --release`
|
//! Build the loom models with: `RUSTFLAGS="--cfg loom" cargo test --lib --release`
|
||||||
|
|
||||||
#[cfg(loom)]
|
#[cfg(loom)]
|
||||||
pub(crate) use loom::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
|
pub(crate) use loom::sync::atomic::{fence, AtomicU64, AtomicUsize, Ordering};
|
||||||
|
|
||||||
#[cfg(not(loom))]
|
#[cfg(not(loom))]
|
||||||
pub(crate) use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
|
pub(crate) use std::sync::atomic::{fence, AtomicU64, AtomicUsize, Ordering};
|
||||||
|
|
||||||
|
// park.rs condvar-parker (loom + non-Linux builds only; the Linux non-loom
|
||||||
|
// build parks on a futex and never touches these — gating them identically
|
||||||
|
// keeps the default build free of unused imports).
|
||||||
|
#[cfg(loom)]
|
||||||
|
pub(crate) use loom::sync::{Condvar, Mutex};
|
||||||
|
|
||||||
|
#[cfg(all(not(loom), not(target_os = "linux")))]
|
||||||
|
pub(crate) use std::sync::{Condvar, Mutex};
|
||||||
|
|
||||||
/// `UnsafeCell` with loom's `with`/`with_mut` access API; pass-through cost
|
/// `UnsafeCell` with loom's `with`/`with_mut` access API; pass-through cost
|
||||||
/// is zero in normal builds (`#[inline]`, newtype over std's cell).
|
/// is zero in normal builds (`#[inline]`, newtype over std's cell).
|
||||||
|
|||||||
+155
-17
@@ -102,6 +102,19 @@ pub struct Entry {
|
|||||||
seq: u64,
|
seq: u64,
|
||||||
pub pid: Pid,
|
pub pid: Pid,
|
||||||
pub reason: Reason,
|
pub reason: Reason,
|
||||||
|
/// RFC 007 virtual time: the global delay ledger reading when this entry
|
||||||
|
/// was (re-)queued. `pop_due` shifts the effective deadline by any delay
|
||||||
|
/// injected since, so timers dilate together with the causally-delayed
|
||||||
|
/// workload instead of firing early in virtual terms.
|
||||||
|
#[cfg(feature = "smarm-causal")]
|
||||||
|
delay_stamp: u64,
|
||||||
|
/// RFC 007: a wall-anchored entry opts out of the virtual-time shift —
|
||||||
|
/// its deadline is honoured in wall time regardless of injected delay.
|
||||||
|
/// Used by the causal controller's own measurement/cooldown sleeps so
|
||||||
|
/// experiment windows keep a fixed wall length; ordinary workload timers
|
||||||
|
/// stay virtual (`false`).
|
||||||
|
#[cfg(feature = "smarm-causal")]
|
||||||
|
wall: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialEq for Entry {
|
impl PartialEq for Entry {
|
||||||
@@ -116,7 +129,9 @@ impl Ord for Entry {
|
|||||||
// Earlier deadline first; ties broken by insertion order so the
|
// Earlier deadline first; ties broken by insertion order so the
|
||||||
// ordering is total. `Reason` and `Pid` deliberately don't
|
// ordering is total. `Reason` and `Pid` deliberately don't
|
||||||
// participate.
|
// participate.
|
||||||
self.deadline.cmp(&other.deadline).then_with(|| self.seq.cmp(&other.seq))
|
self.deadline
|
||||||
|
.cmp(&other.deadline)
|
||||||
|
.then_with(|| self.seq.cmp(&other.seq))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -128,6 +143,14 @@ impl PartialOrd for Entry {
|
|||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct Timers {
|
pub struct Timers {
|
||||||
|
/// RFC 018: the scheduler coordination layer. Attached once at
|
||||||
|
/// `RuntimeInner::new`; every insert notes its deadline (min-maintained
|
||||||
|
/// snapshot for the busy-path due-check + the timekeeper re-arm wake)
|
||||||
|
/// and every pop/clear re-anchors the snapshot to the heap minimum.
|
||||||
|
/// All calls happen under the timers mutex — the serialization the
|
||||||
|
/// coordinator's timer protocol mandates. `None` only in unit tests
|
||||||
|
/// that construct a bare `Timers`.
|
||||||
|
coord: Option<std::sync::Arc<crate::park::Coordinator>>,
|
||||||
/// Reverse-wrapped so the smallest deadline is at the top.
|
/// Reverse-wrapped so the smallest deadline is at the top.
|
||||||
heap: BinaryHeap<Reverse<Entry>>,
|
heap: BinaryHeap<Reverse<Entry>>,
|
||||||
/// Monotonic counter for the tiebreaker `seq` field (and the `TimerId` of a
|
/// Monotonic counter for the tiebreaker `seq` field (and the `TimerId` of a
|
||||||
@@ -144,7 +167,18 @@ pub struct Timers {
|
|||||||
|
|
||||||
impl Timers {
|
impl Timers {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self { heap: BinaryHeap::new(), next_seq: 0, armed: std::collections::HashSet::new() }
|
Self {
|
||||||
|
coord: None,
|
||||||
|
heap: BinaryHeap::new(),
|
||||||
|
next_seq: 0,
|
||||||
|
armed: std::collections::HashSet::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Attach the scheduler coordination layer (RFC 018). Called once, at
|
||||||
|
/// runtime construction, before any scheduler thread exists.
|
||||||
|
pub(crate) fn attach_coordinator(&mut self, c: std::sync::Arc<crate::park::Coordinator>) {
|
||||||
|
self.coord = Some(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Insert a `Sleep` timer. Convenience for the common case.
|
/// Insert a `Sleep` timer. Convenience for the common case.
|
||||||
@@ -152,6 +186,19 @@ impl Timers {
|
|||||||
self.insert(deadline, pid, Reason::Sleep { epoch });
|
self.insert(deadline, pid, Reason::Sleep { epoch });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Insert a *wall-anchored* `Sleep` timer: fires at `deadline` in wall
|
||||||
|
/// time even while causal profiling (feature `smarm-causal`) is injecting
|
||||||
|
/// virtual delay — it never chases the delay ledger. Without the feature
|
||||||
|
/// this is identical to [`insert_sleep`](Self::insert_sleep).
|
||||||
|
///
|
||||||
|
/// Intended for measurement machinery (the causal controller's window and
|
||||||
|
/// cooldown sleeps, TSC calibration) whose durations *define* wall time
|
||||||
|
/// rather than participate in the workload. Workload code should use the
|
||||||
|
/// ordinary virtual-anchored timers.
|
||||||
|
pub fn insert_sleep_wall(&mut self, deadline: Instant, pid: Pid, epoch: u32) {
|
||||||
|
self.push(deadline, pid, Reason::Sleep { epoch }, true);
|
||||||
|
}
|
||||||
|
|
||||||
/// Arm a cancellable `send_after` timer: run `fire` at `deadline` unless
|
/// Arm a cancellable `send_after` timer: run `fire` at `deadline` unless
|
||||||
/// [`cancel`](Self::cancel)led first. `pid` is informational only (the
|
/// [`cancel`](Self::cancel)led first. `pid` is informational only (the
|
||||||
/// destination, or who armed it — useful for introspection); it is *not*
|
/// destination, or who armed it — useful for introspection); it is *not*
|
||||||
@@ -163,11 +210,24 @@ impl Timers {
|
|||||||
pid: Pid,
|
pid: Pid,
|
||||||
fire: Box<dyn FnOnce() + Send>,
|
fire: Box<dyn FnOnce() + Send>,
|
||||||
) -> TimerId {
|
) -> TimerId {
|
||||||
let seq = self.next_seq;
|
self.armed.insert(self.next_seq);
|
||||||
self.next_seq = self.next_seq.wrapping_add(1);
|
TimerId(self.push(deadline, pid, Reason::Send { fire }, false))
|
||||||
self.armed.insert(seq);
|
}
|
||||||
self.heap.push(Reverse(Entry { deadline, seq, pid, reason: Reason::Send { fire } }));
|
|
||||||
TimerId(seq)
|
/// Arm a *wall-anchored* cancellable `send_after` timer (RFC 007): the
|
||||||
|
/// same contract as [`insert_send`](Self::insert_send), but the entry
|
||||||
|
/// opts out of the virtual-time shift and fires at its raw deadline
|
||||||
|
/// regardless of injected delay — the `Send`-reason sibling of
|
||||||
|
/// [`insert_sleep_wall`](Self::insert_sleep_wall). Without the
|
||||||
|
/// `smarm-causal` feature this is identical to `insert_send`.
|
||||||
|
pub fn insert_send_wall(
|
||||||
|
&mut self,
|
||||||
|
deadline: Instant,
|
||||||
|
pid: Pid,
|
||||||
|
fire: Box<dyn FnOnce() + Send>,
|
||||||
|
) -> TimerId {
|
||||||
|
self.armed.insert(self.next_seq);
|
||||||
|
TimerId(self.push(deadline, pid, Reason::Send { fire }, true))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Cancel an armed `send_after` timer. Returns `true` if the timer was
|
/// Cancel an armed `send_after` timer. Returns `true` if the timer was
|
||||||
@@ -179,11 +239,38 @@ impl Timers {
|
|||||||
self.armed.remove(&id.0)
|
self.armed.remove(&id.0)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Insert an arbitrary timer entry.
|
/// Insert an arbitrary (virtual-anchored) timer entry.
|
||||||
pub fn insert(&mut self, deadline: Instant, pid: Pid, reason: Reason) {
|
pub fn insert(&mut self, deadline: Instant, pid: Pid, reason: Reason) {
|
||||||
|
self.push(deadline, pid, reason, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Common insertion path. `wall` selects the RFC 007 anchor (see
|
||||||
|
/// [`insert_sleep_wall`](Self::insert_sleep_wall)); it is accepted — and
|
||||||
|
/// ignored — without the `smarm-causal` feature so callers don't fork.
|
||||||
|
/// Returns the entry's `seq`.
|
||||||
|
fn push(&mut self, deadline: Instant, pid: Pid, reason: Reason, wall: bool) -> u64 {
|
||||||
|
#[cfg(not(feature = "smarm-causal"))]
|
||||||
|
let _ = wall;
|
||||||
let seq = self.next_seq;
|
let seq = self.next_seq;
|
||||||
self.next_seq = self.next_seq.wrapping_add(1);
|
self.next_seq = self.next_seq.wrapping_add(1);
|
||||||
self.heap.push(Reverse(Entry { deadline, seq, pid, reason }));
|
self.heap.push(Reverse(Entry {
|
||||||
|
deadline,
|
||||||
|
seq,
|
||||||
|
pid,
|
||||||
|
reason,
|
||||||
|
#[cfg(feature = "smarm-causal")]
|
||||||
|
delay_stamp: crate::causal::global_delay_cycles(),
|
||||||
|
#[cfg(feature = "smarm-causal")]
|
||||||
|
wall,
|
||||||
|
}));
|
||||||
|
// RFC 018: publish the (possibly new-minimum) deadline to the
|
||||||
|
// busy-path snapshot and wake the timekeeper if it is parked
|
||||||
|
// toward a later one. We hold the timers mutex — the mandated
|
||||||
|
// serialization for both.
|
||||||
|
if let Some(c) = &self.coord {
|
||||||
|
c.note_deadline(deadline);
|
||||||
|
}
|
||||||
|
seq
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_empty(&self) -> bool {
|
pub fn is_empty(&self) -> bool {
|
||||||
@@ -196,6 +283,9 @@ impl Timers {
|
|||||||
pub fn clear(&mut self) {
|
pub fn clear(&mut self) {
|
||||||
self.heap.clear();
|
self.heap.clear();
|
||||||
self.armed.clear();
|
self.armed.clear();
|
||||||
|
if let Some(c) = &self.coord {
|
||||||
|
c.refresh_deadline(None);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Soonest pending deadline, or `None` if the heap is empty.
|
/// Soonest pending deadline, or `None` if the heap is empty.
|
||||||
@@ -210,19 +300,67 @@ impl Timers {
|
|||||||
/// one is silently dropped here (its `seq` was already removed from
|
/// one is silently dropped here (its `seq` was already removed from
|
||||||
/// `armed` by [`cancel`](Self::cancel)). Returning it removes it from
|
/// `armed` by [`cancel`](Self::cancel)). Returning it removes it from
|
||||||
/// `armed`, so a later `cancel` of a fired timer reports `false`.
|
/// `armed`, so a later `cancel` of a fired timer reports `false`.
|
||||||
|
///
|
||||||
|
/// RFC 007 virtual time (feature `smarm-causal`): before an entry fires,
|
||||||
|
/// any global delay injected since it was (re-)queued is added to its
|
||||||
|
/// deadline; an entry whose *effective* deadline hasn't passed is pushed
|
||||||
|
/// back with the shifted deadline and a fresh stamp, so it keeps chasing
|
||||||
|
/// delay injected while it waits. Consequences, both benign:
|
||||||
|
/// [`peek_deadline`](Self::peek_deadline) may under-report (raw deadline
|
||||||
|
/// earlier than effective), costing at most one spurious scheduler wake
|
||||||
|
/// per injected chunk; and a shift never converts wall time — with zero
|
||||||
|
/// debt the path is byte-identical to the featureless one. Wall-anchored
|
||||||
|
/// entries ([`insert_sleep_wall`](Self::insert_sleep_wall)) are exempt
|
||||||
|
/// from the shift and always fire at their raw deadline.
|
||||||
pub fn pop_due(&mut self, now: Instant) -> Vec<Entry> {
|
pub fn pop_due(&mut self, now: Instant) -> Vec<Entry> {
|
||||||
let mut out = Vec::new();
|
let mut out = Vec::new();
|
||||||
|
#[cfg(feature = "smarm-causal")]
|
||||||
|
let global = crate::causal::global_delay_cycles();
|
||||||
while let Some(r) = self.heap.peek() {
|
while let Some(r) = self.heap.peek() {
|
||||||
if r.0.deadline <= now {
|
if r.0.deadline > now {
|
||||||
let entry = self.heap.pop().unwrap().0;
|
|
||||||
if matches!(entry.reason, Reason::Send { .. }) && !self.armed.remove(&entry.seq) {
|
|
||||||
// Cancelled before it came due: discard, do not deliver.
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
out.push(entry);
|
|
||||||
} else {
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
#[allow(unused_mut)]
|
||||||
|
let mut entry = match self.heap.pop() {
|
||||||
|
Some(e) => e.0,
|
||||||
|
None => panic!("smarm: timer heap pop after peek returned None (core corrupt)"),
|
||||||
|
};
|
||||||
|
if matches!(entry.reason, Reason::Send { .. }) && !self.armed.contains(&entry.seq) {
|
||||||
|
// Cancelled before it came due: discard, do not deliver.
|
||||||
|
// (Checked before any shift so a cancelled entry is never
|
||||||
|
// re-queued just to be discarded later.)
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
#[cfg(feature = "smarm-causal")]
|
||||||
|
if !entry.wall {
|
||||||
|
let debt = global.saturating_sub(entry.delay_stamp);
|
||||||
|
if debt > 0 {
|
||||||
|
let shifted = entry
|
||||||
|
.deadline
|
||||||
|
.checked_add(crate::causal::cycles_to_duration(debt))
|
||||||
|
.unwrap_or(entry.deadline);
|
||||||
|
if shifted > now {
|
||||||
|
// Not due in virtual time: re-queue at the shifted
|
||||||
|
// deadline, stamped, keeping `seq` (and thus `Send`
|
||||||
|
// cancellation identity) intact.
|
||||||
|
entry.deadline = shifted;
|
||||||
|
entry.delay_stamp = global;
|
||||||
|
self.heap.push(Reverse(entry));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if matches!(entry.reason, Reason::Send { .. }) {
|
||||||
|
self.armed.remove(&entry.seq);
|
||||||
|
}
|
||||||
|
out.push(entry);
|
||||||
|
}
|
||||||
|
// RFC 018: re-anchor the busy-path snapshot to the new heap minimum
|
||||||
|
// (still under the timers mutex). A causal-shift re-queue above went
|
||||||
|
// through `heap.push` directly, so this peek is the one place the
|
||||||
|
// snapshot is guaranteed to catch up.
|
||||||
|
if let Some(c) = &self.coord {
|
||||||
|
c.refresh_deadline(self.peek_deadline());
|
||||||
}
|
}
|
||||||
out
|
out
|
||||||
}
|
}
|
||||||
|
|||||||
+96
-38
@@ -11,18 +11,22 @@
|
|||||||
//! cargo test --test runtime <test_name> --features smarm-trace
|
//! cargo test --test runtime <test_name> --features smarm-trace
|
||||||
//!
|
//!
|
||||||
//! Output: smarm_trace.json in cwd, or $SMARM_TRACE_FILE.
|
//! Output: smarm_trace.json in cwd, or $SMARM_TRACE_FILE.
|
||||||
//! View: https://ui.perfetto.dev or chrome://tracing
|
//! View: <https://ui.perfetto.dev> or chrome://tracing
|
||||||
|
|
||||||
#[cfg(feature = "smarm-trace")]
|
#[cfg(feature = "smarm-trace")]
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! te {
|
macro_rules! te {
|
||||||
($kind:expr) => { $crate::trace::record($kind) };
|
($kind:expr) => {
|
||||||
|
$crate::trace::record($kind)
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "smarm-trace"))]
|
#[cfg(not(feature = "smarm-trace"))]
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! te {
|
macro_rules! te {
|
||||||
($kind:expr) => { () };
|
($kind:expr) => {
|
||||||
|
()
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "smarm-trace")]
|
#[cfg(feature = "smarm-trace")]
|
||||||
@@ -42,17 +46,32 @@ mod inner {
|
|||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub enum Event {
|
pub enum Event {
|
||||||
// Actor lifecycle
|
// Actor lifecycle
|
||||||
Spawn { parent: Pid, child: Pid },
|
Spawn {
|
||||||
|
parent: Pid,
|
||||||
|
child: Pid,
|
||||||
|
},
|
||||||
Resume(Pid),
|
Resume(Pid),
|
||||||
Yield(Pid),
|
Yield(Pid),
|
||||||
Park(Pid),
|
Park(Pid),
|
||||||
Done(Pid),
|
Done(Pid),
|
||||||
|
/// Root exit found a live forest root (an actor nobody supervises)
|
||||||
|
/// and delivered `request_shutdown` to it. `trapping` says whether it
|
||||||
|
/// got the chance to drain (true) or was stopped outright (false).
|
||||||
|
/// Every such line is an actor whose lifetime was nobody's business
|
||||||
|
/// but the runtime's — the way to *see* unsupervised leftovers.
|
||||||
|
RootSweep {
|
||||||
|
target: Pid,
|
||||||
|
trapping: bool,
|
||||||
|
},
|
||||||
// Wakeup paths
|
// Wakeup paths
|
||||||
UnparkDirect(Pid), // unpark() saw Parked -> re-queued immediately
|
UnparkDirect(Pid), // unpark() saw Parked -> re-queued immediately
|
||||||
UnparkDeferred(Pid), // unpark() saw Runnable -> set pending_unpark flag
|
UnparkDeferred(Pid), // unpark() saw Runnable -> set pending_unpark flag
|
||||||
UnparkFlagConsumed(Pid), // scheduler saw flag on Park -> re-queued instead
|
UnparkFlagConsumed(Pid), // scheduler saw flag on Park -> re-queued instead
|
||||||
// Channel
|
// Channel
|
||||||
Send { sender: Pid, receiver: Option<Pid> },
|
Send {
|
||||||
|
sender: Pid,
|
||||||
|
receiver: Option<Pid>,
|
||||||
|
},
|
||||||
RecvPark(Pid),
|
RecvPark(Pid),
|
||||||
RecvWake(Pid),
|
RecvWake(Pid),
|
||||||
// Queue
|
// Queue
|
||||||
@@ -61,6 +80,13 @@ mod inner {
|
|||||||
// RFC 005 wake slot
|
// RFC 005 wake slot
|
||||||
SlotPush(Pid), // actor-context wake parked in the waking thread's slot
|
SlotPush(Pid), // actor-context wake parked in the waking thread's slot
|
||||||
SlotPop(Pid), // scheduler resumed a pid from its own slot
|
SlotPop(Pid), // scheduler resumed a pid from its own slot
|
||||||
|
// Cluster (RFC 010): the conn actor's verdict on one inbound frame —
|
||||||
|
// local knowledge only, never on the wire; the label is
|
||||||
|
// `InboundVerdict::label()`. No pid: a refused frame has none.
|
||||||
|
ClusterInbound(&'static str),
|
||||||
|
// Cluster (RFC 010): the connector's verdict on one dial attempt —
|
||||||
|
// `"ok"` or `DialError::label()`. No pid.
|
||||||
|
ClusterDial(&'static str),
|
||||||
}
|
}
|
||||||
|
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
@@ -68,8 +94,8 @@ mod inner {
|
|||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
|
|
||||||
struct Record {
|
struct Record {
|
||||||
nanos: u64, // ns since open()
|
nanos: u64, // ns since open()
|
||||||
tid: u64, // OS thread id
|
tid: u64, // OS thread id
|
||||||
event: Event,
|
event: Event,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,8 +110,8 @@ mod inner {
|
|||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
|
|
||||||
struct Global {
|
struct Global {
|
||||||
sender: mpsc::Sender<Msg>,
|
sender: mpsc::Sender<Msg>,
|
||||||
start: Instant,
|
start: Instant,
|
||||||
}
|
}
|
||||||
|
|
||||||
static GLOBAL: Mutex<Option<Global>> = Mutex::new(None);
|
static GLOBAL: Mutex<Option<Global>> = Mutex::new(None);
|
||||||
@@ -95,13 +121,13 @@ mod inner {
|
|||||||
// The start Instant is copied alongside it — also one mutex hit per thread.
|
// The start Instant is copied alongside it — also one mutex hit per thread.
|
||||||
// record() never touches GLOBAL after that.
|
// record() never touches GLOBAL after that.
|
||||||
struct LocalState {
|
struct LocalState {
|
||||||
tx: mpsc::Sender<Msg>,
|
tx: mpsc::Sender<Msg>,
|
||||||
start: Instant,
|
start: Instant,
|
||||||
}
|
}
|
||||||
|
|
||||||
thread_local! {
|
thread_local! {
|
||||||
static LOCAL_STATE: std::cell::RefCell<Option<LocalState>> =
|
static LOCAL_STATE: std::cell::RefCell<Option<LocalState>> =
|
||||||
std::cell::RefCell::new(None);
|
const { std::cell::RefCell::new(None) };
|
||||||
}
|
}
|
||||||
|
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
@@ -109,20 +135,26 @@ mod inner {
|
|||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
|
|
||||||
pub fn open() {
|
pub fn open() {
|
||||||
let path = std::env::var("SMARM_TRACE_FILE")
|
let path =
|
||||||
.unwrap_or_else(|_| "smarm_trace.json".to_owned());
|
std::env::var("SMARM_TRACE_FILE").unwrap_or_else(|_| "smarm_trace.json".to_owned());
|
||||||
|
|
||||||
let (tx, rx) = mpsc::channel::<Msg>();
|
let (tx, rx) = mpsc::channel::<Msg>();
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
|
|
||||||
*GLOBAL.lock().unwrap() = Some(Global { sender: tx, start });
|
match GLOBAL.lock() {
|
||||||
|
Ok(mut g) => *g = Some(Global { sender: tx, start }),
|
||||||
|
Err(e) => panic!("smarm: trace lock poisoned (core corrupt): {e}"),
|
||||||
|
}
|
||||||
|
|
||||||
// Drain thread: owns the Receiver, writes to disk.
|
// Drain thread: owns the Receiver, writes to disk.
|
||||||
let path_for_thread = path.clone();
|
let path_for_thread = path.clone();
|
||||||
std::thread::Builder::new()
|
match std::thread::Builder::new()
|
||||||
.name("smarm-trace-drain".into())
|
.name("smarm-trace-drain".into())
|
||||||
.spawn(move || drain_thread(rx, &path_for_thread))
|
.spawn(move || drain_thread(rx, &path_for_thread))
|
||||||
.expect("failed to spawn trace drain thread");
|
{
|
||||||
|
Ok(_) => {}
|
||||||
|
Err(e) => panic!("smarm: failed to spawn trace drain thread: {e}"),
|
||||||
|
}
|
||||||
|
|
||||||
eprintln!("[smarm-trace] writing to {}", path);
|
eprintln!("[smarm-trace] writing to {}", path);
|
||||||
}
|
}
|
||||||
@@ -133,7 +165,10 @@ mod inner {
|
|||||||
// Drop the global sender so the drain thread's recv() returns Err
|
// Drop the global sender so the drain thread's recv() returns Err
|
||||||
// after the Flush sentinel, signalling clean shutdown.
|
// after the Flush sentinel, signalling clean shutdown.
|
||||||
let sender = {
|
let sender = {
|
||||||
let mut g = GLOBAL.lock().unwrap();
|
let mut g = match GLOBAL.lock() {
|
||||||
|
Ok(g) => g,
|
||||||
|
Err(e) => panic!("smarm: trace lock poisoned (core corrupt): {e}"),
|
||||||
|
};
|
||||||
g.take().map(|g| g.sender)
|
g.take().map(|g| g.sender)
|
||||||
};
|
};
|
||||||
if let Some(tx) = sender {
|
if let Some(tx) = sender {
|
||||||
@@ -148,33 +183,38 @@ mod inner {
|
|||||||
// Hot path
|
// Hot path
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
|
|
||||||
|
#[inline(never)]
|
||||||
pub fn record(event: Event) {
|
pub fn record(event: Event) {
|
||||||
|
crate::context::tls_fence();
|
||||||
// Disable preemption for the entire duration of record(). Any
|
// Disable preemption for the entire duration of record(). Any
|
||||||
// allocation here (mutex internals, channel send, lazy init) would
|
// allocation here (mutex internals, channel send, lazy init) would
|
||||||
// trigger PreemptingAllocator -> maybe_preempt -> switch_to_scheduler,
|
// trigger PreemptingAllocator -> maybe_preempt -> switch_to_scheduler,
|
||||||
// which would try to re-acquire inner.shared (already held at many
|
// which would try to re-acquire inner.shared (already held at many
|
||||||
// te!() call sites) -> deadlock. Guard at the very top, before any
|
// te!() call sites) -> deadlock. Guard at the very top, before any
|
||||||
// allocation-capable call.
|
// allocation-capable call.
|
||||||
let was_enabled = crate::preempt::PREEMPTION_ENABLED
|
let was_enabled = crate::preempt::preemption_swap(false);
|
||||||
.with(|e| { let v = e.get(); e.set(false); v });
|
|
||||||
|
|
||||||
LOCAL_STATE.with(|cell| {
|
LOCAL_STATE.with(|cell| {
|
||||||
let mut opt = cell.borrow_mut();
|
let mut opt = cell.borrow_mut();
|
||||||
// Lazily initialise: one mutex hit per thread, ever.
|
// Lazily initialise: one mutex hit per thread, ever.
|
||||||
if opt.is_none() {
|
if opt.is_none() {
|
||||||
if let Some(g) = GLOBAL.lock().unwrap().as_ref() {
|
let guard = match GLOBAL.lock() {
|
||||||
|
Ok(g) => g,
|
||||||
|
Err(e) => panic!("smarm: trace lock poisoned (core corrupt): {e}"),
|
||||||
|
};
|
||||||
|
if let Some(g) = guard.as_ref() {
|
||||||
let tx = g.sender.clone();
|
let tx = g.sender.clone();
|
||||||
*opt = Some(LocalState { tx, start: g.start });
|
*opt = Some(LocalState { tx, start: g.start });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let Some(ls) = opt.as_ref() {
|
if let Some(ls) = opt.as_ref() {
|
||||||
let nanos = ls.start.elapsed().as_nanos() as u64;
|
let nanos = ls.start.elapsed().as_nanos() as u64;
|
||||||
let tid = os_tid();
|
let tid = os_tid();
|
||||||
let _ = ls.tx.send(Msg::Event(Record { nanos, tid, event }));
|
let _ = ls.tx.send(Msg::Event(Record { nanos, tid, event }));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
crate::preempt::PREEMPTION_ENABLED.with(|e| e.set(was_enabled));
|
crate::preempt::preemption_swap(was_enabled);
|
||||||
}
|
}
|
||||||
|
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
@@ -184,7 +224,10 @@ mod inner {
|
|||||||
fn drain_thread(rx: mpsc::Receiver<Msg>, path: &str) {
|
fn drain_thread(rx: mpsc::Receiver<Msg>, path: &str) {
|
||||||
let f = match std::fs::File::create(path) {
|
let f = match std::fs::File::create(path) {
|
||||||
Ok(f) => f,
|
Ok(f) => f,
|
||||||
Err(e) => { eprintln!("[smarm-trace] create failed: {}", e); return; }
|
Err(e) => {
|
||||||
|
eprintln!("[smarm-trace] create failed: {}", e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
let mut w = std::io::BufWriter::new(f);
|
let mut w = std::io::BufWriter::new(f);
|
||||||
let _ = writeln!(w, "{{\"traceEvents\":[");
|
let _ = writeln!(w, "{{\"traceEvents\":[");
|
||||||
@@ -197,7 +240,9 @@ mod inner {
|
|||||||
Ok(Msg::Event(r)) => {
|
Ok(Msg::Event(r)) => {
|
||||||
let (name, actor_idx) = chrome_fields(&r.event);
|
let (name, actor_idx) = chrome_fields(&r.event);
|
||||||
let ts_us = r.nanos as f64 / 1000.0;
|
let ts_us = r.nanos as f64 / 1000.0;
|
||||||
if !first { let _ = w.write_all(b",\n"); }
|
if !first {
|
||||||
|
let _ = w.write_all(b",\n");
|
||||||
|
}
|
||||||
first = false;
|
first = false;
|
||||||
let _ = write!(w,
|
let _ = write!(w,
|
||||||
"{{\"ph\":\"i\",\"ts\":{:.3},\"pid\":{},\"tid\":{},\"name\":{:?},\"s\":\"g\"}}",
|
"{{\"ph\":\"i\",\"ts\":{:.3},\"pid\":{},\"tid\":{},\"name\":{:?},\"s\":\"g\"}}",
|
||||||
@@ -221,27 +266,40 @@ mod inner {
|
|||||||
|
|
||||||
fn chrome_fields(ev: &Event) -> (String, u32) {
|
fn chrome_fields(ev: &Event) -> (String, u32) {
|
||||||
match ev {
|
match ev {
|
||||||
Event::Spawn { parent, child } =>
|
Event::Spawn { parent, child } => {
|
||||||
(format!("spawn c={}", child.index()), parent.index()),
|
(format!("spawn c={}", child.index()), parent.index())
|
||||||
Event::Resume(p) => ("resume".into(), p.index()),
|
}
|
||||||
Event::Yield(p) => ("yield".into(), p.index()),
|
Event::Resume(p) => ("resume".into(), p.index()),
|
||||||
Event::Park(p) => ("park".into(), p.index()),
|
Event::Yield(p) => ("yield".into(), p.index()),
|
||||||
Event::Done(p) => ("done".into(), p.index()),
|
Event::Park(p) => ("park".into(), p.index()),
|
||||||
Event::UnparkDirect(p) => ("unpark_direct".into(), p.index()),
|
Event::Done(p) => ("done".into(), p.index()),
|
||||||
Event::UnparkDeferred(p) => ("unpark_deferred".into(), p.index()),
|
Event::RootSweep { target, trapping } => (
|
||||||
|
format!(
|
||||||
|
"root_sweep {}",
|
||||||
|
if *trapping { "shutdown" } else { "stopped" }
|
||||||
|
),
|
||||||
|
target.index(),
|
||||||
|
),
|
||||||
|
Event::UnparkDirect(p) => ("unpark_direct".into(), p.index()),
|
||||||
|
Event::UnparkDeferred(p) => ("unpark_deferred".into(), p.index()),
|
||||||
Event::UnparkFlagConsumed(p) => ("unpark_flag_consumed".into(), p.index()),
|
Event::UnparkFlagConsumed(p) => ("unpark_flag_consumed".into(), p.index()),
|
||||||
Event::Send { sender, receiver } => (
|
Event::Send { sender, receiver } => (
|
||||||
format!("send rx={}", receiver
|
format!(
|
||||||
.map(|p| p.index().to_string())
|
"send rx={}",
|
||||||
.unwrap_or_else(|| "none".into())),
|
receiver
|
||||||
|
.map(|p| p.index().to_string())
|
||||||
|
.unwrap_or_else(|| "none".into())
|
||||||
|
),
|
||||||
sender.index(),
|
sender.index(),
|
||||||
),
|
),
|
||||||
Event::RecvPark(p) => ("recv_park".into(), p.index()),
|
Event::RecvPark(p) => ("recv_park".into(), p.index()),
|
||||||
Event::RecvWake(p) => ("recv_wake".into(), p.index()),
|
Event::RecvWake(p) => ("recv_wake".into(), p.index()),
|
||||||
Event::Enqueue(p) => ("enqueue".into(), p.index()),
|
Event::Enqueue(p) => ("enqueue".into(), p.index()),
|
||||||
Event::Dequeue(p) => ("dequeue".into(), p.index()),
|
Event::Dequeue(p) => ("dequeue".into(), p.index()),
|
||||||
Event::SlotPush(p) => ("slot_push".into(), p.index()),
|
Event::SlotPush(p) => ("slot_push".into(), p.index()),
|
||||||
Event::SlotPop(p) => ("slot_pop".into(), p.index()),
|
Event::SlotPop(p) => ("slot_pop".into(), p.index()),
|
||||||
|
Event::ClusterInbound(v) => (format!("cluster_inbound {v}"), 0),
|
||||||
|
Event::ClusterDial(v) => (format!("cluster_dial {v}"), 0),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+24
-6
@@ -49,8 +49,14 @@ fn looping_actor_on_check_is_stopped() {
|
|||||||
}
|
}
|
||||||
let _ = h.join();
|
let _ = h.join();
|
||||||
});
|
});
|
||||||
assert!(saw_stopped.load(Ordering::SeqCst), "expected DownReason::Stopped");
|
assert!(
|
||||||
assert!(dropped.load(Ordering::SeqCst), "Drop guard must run during the cancellation unwind");
|
saw_stopped.load(Ordering::SeqCst),
|
||||||
|
"expected DownReason::Stopped"
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
dropped.load(Ordering::SeqCst),
|
||||||
|
"Drop guard must run during the cancellation unwind"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -79,8 +85,14 @@ fn parked_on_recv_actor_is_stopped() {
|
|||||||
}
|
}
|
||||||
let _ = h.join();
|
let _ = h.join();
|
||||||
});
|
});
|
||||||
assert!(saw_stopped.load(Ordering::SeqCst), "expected DownReason::Stopped");
|
assert!(
|
||||||
assert!(dropped.load(Ordering::SeqCst), "Drop guard must run on cancellation of a parked actor");
|
saw_stopped.load(Ordering::SeqCst),
|
||||||
|
"expected DownReason::Stopped"
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
dropped.load(Ordering::SeqCst),
|
||||||
|
"Drop guard must run on cancellation of a parked actor"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -185,6 +197,12 @@ fn stop_flagged_while_queued_lands_at_first_park() {
|
|||||||
.recv_timeout(Duration::from_secs(10))
|
.recv_timeout(Duration::from_secs(10))
|
||||||
.expect("runtime deadlocked: stop against a QUEUED actor was lost at its first park");
|
.expect("runtime deadlocked: stop against a QUEUED actor was lost at its first park");
|
||||||
|
|
||||||
assert!(saw_stopped.load(Ordering::SeqCst), "expected DownReason::Stopped");
|
assert!(
|
||||||
assert!(dropped.load(Ordering::SeqCst), "Drop guard must run during the cancellation unwind");
|
saw_stopped.load(Ordering::SeqCst),
|
||||||
|
"expected DownReason::Stopped"
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
dropped.load(Ordering::SeqCst),
|
||||||
|
"Drop guard must run during the cancellation unwind"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
+1039
File diff suppressed because it is too large
Load Diff
+22
-12
@@ -138,10 +138,12 @@ fn channel_ops_interleaved_with_monitor_churn_multi_thread() {
|
|||||||
let tx = tx.clone();
|
let tx = tx.clone();
|
||||||
handles.push(spawn(move || {
|
handles.push(spawn(move || {
|
||||||
// Short-lived target whose death fires the monitor below.
|
// Short-lived target whose death fires the monitor below.
|
||||||
let t = spawn(move || {
|
// spawn_monitor: registered before publish, so the Down is
|
||||||
|
// the finalize-sent Exit this test is about, never NoProc
|
||||||
|
// (spawn-then-monitor raced ~8% at 4 threads).
|
||||||
|
let (t, m) = smarm::spawn_monitor(move || {
|
||||||
tx.send(i).unwrap();
|
tx.send(i).unwrap();
|
||||||
});
|
});
|
||||||
let m = smarm::monitor(t.pid());
|
|
||||||
t.join().unwrap();
|
t.join().unwrap();
|
||||||
// Down delivery exercises send-from-finalize.
|
// Down delivery exercises send-from-finalize.
|
||||||
let d = m.rx.recv().unwrap();
|
let d = m.rx.recv().unwrap();
|
||||||
@@ -154,7 +156,10 @@ fn channel_ops_interleaved_with_monitor_churn_multi_thread() {
|
|||||||
}
|
}
|
||||||
consumer.join().unwrap();
|
consumer.join().unwrap();
|
||||||
});
|
});
|
||||||
assert_eq!(total.load(std::sync::atomic::Ordering::Relaxed), (0..32).sum::<i64>());
|
assert_eq!(
|
||||||
|
total.load(std::sync::atomic::Ordering::Relaxed),
|
||||||
|
(0..32).sum::<i64>()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
@@ -220,7 +225,10 @@ fn recv_timeout_reports_disconnected_on_close() {
|
|||||||
fn recv_timeout_zero_duration_is_a_bounded_poll() {
|
fn recv_timeout_zero_duration_is_a_bounded_poll() {
|
||||||
run(|| {
|
run(|| {
|
||||||
let (_tx, rx) = channel::<i64>();
|
let (_tx, rx) = channel::<i64>();
|
||||||
assert_eq!(rx.recv_timeout(Duration::ZERO), Err(RecvTimeoutError::Timeout));
|
assert_eq!(
|
||||||
|
rx.recv_timeout(Duration::ZERO),
|
||||||
|
Err(RecvTimeoutError::Timeout)
|
||||||
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -262,15 +270,17 @@ fn recv_timeout_many_waiters_multi_thread() {
|
|||||||
let (tx, rx) = channel::<i64>();
|
let (tx, rx) = channel::<i64>();
|
||||||
let got = got2.clone();
|
let got = got2.clone();
|
||||||
let timed_out = timed_out2.clone();
|
let timed_out = timed_out2.clone();
|
||||||
handles.push(spawn(move || match rx.recv_timeout(Duration::from_millis(100)) {
|
handles.push(spawn(move || {
|
||||||
Ok(v) => {
|
match rx.recv_timeout(Duration::from_millis(100)) {
|
||||||
assert_eq!(v, i);
|
Ok(v) => {
|
||||||
got.fetch_add(1, Ordering::Relaxed);
|
assert_eq!(v, i);
|
||||||
|
got.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
Err(RecvTimeoutError::Timeout) => {
|
||||||
|
timed_out.fetch_add(1, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
Err(e) => panic!("unexpected: {e}"),
|
||||||
}
|
}
|
||||||
Err(RecvTimeoutError::Timeout) => {
|
|
||||||
timed_out.fetch_add(1, Ordering::Relaxed);
|
|
||||||
}
|
|
||||||
Err(e) => panic!("unexpected: {e}"),
|
|
||||||
}));
|
}));
|
||||||
if i % 2 == 0 {
|
if i % 2 == 0 {
|
||||||
handles.push(spawn(move || {
|
handles.push(spawn(move || {
|
||||||
|
|||||||
@@ -0,0 +1,117 @@
|
|||||||
|
//! RFC 010 c6a — connection-actor lifecycle against the manager table.
|
||||||
|
//!
|
||||||
|
//! The handshake is bypassed here (c6b wires it): each connection is
|
||||||
|
//! constructed already-established over a real localhost TCP pair, handed a
|
||||||
|
//! fabricated `Peer`, and spawned. `spawn_established` registers it with the
|
||||||
|
//! manager, which takes its handle and monitors it, so the table reflects the
|
||||||
|
//! connection while it lives and reaps it on any exit path. This proves three
|
||||||
|
//! things at once: a live connection shows up, a commanded `Disconnect`
|
||||||
|
//! removes exactly that one, and a peer close (EOF, no command) removes the
|
||||||
|
//! other.
|
||||||
|
//!
|
||||||
|
//! TCP parks the calling actor, so everything runs inside `smarm::run`; the
|
||||||
|
//! single-threaded runtime is fine because every wait is a cooperative fd park.
|
||||||
|
#![cfg(feature = "cluster")]
|
||||||
|
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use smarm::cluster::envelope::NodeMeta;
|
||||||
|
use smarm::cluster::handshake::Peer;
|
||||||
|
use smarm::cluster::manager::{Call, Manager, Reply, MANAGER};
|
||||||
|
use smarm::cluster::spawn_established;
|
||||||
|
use smarm::cluster::transport::tcp::TcpTransport;
|
||||||
|
use smarm::cluster::transport::{Conn, FramedConn, Transport};
|
||||||
|
use smarm::cluster::Timing;
|
||||||
|
use smarm::gen_server::{self, GenServerBuilder};
|
||||||
|
use smarm::pg::Incarnation;
|
||||||
|
use smarm::{run, sleep};
|
||||||
|
|
||||||
|
/// A fabricated post-handshake peer identity. Only `node_name` matters to the
|
||||||
|
/// manager table; the rest is filler until c7 consumes it.
|
||||||
|
fn peer(name: &str) -> Peer {
|
||||||
|
Peer {
|
||||||
|
node_name: name.to_string(),
|
||||||
|
incarnation: Incarnation::new(1),
|
||||||
|
meta: NodeMeta {
|
||||||
|
role: "test".to_string(),
|
||||||
|
region: "test".to_string(),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// One established transport pair over localhost. Relies on TCP backlog so the
|
||||||
|
/// sequential dial-then-accept needs no concurrent acceptor (same assumption as
|
||||||
|
/// the c3 conformance suite).
|
||||||
|
fn pair(t: &dyn Transport) -> (Box<dyn Conn>, Box<dyn Conn>) {
|
||||||
|
let mut l = t.listen("127.0.0.1:0").unwrap();
|
||||||
|
let a = t.dial(&l.local_addr()).unwrap();
|
||||||
|
let b = l.accept().unwrap();
|
||||||
|
(a, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Poll the manager until its peer set matches `expected` (sorted), or fail.
|
||||||
|
/// The bound is generous against a sub-millisecond real cost.
|
||||||
|
fn wait_peers(expected: &[&str]) {
|
||||||
|
let want: Vec<String> = expected.iter().map(|s| s.to_string()).collect();
|
||||||
|
for _ in 0..2000 {
|
||||||
|
if let Ok(Reply::Peers(got)) = gen_server::call(MANAGER, Call::Peers) {
|
||||||
|
if got == want {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sleep(Duration::from_millis(1));
|
||||||
|
}
|
||||||
|
let got = gen_server::call(MANAGER, Call::Peers);
|
||||||
|
panic!("timed out waiting for peers == {want:?}; last = {got:?}");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn connection_up_commanded_shutdown_and_eof_all_reflected_in_table() {
|
||||||
|
run(|| {
|
||||||
|
// The manager, started plainly and reachable at its well-known name.
|
||||||
|
// (The supervised subtree in `cluster::start` is permanent by design;
|
||||||
|
// a plainly-started manager lets this test terminate cleanly.)
|
||||||
|
let mgr = GenServerBuilder::new(Manager::new())
|
||||||
|
.named(MANAGER)
|
||||||
|
.start()
|
||||||
|
.expect("manager name is free");
|
||||||
|
|
||||||
|
let t = TcpTransport;
|
||||||
|
let (a1, b1) = pair(&t);
|
||||||
|
let (a2, b2) = pair(&t);
|
||||||
|
|
||||||
|
// Manage the `a` ends as peers node-b and node-c; keep the `b` far ends
|
||||||
|
// open so neither socket is closed from the far side yet.
|
||||||
|
spawn_established(FramedConn::new(a1), peer("node-b"), Timing::default())
|
||||||
|
.expect("node-b registers");
|
||||||
|
spawn_established(FramedConn::new(a2), peer("node-c"), Timing::default())
|
||||||
|
.expect("node-c registers");
|
||||||
|
|
||||||
|
// Up: both connections register and the table shows them.
|
||||||
|
wait_peers(&["node-b", "node-c"]);
|
||||||
|
|
||||||
|
// A commanded disconnect reaps exactly its own connection: the
|
||||||
|
// manager drops that entry's handle and the actor stops.
|
||||||
|
assert!(matches!(
|
||||||
|
gen_server::call(
|
||||||
|
MANAGER,
|
||||||
|
Call::Disconnect {
|
||||||
|
name: "node-b".to_string()
|
||||||
|
}
|
||||||
|
),
|
||||||
|
Ok(Reply::Disconnected)
|
||||||
|
));
|
||||||
|
wait_peers(&["node-c"]);
|
||||||
|
|
||||||
|
// A peer close (EOF) reaps the other with no command at all.
|
||||||
|
drop(b2);
|
||||||
|
wait_peers(&[]);
|
||||||
|
|
||||||
|
// node-b's far end stayed open until here, so its removal above was the
|
||||||
|
// disconnect command and not an EOF.
|
||||||
|
drop(b1);
|
||||||
|
|
||||||
|
// All connection actors have exited; stop the manager so `run` returns.
|
||||||
|
mgr.shutdown();
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -0,0 +1,180 @@
|
|||||||
|
//! RFC 010 c6c — heartbeat send + fixed-timeout liveness + teardown.
|
||||||
|
//!
|
||||||
|
//! Each case runs one real connection actor over an in-process localhost TCP
|
||||||
|
//! pair, with the far end held as a raw `FramedConn` (no actor) so the test
|
||||||
|
//! controls exactly what — if anything — the peer says. That gives the three
|
||||||
|
//! protocol-visible facts direct handles: heartbeats appear on the wire
|
||||||
|
//! unprompted; a mute peer is torn down (and reaped from the manager table)
|
||||||
|
//! once `LIVENESS_TIMEOUT` empties; and a peer that does nothing but send
|
||||||
|
//! heartbeats keeps the connection alive past that same window.
|
||||||
|
//!
|
||||||
|
//! Loopback has no fd and cannot drive liveness (documented on the actor),
|
||||||
|
//! so everything here is TCP. TCP parks the calling actor, so everything
|
||||||
|
//! runs inside `smarm::run`.
|
||||||
|
#![cfg(feature = "cluster")]
|
||||||
|
|
||||||
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
|
use smarm::cluster::conn::{HEARTBEAT_INTERVAL, LIVENESS_TIMEOUT};
|
||||||
|
use smarm::cluster::envelope::{Frame, NodeMeta};
|
||||||
|
use smarm::cluster::handshake::Peer;
|
||||||
|
use smarm::cluster::manager::{Call, Manager, Reply, MANAGER};
|
||||||
|
use smarm::cluster::spawn_established;
|
||||||
|
use smarm::cluster::transport::tcp::TcpTransport;
|
||||||
|
use smarm::cluster::transport::{Conn, FramedConn, Transport};
|
||||||
|
use smarm::cluster::Timing;
|
||||||
|
use smarm::gen_server::{self, GenServerBuilder};
|
||||||
|
use smarm::pg::Incarnation;
|
||||||
|
use smarm::{run, sleep, spawn};
|
||||||
|
|
||||||
|
/// A fabricated post-handshake peer identity (same shape as the c6a suite).
|
||||||
|
fn peer(name: &str) -> Peer {
|
||||||
|
Peer {
|
||||||
|
node_name: name.to_string(),
|
||||||
|
incarnation: Incarnation::new(1),
|
||||||
|
meta: NodeMeta {
|
||||||
|
role: "test".to_string(),
|
||||||
|
region: "test".to_string(),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// One established transport pair over localhost (TCP backlog covers the
|
||||||
|
/// sequential dial-then-accept, as in the c3 conformance suite).
|
||||||
|
fn pair(t: &dyn Transport) -> (Box<dyn Conn>, Box<dyn Conn>) {
|
||||||
|
let mut l = t.listen("127.0.0.1:0").unwrap();
|
||||||
|
let a = t.dial(&l.local_addr()).unwrap();
|
||||||
|
let b = l.accept().unwrap();
|
||||||
|
(a, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn peers() -> Vec<String> {
|
||||||
|
match gen_server::call(MANAGER, Call::Peers) {
|
||||||
|
Ok(Reply::Peers(p)) => p,
|
||||||
|
other => panic!("manager unreachable: {other:?}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Poll until the manager's peer set matches `expected` (sorted) or `budget`
|
||||||
|
/// runs out.
|
||||||
|
fn wait_peers(expected: &[&str], budget: Duration) {
|
||||||
|
let want: Vec<String> = expected.iter().map(|s| s.to_string()).collect();
|
||||||
|
let deadline = Instant::now() + budget;
|
||||||
|
while Instant::now() < deadline {
|
||||||
|
if peers() == want {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
sleep(Duration::from_millis(10));
|
||||||
|
}
|
||||||
|
panic!(
|
||||||
|
"timed out waiting for peers == {want:?}; last = {:?}",
|
||||||
|
peers()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The actor emits heartbeats unprompted: the raw far end, saying nothing,
|
||||||
|
/// sees a `Frame::Heartbeat` well within one interval (the first goes out at
|
||||||
|
/// spawn).
|
||||||
|
#[test]
|
||||||
|
fn heartbeats_are_sent_unprompted() {
|
||||||
|
run(|| {
|
||||||
|
let mgr = GenServerBuilder::new(Manager::new())
|
||||||
|
.named(MANAGER)
|
||||||
|
.start()
|
||||||
|
.expect("manager name is free");
|
||||||
|
|
||||||
|
let (a, b) = pair(&TcpTransport);
|
||||||
|
spawn_established(FramedConn::new(a), peer("hb-send"), Timing::default())
|
||||||
|
.expect("register");
|
||||||
|
let mut far = FramedConn::new(b);
|
||||||
|
|
||||||
|
let frame = far
|
||||||
|
.recv_deadline(Instant::now() + HEARTBEAT_INTERVAL)
|
||||||
|
.expect("a heartbeat before one interval elapses");
|
||||||
|
assert_eq!(frame, Some(Frame::Heartbeat));
|
||||||
|
|
||||||
|
// Teardown: closing the far end is an EOF at the actor.
|
||||||
|
far.close();
|
||||||
|
wait_peers(&[], Duration::from_secs(2));
|
||||||
|
mgr.shutdown();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A mute peer is dead: no inbound frame for `LIVENESS_TIMEOUT` tears the
|
||||||
|
/// connection down and the manager's monitor reaps the table entry. The
|
||||||
|
/// entry is still present well inside the window — the teardown is the
|
||||||
|
/// timer, not an accident of setup.
|
||||||
|
#[test]
|
||||||
|
fn mute_peer_is_torn_down_after_liveness_timeout() {
|
||||||
|
run(|| {
|
||||||
|
let mgr = GenServerBuilder::new(Manager::new())
|
||||||
|
.named(MANAGER)
|
||||||
|
.start()
|
||||||
|
.expect("manager name is free");
|
||||||
|
|
||||||
|
let (a, b) = pair(&TcpTransport);
|
||||||
|
spawn_established(FramedConn::new(a), peer("mute"), Timing::default()).expect("register");
|
||||||
|
// Held open and silent: no frames, no EOF. (Unread inbound
|
||||||
|
// heartbeats sit in kernel buffers; they are 5 bytes each.)
|
||||||
|
let _far = FramedConn::new(b);
|
||||||
|
|
||||||
|
// Well inside the window the connection is still up.
|
||||||
|
sleep(LIVENESS_TIMEOUT / 2);
|
||||||
|
assert_eq!(peers(), vec!["mute".to_string()], "torn down too early");
|
||||||
|
|
||||||
|
// ...and once the window empties it is gone. Generous budget over
|
||||||
|
// the remaining half-window.
|
||||||
|
wait_peers(&[], LIVENESS_TIMEOUT);
|
||||||
|
mgr.shutdown();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Heartbeats alone keep a connection alive past `LIVENESS_TIMEOUT`: a far
|
||||||
|
/// end that sends `Frame::Heartbeat` at the interval (and nothing else)
|
||||||
|
/// holds the entry; when it goes quiet, liveness finally fires.
|
||||||
|
#[test]
|
||||||
|
fn heartbeats_keep_the_connection_alive() {
|
||||||
|
run(|| {
|
||||||
|
let mgr = GenServerBuilder::new(Manager::new())
|
||||||
|
.named(MANAGER)
|
||||||
|
.start()
|
||||||
|
.expect("manager name is free");
|
||||||
|
|
||||||
|
let (a, b) = pair(&TcpTransport);
|
||||||
|
spawn_established(FramedConn::new(a), peer("kept"), Timing::default()).expect("register");
|
||||||
|
|
||||||
|
// The far heartbeat pump: interval-paced sends until told to stop,
|
||||||
|
// then holds the socket open, silent, so the eventual teardown is
|
||||||
|
// liveness — not EOF.
|
||||||
|
let (ctl_tx, ctl_rx) = smarm::channel::channel::<()>();
|
||||||
|
spawn(move || {
|
||||||
|
let mut far = FramedConn::new(b);
|
||||||
|
// Phase 1: heartbeat at the interval until the first signal.
|
||||||
|
while matches!(ctl_rx.try_recv(), Ok(None)) {
|
||||||
|
far.send(&Frame::Heartbeat).expect("far send");
|
||||||
|
sleep(HEARTBEAT_INTERVAL);
|
||||||
|
}
|
||||||
|
// Phase 2: silent but with the socket held open — dropping
|
||||||
|
// `far` here would EOF the actor and mask the liveness path.
|
||||||
|
// Exits when the test's closure ends and drops `ctl_tx` (an
|
||||||
|
// eternal park would stop `run` from ever returning).
|
||||||
|
while matches!(ctl_rx.try_recv(), Ok(None)) {
|
||||||
|
sleep(Duration::from_millis(20));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Past the liveness window with margin: still up.
|
||||||
|
sleep(LIVENESS_TIMEOUT + LIVENESS_TIMEOUT / 2);
|
||||||
|
assert_eq!(
|
||||||
|
peers(),
|
||||||
|
vec!["kept".to_string()],
|
||||||
|
"liveness fired despite heartbeats"
|
||||||
|
);
|
||||||
|
|
||||||
|
// Silence the pump; liveness now empties and the entry goes.
|
||||||
|
ctl_tx.send(()).expect("pump alive");
|
||||||
|
wait_peers(&[], LIVENESS_TIMEOUT * 2);
|
||||||
|
mgr.shutdown();
|
||||||
|
// `ctl_tx` drops here, releasing the pump's phase-2 wait.
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -0,0 +1,482 @@
|
|||||||
|
//! RFC 010 c6b — the handshake on the accept/connect path.
|
||||||
|
//!
|
||||||
|
//! Path-level tests drive [`dial_handshake`]/[`accept_handshake`] over the
|
||||||
|
//! loopback transport on plain threads (its intended use — synchronous, no
|
||||||
|
//! runtime). Integration tests run the manager-backed [`dial`] and
|
||||||
|
//! [`spawn_acceptor`] over real localhost TCP inside `smarm::run`, and the
|
||||||
|
//! two-node case as subprocesses via the c4 harness. Flake budget: see
|
||||||
|
//! tests/common/mod.rs.
|
||||||
|
#![cfg(feature = "cluster")]
|
||||||
|
|
||||||
|
mod common;
|
||||||
|
|
||||||
|
use std::sync::mpsc;
|
||||||
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
|
use common::{maybe_child, spawn_node, WAIT};
|
||||||
|
use smarm::cluster::connect::{
|
||||||
|
accept_handshake, dial, dial_handshake, spawn_acceptor, DialError, HandshakeError,
|
||||||
|
HANDSHAKE_TIMEOUT,
|
||||||
|
};
|
||||||
|
use smarm::cluster::envelope::{Frame, NodeMeta, RejectReason};
|
||||||
|
use smarm::cluster::handshake::{Local, PeerStanding};
|
||||||
|
use smarm::cluster::manager::{Call, Manager, Reply, MANAGER};
|
||||||
|
use smarm::cluster::transport::loopback::LoopbackTransport;
|
||||||
|
use smarm::cluster::transport::tcp::TcpTransport;
|
||||||
|
use smarm::cluster::transport::{FramedConn, Transport};
|
||||||
|
use smarm::cluster::Timing;
|
||||||
|
use smarm::gen_server::{self, GenServerBuilder};
|
||||||
|
use smarm::pg::Incarnation;
|
||||||
|
use smarm::{run, sleep};
|
||||||
|
|
||||||
|
const ROLES: &[(&str, fn())] = &[
|
||||||
|
("hs_listener", role_hs_listener),
|
||||||
|
("hs_dialer", role_hs_dialer),
|
||||||
|
];
|
||||||
|
|
||||||
|
const HASH: u64 = 0xC6B0_C6B0_C6B0_C6B0;
|
||||||
|
|
||||||
|
fn local(name: &str) -> Local {
|
||||||
|
Local {
|
||||||
|
node_name: name.into(),
|
||||||
|
incarnation: Incarnation::new(3),
|
||||||
|
build_hash: HASH,
|
||||||
|
meta: NodeMeta {
|
||||||
|
role: "test".into(),
|
||||||
|
region: "test".into(),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A loopback conn pair as `FramedConn`s, ready for a threaded handshake.
|
||||||
|
fn loopback_pair() -> (FramedConn, FramedConn) {
|
||||||
|
let t = LoopbackTransport::default();
|
||||||
|
let mut l = t.listen("hs").unwrap();
|
||||||
|
let dialer = FramedConn::new(t.dial("hs").unwrap());
|
||||||
|
let accepted = FramedConn::new(l.accept().unwrap());
|
||||||
|
(dialer, accepted)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Far-future deadline for loopback paths, where it cannot fire anyway.
|
||||||
|
fn no_deadline() -> Instant {
|
||||||
|
Instant::now() + Duration::from_secs(3600)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Park the node forever: it has announced everything the parent asserts on,
|
||||||
|
/// and must now hold its connection open until SIGKILLed.
|
||||||
|
fn park() -> ! {
|
||||||
|
loop {
|
||||||
|
sleep(Duration::from_secs(1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Cooperative bounded receive across the closure/actor boundary. A blocking
|
||||||
|
/// `std::mpsc` wait would park the OS thread and starve the single-threaded
|
||||||
|
/// scheduler, so every wait inside `run` polls with [`sleep`] instead.
|
||||||
|
fn poll_recv<T>(rx: &mpsc::Receiver<T>, what: &str) -> T {
|
||||||
|
let deadline = Instant::now() + WAIT;
|
||||||
|
loop {
|
||||||
|
match rx.try_recv() {
|
||||||
|
Ok(v) => return v,
|
||||||
|
Err(mpsc::TryRecvError::Empty) => {
|
||||||
|
assert!(Instant::now() < deadline, "timed out waiting for {what}");
|
||||||
|
sleep(Duration::from_millis(1));
|
||||||
|
}
|
||||||
|
Err(mpsc::TryRecvError::Disconnected) => panic!("channel closed waiting for {what}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Path level, over loopback on plain threads
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn loopback_happy_path_establishes_both_ends() {
|
||||||
|
maybe_child(ROLES);
|
||||||
|
let (mut dialer, mut accepted) = loopback_pair();
|
||||||
|
let responder = std::thread::spawn(move || {
|
||||||
|
accept_handshake(
|
||||||
|
&mut accepted,
|
||||||
|
local("node-b"),
|
||||||
|
|name| {
|
||||||
|
assert_eq!(name, "node-a");
|
||||||
|
PeerStanding::Free
|
||||||
|
},
|
||||||
|
no_deadline(),
|
||||||
|
)
|
||||||
|
});
|
||||||
|
let peer_of_dialer = dial_handshake(&mut dialer, &local("node-a"), no_deadline()).unwrap();
|
||||||
|
let peer_of_acceptor = responder.join().unwrap().unwrap();
|
||||||
|
assert_eq!(peer_of_dialer.node_name, "node-b");
|
||||||
|
assert_eq!(peer_of_acceptor.node_name, "node-a");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn loopback_hash_mismatch_rejected_with_frame_then_eof() {
|
||||||
|
maybe_child(ROLES);
|
||||||
|
let (mut dialer, mut accepted) = loopback_pair();
|
||||||
|
let mut wrong = local("node-b");
|
||||||
|
wrong.build_hash ^= 1;
|
||||||
|
let responder = std::thread::spawn(move || {
|
||||||
|
accept_handshake(&mut accepted, wrong, |_| PeerStanding::Free, no_deadline())
|
||||||
|
});
|
||||||
|
// The dial side receives the reject frame — the compatibility anchor.
|
||||||
|
match dial_handshake(&mut dialer, &local("node-a"), no_deadline()) {
|
||||||
|
Err(HandshakeError::Rejected(RejectReason::HashMismatch)) => {}
|
||||||
|
other => panic!("expected HashMismatch reject, got {other:?}"),
|
||||||
|
}
|
||||||
|
match responder.join().unwrap() {
|
||||||
|
Err(HandshakeError::Rejected(RejectReason::HashMismatch)) => {}
|
||||||
|
other => panic!("expected accept side to report the reject, got {other:?}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn loopback_tie_break_loser_closed_silently() {
|
||||||
|
maybe_child(ROLES);
|
||||||
|
// The inbound dial is from "node-z"; we are "node-a" with our own dial to
|
||||||
|
// node-z in flight. dial_wins("node-z", "node-a") is false, so the
|
||||||
|
// inbound loses: closed with no frame at all.
|
||||||
|
let (mut dialer, mut accepted) = loopback_pair();
|
||||||
|
let responder = std::thread::spawn(move || {
|
||||||
|
accept_handshake(
|
||||||
|
&mut accepted,
|
||||||
|
local("node-a"),
|
||||||
|
|_| PeerStanding::Dialing,
|
||||||
|
no_deadline(),
|
||||||
|
)
|
||||||
|
});
|
||||||
|
// Silent close: the dial side sees EOF, never a frame.
|
||||||
|
match dial_handshake(&mut dialer, &local("node-z"), no_deadline()) {
|
||||||
|
Err(HandshakeError::Closed) => {}
|
||||||
|
other => panic!("expected silent close (Closed), got {other:?}"),
|
||||||
|
}
|
||||||
|
match responder.join().unwrap() {
|
||||||
|
Err(HandshakeError::TieBreakLoss) => {}
|
||||||
|
other => panic!("expected TieBreakLoss on the accept side, got {other:?}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn loopback_read_ahead_past_hello_survives_into_established_conn() {
|
||||||
|
maybe_child(ROLES);
|
||||||
|
// The buffer trap, proven: the dialer coalesces Hello + Heartbeat before
|
||||||
|
// the responder's first read, so the Heartbeat lands in the shared
|
||||||
|
// FramedConn's decode buffer during the handshake. The dialer sends
|
||||||
|
// nothing afterwards — the post-handshake recv can only succeed if the
|
||||||
|
// read-ahead travelled with the FramedConn.
|
||||||
|
let (mut dialer, mut accepted) = loopback_pair();
|
||||||
|
let (_init, hello) = smarm::cluster::handshake::Initiator::new(&local("node-a"));
|
||||||
|
dialer.send(&hello).unwrap();
|
||||||
|
dialer.send(&Frame::Heartbeat).unwrap();
|
||||||
|
// Both frames are buffered before the responder reads at all.
|
||||||
|
let (tx, rx) = mpsc::channel();
|
||||||
|
std::thread::spawn(move || {
|
||||||
|
let peer = accept_handshake(
|
||||||
|
&mut accepted,
|
||||||
|
local("node-b"),
|
||||||
|
|_| PeerStanding::Free,
|
||||||
|
no_deadline(),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
let next = accepted.recv();
|
||||||
|
let _ = tx.send((peer, next));
|
||||||
|
});
|
||||||
|
// A bounded wait: if the Heartbeat were NOT carried in the buffer, the
|
||||||
|
// recv above would block forever (the dialer stays open and silent).
|
||||||
|
let (peer, next) = rx
|
||||||
|
.recv_timeout(Duration::from_secs(5))
|
||||||
|
.expect("read-ahead lost: post-handshake recv blocked");
|
||||||
|
assert_eq!(peer.node_name, "node-a");
|
||||||
|
match next {
|
||||||
|
Ok(Some(Frame::Heartbeat)) => {}
|
||||||
|
other => panic!("expected the read-ahead Heartbeat, got {other:?}"),
|
||||||
|
}
|
||||||
|
drop(dialer);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Deadline + manager integration, over TCP inside the runtime
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn tcp_silent_peer_times_out_on_the_accept_path() {
|
||||||
|
maybe_child(ROLES);
|
||||||
|
run(|| {
|
||||||
|
let t = TcpTransport;
|
||||||
|
let mut l = t.listen("127.0.0.1:0").unwrap();
|
||||||
|
// Connect and then say nothing at all.
|
||||||
|
let silent = t.dial(&l.local_addr()).unwrap();
|
||||||
|
let mut accepted = FramedConn::new(l.accept().unwrap());
|
||||||
|
let (tx, rx) = mpsc::channel();
|
||||||
|
smarm::spawn(move || {
|
||||||
|
let r = accept_handshake(
|
||||||
|
&mut accepted,
|
||||||
|
local("node-b"),
|
||||||
|
|_| PeerStanding::Free,
|
||||||
|
Instant::now() + Duration::from_millis(200),
|
||||||
|
);
|
||||||
|
let _ = tx.send(r);
|
||||||
|
});
|
||||||
|
match poll_recv(&rx, "accept-path outcome") {
|
||||||
|
Err(HandshakeError::TimedOut) => {}
|
||||||
|
other => panic!("expected TimedOut, got {other:?}"),
|
||||||
|
}
|
||||||
|
drop(silent);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Poll the manager until its peer set matches `expected` (sorted), or fail.
|
||||||
|
fn wait_peers(expected: &[&str]) {
|
||||||
|
let want: Vec<String> = expected.iter().map(|s| s.to_string()).collect();
|
||||||
|
for _ in 0..5000 {
|
||||||
|
if let Ok(Reply::Peers(got)) = gen_server::call(MANAGER, Call::Peers) {
|
||||||
|
if got == want {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sleep(Duration::from_millis(1));
|
||||||
|
}
|
||||||
|
let got = gen_server::call(MANAGER, Call::Peers);
|
||||||
|
panic!("timed out waiting for peers == {want:?}; last = {got:?}");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn tcp_duplicate_name_rejected_by_acceptor() {
|
||||||
|
maybe_child(ROLES);
|
||||||
|
run(|| {
|
||||||
|
let mgr = GenServerBuilder::new(Manager::new())
|
||||||
|
.named(MANAGER)
|
||||||
|
.start()
|
||||||
|
.expect("manager name is free");
|
||||||
|
let listener = TcpTransport.listen("127.0.0.1:0").unwrap();
|
||||||
|
let acceptor = spawn_acceptor(listener, local("node-b"), Timing::default());
|
||||||
|
let addr = acceptor.local_addr().to_string();
|
||||||
|
|
||||||
|
// First dial offering "dup-node": establishes and registers.
|
||||||
|
let mut first = FramedConn::new(TcpTransport.dial(&addr).unwrap());
|
||||||
|
let peer = dial_handshake(
|
||||||
|
&mut first,
|
||||||
|
&local("dup-node"),
|
||||||
|
Instant::now() + HANDSHAKE_TIMEOUT,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(peer.node_name, "node-b");
|
||||||
|
wait_peers(&["dup-node"]);
|
||||||
|
|
||||||
|
// Second dial offering the same name: deterministic NameTaken.
|
||||||
|
let mut second = FramedConn::new(TcpTransport.dial(&addr).unwrap());
|
||||||
|
match dial_handshake(
|
||||||
|
&mut second,
|
||||||
|
&local("dup-node"),
|
||||||
|
Instant::now() + HANDSHAKE_TIMEOUT,
|
||||||
|
) {
|
||||||
|
Err(HandshakeError::Rejected(RejectReason::NameTaken)) => {}
|
||||||
|
other => panic!("expected NameTaken, got {other:?}"),
|
||||||
|
}
|
||||||
|
// The established connection was untouched by the rejected one.
|
||||||
|
wait_peers(&["dup-node"]);
|
||||||
|
|
||||||
|
// Teardown: the acceptor owns no connections, so the established one
|
||||||
|
// is torn down through the table.
|
||||||
|
acceptor.shutdown();
|
||||||
|
assert!(matches!(
|
||||||
|
gen_server::call(
|
||||||
|
MANAGER,
|
||||||
|
Call::Disconnect {
|
||||||
|
name: "dup-node".to_string()
|
||||||
|
}
|
||||||
|
),
|
||||||
|
Ok(Reply::Disconnected)
|
||||||
|
));
|
||||||
|
wait_peers(&[]);
|
||||||
|
first.close();
|
||||||
|
mgr.shutdown();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn dial_intent_cleared_when_dialer_dies() {
|
||||||
|
maybe_child(ROLES);
|
||||||
|
run(|| {
|
||||||
|
let mgr = GenServerBuilder::new(Manager::new())
|
||||||
|
.named(MANAGER)
|
||||||
|
.start()
|
||||||
|
.expect("manager name is free");
|
||||||
|
let (begun_tx, begun_rx) = mpsc::channel();
|
||||||
|
let (go_tx, go_rx) = mpsc::channel::<()>();
|
||||||
|
smarm::spawn(move || {
|
||||||
|
let me = smarm::self_pid();
|
||||||
|
match gen_server::call(
|
||||||
|
MANAGER,
|
||||||
|
Call::DialBegin {
|
||||||
|
name: "ghost".into(),
|
||||||
|
pid: me,
|
||||||
|
},
|
||||||
|
) {
|
||||||
|
Ok(Reply::DialBegan(true)) => {}
|
||||||
|
other => panic!("DialBegin failed: {other:?}"),
|
||||||
|
}
|
||||||
|
let _ = begun_tx.send(());
|
||||||
|
let () = poll_recv(&go_rx, "go signal");
|
||||||
|
panic!("dialer dies mid-dial");
|
||||||
|
});
|
||||||
|
poll_recv(&begun_rx, "DialBegin done");
|
||||||
|
// While the dialer lives, the intent is visible.
|
||||||
|
match gen_server::call(
|
||||||
|
MANAGER,
|
||||||
|
Call::Standing {
|
||||||
|
peer_name: "ghost".into(),
|
||||||
|
},
|
||||||
|
) {
|
||||||
|
Ok(Reply::Standing(s)) => assert_eq!(s, PeerStanding::Dialing),
|
||||||
|
other => panic!("PeerStanding failed: {other:?}"),
|
||||||
|
}
|
||||||
|
// Kill it; the monitor must clear the intent without cooperation.
|
||||||
|
go_tx.send(()).unwrap();
|
||||||
|
let deadline = Instant::now() + WAIT;
|
||||||
|
loop {
|
||||||
|
match gen_server::call(
|
||||||
|
MANAGER,
|
||||||
|
Call::Standing {
|
||||||
|
peer_name: "ghost".into(),
|
||||||
|
},
|
||||||
|
) {
|
||||||
|
Ok(Reply::Standing(s)) if s != PeerStanding::Dialing => break,
|
||||||
|
_ if Instant::now() > deadline => {
|
||||||
|
panic!("dial intent not cleared after dialer death")
|
||||||
|
}
|
||||||
|
_ => sleep(Duration::from_millis(1)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mgr.shutdown();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Two nodes, two processes: the integrated dial against a real acceptor
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
/// Announce, then park forever. Neither role ever tears its connection
|
||||||
|
/// down: a table entry only exists while the *peer* holds its side open, so
|
||||||
|
/// any teardown here would retract the other node's observation before it
|
||||||
|
/// had made it. The parent reaps both with SIGKILL once it has both
|
||||||
|
/// announcements (see [`common::Node`]'s `Drop`).
|
||||||
|
fn role_hs_listener() {
|
||||||
|
run(|| {
|
||||||
|
let _mgr = GenServerBuilder::new(Manager::new())
|
||||||
|
.named(MANAGER)
|
||||||
|
.start()
|
||||||
|
.expect("manager name is free");
|
||||||
|
let listener = TcpTransport.listen("127.0.0.1:0").unwrap();
|
||||||
|
let acceptor = spawn_acceptor(listener, local("node-b"), Timing::default());
|
||||||
|
println!("LISTENING {}", acceptor.local_addr());
|
||||||
|
wait_peers(&["node-a"]);
|
||||||
|
println!("PEERS node-a");
|
||||||
|
park();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn role_hs_dialer() {
|
||||||
|
let addr = std::env::var("SMARM_PEER_ADDR").expect("SMARM_PEER_ADDR not set");
|
||||||
|
run(move || {
|
||||||
|
let _mgr = GenServerBuilder::new(Manager::new())
|
||||||
|
.named(MANAGER)
|
||||||
|
.start()
|
||||||
|
.expect("manager name is free");
|
||||||
|
let (tx, rx) = mpsc::channel();
|
||||||
|
smarm::spawn(move || {
|
||||||
|
let r = dial(
|
||||||
|
&TcpTransport,
|
||||||
|
&addr,
|
||||||
|
"node-b",
|
||||||
|
&local("node-a"),
|
||||||
|
Timing::default(),
|
||||||
|
);
|
||||||
|
let _ = tx.send(r);
|
||||||
|
});
|
||||||
|
if let Err(e) = poll_recv(&rx, "dial outcome") {
|
||||||
|
println!("DIAL failed: {e:?}");
|
||||||
|
std::process::exit(3);
|
||||||
|
}
|
||||||
|
wait_peers(&["node-b"]);
|
||||||
|
println!("PEERS node-b");
|
||||||
|
park();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn two_node_integrated_handshake_over_tcp() {
|
||||||
|
maybe_child(ROLES);
|
||||||
|
let mut listener = spawn_node("hs_listener", &[]);
|
||||||
|
let addr = listener.wait_listening();
|
||||||
|
let mut dialer = spawn_node("hs_dialer", &[("SMARM_PEER_ADDR", &addr)]);
|
||||||
|
// Each node reports its own table naming the other: a real dial against a
|
||||||
|
// real acceptor established in both directions. Both nodes then park —
|
||||||
|
// clean-exit behaviour is the c4 harness's own smoke test, and demanding
|
||||||
|
// it here would mean a teardown, which is exactly what cannot be ordered
|
||||||
|
// safely across two processes. Dropping the nodes SIGKILLs them.
|
||||||
|
dialer.wait_line("PEERS node-b", |l| l == "PEERS node-b");
|
||||||
|
listener.wait_line("PEERS node-a", |l| l == "PEERS node-a");
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Integrated-dial guardrails (no acceptor involved)
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn concurrent_dial_to_same_name_refused() {
|
||||||
|
maybe_child(ROLES);
|
||||||
|
run(|| {
|
||||||
|
let mgr = GenServerBuilder::new(Manager::new())
|
||||||
|
.named(MANAGER)
|
||||||
|
.start()
|
||||||
|
.expect("manager name is free");
|
||||||
|
let (begun_tx, begun_rx) = mpsc::channel();
|
||||||
|
let (go_tx, go_rx) = mpsc::channel::<()>();
|
||||||
|
// First dialer parks with the intent held (it never connects —
|
||||||
|
// 'holding the intent' is all this test needs from it).
|
||||||
|
smarm::spawn(move || {
|
||||||
|
let me = smarm::self_pid();
|
||||||
|
assert!(matches!(
|
||||||
|
gen_server::call(
|
||||||
|
MANAGER,
|
||||||
|
Call::DialBegin {
|
||||||
|
name: "node-x".into(),
|
||||||
|
pid: me,
|
||||||
|
}
|
||||||
|
),
|
||||||
|
Ok(Reply::DialBegan(true))
|
||||||
|
));
|
||||||
|
let _ = begun_tx.send(());
|
||||||
|
let () = poll_recv(&go_rx, "go signal");
|
||||||
|
let _ = gen_server::call(
|
||||||
|
MANAGER,
|
||||||
|
Call::DialEnd {
|
||||||
|
name: "node-x".into(),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
poll_recv(&begun_rx, "DialBegin done");
|
||||||
|
// Second integrated dial to the same name: refused before connecting
|
||||||
|
// (the addr is unroutable on purpose — it must never be dialed).
|
||||||
|
let (tx, rx) = mpsc::channel();
|
||||||
|
smarm::spawn(move || {
|
||||||
|
let r = dial(
|
||||||
|
&TcpTransport,
|
||||||
|
"127.0.0.1:1",
|
||||||
|
"node-x",
|
||||||
|
&local("node-a"),
|
||||||
|
Timing::default(),
|
||||||
|
);
|
||||||
|
let _ = tx.send(r);
|
||||||
|
});
|
||||||
|
match poll_recv(&rx, "second dial outcome") {
|
||||||
|
Err(DialError::AlreadyDialing) => {}
|
||||||
|
other => panic!("expected AlreadyDialing, got {other:?}"),
|
||||||
|
}
|
||||||
|
go_tx.send(()).unwrap();
|
||||||
|
mgr.shutdown();
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -0,0 +1,115 @@
|
|||||||
|
//! RFC 010 — a seed whose address answers as a *different* name
|
||||||
|
//! (`DialError::PeerNameMismatch`) is dialed once and then parked: the
|
||||||
|
//! connector must not redial it on backoff forever.
|
||||||
|
//!
|
||||||
|
//! Observed from the misdialed peer: each such dial establishes at the
|
||||||
|
//! responder (it registers, `node_up`), then the dialer closes on the name
|
||||||
|
//! check (`node_down`) — one membership blip per attempt. Cross-process: a
|
||||||
|
//! *server* named `server` subscribes and reports; a *client* on fast
|
||||||
|
//! timing (50–500ms backoff) seeds `("wrongname", server_addr)`. After the
|
||||||
|
//! first blip the server counts further `NodeUp`s across 2s — several
|
||||||
|
//! backoff periods. Parked ⇒ zero. Negative-control-verified: with the park
|
||||||
|
//! stubbed out the count is ≥ 1 in the same window.
|
||||||
|
#![cfg(feature = "cluster")]
|
||||||
|
|
||||||
|
mod common;
|
||||||
|
|
||||||
|
use common::{maybe_child, spawn_node};
|
||||||
|
use smarm::cluster::envelope::NodeMeta;
|
||||||
|
use smarm::cluster::membership::{subscribe, NodeEvent};
|
||||||
|
use smarm::cluster::{start, Config, StaticSeeds, Timing};
|
||||||
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
|
const ROLES: &[(&str, fn())] = &[("server", role_server), ("client", role_client)];
|
||||||
|
|
||||||
|
fn meta() -> NodeMeta {
|
||||||
|
NodeMeta {
|
||||||
|
role: "mismatch".into(),
|
||||||
|
region: "local".into(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn timing() -> Timing {
|
||||||
|
Timing {
|
||||||
|
initial_backoff: Duration::from_millis(50),
|
||||||
|
max_backoff: Duration::from_millis(500),
|
||||||
|
..Timing::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn role_server() {
|
||||||
|
smarm::run(|| {
|
||||||
|
let cluster = start(Config {
|
||||||
|
node_name: "server".into(),
|
||||||
|
meta: meta(),
|
||||||
|
listen_addr: std::env::var("SMARM_LISTEN_ADDR")
|
||||||
|
.unwrap_or_else(|_| "127.0.0.1:0".into()),
|
||||||
|
strategy: Box::new(StaticSeeds::new(Vec::<(String, String)>::new())),
|
||||||
|
timing: timing(),
|
||||||
|
})
|
||||||
|
.expect("binds");
|
||||||
|
let ev = subscribe().unwrap();
|
||||||
|
println!("LISTENING {}", cluster.local_addr());
|
||||||
|
// First blip: the misdialed client establishes, then closes on us.
|
||||||
|
loop {
|
||||||
|
match ev.rx.recv() {
|
||||||
|
Ok(NodeEvent::NodeDown(i)) if i.name == "client" => break,
|
||||||
|
Ok(_) => continue,
|
||||||
|
Err(_) => panic!("manager gone"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
println!("BLIP");
|
||||||
|
// Now count further NodeUps across several backoff periods.
|
||||||
|
let mut more = 0usize;
|
||||||
|
let t0 = Instant::now();
|
||||||
|
while t0.elapsed() < Duration::from_millis(2000) {
|
||||||
|
match ev.rx.try_recv() {
|
||||||
|
Ok(Some(NodeEvent::NodeUp(i))) if i.name == "client" => more += 1,
|
||||||
|
Ok(_) => {}
|
||||||
|
Err(_) => panic!("manager gone"),
|
||||||
|
}
|
||||||
|
smarm::sleep(Duration::from_millis(50));
|
||||||
|
}
|
||||||
|
println!("MORE {more}");
|
||||||
|
loop {
|
||||||
|
smarm::sleep(Duration::from_secs(3600));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn role_client() {
|
||||||
|
let server_addr = std::env::var("SMARM_SERVER_ADDR").expect("SMARM_SERVER_ADDR");
|
||||||
|
smarm::run(move || {
|
||||||
|
let _cluster = start(Config {
|
||||||
|
node_name: "client".into(),
|
||||||
|
meta: meta(),
|
||||||
|
listen_addr: "127.0.0.1:0".into(),
|
||||||
|
strategy: Box::new(StaticSeeds::new(vec![(
|
||||||
|
"wrongname".to_string(),
|
||||||
|
server_addr,
|
||||||
|
)])),
|
||||||
|
timing: timing(),
|
||||||
|
})
|
||||||
|
.expect("binds");
|
||||||
|
println!("CLIENT UP");
|
||||||
|
loop {
|
||||||
|
smarm::sleep(Duration::from_secs(3600));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn mismatched_seed_is_dialed_once_then_parked() {
|
||||||
|
maybe_child(ROLES);
|
||||||
|
let mut server = spawn_node("server", &[]);
|
||||||
|
let saddr = server.wait_listening();
|
||||||
|
let mut client = spawn_node("client", &[("SMARM_SERVER_ADDR", &saddr)]);
|
||||||
|
client.wait_line("CLIENT UP", |l| l == "CLIENT UP");
|
||||||
|
server.wait_line("BLIP", |l| l == "BLIP");
|
||||||
|
let line = server.wait_line("MORE", |l| l.starts_with("MORE "));
|
||||||
|
let more: usize = line.split_whitespace().nth(1).unwrap().parse().unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
more, 0,
|
||||||
|
"mismatched seed was redialed {more}× after being parked"
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,379 @@
|
|||||||
|
//! RFC 010 c13 — connection-loss synthesis.
|
||||||
|
//!
|
||||||
|
//! Local suite (`run()`, no network): the read-side backstop. A
|
||||||
|
//! `RemoteMonitor` whose channel closes without a notice reads as
|
||||||
|
//! `Disconnected` exactly once (a `Monitor` command that reached the conn
|
||||||
|
//! actor's inbox but was never processed — the drain gap); after
|
||||||
|
//! `demonitor_remote` a closed channel stays a plain `Err`, never a notice.
|
||||||
|
//!
|
||||||
|
//! Cross-process: the headline contrast — an actor's own death gives its
|
||||||
|
//! TRUE reason, loss of the LINK gives `Disconnected` (both a commanded
|
||||||
|
//! `Disconnect` and a SIGKILLed peer process are `Disconnected` from the
|
||||||
|
//! monitor's view: nobody is left to say otherwise). Reconnect does not
|
||||||
|
//! resurrect: the old monitor yields nothing more, proven by stream ORDER
|
||||||
|
//! (a fresh monitor over the new link delivers first). The ignored test
|
||||||
|
//! trips liveness by SIGSTOP and then drops the link too, asserting exactly
|
||||||
|
//! one notice for one monitor.
|
||||||
|
#![cfg(feature = "cluster")]
|
||||||
|
|
||||||
|
mod common;
|
||||||
|
|
||||||
|
use common::{maybe_child, spawn_node};
|
||||||
|
use smarm::cluster::envelope::NodeMeta;
|
||||||
|
use smarm::cluster::expose::{expose, expose_type};
|
||||||
|
use smarm::cluster::manager::{Call, Reply, MANAGER};
|
||||||
|
use smarm::cluster::membership::{subscribe, NodeEvent};
|
||||||
|
use smarm::cluster::remote::{
|
||||||
|
self, demonitor_remote, monitor_remote, send_to_remote, RemoteName, RemotePid,
|
||||||
|
};
|
||||||
|
use smarm::cluster::{start, Config, RemoteDownReason, StaticSeeds, Timing};
|
||||||
|
use smarm::pg::Incarnation;
|
||||||
|
use smarm::{
|
||||||
|
channel, gen_server, install, register, run, spawn, Addressable, DownReason, Erased, Name, Pid,
|
||||||
|
};
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
// ---- message types (hand-rolled serde; the crate is derive-less) ---------
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct Ctl {
|
||||||
|
cmd: String,
|
||||||
|
reply_to: RemotePid<Client>,
|
||||||
|
}
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct Answer {
|
||||||
|
text: String,
|
||||||
|
pid: Option<RemotePid<Erased>>,
|
||||||
|
}
|
||||||
|
struct Client;
|
||||||
|
impl Addressable for Client {
|
||||||
|
type Msg = Answer;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl serde::Serialize for Ctl {
|
||||||
|
fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
|
||||||
|
use serde::ser::SerializeTuple;
|
||||||
|
let mut t = s.serialize_tuple(2)?;
|
||||||
|
t.serialize_element(&self.cmd)?;
|
||||||
|
t.serialize_element(&self.reply_to)?;
|
||||||
|
t.end()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<'de> serde::Deserialize<'de> for Ctl {
|
||||||
|
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
|
||||||
|
let (cmd, reply_to) = <(String, RemotePid<Client>)>::deserialize(d)?;
|
||||||
|
Ok(Ctl { cmd, reply_to })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl serde::Serialize for Answer {
|
||||||
|
fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
|
||||||
|
use serde::ser::SerializeTuple;
|
||||||
|
let mut t = s.serialize_tuple(2)?;
|
||||||
|
t.serialize_element(&self.text)?;
|
||||||
|
t.serialize_element(&self.pid)?;
|
||||||
|
t.end()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<'de> serde::Deserialize<'de> for Answer {
|
||||||
|
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
|
||||||
|
let (text, pid) = <(String, Option<RemotePid<Erased>>)>::deserialize(d)?;
|
||||||
|
Ok(Answer { text, pid })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ================= local suite =========================================
|
||||||
|
|
||||||
|
/// A `Monitor` command handed to the connection but never processed (its
|
||||||
|
/// receiver dropped unread) reads as `Disconnected` — once. A second read
|
||||||
|
/// is the ordinary closed-channel `Err`, so "exactly one notice" holds.
|
||||||
|
#[test]
|
||||||
|
fn unread_command_reads_as_disconnected_once() {
|
||||||
|
maybe_child(ROLES);
|
||||||
|
run(|| {
|
||||||
|
remote::set_local_identity("me", Incarnation::new(7));
|
||||||
|
let (probe_tx, _probe_rx) = channel();
|
||||||
|
let inbox =
|
||||||
|
remote::bind_outbound_probe_with_monitors("peer", Incarnation::new(5), probe_tx);
|
||||||
|
let target = RemotePid::<Erased>::from_parts("peer", Incarnation::new(5), 9, 1);
|
||||||
|
let m = monitor_remote(target.clone());
|
||||||
|
assert!(
|
||||||
|
matches!(m.try_recv(), Ok(None)),
|
||||||
|
"command is in flight, no notice yet"
|
||||||
|
);
|
||||||
|
drop(inbox); // the conn actor died with the command unread
|
||||||
|
let d = m.recv().unwrap();
|
||||||
|
assert_eq!(d.pid, target);
|
||||||
|
assert_eq!(d.reason, RemoteDownReason::Disconnected);
|
||||||
|
assert!(
|
||||||
|
m.recv().is_err(),
|
||||||
|
"second read is closed, not a second notice"
|
||||||
|
);
|
||||||
|
assert!(m.try_recv().is_err());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// After `demonitor_remote`, a closed channel is a closed channel: no
|
||||||
|
/// notice is synthesized for a monitor the caller cancelled.
|
||||||
|
#[test]
|
||||||
|
fn cancelled_monitor_never_synthesizes() {
|
||||||
|
maybe_child(ROLES);
|
||||||
|
run(|| {
|
||||||
|
remote::set_local_identity("me", Incarnation::new(7));
|
||||||
|
let (probe_tx, _probe_rx) = channel();
|
||||||
|
let inbox =
|
||||||
|
remote::bind_outbound_probe_with_monitors("peer", Incarnation::new(5), probe_tx);
|
||||||
|
let target = RemotePid::<Erased>::from_parts("peer", Incarnation::new(5), 9, 1);
|
||||||
|
let m = monitor_remote(target);
|
||||||
|
demonitor_remote(&m);
|
||||||
|
drop(inbox);
|
||||||
|
assert!(m.recv().is_err());
|
||||||
|
assert!(m.try_recv().is_err());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// ================= cross-process ======================================
|
||||||
|
|
||||||
|
const ROLES: &[(&str, fn())] = &[
|
||||||
|
("server", role_server),
|
||||||
|
("client", role_client),
|
||||||
|
("client_stop", role_client_stop),
|
||||||
|
];
|
||||||
|
|
||||||
|
const CTL: Name<Ctl> = Name::new("c13.ctl");
|
||||||
|
|
||||||
|
fn cfg(name: &str, seeds: Vec<(String, String)>) -> Config {
|
||||||
|
Config {
|
||||||
|
node_name: name.to_string(),
|
||||||
|
meta: NodeMeta {
|
||||||
|
role: "c13".into(),
|
||||||
|
region: "local".into(),
|
||||||
|
},
|
||||||
|
listen_addr: std::env::var("SMARM_LISTEN_ADDR").unwrap_or_else(|_| "127.0.0.1:0".into()),
|
||||||
|
strategy: Box::new(StaticSeeds::new(seeds)),
|
||||||
|
timing: timing(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The p11 knobs make the liveness test fast: both roles of that test are
|
||||||
|
/// spawned with `SMARM_FAST_TIMING=1` and agree on a 100ms heartbeat /
|
||||||
|
/// 500ms liveness window. Everything else runs the shipping defaults.
|
||||||
|
fn timing() -> Timing {
|
||||||
|
if std::env::var_os("SMARM_FAST_TIMING").is_some() {
|
||||||
|
Timing {
|
||||||
|
heartbeat_interval: Duration::from_millis(100),
|
||||||
|
liveness_timeout: Duration::from_millis(500),
|
||||||
|
initial_backoff: Duration::from_millis(50),
|
||||||
|
max_backoff: Duration::from_millis(500),
|
||||||
|
..Timing::default()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Timing::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn wait_up(events: &smarm::cluster::membership::MembershipEvents, who: &str) {
|
||||||
|
loop {
|
||||||
|
match events.rx.recv() {
|
||||||
|
Ok(NodeEvent::NodeUp(i)) if i.name == who => return,
|
||||||
|
Ok(_) => continue,
|
||||||
|
Err(_) => panic!("manager gone"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn disconnect(name: &str) {
|
||||||
|
assert!(matches!(
|
||||||
|
gen_server::call(
|
||||||
|
MANAGER,
|
||||||
|
Call::Disconnect {
|
||||||
|
name: name.to_string()
|
||||||
|
}
|
||||||
|
),
|
||||||
|
Ok(Reply::Disconnected)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Server: `spawn` ⇒ a parked worker (answer carries its pid);
|
||||||
|
/// `kill:<index>` releases it, whereupon it returns (Exit).
|
||||||
|
fn role_server() {
|
||||||
|
smarm::run(move || {
|
||||||
|
let cluster = start(cfg("server", vec![])).expect("binds");
|
||||||
|
println!("LISTENING {}", cluster.local_addr());
|
||||||
|
let (tx, rx) = channel::<Ctl>();
|
||||||
|
register(CTL, tx).unwrap();
|
||||||
|
expose(CTL);
|
||||||
|
println!("READY");
|
||||||
|
let mut workers: HashMap<u32, smarm::channel::Sender<()>> = HashMap::new();
|
||||||
|
loop {
|
||||||
|
let ctl = rx.recv().unwrap();
|
||||||
|
println!("CTL {}", ctl.cmd);
|
||||||
|
let (text, pid): (String, Option<RemotePid<Erased>>) = match ctl.cmd.as_str() {
|
||||||
|
"spawn" => {
|
||||||
|
let (go_tx, go_rx) = channel::<()>();
|
||||||
|
let p: Pid = spawn(move || {
|
||||||
|
let _ = go_rx.recv();
|
||||||
|
})
|
||||||
|
.pid();
|
||||||
|
workers.insert(p.index(), go_tx);
|
||||||
|
(
|
||||||
|
"ok".into(),
|
||||||
|
Some(RemotePid::from_local(p).expect("identity set")),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
other => {
|
||||||
|
let idx: u32 = other.strip_prefix("kill:").unwrap().parse().unwrap();
|
||||||
|
if let Some(go) = workers.remove(&idx) {
|
||||||
|
let _ = go.send(());
|
||||||
|
}
|
||||||
|
("killed".into(), None)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
send_to_remote(ctl.reply_to, Answer { text, pid }).unwrap();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Client-side setup shared by both client roles: join, expose the reply
|
||||||
|
/// path, hand back an `ask` closure and the membership stream.
|
||||||
|
fn client_setup() -> (
|
||||||
|
smarm::cluster::Cluster,
|
||||||
|
smarm::cluster::membership::MembershipEvents,
|
||||||
|
impl Fn(&str) -> Answer,
|
||||||
|
) {
|
||||||
|
let server_addr = std::env::var("SMARM_SERVER_ADDR").expect("SMARM_SERVER_ADDR");
|
||||||
|
let cluster = start(cfg("client", vec![("server".into(), server_addr)])).expect("binds");
|
||||||
|
let ev = subscribe().unwrap();
|
||||||
|
wait_up(&ev, "server");
|
||||||
|
let (tx, rx) = channel::<Answer>();
|
||||||
|
let me: Pid<Client> = install::<Client>(tx);
|
||||||
|
expose_type::<Answer>();
|
||||||
|
let ask = move |cmd: &str| -> Answer {
|
||||||
|
remote::send(
|
||||||
|
RemoteName::new("server", CTL),
|
||||||
|
Ctl {
|
||||||
|
cmd: cmd.into(),
|
||||||
|
reply_to: RemotePid::from_local(me).expect("identity set"),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
rx.recv().unwrap()
|
||||||
|
};
|
||||||
|
(cluster, ev, ask)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn role_client() {
|
||||||
|
smarm::run(move || {
|
||||||
|
let (_cluster, ev, ask) = client_setup();
|
||||||
|
|
||||||
|
// 1. Headline: actor death ⇒ TRUE reason; link cut ⇒ Disconnected.
|
||||||
|
let a = ask("spawn").pid.unwrap();
|
||||||
|
let b = ask("spawn").pid.unwrap();
|
||||||
|
let ma = monitor_remote(a.clone());
|
||||||
|
let mb = monitor_remote(b.clone());
|
||||||
|
ask(&format!("kill:{}", a.index()));
|
||||||
|
let d = ma.recv().unwrap();
|
||||||
|
assert_eq!(d.pid, a);
|
||||||
|
println!("DOWN actor {:?}", d.reason);
|
||||||
|
disconnect("server");
|
||||||
|
let d = mb.recv().unwrap();
|
||||||
|
assert_eq!(d.pid, b);
|
||||||
|
println!("DOWN link {:?}", d.reason);
|
||||||
|
|
||||||
|
// 2. Reconnect does not resurrect. The connector redials on
|
||||||
|
// node_down; over the NEW link a fresh monitor delivers, while
|
||||||
|
// the old one (already answered) yields nothing further — order
|
||||||
|
// proves it, and `b` is even still alive on the server.
|
||||||
|
wait_up(&ev, "server");
|
||||||
|
println!("RECONNECTED");
|
||||||
|
let c = ask("spawn").pid.unwrap();
|
||||||
|
let mc = monitor_remote(c.clone());
|
||||||
|
ask(&format!("kill:{}", b.index()));
|
||||||
|
ask(&format!("kill:{}", c.index()));
|
||||||
|
assert_eq!(mc.recv().unwrap().reason, DownReason::Exit.into());
|
||||||
|
let stray = matches!(mb.try_recv(), Ok(Some(_)));
|
||||||
|
println!("RESURRECT stray={stray}");
|
||||||
|
|
||||||
|
// 3. Peer PROCESS killed ⇒ Disconnected too (nobody is left to send
|
||||||
|
// Down): the parent SIGKILLs the server once it sees the marker.
|
||||||
|
let e = ask("spawn").pid.unwrap();
|
||||||
|
let me_ = monitor_remote(e.clone());
|
||||||
|
println!("KILL SERVER NOW");
|
||||||
|
let d = me_.recv().unwrap();
|
||||||
|
assert_eq!(d.pid, e);
|
||||||
|
println!("DOWN procdeath {:?}", d.reason);
|
||||||
|
|
||||||
|
println!("CLIENT DONE");
|
||||||
|
loop {
|
||||||
|
smarm::sleep(Duration::from_secs(3600));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The slow role: liveness expiry (peer SIGSTOPped) followed by the link
|
||||||
|
/// dropping for real (peer SIGKILLed) — one monitor, exactly one notice.
|
||||||
|
fn role_client_stop() {
|
||||||
|
smarm::run(move || {
|
||||||
|
let (_cluster, _ev, ask) = client_setup();
|
||||||
|
let a = ask("spawn").pid.unwrap();
|
||||||
|
let ma = monitor_remote(a.clone());
|
||||||
|
println!("STOP SERVER NOW");
|
||||||
|
let d = ma.recv().unwrap(); // liveness expiry, ~liveness_timeout
|
||||||
|
assert_eq!(d.pid, a);
|
||||||
|
println!("DOWN stopped {:?}", d.reason);
|
||||||
|
println!("KILL SERVER NOW");
|
||||||
|
// Give the drop every chance to produce a second notice, then look.
|
||||||
|
smarm::sleep(Duration::from_secs(1));
|
||||||
|
let dup = matches!(ma.try_recv(), Ok(Some(_)));
|
||||||
|
println!("DUPLICATE dup={dup}");
|
||||||
|
println!("CLIENT DONE");
|
||||||
|
loop {
|
||||||
|
smarm::sleep(Duration::from_secs(3600));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The Phase 4 c13 gate: partition vs. death distinguishable; nothing
|
||||||
|
/// survives reconnect; a dead peer process is a Disconnected too.
|
||||||
|
#[test]
|
||||||
|
fn link_loss_is_disconnected_and_does_not_survive_reconnect() {
|
||||||
|
maybe_child(ROLES);
|
||||||
|
let mut server = spawn_node("server", &[]);
|
||||||
|
let saddr = server.wait_listening();
|
||||||
|
server.wait_line("READY", |l| l == "READY");
|
||||||
|
let mut client = spawn_node("client", &[("SMARM_SERVER_ADDR", &saddr)]);
|
||||||
|
client.wait_line("DOWN actor Local(Exit)", |l| l == "DOWN actor Local(Exit)");
|
||||||
|
client.wait_line("DOWN link Disconnected", |l| l == "DOWN link Disconnected");
|
||||||
|
client.wait_line("RECONNECTED", |l| l == "RECONNECTED");
|
||||||
|
client.wait_line("RESURRECT stray=false", |l| l == "RESURRECT stray=false");
|
||||||
|
client.wait_line("KILL SERVER NOW", |l| l == "KILL SERVER NOW");
|
||||||
|
server.kill();
|
||||||
|
client.wait_line("DOWN procdeath Disconnected", |l| {
|
||||||
|
l == "DOWN procdeath Disconnected"
|
||||||
|
});
|
||||||
|
client.wait_line("CLIENT DONE", |l| l == "CLIENT DONE");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Covers the invariant the headline test cannot: liveness expiry and the
|
||||||
|
/// transport drop both firing for the same connection yield ONE notice.
|
||||||
|
/// Runs on the fast [`timing`] (both roles) — was `#[ignore]`d at the 4s
|
||||||
|
/// default until the p11 knobs landed.
|
||||||
|
#[test]
|
||||||
|
fn timeout_then_drop_yields_one_notice() {
|
||||||
|
maybe_child(ROLES);
|
||||||
|
let fast = ("SMARM_FAST_TIMING", "1");
|
||||||
|
let mut server = spawn_node("server", &[fast]);
|
||||||
|
let saddr = server.wait_listening();
|
||||||
|
server.wait_line("READY", |l| l == "READY");
|
||||||
|
let mut client = spawn_node("client_stop", &[("SMARM_SERVER_ADDR", &saddr), fast]);
|
||||||
|
client.wait_line("STOP SERVER NOW", |l| l == "STOP SERVER NOW");
|
||||||
|
let spid = server.pid().expect("server alive") as libc::pid_t;
|
||||||
|
assert_eq!(unsafe { libc::kill(spid, libc::SIGSTOP) }, 0);
|
||||||
|
client.wait_line("DOWN stopped Disconnected", |l| {
|
||||||
|
l == "DOWN stopped Disconnected"
|
||||||
|
});
|
||||||
|
client.wait_line("KILL SERVER NOW", |l| l == "KILL SERVER NOW");
|
||||||
|
server.kill(); // SIGKILL works on a stopped process; Drop would too
|
||||||
|
client.wait_line("DUPLICATE dup=false", |l| l == "DUPLICATE dup=false");
|
||||||
|
client.wait_line("CLIENT DONE", |l| l == "CLIENT DONE");
|
||||||
|
}
|
||||||
@@ -0,0 +1,161 @@
|
|||||||
|
//! RFC 010 — `Discovery::Withdrawn`: a strategy retracts a candidate and the
|
||||||
|
//! connector stops dialing it.
|
||||||
|
//!
|
||||||
|
//! Cross-process: a plain *server* node, and a *client* whose strategy is a
|
||||||
|
//! script: announce a decoy `(ghost, addr)` where `addr` is a raw
|
||||||
|
//! `TcpListener` the client itself holds (an OS thread accepts and
|
||||||
|
//! immediately closes, so every dial fails at handshake and the connector
|
||||||
|
//! keeps retrying on backoff — the accept count is the dial count); after a
|
||||||
|
//! beat, withdraw the decoy and announce the real server. The client waits
|
||||||
|
//! for the server's `node_up` — which is *after* the withdrawal in the
|
||||||
|
//! strategy's own stream — then watches the decoy's accept count stay flat
|
||||||
|
//! across a window longer than the pending backoff. Before withdrawal it
|
||||||
|
//! must have been climbing (≥ 1), or the negative proves nothing.
|
||||||
|
#![cfg(feature = "cluster")]
|
||||||
|
|
||||||
|
mod common;
|
||||||
|
|
||||||
|
use common::{maybe_child, spawn_node};
|
||||||
|
use smarm::channel::Sender;
|
||||||
|
use smarm::cluster::discovery::{Discovery, Strategy};
|
||||||
|
use smarm::cluster::envelope::NodeMeta;
|
||||||
|
use smarm::cluster::membership::{subscribe, NodeEvent};
|
||||||
|
use smarm::cluster::{start, Config, StaticSeeds, Timing};
|
||||||
|
use std::net::TcpListener;
|
||||||
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||||
|
use std::sync::Arc;
|
||||||
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
|
const ROLES: &[(&str, fn())] = &[("server", role_server), ("client", role_client)];
|
||||||
|
|
||||||
|
fn meta() -> NodeMeta {
|
||||||
|
NodeMeta {
|
||||||
|
role: "withdraw".into(),
|
||||||
|
region: "local".into(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn role_server() {
|
||||||
|
smarm::run(|| {
|
||||||
|
let cluster = start(Config {
|
||||||
|
node_name: "server".into(),
|
||||||
|
meta: meta(),
|
||||||
|
listen_addr: std::env::var("SMARM_LISTEN_ADDR")
|
||||||
|
.unwrap_or_else(|_| "127.0.0.1:0".into()),
|
||||||
|
strategy: Box::new(StaticSeeds::new(Vec::<(String, String)>::new())),
|
||||||
|
timing: Timing::default(),
|
||||||
|
})
|
||||||
|
.expect("binds");
|
||||||
|
println!("LISTENING {}", cluster.local_addr());
|
||||||
|
loop {
|
||||||
|
smarm::sleep(Duration::from_secs(3600));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Scripted strategy: decoy, pause, withdraw decoy, real server, done.
|
||||||
|
struct Script {
|
||||||
|
decoy: String,
|
||||||
|
server: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Strategy for Script {
|
||||||
|
fn run(self: Box<Self>, out: Sender<Discovery>) {
|
||||||
|
let _ = out.send(Discovery::Candidate {
|
||||||
|
name: "ghost".into(),
|
||||||
|
addr: self.decoy.clone(),
|
||||||
|
});
|
||||||
|
// Long enough for the 250ms/500ms retries to land: ≥ 3 dials.
|
||||||
|
smarm::sleep(Duration::from_millis(1100));
|
||||||
|
let _ = out.send(Discovery::Withdrawn {
|
||||||
|
name: "ghost".into(),
|
||||||
|
addr: self.decoy,
|
||||||
|
});
|
||||||
|
let _ = out.send(Discovery::Candidate {
|
||||||
|
name: "server".into(),
|
||||||
|
addr: self.server,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn role_client() {
|
||||||
|
let server_addr = std::env::var("SMARM_SERVER_ADDR").expect("SMARM_SERVER_ADDR");
|
||||||
|
// The decoy: accept-and-close on an OS thread; count every accept.
|
||||||
|
let decoy = TcpListener::bind("127.0.0.1:0").unwrap();
|
||||||
|
let decoy_addr = decoy.local_addr().unwrap().to_string();
|
||||||
|
let dials = Arc::new(AtomicUsize::new(0));
|
||||||
|
let counter = dials.clone();
|
||||||
|
std::thread::spawn(move || {
|
||||||
|
for conn in decoy.incoming() {
|
||||||
|
counter.fetch_add(1, Ordering::SeqCst);
|
||||||
|
drop(conn);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
smarm::run(move || {
|
||||||
|
let _cluster = start(Config {
|
||||||
|
node_name: "client".into(),
|
||||||
|
meta: meta(),
|
||||||
|
listen_addr: "127.0.0.1:0".into(),
|
||||||
|
strategy: Box::new(Script {
|
||||||
|
decoy: decoy_addr,
|
||||||
|
server: server_addr,
|
||||||
|
}),
|
||||||
|
timing: Timing::default(),
|
||||||
|
})
|
||||||
|
.expect("binds");
|
||||||
|
let ev = subscribe().unwrap();
|
||||||
|
loop {
|
||||||
|
match ev.rx.recv() {
|
||||||
|
Ok(NodeEvent::NodeUp(i)) if i.name == "server" => break,
|
||||||
|
Ok(_) => continue,
|
||||||
|
Err(_) => panic!("manager gone"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// The withdrawal preceded the server candidate in the strategy's
|
||||||
|
// stream, so it has been applied. Any dial that started before it
|
||||||
|
// is bounded by the connect+handshake deadlines; let it drain, then
|
||||||
|
// hold the count flat across a window longer than the pending
|
||||||
|
// backoff would be (1s at this point, 2s next).
|
||||||
|
let before = dials.load(Ordering::SeqCst);
|
||||||
|
smarm::sleep(Duration::from_millis(500));
|
||||||
|
let settled = dials.load(Ordering::SeqCst);
|
||||||
|
let t0 = Instant::now();
|
||||||
|
while t0.elapsed() < Duration::from_millis(3000) {
|
||||||
|
smarm::sleep(Duration::from_millis(100));
|
||||||
|
}
|
||||||
|
let after = dials.load(Ordering::SeqCst);
|
||||||
|
println!("WITHDRAWN before={before} settled={settled} after={after}");
|
||||||
|
println!("CLIENT DONE");
|
||||||
|
loop {
|
||||||
|
smarm::sleep(Duration::from_secs(3600));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn withdrawn_candidate_is_no_longer_dialed() {
|
||||||
|
maybe_child(ROLES);
|
||||||
|
let mut server = spawn_node("server", &[]);
|
||||||
|
let saddr = server.wait_listening();
|
||||||
|
let mut client = spawn_node("client", &[("SMARM_SERVER_ADDR", &saddr)]);
|
||||||
|
let line = client.wait_line("WITHDRAWN", |l| l.starts_with("WITHDRAWN "));
|
||||||
|
let mut nums = line
|
||||||
|
.split_whitespace()
|
||||||
|
.skip(1)
|
||||||
|
.map(|kv| kv.split_once('=').unwrap().1.parse::<usize>().unwrap());
|
||||||
|
let (before, settled, after) = (
|
||||||
|
nums.next().unwrap(),
|
||||||
|
nums.next().unwrap(),
|
||||||
|
nums.next().unwrap(),
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
before >= 1,
|
||||||
|
"decoy was never dialed; the negative proves nothing: {line}"
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
settled, after,
|
||||||
|
"connector kept dialing a withdrawn candidate: {line}"
|
||||||
|
);
|
||||||
|
client.wait_line("CLIENT DONE", |l| l == "CLIENT DONE");
|
||||||
|
}
|
||||||
@@ -0,0 +1,276 @@
|
|||||||
|
//! RFC 010 c2 — owned envelope tests (roadmap: per-frame roundtrip,
|
||||||
|
//! truncation mid-field, unknown tag, length prefix lying long and short,
|
||||||
|
//! zero-length payload, adversarial lengths).
|
||||||
|
#![cfg(feature = "cluster")]
|
||||||
|
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use smarm::cluster::envelope::{
|
||||||
|
decode_payload, encode_payload, DecodeError, Frame, NodeMeta, RejectReason, MAX_FRAME_LEN,
|
||||||
|
PROTO_VERSION,
|
||||||
|
};
|
||||||
|
use smarm::cluster::RemoteDownReason;
|
||||||
|
use smarm::monitor::DownReason;
|
||||||
|
use smarm::pg::Incarnation;
|
||||||
|
|
||||||
|
fn meta() -> NodeMeta {
|
||||||
|
NodeMeta {
|
||||||
|
role: "worker".into(),
|
||||||
|
region: "eu-west".into(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn all_frames() -> Vec<Frame> {
|
||||||
|
vec![
|
||||||
|
Frame::Hello {
|
||||||
|
proto_version: PROTO_VERSION,
|
||||||
|
build_hash: 0xDEAD_BEEF_CAFE_F00D,
|
||||||
|
node_name: "alpha".into(),
|
||||||
|
incarnation: Incarnation::new(7),
|
||||||
|
meta: meta(),
|
||||||
|
},
|
||||||
|
Frame::HelloAck {
|
||||||
|
node_name: "beta".into(),
|
||||||
|
incarnation: Incarnation::new(9),
|
||||||
|
meta: meta(),
|
||||||
|
},
|
||||||
|
Frame::HelloReject {
|
||||||
|
reason: RejectReason::NameTaken,
|
||||||
|
},
|
||||||
|
Frame::Heartbeat,
|
||||||
|
Frame::Send {
|
||||||
|
index: 42,
|
||||||
|
generation: 3,
|
||||||
|
type_hash: 0x1234_5678_9ABC_DEF0,
|
||||||
|
payload: vec![1, 2, 3, 4, 5],
|
||||||
|
},
|
||||||
|
Frame::SendNamed {
|
||||||
|
name: "the_counter".into(),
|
||||||
|
type_hash: 0xFFFF_0000_FFFF_0000,
|
||||||
|
payload: vec![],
|
||||||
|
},
|
||||||
|
Frame::Monitor {
|
||||||
|
monitor_id: 77,
|
||||||
|
index: 42,
|
||||||
|
generation: 3,
|
||||||
|
},
|
||||||
|
Frame::Demonitor { monitor_id: 77 },
|
||||||
|
Frame::Down {
|
||||||
|
monitor_id: 77,
|
||||||
|
reason: RemoteDownReason::Local(DownReason::Panic),
|
||||||
|
},
|
||||||
|
Frame::Down {
|
||||||
|
monitor_id: 78,
|
||||||
|
reason: RemoteDownReason::Disconnected,
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn encode_one(f: &Frame) -> Vec<u8> {
|
||||||
|
let mut buf = Vec::new();
|
||||||
|
f.encode(&mut buf).unwrap();
|
||||||
|
buf
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn per_frame_roundtrip() {
|
||||||
|
for f in all_frames() {
|
||||||
|
let buf = encode_one(&f);
|
||||||
|
let (decoded, consumed) = Frame::decode(&buf).unwrap().unwrap();
|
||||||
|
assert_eq!(decoded, f, "roundtrip mismatch");
|
||||||
|
assert_eq!(consumed, buf.len(), "consumed != buffer length for {f:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn back_to_back_frames_decode_sequentially() {
|
||||||
|
let mut buf = Vec::new();
|
||||||
|
for f in all_frames() {
|
||||||
|
f.encode(&mut buf).unwrap();
|
||||||
|
}
|
||||||
|
let mut off = 0;
|
||||||
|
let mut decoded = Vec::new();
|
||||||
|
while off < buf.len() {
|
||||||
|
let (f, n) = Frame::decode(&buf[off..]).unwrap().unwrap();
|
||||||
|
decoded.push(f);
|
||||||
|
off += n;
|
||||||
|
}
|
||||||
|
assert_eq!(decoded, all_frames());
|
||||||
|
assert_eq!(off, buf.len());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn heartbeat_golden_bytes() {
|
||||||
|
// Locks the layout: u32 LE length prefix, then the tag byte.
|
||||||
|
let buf = encode_one(&Frame::Heartbeat);
|
||||||
|
assert_eq!(buf, vec![1, 0, 0, 0, 4]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn zero_length_payload_roundtrips() {
|
||||||
|
let f = Frame::Send {
|
||||||
|
index: 0,
|
||||||
|
generation: 0,
|
||||||
|
type_hash: 0,
|
||||||
|
payload: vec![],
|
||||||
|
};
|
||||||
|
let buf = encode_one(&f);
|
||||||
|
let (decoded, consumed) = Frame::decode(&buf).unwrap().unwrap();
|
||||||
|
assert_eq!(decoded, f);
|
||||||
|
assert_eq!(consumed, buf.len());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn incomplete_is_none_not_error() {
|
||||||
|
let buf = encode_one(&all_frames()[0]);
|
||||||
|
// Every strict prefix short of the full frame must report "need more".
|
||||||
|
for cut in 0..buf.len() {
|
||||||
|
assert_eq!(
|
||||||
|
Frame::decode(&buf[..cut]).unwrap(),
|
||||||
|
None,
|
||||||
|
"cut at {cut} should be incomplete"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn unknown_frame_tag() {
|
||||||
|
let buf = vec![1, 0, 0, 0, 250];
|
||||||
|
assert_eq!(Frame::decode(&buf), Err(DecodeError::UnknownTag(250)));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn unknown_enum_tags() {
|
||||||
|
// HelloReject with a bogus reason tag.
|
||||||
|
let buf = vec![2, 0, 0, 0, 3, 99];
|
||||||
|
assert_eq!(
|
||||||
|
Frame::decode(&buf),
|
||||||
|
Err(DecodeError::UnknownEnumTag {
|
||||||
|
what: "RejectReason",
|
||||||
|
tag: 99
|
||||||
|
})
|
||||||
|
);
|
||||||
|
// Down with a bogus reason tag (id = 0u64).
|
||||||
|
let mut buf = vec![10, 0, 0, 0, 9];
|
||||||
|
buf.extend_from_slice(&0u64.to_le_bytes());
|
||||||
|
buf.push(200);
|
||||||
|
assert_eq!(
|
||||||
|
Frame::decode(&buf),
|
||||||
|
Err(DecodeError::UnknownEnumTag {
|
||||||
|
what: "RemoteDownReason",
|
||||||
|
tag: 200
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn length_prefix_lying_long_with_bytes_present_is_trailing() {
|
||||||
|
let mut buf = encode_one(&Frame::Heartbeat);
|
||||||
|
// Declare 3 extra body bytes and actually supply them.
|
||||||
|
let declared = u32::from_le_bytes([buf[0], buf[1], buf[2], buf[3]]) + 3;
|
||||||
|
buf[0..4].copy_from_slice(&declared.to_le_bytes());
|
||||||
|
buf.extend_from_slice(&[0xAA, 0xBB, 0xCC]);
|
||||||
|
assert_eq!(Frame::decode(&buf), Err(DecodeError::Trailing { extra: 3 }));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn length_prefix_lying_long_without_bytes_is_incomplete() {
|
||||||
|
// Indistinguishable from a partial read — must be None, not an error.
|
||||||
|
let mut buf = encode_one(&Frame::Heartbeat);
|
||||||
|
let declared = u32::from_le_bytes([buf[0], buf[1], buf[2], buf[3]]) + 3;
|
||||||
|
buf[0..4].copy_from_slice(&declared.to_le_bytes());
|
||||||
|
assert_eq!(Frame::decode(&buf).unwrap(), None);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn length_prefix_lying_short_truncates_a_field() {
|
||||||
|
let f = &all_frames()[0]; // Hello: plenty of fields to cut into
|
||||||
|
let mut buf = encode_one(f);
|
||||||
|
let declared = u32::from_le_bytes([buf[0], buf[1], buf[2], buf[3]]);
|
||||||
|
let lie = declared - 4; // cut mid-field
|
||||||
|
buf[0..4].copy_from_slice(&lie.to_le_bytes());
|
||||||
|
assert_eq!(Frame::decode(&buf), Err(DecodeError::Truncated));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn truncation_mid_string_field() {
|
||||||
|
// A frame whose declared length is intact but whose inner string length
|
||||||
|
// runs past the body: SendNamed claiming a 1000-byte name in a tiny body.
|
||||||
|
let mut body = vec![6u8]; // TAG_SEND_NAMED
|
||||||
|
body.extend_from_slice(&1000u16.to_le_bytes());
|
||||||
|
body.extend_from_slice(b"short");
|
||||||
|
let mut buf = (body.len() as u32).to_le_bytes().to_vec();
|
||||||
|
buf.extend_from_slice(&body);
|
||||||
|
assert_eq!(Frame::decode(&buf), Err(DecodeError::Truncated));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn adversarial_lengths() {
|
||||||
|
// Length prefix of u32::MAX: reject as oversized, do not wait for 4 GiB.
|
||||||
|
let buf = [0xFF, 0xFF, 0xFF, 0xFF, 0];
|
||||||
|
assert_eq!(
|
||||||
|
Frame::decode(&buf),
|
||||||
|
Err(DecodeError::FrameTooLarge {
|
||||||
|
declared: u32::MAX as usize
|
||||||
|
})
|
||||||
|
);
|
||||||
|
// Just over the cap: also rejected.
|
||||||
|
let over = (MAX_FRAME_LEN as u32 + 1).to_le_bytes();
|
||||||
|
assert!(matches!(
|
||||||
|
Frame::decode(&over),
|
||||||
|
Err(DecodeError::FrameTooLarge { .. })
|
||||||
|
));
|
||||||
|
// Zero-length frame: there is no tag byte; corrupt, not incomplete.
|
||||||
|
let buf = [0, 0, 0, 0];
|
||||||
|
assert_eq!(Frame::decode(&buf), Err(DecodeError::EmptyFrame));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn invalid_utf8_in_string_field() {
|
||||||
|
let mut buf = encode_one(&Frame::SendNamed {
|
||||||
|
name: "abcd".into(),
|
||||||
|
type_hash: 0,
|
||||||
|
payload: vec![],
|
||||||
|
});
|
||||||
|
// name bytes start after: 4 (len) + 1 (tag) + 2 (str len) = offset 7
|
||||||
|
buf[7] = 0xFF;
|
||||||
|
assert_eq!(Frame::decode(&buf), Err(DecodeError::Utf8));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||||
|
struct Ping {
|
||||||
|
seq: u64,
|
||||||
|
label: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn payload_seam_roundtrip() {
|
||||||
|
let ping = Ping {
|
||||||
|
seq: 31337,
|
||||||
|
label: "hello".into(),
|
||||||
|
};
|
||||||
|
let blob = encode_payload(&ping).unwrap();
|
||||||
|
// Carry it through a real frame, as it will travel in c9.
|
||||||
|
let f = Frame::Send {
|
||||||
|
index: 1,
|
||||||
|
generation: 1,
|
||||||
|
type_hash: 0xABCD,
|
||||||
|
payload: blob,
|
||||||
|
};
|
||||||
|
let buf = encode_one(&f);
|
||||||
|
let (decoded, _) = Frame::decode(&buf).unwrap().unwrap();
|
||||||
|
let Frame::Send { payload, .. } = decoded else {
|
||||||
|
panic!("wrong frame");
|
||||||
|
};
|
||||||
|
let back: Ping = decode_payload(&payload).unwrap();
|
||||||
|
assert_eq!(back, ping);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn payload_seam_rejects_truncated_blob() {
|
||||||
|
let blob = encode_payload(&Ping {
|
||||||
|
seq: 1,
|
||||||
|
label: "x".into(),
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
assert!(decode_payload::<Ping>(&blob[..blob.len() - 1]).is_err());
|
||||||
|
}
|
||||||
@@ -0,0 +1,161 @@
|
|||||||
|
//! RFC 010 c8 — exposure registry + type hashing. Purely local, no network.
|
||||||
|
//!
|
||||||
|
//! Payload types are std types (`String`, `u64`) because the crate's serde is
|
||||||
|
//! deliberately derive-less (`default-features = false`) — user crates bring
|
||||||
|
//! their own derive; the contract here is `DeserializeOwned`.
|
||||||
|
//!
|
||||||
|
//! The hash-stability test re-execs the current binary (the c4 harness): the
|
||||||
|
//! guarantee under test is "stable across runs in the SAME binary" — exactly
|
||||||
|
//! what the build-hash handshake reduces the mesh to — not stability across
|
||||||
|
//! builds, which the scope guard explicitly rejects.
|
||||||
|
#![cfg(feature = "cluster")]
|
||||||
|
|
||||||
|
mod common;
|
||||||
|
|
||||||
|
use common::{maybe_child, spawn_node};
|
||||||
|
use smarm::cluster::envelope::encode_payload;
|
||||||
|
use smarm::cluster::expose::{
|
||||||
|
decode_deliver, decoder_registered, expose, expose_type, exposed_hash, exposed_names,
|
||||||
|
type_hash, DeliverError,
|
||||||
|
};
|
||||||
|
use smarm::monitor::{monitor, terminal_reason, DownReason};
|
||||||
|
use smarm::{channel, register, run, spawn, Name};
|
||||||
|
|
||||||
|
const ROLES: &[(&str, fn())] = &[("hasher", role_hasher)];
|
||||||
|
|
||||||
|
/// Print the hashes this process computes; the parent (a different run of
|
||||||
|
/// the same binary) compares against its own.
|
||||||
|
fn role_hasher() {
|
||||||
|
println!("HASH-STRING {}", type_hash::<String>());
|
||||||
|
println!("HASH-U64 {}", type_hash::<u64>());
|
||||||
|
}
|
||||||
|
|
||||||
|
const GREETER: Name<String> = Name::new("expose-test.greeter");
|
||||||
|
|
||||||
|
/// Exposed and unexposed lookup, the returned hash, and the audit listing.
|
||||||
|
#[test]
|
||||||
|
fn exposed_and_unexposed_lookup() {
|
||||||
|
maybe_child(ROLES);
|
||||||
|
run(|| {
|
||||||
|
let h = expose(GREETER);
|
||||||
|
assert_eq!(h, type_hash::<String>());
|
||||||
|
assert_eq!(exposed_hash("expose-test.greeter"), Some(h));
|
||||||
|
assert_eq!(exposed_hash("never-exposed"), None);
|
||||||
|
assert!(exposed_names().contains(&("expose-test.greeter", h)));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Distinct types land on distinct hashes (FNV over distinct TypeIds — a
|
||||||
|
/// smoke assertion; a collision would degrade to a decode error, never a
|
||||||
|
/// misroute, per RFC §3).
|
||||||
|
#[test]
|
||||||
|
fn distinct_types_distinct_hashes() {
|
||||||
|
maybe_child(ROLES);
|
||||||
|
run(|| {
|
||||||
|
assert_ne!(type_hash::<String>(), type_hash::<u64>());
|
||||||
|
assert_ne!(type_hash::<String>(), type_hash::<Vec<u8>>());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The decode-and-deliver contract: a registered hash decodes into the
|
||||||
|
/// target's typed channel; an unknown hash, corrupt bytes, and a missing
|
||||||
|
/// channel each fail without delivering — `WrongChannel`, never a misroute.
|
||||||
|
#[test]
|
||||||
|
fn decoder_registration_and_delivery() {
|
||||||
|
maybe_child(ROLES);
|
||||||
|
run(|| {
|
||||||
|
let h_string = expose_type::<String>();
|
||||||
|
let h_u64 = expose_type::<u64>();
|
||||||
|
assert!(decoder_registered(h_string));
|
||||||
|
assert!(!decoder_registered(h_string.wrapping_add(1)));
|
||||||
|
|
||||||
|
// A live actor with a String channel (registered from its own body,
|
||||||
|
// announced via a ready signal — the tests/registry.rs idiom).
|
||||||
|
let (ready_tx, ready_rx) = channel::<()>();
|
||||||
|
let (stop_tx, stop_rx) = channel::<()>();
|
||||||
|
let (msg_tx, msg_rx) = channel::<String>();
|
||||||
|
let pid = spawn(move || {
|
||||||
|
register(Name::<String>::new("expose-test.sink"), msg_tx).unwrap();
|
||||||
|
ready_tx.send(()).unwrap();
|
||||||
|
let _ = stop_rx.recv();
|
||||||
|
})
|
||||||
|
.pid();
|
||||||
|
ready_rx.recv().unwrap();
|
||||||
|
|
||||||
|
// Happy path: decode + deliver through the published channel.
|
||||||
|
let bytes = encode_payload("hello across the seam").unwrap();
|
||||||
|
decode_deliver(h_string, pid, &bytes).unwrap();
|
||||||
|
assert_eq!(msg_rx.recv().unwrap(), "hello across the seam");
|
||||||
|
|
||||||
|
// Unknown hash: nothing was registered under it.
|
||||||
|
assert!(matches!(
|
||||||
|
decode_deliver(h_string.wrapping_add(1), pid, &bytes),
|
||||||
|
Err(DeliverError::UnknownType)
|
||||||
|
));
|
||||||
|
|
||||||
|
// Corrupt bytes: the decoder fails before any send.
|
||||||
|
assert!(matches!(
|
||||||
|
decode_deliver(h_string, pid, &[0xff; 3]),
|
||||||
|
Err(DeliverError::Decode(_))
|
||||||
|
));
|
||||||
|
|
||||||
|
// Right decoder, wrong channel: the actor has no u64 channel, so the
|
||||||
|
// decoded value is refused — the NoChannel guarantee.
|
||||||
|
let u64_bytes = encode_payload(&7u64).unwrap();
|
||||||
|
assert!(matches!(
|
||||||
|
decode_deliver(h_u64, pid, &u64_bytes),
|
||||||
|
Err(DeliverError::WrongChannel)
|
||||||
|
));
|
||||||
|
|
||||||
|
stop_tx.send(()).unwrap();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `expose` and the bridge crossing agree on the resulting set: both funnel
|
||||||
|
/// the pid-boundary mark through the watchable machinery, so an exposed
|
||||||
|
/// name's holder dies with a terminal record — the exact observable
|
||||||
|
/// `mark_watchable` guarantees the membrane. (For named holders the mark is
|
||||||
|
/// already stamped by `register` itself; this pins the shared contract.)
|
||||||
|
#[test]
|
||||||
|
fn expose_and_bridge_crossing_agree_on_the_set() {
|
||||||
|
maybe_child(ROLES);
|
||||||
|
run(|| {
|
||||||
|
let (ready_tx, ready_rx) = channel::<()>();
|
||||||
|
let (stop_tx, stop_rx) = channel::<()>();
|
||||||
|
let (msg_tx, _msg_rx) = channel::<String>();
|
||||||
|
let pid = spawn(move || {
|
||||||
|
register(GREETER, msg_tx).unwrap();
|
||||||
|
ready_tx.send(()).unwrap();
|
||||||
|
let _ = stop_rx.recv();
|
||||||
|
})
|
||||||
|
.pid();
|
||||||
|
ready_rx.recv().unwrap();
|
||||||
|
|
||||||
|
expose(GREETER);
|
||||||
|
let m = monitor(pid);
|
||||||
|
stop_tx.send(()).unwrap();
|
||||||
|
assert_eq!(m.rx.recv().unwrap().reason, DownReason::Exit);
|
||||||
|
assert_eq!(terminal_reason(pid), Some(DownReason::Exit));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Hash stability across runs in the same binary: a re-exec of this binary
|
||||||
|
/// computes the same hashes this process does.
|
||||||
|
#[test]
|
||||||
|
fn hash_stable_across_runs_in_same_binary() {
|
||||||
|
maybe_child(ROLES);
|
||||||
|
let (mine_string, mine_u64) = {
|
||||||
|
// Computing a TypeId hash needs no runtime, but keep the contract
|
||||||
|
// uniform with real call sites.
|
||||||
|
(type_hash::<String>(), type_hash::<u64>())
|
||||||
|
};
|
||||||
|
let mut child = spawn_node("hasher", &[]);
|
||||||
|
let line = child.wait_line("HASH-STRING", |l| l.starts_with("HASH-STRING "));
|
||||||
|
assert_eq!(
|
||||||
|
line["HASH-STRING ".len()..].parse::<u64>().unwrap(),
|
||||||
|
mine_string
|
||||||
|
);
|
||||||
|
let line = child.wait_line("HASH-U64", |l| l.starts_with("HASH-U64 "));
|
||||||
|
assert_eq!(line["HASH-U64 ".len()..].parse::<u64>().unwrap(), mine_u64);
|
||||||
|
child.wait_exit();
|
||||||
|
}
|
||||||
@@ -0,0 +1,240 @@
|
|||||||
|
//! RFC 010 c5 — handshake state-machine tests (roadmap: happy path; hash
|
||||||
|
//! mismatch; proto-version mismatch; name already claimed; simultaneous-connect
|
||||||
|
//! tie-break; garbage before Hello). Pure — no IO, no actors, no runtime.
|
||||||
|
#![cfg(feature = "cluster")]
|
||||||
|
|
||||||
|
use smarm::cluster::envelope::{Frame, NodeMeta, RejectReason, PROTO_VERSION};
|
||||||
|
use smarm::cluster::handshake::{
|
||||||
|
dial_wins, Initiator, InitiatorOutcome, Local, PeerStanding, Responder, ResponderOutcome,
|
||||||
|
};
|
||||||
|
use smarm::pg::Incarnation;
|
||||||
|
|
||||||
|
const HASH: u64 = 0xDEAD_BEEF_CAFE_F00D;
|
||||||
|
|
||||||
|
fn local(name: &str) -> Local {
|
||||||
|
Local {
|
||||||
|
node_name: name.into(),
|
||||||
|
incarnation: Incarnation::new(7),
|
||||||
|
build_hash: HASH,
|
||||||
|
meta: NodeMeta {
|
||||||
|
role: "worker".into(),
|
||||||
|
region: "eu-west".into(),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The Hello that `Initiator::new(&local(name))` emits, built by hand.
|
||||||
|
fn hello_from(name: &str) -> Frame {
|
||||||
|
let l = local(name);
|
||||||
|
Frame::Hello {
|
||||||
|
proto_version: PROTO_VERSION,
|
||||||
|
build_hash: l.build_hash,
|
||||||
|
node_name: l.node_name,
|
||||||
|
incarnation: l.incarnation,
|
||||||
|
meta: l.meta,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn happy_path_establishes_both_ends() {
|
||||||
|
// alpha dials beta.
|
||||||
|
let (initiator, hello) = Initiator::new(&local("alpha"));
|
||||||
|
assert_eq!(hello, hello_from("alpha"), "initiator emits its identity");
|
||||||
|
|
||||||
|
let responder = Responder::new(local("beta"));
|
||||||
|
let (reply, peer) = match responder.on_frame(hello, PeerStanding::Free) {
|
||||||
|
ResponderOutcome::Accepted { reply, peer } => (reply, peer),
|
||||||
|
other => panic!("expected Accepted, got {other:?}"),
|
||||||
|
};
|
||||||
|
assert_eq!(peer.node_name, "alpha");
|
||||||
|
assert_eq!(peer.incarnation, Incarnation::new(7));
|
||||||
|
assert_eq!(peer.meta.role, "worker");
|
||||||
|
|
||||||
|
// The ack carries the responder's identity, no hash/version (one-sided
|
||||||
|
// check — sound because equality is symmetric).
|
||||||
|
let l = local("beta");
|
||||||
|
assert_eq!(
|
||||||
|
reply,
|
||||||
|
Frame::HelloAck {
|
||||||
|
node_name: l.node_name,
|
||||||
|
incarnation: l.incarnation,
|
||||||
|
meta: l.meta,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
match initiator.on_frame(reply) {
|
||||||
|
InitiatorOutcome::Established(peer) => {
|
||||||
|
assert_eq!(peer.node_name, "beta");
|
||||||
|
assert_eq!(peer.incarnation, Incarnation::new(7));
|
||||||
|
assert_eq!(peer.meta.region, "eu-west");
|
||||||
|
}
|
||||||
|
other => panic!("expected Established, got {other:?}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn hash_mismatch_rejected() {
|
||||||
|
let responder = Responder::new(local("beta"));
|
||||||
|
let hello = Frame::Hello {
|
||||||
|
proto_version: PROTO_VERSION,
|
||||||
|
build_hash: HASH ^ 1,
|
||||||
|
node_name: "alpha".into(),
|
||||||
|
incarnation: Incarnation::new(7),
|
||||||
|
meta: local("alpha").meta,
|
||||||
|
};
|
||||||
|
match responder.on_frame(hello, PeerStanding::Free) {
|
||||||
|
ResponderOutcome::Rejected { reply, reason } => {
|
||||||
|
assert_eq!(reason, RejectReason::HashMismatch);
|
||||||
|
assert_eq!(reply, Frame::HelloReject { reason });
|
||||||
|
}
|
||||||
|
other => panic!("expected Rejected, got {other:?}"),
|
||||||
|
}
|
||||||
|
|
||||||
|
// The dialer side of the same story: a reject frame comes back.
|
||||||
|
let (initiator, _hello) = Initiator::new(&local("alpha"));
|
||||||
|
match initiator.on_frame(Frame::HelloReject {
|
||||||
|
reason: RejectReason::HashMismatch,
|
||||||
|
}) {
|
||||||
|
InitiatorOutcome::Rejected(RejectReason::HashMismatch) => {}
|
||||||
|
other => panic!("expected Rejected(HashMismatch), got {other:?}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn proto_version_mismatch_rejected_and_checked_first() {
|
||||||
|
// Both proto and hash wrong: proto wins — nothing after the version can
|
||||||
|
// be trusted, and HelloReject is the cross-version compatibility anchor.
|
||||||
|
let responder = Responder::new(local("beta"));
|
||||||
|
let hello = Frame::Hello {
|
||||||
|
proto_version: PROTO_VERSION + 1,
|
||||||
|
build_hash: HASH ^ 1,
|
||||||
|
node_name: "alpha".into(),
|
||||||
|
incarnation: Incarnation::new(7),
|
||||||
|
meta: local("alpha").meta,
|
||||||
|
};
|
||||||
|
match responder.on_frame(hello, PeerStanding::Free) {
|
||||||
|
ResponderOutcome::Rejected { reason, .. } => {
|
||||||
|
assert_eq!(reason, RejectReason::ProtoVersion);
|
||||||
|
}
|
||||||
|
other => panic!("expected Rejected, got {other:?}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn claimed_name_rejected() {
|
||||||
|
let responder = Responder::new(local("beta"));
|
||||||
|
let ctx = PeerStanding::Claimed;
|
||||||
|
match responder.on_frame(hello_from("alpha"), ctx) {
|
||||||
|
ResponderOutcome::Rejected { reason, .. } => {
|
||||||
|
assert_eq!(reason, RejectReason::NameTaken);
|
||||||
|
}
|
||||||
|
other => panic!("expected Rejected, got {other:?}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn own_name_offered_rejected_as_name_taken() {
|
||||||
|
// Self-connect or genuine collision: the responder's own name arrives.
|
||||||
|
let responder = Responder::new(local("beta"));
|
||||||
|
match responder.on_frame(hello_from("beta"), PeerStanding::Free) {
|
||||||
|
ResponderOutcome::Rejected { reason, .. } => {
|
||||||
|
assert_eq!(reason, RejectReason::NameTaken);
|
||||||
|
}
|
||||||
|
other => panic!("expected Rejected, got {other:?}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn hash_checked_before_name() {
|
||||||
|
// Wrong hash AND claimed name: hash wins (validity before identity).
|
||||||
|
let responder = Responder::new(local("beta"));
|
||||||
|
let hello = Frame::Hello {
|
||||||
|
proto_version: PROTO_VERSION,
|
||||||
|
build_hash: HASH ^ 1,
|
||||||
|
node_name: "alpha".into(),
|
||||||
|
incarnation: Incarnation::new(7),
|
||||||
|
meta: local("alpha").meta,
|
||||||
|
};
|
||||||
|
let ctx = PeerStanding::Claimed;
|
||||||
|
match responder.on_frame(hello, ctx) {
|
||||||
|
ResponderOutcome::Rejected { reason, .. } => {
|
||||||
|
assert_eq!(reason, RejectReason::HashMismatch);
|
||||||
|
}
|
||||||
|
other => panic!("expected Rejected, got {other:?}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn dial_wins_is_deterministic_and_antisymmetric() {
|
||||||
|
// The smaller name's dial survives; both ends compute the same verdict.
|
||||||
|
assert!(dial_wins("alpha", "beta"));
|
||||||
|
assert!(!dial_wins("beta", "alpha"));
|
||||||
|
for (a, b) in [("a", "b"), ("node-1", "node-2"), ("x", "xx")] {
|
||||||
|
assert_ne!(dial_wins(a, b), dial_wins(b, a), "({a}, {b})");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn simultaneous_connect_exactly_one_side_accepts() {
|
||||||
|
// alpha and beta dial each other at once. Each responder sees the peer's
|
||||||
|
// Hello while its own dial is in flight.
|
||||||
|
let ctx = PeerStanding::Dialing;
|
||||||
|
|
||||||
|
// On beta: inbound is alpha's dial; alpha < beta, so the inbound wins.
|
||||||
|
let on_beta = Responder::new(local("beta")).on_frame(hello_from("alpha"), ctx);
|
||||||
|
assert!(
|
||||||
|
matches!(on_beta, ResponderOutcome::Accepted { .. }),
|
||||||
|
"beta must accept alpha's dial, got {on_beta:?}"
|
||||||
|
);
|
||||||
|
|
||||||
|
// On alpha: inbound is beta's dial; it loses — close silently, no frame
|
||||||
|
// (ratified: both ends can compute the outcome, a reject adds nothing).
|
||||||
|
let on_alpha = Responder::new(local("alpha")).on_frame(hello_from("beta"), ctx);
|
||||||
|
assert!(
|
||||||
|
matches!(on_alpha, ResponderOutcome::TieBreakLoss),
|
||||||
|
"alpha must silently drop beta's dial, got {on_alpha:?}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn tiebreak_loss_only_applies_when_dialing() {
|
||||||
|
// Same inbound Hello, no dial in flight: plain accept.
|
||||||
|
let on_alpha = Responder::new(local("alpha")).on_frame(hello_from("beta"), PeerStanding::Free);
|
||||||
|
assert!(matches!(on_alpha, ResponderOutcome::Accepted { .. }));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn garbage_before_hello_fails_without_reply() {
|
||||||
|
// Any valid-but-wrong frame before Hello is a protocol violation: close,
|
||||||
|
// no reject frame. (Undecodable bytes are the codec's Err, not ours.)
|
||||||
|
for frame in [
|
||||||
|
Frame::Heartbeat,
|
||||||
|
Frame::HelloAck {
|
||||||
|
node_name: "alpha".into(),
|
||||||
|
incarnation: Incarnation::new(7),
|
||||||
|
meta: local("alpha").meta,
|
||||||
|
},
|
||||||
|
Frame::Demonitor { monitor_id: 3 },
|
||||||
|
] {
|
||||||
|
let out = Responder::new(local("beta")).on_frame(frame.clone(), PeerStanding::Free);
|
||||||
|
match out {
|
||||||
|
ResponderOutcome::Failed(f) => assert_eq!(f, frame),
|
||||||
|
other => panic!("expected Failed({frame:?}), got {other:?}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn garbage_before_ack_fails_the_initiator() {
|
||||||
|
for frame in [
|
||||||
|
Frame::Heartbeat,
|
||||||
|
hello_from("beta"),
|
||||||
|
Frame::Demonitor { monitor_id: 3 },
|
||||||
|
] {
|
||||||
|
let (initiator, _hello) = Initiator::new(&local("alpha"));
|
||||||
|
match initiator.on_frame(frame.clone()) {
|
||||||
|
InitiatorOutcome::Failed(f) => assert_eq!(f, frame),
|
||||||
|
other => panic!("expected Failed({frame:?}), got {other:?}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,270 @@
|
|||||||
|
//! RFC 010 c7a — membership events and the view, at the manager.
|
||||||
|
//!
|
||||||
|
//! Same construction as the c6a lifecycle suite: the handshake is bypassed,
|
||||||
|
//! connections are built already-established over localhost TCP pairs with
|
||||||
|
//! fabricated `Peer`s, and the manager is started plainly so the test can
|
||||||
|
//! terminate. What is under test is the membership layer that c7 adds to the
|
||||||
|
//! manager: `node_up`/`node_down` events to subscribers (snapshot-then-stream),
|
||||||
|
//! the view, and NodeId identity — memoized per `(name, incarnation)`, so a
|
||||||
|
//! reconnect blip keeps its id and a restart (new incarnation) gets a fresh one.
|
||||||
|
#![cfg(feature = "cluster")]
|
||||||
|
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use smarm::cluster::envelope::NodeMeta;
|
||||||
|
use smarm::cluster::handshake::Peer;
|
||||||
|
use smarm::cluster::manager::{Call, Manager, Reply, MANAGER};
|
||||||
|
use smarm::cluster::membership::{subscribe, view, MembershipEvents, NodeEvent};
|
||||||
|
use smarm::cluster::spawn_established;
|
||||||
|
use smarm::cluster::transport::tcp::TcpTransport;
|
||||||
|
use smarm::cluster::transport::{Conn, FramedConn, Transport};
|
||||||
|
use smarm::cluster::Timing;
|
||||||
|
use smarm::gen_server::{self, GenServerBuilder};
|
||||||
|
use smarm::pg::{Incarnation, NodeId};
|
||||||
|
use smarm::run;
|
||||||
|
|
||||||
|
/// A fabricated post-handshake peer identity, with the incarnation under the
|
||||||
|
/// test's control (it is identity-bearing here, unlike in the c6a suite).
|
||||||
|
fn peer(name: &str, inc: u32) -> Peer {
|
||||||
|
Peer {
|
||||||
|
node_name: name.to_string(),
|
||||||
|
incarnation: Incarnation::new(inc),
|
||||||
|
meta: NodeMeta {
|
||||||
|
role: "test".to_string(),
|
||||||
|
region: "test".to_string(),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// One established transport pair over localhost (TCP backlog covers the
|
||||||
|
/// sequential dial-then-accept, as in the c3 conformance suite).
|
||||||
|
fn pair(t: &dyn Transport) -> (Box<dyn Conn>, Box<dyn Conn>) {
|
||||||
|
let mut l = t.listen("127.0.0.1:0").unwrap();
|
||||||
|
let a = t.dial(&l.local_addr()).unwrap();
|
||||||
|
let b = l.accept().unwrap();
|
||||||
|
(a, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The next event, or a panic naming the wait. The bound is generous against
|
||||||
|
/// a sub-millisecond real cost.
|
||||||
|
fn next_event(ev: &MembershipEvents, waiting_for: &str) -> NodeEvent {
|
||||||
|
ev.rx
|
||||||
|
.recv_timeout(Duration::from_secs(5))
|
||||||
|
.unwrap_or_else(|e| panic!("timed out waiting for {waiting_for}: {e:?}"))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Assert the subscription is drained: no event is pending.
|
||||||
|
fn assert_quiet(ev: &MembershipEvents) {
|
||||||
|
assert!(matches!(ev.rx.try_recv(), Ok(None)));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn disconnect(name: &str) {
|
||||||
|
assert!(matches!(
|
||||||
|
gen_server::call(
|
||||||
|
MANAGER,
|
||||||
|
Call::Disconnect {
|
||||||
|
name: name.to_string()
|
||||||
|
}
|
||||||
|
),
|
||||||
|
Ok(Reply::Disconnected)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Live subscription: an empty snapshot, then `NodeUp` on registration and
|
||||||
|
/// `NodeDown` (same id) on commanded disconnect and on peer EOF alike.
|
||||||
|
#[test]
|
||||||
|
fn subscriber_sees_up_and_down() {
|
||||||
|
run(|| {
|
||||||
|
let mgr = GenServerBuilder::new(Manager::new())
|
||||||
|
.named(MANAGER)
|
||||||
|
.start()
|
||||||
|
.expect("manager name is free");
|
||||||
|
|
||||||
|
let ev = subscribe().expect("manager is up");
|
||||||
|
assert_quiet(&ev); // nothing live: the snapshot is empty
|
||||||
|
|
||||||
|
let t = TcpTransport;
|
||||||
|
let (a1, b1) = pair(&t);
|
||||||
|
let (a2, b2) = pair(&t);
|
||||||
|
spawn_established(FramedConn::new(a1), peer("node-b", 1), Timing::default())
|
||||||
|
.expect("node-b registers");
|
||||||
|
spawn_established(FramedConn::new(a2), peer("node-c", 1), Timing::default())
|
||||||
|
.expect("node-c registers");
|
||||||
|
|
||||||
|
let up_b = match next_event(&ev, "node_up(node-b)") {
|
||||||
|
NodeEvent::NodeUp(info) => {
|
||||||
|
assert_eq!(info.name, "node-b");
|
||||||
|
assert_eq!(info.incarnation, Incarnation::new(1));
|
||||||
|
assert_eq!(info.meta.role, "test");
|
||||||
|
info
|
||||||
|
}
|
||||||
|
other => panic!("expected node_up(node-b), got {other:?}"),
|
||||||
|
};
|
||||||
|
let up_c = match next_event(&ev, "node_up(node-c)") {
|
||||||
|
NodeEvent::NodeUp(info) => {
|
||||||
|
assert_eq!(info.name, "node-c");
|
||||||
|
info
|
||||||
|
}
|
||||||
|
other => panic!("expected node_up(node-c), got {other:?}"),
|
||||||
|
};
|
||||||
|
assert_ne!(up_b.node, up_c.node, "distinct peers get distinct ids");
|
||||||
|
|
||||||
|
// Commanded disconnect: down with node-b's id.
|
||||||
|
disconnect("node-b");
|
||||||
|
assert_eq!(
|
||||||
|
next_event(&ev, "node_down(node-b)"),
|
||||||
|
NodeEvent::NodeDown(up_b.clone())
|
||||||
|
);
|
||||||
|
|
||||||
|
// Peer EOF, no command: down with node-c's id.
|
||||||
|
drop(b2);
|
||||||
|
assert_eq!(
|
||||||
|
next_event(&ev, "node_down(node-c)"),
|
||||||
|
NodeEvent::NodeDown(up_c.clone())
|
||||||
|
);
|
||||||
|
assert_quiet(&ev);
|
||||||
|
|
||||||
|
drop(b1);
|
||||||
|
mgr.shutdown();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Snapshot-then-stream: a subscriber arriving after connections established
|
||||||
|
/// receives one `NodeUp` per live peer before anything else, and the view
|
||||||
|
/// call agrees with it.
|
||||||
|
#[test]
|
||||||
|
fn late_subscriber_gets_snapshot_and_view_agrees() {
|
||||||
|
run(|| {
|
||||||
|
let mgr = GenServerBuilder::new(Manager::new())
|
||||||
|
.named(MANAGER)
|
||||||
|
.start()
|
||||||
|
.expect("manager name is free");
|
||||||
|
|
||||||
|
let t = TcpTransport;
|
||||||
|
let (a1, b1) = pair(&t);
|
||||||
|
let (a2, b2) = pair(&t);
|
||||||
|
spawn_established(FramedConn::new(a1), peer("node-b", 1), Timing::default())
|
||||||
|
.expect("node-b registers");
|
||||||
|
spawn_established(FramedConn::new(a2), peer("node-c", 1), Timing::default())
|
||||||
|
.expect("node-c registers");
|
||||||
|
|
||||||
|
let ev = subscribe().expect("manager is up");
|
||||||
|
let mut names = Vec::new();
|
||||||
|
for _ in 0..2 {
|
||||||
|
match next_event(&ev, "a snapshot node_up") {
|
||||||
|
NodeEvent::NodeUp(info) => names.push(info.name),
|
||||||
|
other => panic!("expected a snapshot node_up, got {other:?}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
names.sort();
|
||||||
|
assert_eq!(names, ["node-b", "node-c"]);
|
||||||
|
assert_quiet(&ev); // the snapshot is exactly the live set
|
||||||
|
|
||||||
|
let mut v = view().expect("manager is up");
|
||||||
|
v.sort_by(|a, b| a.name.cmp(&b.name));
|
||||||
|
assert_eq!(v.len(), 2);
|
||||||
|
assert_eq!(v[0].name, "node-b");
|
||||||
|
assert_eq!(v[1].name, "node-c");
|
||||||
|
|
||||||
|
disconnect("node-b");
|
||||||
|
disconnect("node-c");
|
||||||
|
drop((b1, b2));
|
||||||
|
// Drain the two downs so the subscription ends quiet.
|
||||||
|
let _ = next_event(&ev, "node_down");
|
||||||
|
let _ = next_event(&ev, "node_down");
|
||||||
|
mgr.shutdown();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// NodeId identity: a restart (same name, new incarnation) is a NEW id — the
|
||||||
|
/// ghost and its successor are distinguishable — while a reconnect blip (same
|
||||||
|
/// name, same incarnation) keeps its id.
|
||||||
|
#[test]
|
||||||
|
fn restart_gets_new_id_blip_keeps_id() {
|
||||||
|
run(|| {
|
||||||
|
let mgr = GenServerBuilder::new(Manager::new())
|
||||||
|
.named(MANAGER)
|
||||||
|
.start()
|
||||||
|
.expect("manager name is free");
|
||||||
|
let ev = subscribe().expect("manager is up");
|
||||||
|
let t = TcpTransport;
|
||||||
|
|
||||||
|
let id = |e: NodeEvent, what: &str| -> NodeId {
|
||||||
|
match e {
|
||||||
|
NodeEvent::NodeUp(info) => info.node,
|
||||||
|
other => panic!("expected node_up ({what}), got {other:?}"),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let down_id = |e: NodeEvent, what: &str| -> NodeId {
|
||||||
|
match e {
|
||||||
|
NodeEvent::NodeDown(info) => info.node,
|
||||||
|
other => panic!("expected node_down ({what}), got {other:?}"),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Up at incarnation 1, then the peer dies (EOF).
|
||||||
|
let (a1, b1) = pair(&t);
|
||||||
|
spawn_established(FramedConn::new(a1), peer("node-b", 1), Timing::default())
|
||||||
|
.expect("registers");
|
||||||
|
let id1 = id(next_event(&ev, "node_up inc 1"), "inc 1");
|
||||||
|
drop(b1);
|
||||||
|
assert_eq!(down_id(next_event(&ev, "node_down inc 1"), "inc 1"), id1);
|
||||||
|
|
||||||
|
// Restart: new incarnation, new id — the ghost's id is not reused.
|
||||||
|
let (a2, b2) = pair(&t);
|
||||||
|
spawn_established(FramedConn::new(a2), peer("node-b", 2), Timing::default())
|
||||||
|
.expect("registers");
|
||||||
|
let id2 = id(next_event(&ev, "node_up inc 2"), "inc 2");
|
||||||
|
assert_ne!(
|
||||||
|
id1, id2,
|
||||||
|
"a restarted node must be distinguishable from its ghost"
|
||||||
|
);
|
||||||
|
|
||||||
|
// Blip: the same incarnation reconnects and keeps its id.
|
||||||
|
disconnect("node-b");
|
||||||
|
assert_eq!(down_id(next_event(&ev, "node_down inc 2"), "inc 2"), id2);
|
||||||
|
let (a3, b3) = pair(&t);
|
||||||
|
spawn_established(FramedConn::new(a3), peer("node-b", 2), Timing::default())
|
||||||
|
.expect("registers");
|
||||||
|
let id3 = id(next_event(&ev, "node_up after blip"), "blip");
|
||||||
|
assert_eq!(
|
||||||
|
id2, id3,
|
||||||
|
"a reconnect at the same incarnation is the same node"
|
||||||
|
);
|
||||||
|
|
||||||
|
disconnect("node-b");
|
||||||
|
let _ = next_event(&ev, "final node_down");
|
||||||
|
drop((b2, b3));
|
||||||
|
mgr.shutdown();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A dropped subscriber is pruned on the next emit and never disturbs the
|
||||||
|
/// manager or a live subscriber.
|
||||||
|
#[test]
|
||||||
|
fn dead_subscriber_is_pruned() {
|
||||||
|
run(|| {
|
||||||
|
let mgr = GenServerBuilder::new(Manager::new())
|
||||||
|
.named(MANAGER)
|
||||||
|
.start()
|
||||||
|
.expect("manager name is free");
|
||||||
|
|
||||||
|
let dead = subscribe().expect("manager is up");
|
||||||
|
drop(dead);
|
||||||
|
let live = subscribe().expect("manager is up");
|
||||||
|
|
||||||
|
let t = TcpTransport;
|
||||||
|
let (a1, b1) = pair(&t);
|
||||||
|
spawn_established(FramedConn::new(a1), peer("node-b", 1), Timing::default())
|
||||||
|
.expect("registers");
|
||||||
|
match next_event(&live, "node_up despite a dead co-subscriber") {
|
||||||
|
NodeEvent::NodeUp(info) => assert_eq!(info.name, "node-b"),
|
||||||
|
other => panic!("expected node_up, got {other:?}"),
|
||||||
|
}
|
||||||
|
|
||||||
|
disconnect("node-b");
|
||||||
|
let _ = next_event(&live, "node_down");
|
||||||
|
drop(b1);
|
||||||
|
mgr.shutdown();
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -0,0 +1,175 @@
|
|||||||
|
//! RFC 010 c7 — the Phase 2 gate: a 3-node mesh under the subprocess
|
||||||
|
//! harness, repeatable.
|
||||||
|
//!
|
||||||
|
//! Each node process runs the integrated `cluster::start` (manager +
|
||||||
|
//! acceptor + connector + static seeds), subscribes to membership like any
|
||||||
|
//! consumer, and announces protocol-visible facts as lines:
|
||||||
|
//! `LISTENING <addr>`, `MEMBER-UP <name> inc=<n>`, `MEMBER-DOWN <name>`.
|
||||||
|
//! Then it **parks forever** — cross-process teardown is retractable state
|
||||||
|
//! (binding trap), so the parent SIGKILLs via `Node`'s `Drop` and clean exit
|
||||||
|
//! stays the c4 harness's own smoke test.
|
||||||
|
//!
|
||||||
|
//! Ports: nodes bind `:0` and report, so the mesh is built by seeding each
|
||||||
|
//! node with the previously-reported addresses (n1: no seeds; n2: n1;
|
||||||
|
//! n3: n1+n2 — inbound covers the reverse edges). The late-seed test is the
|
||||||
|
//! one exception: the parent pre-reserves a port by binding-and-closing it,
|
||||||
|
//! seeds one node with it, then starts the second node on that exact
|
||||||
|
//! address. In principle another process could steal the port in the gap;
|
||||||
|
//! in practice the window is microseconds on a local runner — accepted, and
|
||||||
|
//! confined to that one test.
|
||||||
|
#![cfg(feature = "cluster")]
|
||||||
|
|
||||||
|
mod common;
|
||||||
|
|
||||||
|
use common::{maybe_child, spawn_node, Node};
|
||||||
|
use smarm::cluster::envelope::NodeMeta;
|
||||||
|
use smarm::cluster::membership::{subscribe, NodeEvent};
|
||||||
|
use smarm::cluster::{start, Config, StaticSeeds, Timing};
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
const ROLES: &[(&str, fn())] = &[("node", role_node)];
|
||||||
|
|
||||||
|
/// A mesh node: identity and seeds from env, membership events to stdout,
|
||||||
|
/// park forever (the parent reaps).
|
||||||
|
fn role_node() {
|
||||||
|
let name = std::env::var("SMARM_NODE_NAME").expect("SMARM_NODE_NAME not set");
|
||||||
|
let listen = std::env::var("SMARM_LISTEN_ADDR").unwrap_or_else(|_| "127.0.0.1:0".to_string());
|
||||||
|
// Seeds: comma-separated `name=addr` pairs; empty or unset means none.
|
||||||
|
let seeds: Vec<(String, String)> = std::env::var("SMARM_SEEDS")
|
||||||
|
.unwrap_or_default()
|
||||||
|
.split(',')
|
||||||
|
.filter(|s| !s.is_empty())
|
||||||
|
.map(|s| {
|
||||||
|
let (n, a) = s.split_once('=').expect("seed must be name=addr");
|
||||||
|
(n.to_string(), a.to_string())
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
smarm::run(move || {
|
||||||
|
let cluster = start(Config {
|
||||||
|
node_name: name,
|
||||||
|
meta: NodeMeta {
|
||||||
|
role: "mesh-test".to_string(),
|
||||||
|
region: "local".to_string(),
|
||||||
|
},
|
||||||
|
listen_addr: listen,
|
||||||
|
strategy: Box::new(StaticSeeds::new(seeds)),
|
||||||
|
timing: Timing::default(),
|
||||||
|
})
|
||||||
|
.expect("listener binds");
|
||||||
|
println!("LISTENING {}", cluster.local_addr());
|
||||||
|
|
||||||
|
let events = subscribe().expect("manager is up");
|
||||||
|
loop {
|
||||||
|
match events.rx.recv() {
|
||||||
|
Ok(NodeEvent::NodeUp(info)) => {
|
||||||
|
println!("MEMBER-UP {} inc={}", info.name, info.incarnation.get());
|
||||||
|
}
|
||||||
|
Ok(NodeEvent::NodeDown(info)) => {
|
||||||
|
println!("MEMBER-DOWN {}", info.name);
|
||||||
|
}
|
||||||
|
Err(_) => break, // manager gone; park below regardless
|
||||||
|
}
|
||||||
|
}
|
||||||
|
loop {
|
||||||
|
smarm::sleep(Duration::from_secs(3600));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn spawn_mesh_node(name: &str, seeds: &str, listen: Option<&str>) -> Node {
|
||||||
|
let mut env: Vec<(&str, &str)> = vec![("SMARM_NODE_NAME", name), ("SMARM_SEEDS", seeds)];
|
||||||
|
if let Some(addr) = listen {
|
||||||
|
env.push(("SMARM_LISTEN_ADDR", addr));
|
||||||
|
}
|
||||||
|
spawn_node("node", &env)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Wait for `MEMBER-UP <peer> inc=<n>` and return the incarnation.
|
||||||
|
fn wait_member_up(node: &mut Node, peer: &str) -> u32 {
|
||||||
|
let prefix = format!("MEMBER-UP {peer} inc=");
|
||||||
|
let line = node.wait_line(&format!("MEMBER-UP {peer}"), |l| l.starts_with(&prefix));
|
||||||
|
line[prefix.len()..].parse().expect("incarnation parses")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn wait_member_down(node: &mut Node, peer: &str) {
|
||||||
|
let want = format!("MEMBER-DOWN {peer}");
|
||||||
|
node.wait_line(&want, |l| l == want);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The gate, plus the kill and restart facts, as one mesh's life: three
|
||||||
|
/// nodes form a full mesh (every node sees both others up); killing one
|
||||||
|
/// yields `node_down` at both survivors; its restart under the same name
|
||||||
|
/// arrives as a NEW incarnation — the ghost and its successor are
|
||||||
|
/// distinguishable at every observer.
|
||||||
|
#[test]
|
||||||
|
fn three_node_mesh_forms_then_kill_then_restart_distinguishable() {
|
||||||
|
maybe_child(ROLES);
|
||||||
|
|
||||||
|
let mut n1 = spawn_mesh_node("node-1", "", None);
|
||||||
|
let a1 = n1.wait_listening();
|
||||||
|
let mut n2 = spawn_mesh_node("node-2", &format!("node-1={a1}"), None);
|
||||||
|
let a2 = n2.wait_listening();
|
||||||
|
let mut n3 = spawn_mesh_node("node-3", &format!("node-1={a1},node-2={a2}"), None);
|
||||||
|
let _a3 = n3.wait_listening();
|
||||||
|
|
||||||
|
// Full mesh: each node reports both peers up (dialed or inbound alike).
|
||||||
|
wait_member_up(&mut n1, "node-2");
|
||||||
|
let inc3_at_n1 = wait_member_up(&mut n1, "node-3");
|
||||||
|
wait_member_up(&mut n2, "node-1");
|
||||||
|
let inc3_at_n2 = wait_member_up(&mut n2, "node-3");
|
||||||
|
wait_member_up(&mut n3, "node-1");
|
||||||
|
wait_member_up(&mut n3, "node-2");
|
||||||
|
assert_eq!(
|
||||||
|
inc3_at_n1, inc3_at_n2,
|
||||||
|
"one node, one incarnation, all observers"
|
||||||
|
);
|
||||||
|
|
||||||
|
// Kill node-3 (SIGKILL via Drop): node_down at both survivors.
|
||||||
|
drop(n3);
|
||||||
|
wait_member_down(&mut n1, "node-3");
|
||||||
|
wait_member_down(&mut n2, "node-3");
|
||||||
|
|
||||||
|
// Restart node-3 under the same name: it re-dials its seeds and comes
|
||||||
|
// up everywhere as a new incarnation — never the ghost's.
|
||||||
|
let mut n3b = spawn_mesh_node("node-3", &format!("node-1={a1},node-2={a2}"), None);
|
||||||
|
let _ = n3b.wait_listening();
|
||||||
|
let inc3b_at_n1 = wait_member_up(&mut n1, "node-3");
|
||||||
|
let inc3b_at_n2 = wait_member_up(&mut n2, "node-3");
|
||||||
|
assert_eq!(inc3b_at_n1, inc3b_at_n2);
|
||||||
|
assert_ne!(
|
||||||
|
inc3_at_n1, inc3b_at_n1,
|
||||||
|
"a restarted node must be distinguishable from its ghost"
|
||||||
|
);
|
||||||
|
wait_member_up(&mut n3b, "node-1");
|
||||||
|
wait_member_up(&mut n3b, "node-2");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A seed that is unreachable at start is not fatal: the connector retries
|
||||||
|
/// on backoff, and when a node finally appears at that address, the mesh
|
||||||
|
/// edge forms.
|
||||||
|
#[test]
|
||||||
|
fn seed_unreachable_at_start_then_arriving_later() {
|
||||||
|
maybe_child(ROLES);
|
||||||
|
|
||||||
|
// Pre-reserve an address by binding and immediately closing it (see the
|
||||||
|
// module docs for the accepted steal window). Dials to it are refused
|
||||||
|
// until node-b starts there.
|
||||||
|
let reserved = {
|
||||||
|
let l = std::net::TcpListener::bind("127.0.0.1:0").expect("bind");
|
||||||
|
l.local_addr().expect("addr").to_string()
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut a = spawn_mesh_node("node-a", &format!("node-b={reserved}"), None);
|
||||||
|
let _ = a.wait_listening();
|
||||||
|
|
||||||
|
// Let a few refused attempts happen before the seed comes up, so the
|
||||||
|
// retry path is what forms the edge (backoff cap 5s < harness WAIT 10s).
|
||||||
|
std::thread::sleep(Duration::from_millis(600));
|
||||||
|
|
||||||
|
let mut b = spawn_mesh_node("node-b", "", Some(&reserved));
|
||||||
|
let _ = b.wait_listening();
|
||||||
|
|
||||||
|
wait_member_up(&mut a, "node-b");
|
||||||
|
wait_member_up(&mut b, "node-a");
|
||||||
|
}
|
||||||
@@ -0,0 +1,359 @@
|
|||||||
|
//! RFC 010 c12 — remote monitors.
|
||||||
|
//!
|
||||||
|
//! Local suite (`run()`, no network): the immediate answers — no connection
|
||||||
|
//! ⇒ `Disconnected`, dead incarnation ⇒ `NoProc` — and the self-node
|
||||||
|
//! collapse (a plain local monitor underneath, incl. `demonitor_remote`).
|
||||||
|
//!
|
||||||
|
//! Cross-process: a *server* exposes a control name and spawns workers on
|
||||||
|
//! request, replying with each worker's pid (via `RemotePid::from_local`,
|
||||||
|
//! the D12 set-site) or, for the deliberately unshipped one, only its raw
|
||||||
|
//! slot numbers. The *client* monitors them and asserts: kill ⇒ the true
|
||||||
|
//! reason (Exit / Panic); a corpse ⇒ its recorded terminal reason, not
|
||||||
|
//! NoProc; a live pid that never crossed the wire ⇒ NoProc (no liveness
|
||||||
|
//! leak); a demonitor racing the kill ⇒ no notice, proven by stream ORDER
|
||||||
|
//! (a later notice on the same connection arrives while the earlier slot
|
||||||
|
//! is still empty), not by sleeping.
|
||||||
|
#![cfg(feature = "cluster")]
|
||||||
|
|
||||||
|
mod common;
|
||||||
|
|
||||||
|
use common::{maybe_child, spawn_node};
|
||||||
|
use smarm::cluster::envelope::NodeMeta;
|
||||||
|
use smarm::cluster::expose::{expose, expose_type};
|
||||||
|
use smarm::cluster::membership::{subscribe, NodeEvent};
|
||||||
|
use smarm::cluster::remote::{
|
||||||
|
self, demonitor_remote, monitor_remote, send_to_remote, RemoteName, RemotePid,
|
||||||
|
};
|
||||||
|
use smarm::cluster::{start, Config, RemoteDownReason, StaticSeeds, Timing};
|
||||||
|
use smarm::pg::Incarnation;
|
||||||
|
use smarm::{channel, install, register, run, spawn, Addressable, DownReason, Erased, Name, Pid};
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
// ---- message types (hand-rolled serde; the crate is derive-less) ---------
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct Ctl {
|
||||||
|
cmd: String,
|
||||||
|
reply_to: RemotePid<Client>,
|
||||||
|
}
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct Answer {
|
||||||
|
text: String,
|
||||||
|
pid: Option<RemotePid<Erased>>,
|
||||||
|
}
|
||||||
|
struct Client;
|
||||||
|
impl Addressable for Client {
|
||||||
|
type Msg = Answer;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl serde::Serialize for Ctl {
|
||||||
|
fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
|
||||||
|
use serde::ser::SerializeTuple;
|
||||||
|
let mut t = s.serialize_tuple(2)?;
|
||||||
|
t.serialize_element(&self.cmd)?;
|
||||||
|
t.serialize_element(&self.reply_to)?;
|
||||||
|
t.end()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<'de> serde::Deserialize<'de> for Ctl {
|
||||||
|
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
|
||||||
|
let (cmd, reply_to) = <(String, RemotePid<Client>)>::deserialize(d)?;
|
||||||
|
Ok(Ctl { cmd, reply_to })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl serde::Serialize for Answer {
|
||||||
|
fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
|
||||||
|
use serde::ser::SerializeTuple;
|
||||||
|
let mut t = s.serialize_tuple(2)?;
|
||||||
|
t.serialize_element(&self.text)?;
|
||||||
|
t.serialize_element(&self.pid)?;
|
||||||
|
t.end()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<'de> serde::Deserialize<'de> for Answer {
|
||||||
|
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
|
||||||
|
let (text, pid) = <(String, Option<RemotePid<Erased>>)>::deserialize(d)?;
|
||||||
|
Ok(Answer { text, pid })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ================= local suite =========================================
|
||||||
|
|
||||||
|
/// No connection to the pid's node: `Disconnected` at once — the remote
|
||||||
|
/// analog of NoProc, and the first thing c11's variant is for.
|
||||||
|
#[test]
|
||||||
|
fn unconnected_node_is_disconnected_immediately() {
|
||||||
|
maybe_child(ROLES);
|
||||||
|
run(|| {
|
||||||
|
remote::set_local_identity("me", Incarnation::new(7));
|
||||||
|
let ghost = RemotePid::<Erased>::from_parts("nowhere", Incarnation::new(1), 3, 1);
|
||||||
|
let m = monitor_remote(ghost.clone());
|
||||||
|
let d = m.recv().unwrap();
|
||||||
|
assert_eq!(d.pid, ghost);
|
||||||
|
assert_eq!(d.reason, RemoteDownReason::Disconnected);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The node is connected but the pid names an earlier incarnation: the
|
||||||
|
/// actor is a known corpse (RFC v2 §3), so `NoProc` at once — never
|
||||||
|
/// `Disconnected`, nothing on the wire.
|
||||||
|
#[test]
|
||||||
|
fn dead_incarnation_is_noproc_immediately() {
|
||||||
|
maybe_child(ROLES);
|
||||||
|
run(|| {
|
||||||
|
remote::set_local_identity("me", Incarnation::new(7));
|
||||||
|
let (probe_tx, probe_rx) = channel();
|
||||||
|
remote::bind_outbound_probe("peer", Incarnation::new(5), probe_tx);
|
||||||
|
let stale = RemotePid::<Erased>::from_parts("peer", Incarnation::new(4), 9, 1);
|
||||||
|
let m = monitor_remote(stale);
|
||||||
|
assert_eq!(m.recv().unwrap().reason, DownReason::NoProc.into());
|
||||||
|
assert!(probe_rx.try_recv().unwrap().is_none(), "no frame emitted");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A self-node pid collapses to an ordinary local monitor: the true reason
|
||||||
|
/// on exit, and `demonitor_remote` cancels it.
|
||||||
|
#[test]
|
||||||
|
fn self_node_pid_collapses_to_local_monitor() {
|
||||||
|
maybe_child(ROLES);
|
||||||
|
run(|| {
|
||||||
|
remote::set_local_identity("me", Incarnation::new(7));
|
||||||
|
let (go_tx, go_rx) = channel::<()>();
|
||||||
|
let (go2_tx, go2_rx) = channel::<()>();
|
||||||
|
let a = spawn(move || {
|
||||||
|
let _ = go_rx.recv();
|
||||||
|
})
|
||||||
|
.pid();
|
||||||
|
let b = spawn(move || {
|
||||||
|
let _ = go2_rx.recv();
|
||||||
|
})
|
||||||
|
.pid();
|
||||||
|
let ma = monitor_remote(RemotePid::from_local(a).expect("identity set"));
|
||||||
|
let mb = monitor_remote(RemotePid::from_local(b).expect("identity set"));
|
||||||
|
assert_ne!(ma.id, mb.id);
|
||||||
|
assert!(ma.target.local() == Some(a));
|
||||||
|
|
||||||
|
demonitor_remote(&mb);
|
||||||
|
go2_tx.send(()).unwrap();
|
||||||
|
go_tx.send(()).unwrap();
|
||||||
|
let d = ma.recv().unwrap();
|
||||||
|
assert_eq!(d.reason, DownReason::Exit.into());
|
||||||
|
assert_eq!(d.pid.local(), Some(a));
|
||||||
|
// `a` is down (its notice arrived), and `b` was killed first on the
|
||||||
|
// same scheduler — a notice for `b` would be here by now. After a
|
||||||
|
// demonitor the channel is closed-empty (`Err`), like the local one.
|
||||||
|
assert!(matches!(mb.try_recv(), Ok(None) | Err(_)));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// ================= cross-process ======================================
|
||||||
|
|
||||||
|
const ROLES: &[(&str, fn())] = &[("server", role_server), ("client", role_client)];
|
||||||
|
|
||||||
|
const CTL: Name<Ctl> = Name::new("c12.ctl");
|
||||||
|
|
||||||
|
fn cfg(name: &str, seeds: Vec<(String, String)>) -> Config {
|
||||||
|
Config {
|
||||||
|
node_name: name.to_string(),
|
||||||
|
meta: NodeMeta {
|
||||||
|
role: "c12".into(),
|
||||||
|
region: "local".into(),
|
||||||
|
},
|
||||||
|
listen_addr: std::env::var("SMARM_LISTEN_ADDR").unwrap_or_else(|_| "127.0.0.1:0".into()),
|
||||||
|
strategy: Box::new(StaticSeeds::new(seeds)),
|
||||||
|
timing: Timing::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn wait_up(events: &smarm::cluster::membership::MembershipEvents, who: &str) {
|
||||||
|
loop {
|
||||||
|
match events.rx.recv() {
|
||||||
|
Ok(NodeEvent::NodeUp(i)) if i.name == who => return,
|
||||||
|
Ok(_) => continue,
|
||||||
|
Err(_) => panic!("manager gone"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Server commands (all answered to `reply_to`):
|
||||||
|
/// - `spawn:exit` / `spawn:panic` — a parked worker; `kill:<index>` releases
|
||||||
|
/// it, whereupon it returns / panics. Answer carries its pid.
|
||||||
|
/// - `spawn:corpse` — a worker that has already exited when the answer is
|
||||||
|
/// sent; the pid was shipped (watchable) before it died.
|
||||||
|
/// - `spawn:unwatched` — a parked worker whose pid is NEVER shipped; the
|
||||||
|
/// answer carries only `text = "slot:<index>:<generation>"`.
|
||||||
|
fn role_server() {
|
||||||
|
smarm::run(move || {
|
||||||
|
let cluster = start(cfg("server", vec![])).expect("binds");
|
||||||
|
println!("LISTENING {}", cluster.local_addr());
|
||||||
|
let (tx, rx) = channel::<Ctl>();
|
||||||
|
register(CTL, tx).unwrap();
|
||||||
|
expose(CTL);
|
||||||
|
println!("READY");
|
||||||
|
let mut workers: HashMap<u32, smarm::channel::Sender<()>> = HashMap::new();
|
||||||
|
loop {
|
||||||
|
let ctl = rx.recv().unwrap();
|
||||||
|
println!("CTL {}", ctl.cmd);
|
||||||
|
let (text, pid): (String, Option<RemotePid<Erased>>) = match ctl.cmd.as_str() {
|
||||||
|
"spawn:exit" | "spawn:panic" => {
|
||||||
|
let panic = ctl.cmd == "spawn:panic";
|
||||||
|
let (go_tx, go_rx) = channel::<()>();
|
||||||
|
let p: Pid = spawn(move || {
|
||||||
|
let _ = go_rx.recv();
|
||||||
|
if panic {
|
||||||
|
panic!("worker asked to panic");
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.pid();
|
||||||
|
workers.insert(p.index(), go_tx);
|
||||||
|
(
|
||||||
|
"ok".into(),
|
||||||
|
Some(RemotePid::from_local(p).expect("identity set")),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
"spawn:corpse" => {
|
||||||
|
let p: Pid = spawn(|| {}).pid();
|
||||||
|
let rp = RemotePid::from_local(p).expect("identity set"); // shipped ⇒ watchable
|
||||||
|
let m = smarm::monitor(p);
|
||||||
|
let _ = m.rx.recv(); // dead before the answer goes out
|
||||||
|
("ok".into(), Some(rp))
|
||||||
|
}
|
||||||
|
"spawn:unwatched" => {
|
||||||
|
let (go_tx, go_rx) = channel::<()>();
|
||||||
|
let p: Pid = spawn(move || {
|
||||||
|
let _ = go_rx.recv();
|
||||||
|
})
|
||||||
|
.pid();
|
||||||
|
workers.insert(p.index(), go_tx);
|
||||||
|
(format!("slot:{}:{}", p.index(), p.generation()), None)
|
||||||
|
}
|
||||||
|
other => {
|
||||||
|
let idx: u32 = other.strip_prefix("kill:").unwrap().parse().unwrap();
|
||||||
|
if let Some(go) = workers.remove(&idx) {
|
||||||
|
let _ = go.send(());
|
||||||
|
}
|
||||||
|
("killed".into(), None)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
send_to_remote(ctl.reply_to, Answer { text, pid }).unwrap();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn role_client() {
|
||||||
|
let server_addr = std::env::var("SMARM_SERVER_ADDR").expect("SMARM_SERVER_ADDR");
|
||||||
|
smarm::run(move || {
|
||||||
|
let _cluster = start(cfg("client", vec![("server".into(), server_addr)])).expect("binds");
|
||||||
|
let ev = subscribe().unwrap();
|
||||||
|
wait_up(&ev, "server");
|
||||||
|
let (tx, rx) = channel::<Answer>();
|
||||||
|
let me: Pid<Client> = install::<Client>(tx);
|
||||||
|
expose_type::<Answer>();
|
||||||
|
let ask = |cmd: &str| -> Answer {
|
||||||
|
remote::send(
|
||||||
|
RemoteName::new("server", CTL),
|
||||||
|
Ctl {
|
||||||
|
cmd: cmd.into(),
|
||||||
|
reply_to: RemotePid::from_local(me).expect("identity set"),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
rx.recv().unwrap()
|
||||||
|
};
|
||||||
|
let server_inc = ev_incarnation();
|
||||||
|
|
||||||
|
// 1. kill ⇒ true reason (Exit).
|
||||||
|
let a = ask("spawn:exit").pid.unwrap();
|
||||||
|
let ma = monitor_remote(a.clone());
|
||||||
|
ask(&format!("kill:{}", a.index()));
|
||||||
|
let d = ma.recv().unwrap();
|
||||||
|
assert_eq!(d.pid, a);
|
||||||
|
println!("DOWN exit {:?}", d.reason);
|
||||||
|
|
||||||
|
// 2. kill ⇒ true reason (Panic).
|
||||||
|
let b = ask("spawn:panic").pid.unwrap();
|
||||||
|
let mb = monitor_remote(b.clone());
|
||||||
|
ask(&format!("kill:{}", b.index()));
|
||||||
|
println!("DOWN panic {:?}", mb.recv().unwrap().reason);
|
||||||
|
|
||||||
|
// 3. corpse ⇒ recorded terminal reason, not NoProc.
|
||||||
|
let c = ask("spawn:corpse").pid.unwrap();
|
||||||
|
println!("DOWN corpse {:?}", monitor_remote(c).recv().unwrap().reason);
|
||||||
|
|
||||||
|
// 4. live but never shipped/exposed ⇒ NoProc (no leak); a made-up
|
||||||
|
// slot on the same node ⇒ NoProc too, indistinguishably.
|
||||||
|
let ans = ask("spawn:unwatched");
|
||||||
|
let mut it = ans.text.strip_prefix("slot:").unwrap().split(':');
|
||||||
|
let (idx, gen): (u32, u32) = (
|
||||||
|
it.next().unwrap().parse().unwrap(),
|
||||||
|
it.next().unwrap().parse().unwrap(),
|
||||||
|
);
|
||||||
|
let hidden = RemotePid::<Erased>::from_parts("server", server_inc, idx, gen);
|
||||||
|
println!(
|
||||||
|
"DOWN hidden {:?}",
|
||||||
|
monitor_remote(hidden).recv().unwrap().reason
|
||||||
|
);
|
||||||
|
let bogus = RemotePid::<Erased>::from_parts("server", server_inc, 100_000, 1);
|
||||||
|
println!(
|
||||||
|
"DOWN bogus {:?}",
|
||||||
|
monitor_remote(bogus).recv().unwrap().reason
|
||||||
|
);
|
||||||
|
|
||||||
|
// 5. demonitor races the kill: no notice for `d1`, proven by order —
|
||||||
|
// `d2`'s notice (same connection, later) arrives while `d1`'s
|
||||||
|
// slot is still empty.
|
||||||
|
let d1 = ask("spawn:exit").pid.unwrap();
|
||||||
|
let m1 = monitor_remote(d1.clone());
|
||||||
|
demonitor_remote(&m1);
|
||||||
|
ask(&format!("kill:{}", d1.index()));
|
||||||
|
let d2 = ask("spawn:exit").pid.unwrap();
|
||||||
|
let m2 = monitor_remote(d2.clone());
|
||||||
|
ask(&format!("kill:{}", d2.index()));
|
||||||
|
assert_eq!(m2.recv().unwrap().reason, DownReason::Exit.into());
|
||||||
|
// Closed-empty (`Err`) or open-empty (`Ok(None)`) both mean no notice.
|
||||||
|
let stray = matches!(m1.try_recv(), Ok(Some(_)));
|
||||||
|
println!("DEMONITOR stray={stray}");
|
||||||
|
|
||||||
|
println!("CLIENT DONE");
|
||||||
|
loop {
|
||||||
|
smarm::sleep(Duration::from_secs(3600));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The server's incarnation as this node sees it — for building pids by hand.
|
||||||
|
fn ev_incarnation() -> Incarnation {
|
||||||
|
smarm::cluster::membership::view()
|
||||||
|
.expect("manager up")
|
||||||
|
.into_iter()
|
||||||
|
.find(|i| i.name == "server")
|
||||||
|
.map(|i| i.incarnation)
|
||||||
|
.expect("server in view")
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The Phase 4 c12 gate: remote monitors report the true reason, honour
|
||||||
|
/// corpses, leak nothing for unshipped pids, and cancel cleanly.
|
||||||
|
#[test]
|
||||||
|
fn remote_monitors_report_true_reasons() {
|
||||||
|
maybe_child(ROLES);
|
||||||
|
let mut server = spawn_node("server", &[]);
|
||||||
|
let saddr = server.wait_listening();
|
||||||
|
server.wait_line("READY", |l| l == "READY");
|
||||||
|
let mut client = spawn_node("client", &[("SMARM_SERVER_ADDR", &saddr)]);
|
||||||
|
client.wait_line("DOWN exit Local(Exit)", |l| l == "DOWN exit Local(Exit)");
|
||||||
|
client.wait_line("DOWN panic Local(Panic)", |l| {
|
||||||
|
l == "DOWN panic Local(Panic)"
|
||||||
|
});
|
||||||
|
client.wait_line("DOWN corpse Local(Exit)", |l| {
|
||||||
|
l == "DOWN corpse Local(Exit)"
|
||||||
|
});
|
||||||
|
client.wait_line("DOWN hidden Local(NoProc)", |l| {
|
||||||
|
l == "DOWN hidden Local(NoProc)"
|
||||||
|
});
|
||||||
|
client.wait_line("DOWN bogus Local(NoProc)", |l| {
|
||||||
|
l == "DOWN bogus Local(NoProc)"
|
||||||
|
});
|
||||||
|
client.wait_line("DEMONITOR stray=false", |l| l == "DEMONITOR stray=false");
|
||||||
|
client.wait_line("CLIENT DONE", |l| l == "CLIENT DONE");
|
||||||
|
}
|
||||||
@@ -0,0 +1,254 @@
|
|||||||
|
//! RFC 010 c15 — distributed pg: sync on `NodeUp`, incremental
|
||||||
|
//! `Join`/`Leave`, eager eviction announced, `NodeDown` sweep.
|
||||||
|
//!
|
||||||
|
//! Two nodes. The *origin* joins two local workers to `"pool"` before the
|
||||||
|
//! *observer* connects (so the observer's view comes from `Sync`), exposes a
|
||||||
|
//! `"go"` command inbox and then does exactly what the observer tells it:
|
||||||
|
//! kill one worker, join a third, leave with the second. The observer drives
|
||||||
|
//! that script through the cluster itself and asserts every step from
|
||||||
|
//! `members_all` — never touching the group on its own side, except once to
|
||||||
|
//! prove a mixed local+remote group reads correctly and that `members` stays
|
||||||
|
//! local. `dispatch_any` is exercised both ways: into the origin's worker
|
||||||
|
//! (remote pick, `send_to_remote`) and, once the origin is gone, into the
|
||||||
|
//! observer's own (local pick, `send_to`). Finally the parent SIGKILLs the
|
||||||
|
//! origin: the observer must sweep every remote member on `NodeDown`.
|
||||||
|
#![cfg(feature = "cluster")]
|
||||||
|
|
||||||
|
mod common;
|
||||||
|
|
||||||
|
use common::{maybe_child, spawn_node};
|
||||||
|
use smarm::cluster::envelope::NodeMeta;
|
||||||
|
use smarm::cluster::expose::{expose, expose_type};
|
||||||
|
use smarm::cluster::membership::{subscribe, NodeEvent};
|
||||||
|
use smarm::cluster::remote::{self, RemoteName};
|
||||||
|
use smarm::cluster::{
|
||||||
|
dispatch_any, members_all, pick_any, start, Config, DispatchAnyError, GroupMember, StaticSeeds,
|
||||||
|
Timing,
|
||||||
|
};
|
||||||
|
use smarm::{channel, join, leave, members, register, send_to, spawn_addr, Addressable, Name, Pid};
|
||||||
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
|
const GO: Name<u8> = Name::new("go");
|
||||||
|
const POOL: &str = "pool";
|
||||||
|
|
||||||
|
/// A pool worker's message: `"die"` stops it, anything else is printed.
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
struct Job(String);
|
||||||
|
struct Worker;
|
||||||
|
impl Addressable for Worker {
|
||||||
|
type Msg = Job;
|
||||||
|
}
|
||||||
|
impl serde::Serialize for Job {
|
||||||
|
fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
|
||||||
|
self.0.serialize(s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<'de> serde::Deserialize<'de> for Job {
|
||||||
|
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
|
||||||
|
String::deserialize(d).map(Job)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const ROLES: &[(&str, fn())] = &[("origin", role_origin), ("observer", role_observer)];
|
||||||
|
|
||||||
|
fn cfg(name: &str, seeds: Vec<(String, String)>) -> Config {
|
||||||
|
Config {
|
||||||
|
node_name: name.into(),
|
||||||
|
meta: NodeMeta {
|
||||||
|
role: "c15".into(),
|
||||||
|
region: "local".into(),
|
||||||
|
},
|
||||||
|
listen_addr: "127.0.0.1:0".into(),
|
||||||
|
strategy: Box::new(StaticSeeds::new(seeds)),
|
||||||
|
timing: Timing::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A pool worker: prints every job it is handed, exits on `"die"`.
|
||||||
|
fn worker() -> Pid<Worker> {
|
||||||
|
spawn_addr::<Worker>(|rx| {
|
||||||
|
while let Ok(Job(s)) = rx.recv() {
|
||||||
|
if s == "die" {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
println!("JOB {s}");
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn role_origin() {
|
||||||
|
smarm::run(|| {
|
||||||
|
let cluster = start(cfg("origin", vec![])).expect("binds");
|
||||||
|
// Remote dispatch lands here only for a type this node accepts.
|
||||||
|
expose_type::<Job>();
|
||||||
|
let w1 = worker();
|
||||||
|
let w2 = worker();
|
||||||
|
assert!(join(POOL, w1));
|
||||||
|
assert!(join(POOL, w2));
|
||||||
|
let (go_tx, go_rx) = channel::<u8>();
|
||||||
|
register(GO, go_tx).unwrap();
|
||||||
|
expose(GO);
|
||||||
|
println!("LISTENING {}", cluster.local_addr());
|
||||||
|
println!("JOINED 2");
|
||||||
|
loop {
|
||||||
|
match go_rx.recv().unwrap() {
|
||||||
|
1 => {
|
||||||
|
send_to(w1, Job("die".into())).unwrap();
|
||||||
|
println!("KILLED w1");
|
||||||
|
}
|
||||||
|
2 => {
|
||||||
|
assert!(leave(POOL, w2));
|
||||||
|
println!("LEFT w2");
|
||||||
|
}
|
||||||
|
3 => {
|
||||||
|
let w3 = worker();
|
||||||
|
assert!(join(POOL, w3));
|
||||||
|
println!("JOINED w3");
|
||||||
|
}
|
||||||
|
n => panic!("unknown command {n}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn remote_count(group: &str) -> usize {
|
||||||
|
members_all(group)
|
||||||
|
.iter()
|
||||||
|
.filter(|m| matches!(m, GroupMember::Remote(_)))
|
||||||
|
.count()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Cooperative poll until `pred`; panics (with the last view) on timeout.
|
||||||
|
fn wait_view(what: &str, group: &str, pred: impl Fn(&[GroupMember]) -> bool) {
|
||||||
|
let deadline = Instant::now() + Duration::from_secs(5);
|
||||||
|
loop {
|
||||||
|
let v = members_all(group);
|
||||||
|
if pred(&v) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
assert!(
|
||||||
|
Instant::now() < deadline,
|
||||||
|
"timed out waiting for {what}; view = {v:?}"
|
||||||
|
);
|
||||||
|
smarm::sleep(Duration::from_millis(5));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn role_observer() {
|
||||||
|
let origin_addr = std::env::var("SMARM_ORIGIN_ADDR").expect("SMARM_ORIGIN_ADDR");
|
||||||
|
smarm::run(move || {
|
||||||
|
let _cluster = start(cfg("observer", vec![("origin".into(), origin_addr)])).expect("binds");
|
||||||
|
let ev = subscribe().unwrap();
|
||||||
|
loop {
|
||||||
|
match ev.rx.recv().unwrap() {
|
||||||
|
NodeEvent::NodeUp(i) if i.name == "origin" => break,
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let go = |n: u8| remote::send(RemoteName::new("origin", GO), n).unwrap();
|
||||||
|
|
||||||
|
// Sync: both pre-existing members arrive with no join on this side.
|
||||||
|
wait_view("sync of 2 remote members", POOL, |v| {
|
||||||
|
v.len() == 2 && v.iter().all(|m| matches!(m, GroupMember::Remote(_)))
|
||||||
|
});
|
||||||
|
let synced = members_all(POOL);
|
||||||
|
assert!(synced.iter().all(|m| match m {
|
||||||
|
GroupMember::Remote(p) => p.node() == "origin",
|
||||||
|
GroupMember::Local(_) => false,
|
||||||
|
}));
|
||||||
|
println!("SEES 2");
|
||||||
|
|
||||||
|
// Origin-side death: the origin's reaper announces the leave.
|
||||||
|
go(1);
|
||||||
|
wait_view("death evicted on observer", POOL, |v| v.len() == 1);
|
||||||
|
println!("SEES 1 after death");
|
||||||
|
|
||||||
|
// Incremental Join.
|
||||||
|
go(3);
|
||||||
|
wait_view("incremental join", POOL, |v| v.len() == 2);
|
||||||
|
println!("SEES 2 after join");
|
||||||
|
|
||||||
|
// Voluntary Leave.
|
||||||
|
go(2);
|
||||||
|
wait_view("incremental leave", POOL, |v| v.len() == 1);
|
||||||
|
println!("SEES 1 after leave");
|
||||||
|
|
||||||
|
// Mixed group: our own member sits beside the remote one in
|
||||||
|
// `members_all`; `members` stays local-only.
|
||||||
|
let me = worker();
|
||||||
|
assert!(join(POOL, me));
|
||||||
|
wait_view("mixed local+remote", POOL, |v| {
|
||||||
|
v.len() == 2 && v.contains(&GroupMember::Local(me.erase()))
|
||||||
|
});
|
||||||
|
assert_eq!(
|
||||||
|
members(POOL),
|
||||||
|
vec![me.erase()],
|
||||||
|
"local API never shows remotes"
|
||||||
|
);
|
||||||
|
assert_eq!(remote_count(POOL), 1);
|
||||||
|
println!("MIXED ok");
|
||||||
|
|
||||||
|
// dispatch_any: the store's first entry is the origin's w3 (it was
|
||||||
|
// announced before we joined), so the pick is remote and the job
|
||||||
|
// crosses the wire — the origin's worker prints it.
|
||||||
|
let picked = pick_any(POOL).expect("pool has members");
|
||||||
|
assert!(
|
||||||
|
matches!(picked, GroupMember::Remote(_)),
|
||||||
|
"first entry is remote: {picked:?}"
|
||||||
|
);
|
||||||
|
let reached = dispatch_any::<Worker>(POOL, Job("from-observer".into())).unwrap();
|
||||||
|
assert_eq!(reached, picked);
|
||||||
|
println!("DISPATCHED remote");
|
||||||
|
|
||||||
|
println!("PARK");
|
||||||
|
// Parent SIGKILLs the origin now: NodeDown must sweep its member,
|
||||||
|
// ours must survive.
|
||||||
|
wait_view("node_down sweep", POOL, |v| {
|
||||||
|
v == [GroupMember::Local(me.erase())]
|
||||||
|
});
|
||||||
|
assert_eq!(members(POOL), vec![me.erase()]);
|
||||||
|
println!("SWEPT");
|
||||||
|
|
||||||
|
// Now the only member is ours: a local pick, a local send.
|
||||||
|
let reached = dispatch_any::<Worker>(POOL, Job("local".into())).unwrap();
|
||||||
|
assert_eq!(reached, GroupMember::Local(me.erase()));
|
||||||
|
// And an empty group hands the message back.
|
||||||
|
match dispatch_any::<Worker>("nobody", Job("lost".into())) {
|
||||||
|
Err(DispatchAnyError::NoMember(Job(s))) => assert_eq!(s, "lost"),
|
||||||
|
other => panic!("expected NoMember, got {other:?}"),
|
||||||
|
}
|
||||||
|
println!("DISPATCHED local");
|
||||||
|
loop {
|
||||||
|
smarm::sleep(Duration::from_secs(3600));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The Phase 5 gate: sync, join, leave, death, node_down — all observed from
|
||||||
|
/// the peer, none of them a group operation on the peer — plus dispatch_any
|
||||||
|
/// reaching a remote member and a local one.
|
||||||
|
#[test]
|
||||||
|
fn groups_span_two_nodes() {
|
||||||
|
maybe_child(ROLES);
|
||||||
|
let mut origin = spawn_node("origin", &[]);
|
||||||
|
let addr = origin.wait_listening();
|
||||||
|
origin.wait_line("JOINED 2", |l| l == "JOINED 2");
|
||||||
|
let mut observer = spawn_node("observer", &[("SMARM_ORIGIN_ADDR", &addr)]);
|
||||||
|
observer.wait_line("SEES 2", |l| l == "SEES 2");
|
||||||
|
origin.wait_line("KILLED w1", |l| l == "KILLED w1");
|
||||||
|
observer.wait_line("SEES 1 after death", |l| l == "SEES 1 after death");
|
||||||
|
origin.wait_line("JOINED w3", |l| l == "JOINED w3");
|
||||||
|
observer.wait_line("SEES 2 after join", |l| l == "SEES 2 after join");
|
||||||
|
origin.wait_line("LEFT w2", |l| l == "LEFT w2");
|
||||||
|
observer.wait_line("SEES 1 after leave", |l| l == "SEES 1 after leave");
|
||||||
|
observer.wait_line("MIXED ok", |l| l == "MIXED ok");
|
||||||
|
observer.wait_line("DISPATCHED remote", |l| l == "DISPATCHED remote");
|
||||||
|
origin.wait_line("JOB from-observer", |l| l == "JOB from-observer");
|
||||||
|
observer.wait_line("PARK", |l| l == "PARK");
|
||||||
|
origin.kill();
|
||||||
|
observer.wait_line("SWEPT", |l| l == "SWEPT");
|
||||||
|
// Order between the root's line and the worker's is scheduling; wait
|
||||||
|
// for the later one to be certain both happened.
|
||||||
|
observer.wait_line("DISPATCHED local", |l| l == "DISPATCHED local");
|
||||||
|
observer.wait_line("JOB local", |l| l == "JOB local");
|
||||||
|
}
|
||||||
@@ -0,0 +1,356 @@
|
|||||||
|
//! RFC 010 c10 — pid targeting + auto-serialization. The Phase 3 gate:
|
||||||
|
//! cross-node call/reply with no ceremony, under the subprocess harness.
|
||||||
|
//!
|
||||||
|
//! Local suite (`run()`, no network): serialize/deserialize shapes,
|
||||||
|
//! self-collapse, the outside-runtime contract, the local send-site
|
||||||
|
//! incarnation check with a probe proving **no frame is emitted**.
|
||||||
|
//!
|
||||||
|
//! Cross-process: two nodes. The *server* exposes a `Name<Req>`; the
|
||||||
|
//! *client* sends a `Req` carrying its own `Pid<Reply>` (auto-serialized to
|
||||||
|
//! a `RemotePid` on the wire); the server replies via `send_to_remote`
|
||||||
|
//! straight back to that pid — no name at the client end, no ceremony. A
|
||||||
|
//! third-node roundtrip: the client's pid travels client→server→relay→
|
||||||
|
//! server→client, and still delivers.
|
||||||
|
#![cfg(feature = "cluster")]
|
||||||
|
|
||||||
|
mod common;
|
||||||
|
|
||||||
|
use common::{maybe_child, spawn_node};
|
||||||
|
use smarm::cluster::envelope::{encode_payload, Frame, NodeMeta};
|
||||||
|
use smarm::cluster::expose::{expose, type_hash};
|
||||||
|
use smarm::cluster::membership::{subscribe, NodeEvent};
|
||||||
|
use smarm::cluster::remote::{self, send_to_remote, RemoteName, RemotePid, ToRemoteError};
|
||||||
|
use smarm::cluster::{start, Config, StaticSeeds, Timing};
|
||||||
|
use smarm::pg::Incarnation;
|
||||||
|
use smarm::{channel, install, register, run, Addressable, Name, Pid};
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
// ---- message types (std-only payloads; the crate's serde is derive-less,
|
||||||
|
// so wire types are hand-rolled with serde's tuple/seq API via `serde::ser`
|
||||||
|
// impls below — the same thing a user's derive would generate) ------------
|
||||||
|
|
||||||
|
/// A request carrying a reply-to. Serialize/Deserialize are written by hand
|
||||||
|
/// here for exactly one reason: this crate deliberately does not pull in
|
||||||
|
/// serde-derive. Field 1 is the auto-serializing pid.
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
struct Req {
|
||||||
|
text: String,
|
||||||
|
reply_to: RemotePid<Replier>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
struct Reply(String);
|
||||||
|
|
||||||
|
struct Replier;
|
||||||
|
impl Addressable for Replier {
|
||||||
|
type Msg = Reply;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl serde::Serialize for Req {
|
||||||
|
fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
|
||||||
|
use serde::ser::SerializeTuple;
|
||||||
|
let mut t = s.serialize_tuple(2)?;
|
||||||
|
t.serialize_element(&self.text)?;
|
||||||
|
t.serialize_element(&self.reply_to)?;
|
||||||
|
t.end()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<'de> serde::Deserialize<'de> for Req {
|
||||||
|
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
|
||||||
|
let (text, reply_to) = <(String, RemotePid<Replier>)>::deserialize(d)?;
|
||||||
|
Ok(Req { text, reply_to })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl serde::Serialize for Reply {
|
||||||
|
fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
|
||||||
|
self.0.serialize(s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<'de> serde::Deserialize<'de> for Reply {
|
||||||
|
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
|
||||||
|
String::deserialize(d).map(Reply)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ================= local suite =========================================
|
||||||
|
|
||||||
|
/// A local `Pid<A>` serializes as a `RemotePid<A>` stamped with this node's
|
||||||
|
/// identity; deserializing it back on the same node collapses to the same
|
||||||
|
/// local pid (`local()` is `Some`, `Pid` round-trips).
|
||||||
|
#[test]
|
||||||
|
fn local_pid_serializes_and_collapses_on_self() {
|
||||||
|
maybe_child(ROLES);
|
||||||
|
run(|| {
|
||||||
|
// The local identity is set by cluster::start; the local suite sets
|
||||||
|
// it directly.
|
||||||
|
remote::set_local_identity("me", Incarnation::new(7));
|
||||||
|
let (tx, _rx) = channel::<Reply>();
|
||||||
|
let me: Pid<Replier> = install::<Replier>(tx);
|
||||||
|
|
||||||
|
let bytes = encode_payload(&me).unwrap();
|
||||||
|
let rp: RemotePid<Replier> = smarm::cluster::envelope::decode_payload(&bytes).unwrap();
|
||||||
|
assert_eq!(rp.node(), "me");
|
||||||
|
assert_eq!(rp.incarnation(), Incarnation::new(7));
|
||||||
|
assert_eq!(
|
||||||
|
rp.local(),
|
||||||
|
Some(me),
|
||||||
|
"self-node pid collapses to the local pid"
|
||||||
|
);
|
||||||
|
|
||||||
|
// Deserializing straight into Pid<A> works for a self-node pid...
|
||||||
|
let back: Pid<Replier> = smarm::cluster::envelope::decode_payload(&bytes).unwrap();
|
||||||
|
assert_eq!(back, me);
|
||||||
|
|
||||||
|
// ...and FAILS for a foreign one (collapse is literal: node == self).
|
||||||
|
let foreign = RemotePid::<Replier>::from_parts("elsewhere", Incarnation::new(1), 3, 1);
|
||||||
|
let fbytes = encode_payload(&foreign).unwrap();
|
||||||
|
assert!(smarm::cluster::envelope::decode_payload::<Pid<Replier>>(&fbytes).is_err());
|
||||||
|
assert_eq!(foreign.local(), None);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `send_to_remote` short-circuits locally for a self-node pid — the
|
||||||
|
/// zero-copy-equivalent collapse: the message object itself lands in the
|
||||||
|
/// local channel, no encode, no frame.
|
||||||
|
#[test]
|
||||||
|
fn send_to_remote_collapses_locally_for_self() {
|
||||||
|
maybe_child(ROLES);
|
||||||
|
run(|| {
|
||||||
|
remote::set_local_identity("me", Incarnation::new(7));
|
||||||
|
let (tx, rx) = channel::<Reply>();
|
||||||
|
let me: Pid<Replier> = install::<Replier>(tx);
|
||||||
|
let rp = RemotePid::from_local(me).expect("identity set");
|
||||||
|
// Probe the outbound path: nothing must be handed to any connection.
|
||||||
|
let (probe_tx, probe_rx) = channel::<Frame>();
|
||||||
|
remote::bind_outbound_probe("me", Incarnation::new(7), probe_tx);
|
||||||
|
|
||||||
|
send_to_remote(rp, Reply("hi".into())).unwrap();
|
||||||
|
assert_eq!(rx.recv().unwrap(), Reply("hi".into()));
|
||||||
|
assert!(
|
||||||
|
matches!(probe_rx.try_recv(), Ok(None)),
|
||||||
|
"no frame for a local collapse"
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// RFC v2 §3: a `RemotePid` whose incarnation is not the current one for its
|
||||||
|
/// node fails at the local send site with `DeadIncarnation`, and NO frame
|
||||||
|
/// is emitted — asserted on a probe sender bound as that node's outbound.
|
||||||
|
#[test]
|
||||||
|
fn stale_incarnation_rejected_locally_no_frame() {
|
||||||
|
maybe_child(ROLES);
|
||||||
|
run(|| {
|
||||||
|
remote::set_local_identity("me", Incarnation::new(7));
|
||||||
|
let (probe_tx, probe_rx) = channel::<Frame>();
|
||||||
|
remote::bind_outbound_probe("peer", Incarnation::new(5), probe_tx);
|
||||||
|
|
||||||
|
let stale = RemotePid::<Replier>::from_parts("peer", Incarnation::new(4), 9, 1);
|
||||||
|
match send_to_remote(stale, Reply("late".into())) {
|
||||||
|
Err(ToRemoteError::DeadIncarnation(Reply(s))) => assert_eq!(s, "late"),
|
||||||
|
other => panic!("expected DeadIncarnation, got {other:?}"),
|
||||||
|
}
|
||||||
|
assert!(
|
||||||
|
matches!(probe_rx.try_recv(), Ok(None)),
|
||||||
|
"stale pid must emit no frame"
|
||||||
|
);
|
||||||
|
|
||||||
|
// The current incarnation goes through: a Send frame with the pid's
|
||||||
|
// (index, generation) and Reply's hash lands on the probe.
|
||||||
|
let live = RemotePid::<Replier>::from_parts("peer", Incarnation::new(5), 9, 1);
|
||||||
|
send_to_remote(live, Reply("now".into())).unwrap();
|
||||||
|
match probe_rx.recv().unwrap() {
|
||||||
|
Frame::Send {
|
||||||
|
index,
|
||||||
|
generation,
|
||||||
|
type_hash: h,
|
||||||
|
payload,
|
||||||
|
} => {
|
||||||
|
assert_eq!((index, generation), (9, 1));
|
||||||
|
assert_eq!(h, type_hash::<Reply>());
|
||||||
|
let r: Reply = smarm::cluster::envelope::decode_payload(&payload).unwrap();
|
||||||
|
assert_eq!(r, Reply("now".into()));
|
||||||
|
}
|
||||||
|
f => panic!("expected Send, got {f:?}"),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unknown node: NotConnected, no frame anywhere.
|
||||||
|
let nowhere = RemotePid::<Replier>::from_parts("nowhere", Incarnation::new(1), 1, 1);
|
||||||
|
assert!(matches!(
|
||||||
|
send_to_remote(nowhere, Reply("x".into())),
|
||||||
|
Err(ToRemoteError::NotConnected(_))
|
||||||
|
));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// ================= cross-process gate ==================================
|
||||||
|
|
||||||
|
const ROLES: &[(&str, fn())] = &[
|
||||||
|
("server", role_server),
|
||||||
|
("client", role_client),
|
||||||
|
("relay", role_relay),
|
||||||
|
];
|
||||||
|
|
||||||
|
const ECHO: Name<Req> = Name::new("c10.echo");
|
||||||
|
const RELAY: Name<Req> = Name::new("c10.relay");
|
||||||
|
|
||||||
|
fn cfg(name: &str, seeds: Vec<(String, String)>) -> Config {
|
||||||
|
Config {
|
||||||
|
node_name: name.to_string(),
|
||||||
|
meta: NodeMeta {
|
||||||
|
role: "c10".into(),
|
||||||
|
region: "local".into(),
|
||||||
|
},
|
||||||
|
listen_addr: std::env::var("SMARM_LISTEN_ADDR").unwrap_or_else(|_| "127.0.0.1:0".into()),
|
||||||
|
strategy: Box::new(StaticSeeds::new(seeds)),
|
||||||
|
timing: Timing::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn wait_up(events: &smarm::cluster::membership::MembershipEvents, who: &str) {
|
||||||
|
loop {
|
||||||
|
match events.rx.recv() {
|
||||||
|
Ok(NodeEvent::NodeUp(i)) if i.name == who => return,
|
||||||
|
Ok(_) => continue,
|
||||||
|
Err(_) => panic!("manager gone"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Server: exposes ECHO; each Req is answered by `send_to_remote` to its
|
||||||
|
/// reply_to — the server never learns a name for the client. If the Req text
|
||||||
|
/// starts with "via-relay:", it forwards the whole Req (reply_to and all) to
|
||||||
|
/// the relay node instead, which sends it back here; the second arrival is
|
||||||
|
/// answered normally. That is the pid's third-node roundtrip.
|
||||||
|
fn role_server() {
|
||||||
|
let relay_addr = std::env::var("SMARM_RELAY_ADDR").ok();
|
||||||
|
smarm::run(move || {
|
||||||
|
let seeds = relay_addr
|
||||||
|
.map(|a| vec![("relay".to_string(), a)])
|
||||||
|
.unwrap_or_default();
|
||||||
|
let cluster = start(cfg("server", seeds)).expect("binds");
|
||||||
|
println!("LISTENING {}", cluster.local_addr());
|
||||||
|
let (tx, rx) = channel::<Req>();
|
||||||
|
register(ECHO, tx).unwrap();
|
||||||
|
expose(ECHO);
|
||||||
|
println!("READY");
|
||||||
|
loop {
|
||||||
|
let req = rx.recv().unwrap();
|
||||||
|
if let Some(rest) = req.text.strip_prefix("via-relay:") {
|
||||||
|
let fwd = Req {
|
||||||
|
text: format!("relayed:{rest}"),
|
||||||
|
reply_to: req.reply_to,
|
||||||
|
};
|
||||||
|
remote::send(RemoteName::new("relay", RELAY), fwd).unwrap();
|
||||||
|
println!("FORWARDED");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
println!("REQ {}", req.text);
|
||||||
|
send_to_remote(req.reply_to, Reply(format!("echo:{}", req.text))).unwrap();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Relay: exposes RELAY; bounces every Req straight back to the server's
|
||||||
|
/// ECHO, untouched. The client's pid inside it now crosses relay→server.
|
||||||
|
fn role_relay() {
|
||||||
|
let server_addr = std::env::var("SMARM_SERVER_ADDR").expect("SMARM_SERVER_ADDR");
|
||||||
|
smarm::run(move || {
|
||||||
|
let cluster = start(cfg("relay", vec![("server".into(), server_addr)])).expect("binds");
|
||||||
|
println!("LISTENING {}", cluster.local_addr());
|
||||||
|
let (tx, rx) = channel::<Req>();
|
||||||
|
register(RELAY, tx).unwrap();
|
||||||
|
expose(RELAY);
|
||||||
|
let ev = subscribe().unwrap();
|
||||||
|
wait_up(&ev, "server");
|
||||||
|
println!("READY");
|
||||||
|
loop {
|
||||||
|
let req = rx.recv().unwrap();
|
||||||
|
println!("RELAYING {}", req.text);
|
||||||
|
remote::send(RemoteName::new("server", ECHO), req).unwrap();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Client: connects to server, installs a Reply inbox on its own pid,
|
||||||
|
/// declares it accepts `Reply` (`expose_type` — the RFC's one kept piece of
|
||||||
|
/// ceremony: nothing is remotely deliverable by default), sends a Req with
|
||||||
|
/// `reply_to = my pid` (auto-serialized), awaits the reply.
|
||||||
|
fn role_client() {
|
||||||
|
let server_addr = std::env::var("SMARM_SERVER_ADDR").expect("SMARM_SERVER_ADDR");
|
||||||
|
let via_relay = std::env::var("SMARM_VIA_RELAY").is_ok();
|
||||||
|
smarm::run(move || {
|
||||||
|
let _cluster = start(cfg("client", vec![("server".into(), server_addr)])).expect("binds");
|
||||||
|
let ev = subscribe().unwrap();
|
||||||
|
wait_up(&ev, "server");
|
||||||
|
println!("MEMBER-UP server");
|
||||||
|
|
||||||
|
let (tx, rx) = channel::<Reply>();
|
||||||
|
let me: Pid<Replier> = install::<Replier>(tx);
|
||||||
|
// The one deliberate line: a pid-targeted inbound is deliverable only
|
||||||
|
// for types this node has said it accepts (RFC §4, the safety).
|
||||||
|
smarm::cluster::expose::expose_type::<Reply>();
|
||||||
|
let text = if via_relay { "via-relay:ping" } else { "ping" };
|
||||||
|
remote::send(
|
||||||
|
RemoteName::new("server", ECHO),
|
||||||
|
Req {
|
||||||
|
text: text.into(),
|
||||||
|
reply_to: RemotePid::from_local(me).expect("identity set"),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
println!("SENT");
|
||||||
|
let Reply(s) = rx.recv().unwrap();
|
||||||
|
println!("REPLY {s}");
|
||||||
|
loop {
|
||||||
|
smarm::sleep(Duration::from_secs(3600));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The gate: cross-node call/reply with no ceremony.
|
||||||
|
#[test]
|
||||||
|
fn cross_node_call_reply_no_ceremony() {
|
||||||
|
maybe_child(ROLES);
|
||||||
|
let mut server = spawn_node("server", &[]);
|
||||||
|
let saddr = server.wait_listening();
|
||||||
|
server.wait_line("READY", |l| l == "READY");
|
||||||
|
let mut client = spawn_node("client", &[("SMARM_SERVER_ADDR", &saddr)]);
|
||||||
|
client.wait_line("SENT", |l| l == "SENT");
|
||||||
|
server.wait_line("REQ ping", |l| l == "REQ ping");
|
||||||
|
client.wait_line("REPLY echo:ping", |l| l == "REPLY echo:ping");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The client's pid, round-tripped through a third node, still delivers.
|
||||||
|
#[test]
|
||||||
|
fn pid_roundtrips_through_third_node() {
|
||||||
|
maybe_child(ROLES);
|
||||||
|
// Relay needs the server address; server needs the relay address —
|
||||||
|
// pre-reserve the relay port (same accepted micro-window as cluster_mesh).
|
||||||
|
let relay_addr = {
|
||||||
|
let l = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
|
||||||
|
l.local_addr().unwrap().to_string()
|
||||||
|
};
|
||||||
|
let mut server = spawn_node("server", &[("SMARM_RELAY_ADDR", &relay_addr)]);
|
||||||
|
let saddr = server.wait_listening();
|
||||||
|
server.wait_line("READY", |l| l == "READY");
|
||||||
|
let mut relay = spawn_node(
|
||||||
|
"relay",
|
||||||
|
&[
|
||||||
|
("SMARM_SERVER_ADDR", &saddr),
|
||||||
|
("SMARM_LISTEN_ADDR", &relay_addr),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
let _ = relay.wait_listening();
|
||||||
|
relay.wait_line("READY", |l| l == "READY");
|
||||||
|
let mut client = spawn_node(
|
||||||
|
"client",
|
||||||
|
&[("SMARM_SERVER_ADDR", &saddr), ("SMARM_VIA_RELAY", "1")],
|
||||||
|
);
|
||||||
|
client.wait_line("SENT", |l| l == "SENT");
|
||||||
|
server.wait_line("FORWARDED", |l| l == "FORWARDED");
|
||||||
|
relay.wait_line("RELAYING", |l| l.starts_with("RELAYING"));
|
||||||
|
server.wait_line("REQ relayed:ping", |l| l == "REQ relayed:ping");
|
||||||
|
client.wait_line("REPLY echo:relayed:ping", |l| {
|
||||||
|
l == "REPLY echo:relayed:ping"
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -0,0 +1,221 @@
|
|||||||
|
//! RFC 010 c9 — remote `Name` sends: the outbound seam and the single
|
||||||
|
//! inbound name-resolution seam, cross-process.
|
||||||
|
//!
|
||||||
|
//! Two node processes each run the integrated `cluster::start`. The
|
||||||
|
//! *receiver* registers a `String` inbox under a name and exposes it (and
|
||||||
|
//! registers a second name it does NOT expose); the *sender* waits for
|
||||||
|
//! `node_up`, then sends. Facts cross as stdout lines: `LISTENING <addr>`,
|
||||||
|
//! `MEMBER-UP <name>`, `GOT <payload>`, `SEND-RESULT <case> <verdict>`.
|
||||||
|
//! Roles park forever afterwards (retractable-state trap); the parent
|
||||||
|
//! SIGKILLs via `Drop`.
|
||||||
|
//!
|
||||||
|
//! What is asserted at each end (roadmap-binding):
|
||||||
|
//! - cross-node name-send delivers the payload;
|
||||||
|
//! - an unexposed name is unreachable — the receiver's inbox stays empty
|
||||||
|
//! even though the name IS registered locally;
|
||||||
|
//! - a wrong type hash is a decode failure at the receiver, never a
|
||||||
|
//! misroute — the `String` inbox does not see a `u64` delivered under a
|
||||||
|
//! made-up hash, nor a `u64` under `u64`'s hash;
|
||||||
|
//! - a send to a disconnected (never-connected) node fails locally with
|
||||||
|
//! `NotConnected`, and `Ok(())` means only "handed to the transport".
|
||||||
|
//!
|
||||||
|
//! Timing note for the "stays empty" assertions: they are proven by
|
||||||
|
//! ORDERING, not by waiting — the sender emits the negative-case frames
|
||||||
|
//! BEFORE the positive one on the same connection (in-order stream), so when
|
||||||
|
//! the receiver has seen the positive payload, the negatives have already
|
||||||
|
//! been processed and refused. No sleep-and-hope.
|
||||||
|
#![cfg(feature = "cluster")]
|
||||||
|
|
||||||
|
mod common;
|
||||||
|
|
||||||
|
use common::{maybe_child, spawn_node, Node};
|
||||||
|
use smarm::cluster::envelope::NodeMeta;
|
||||||
|
use smarm::cluster::expose::expose;
|
||||||
|
use smarm::cluster::membership::{subscribe, NodeEvent};
|
||||||
|
use smarm::cluster::remote::{send_remote_raw, RemoteName, RemoteSendError};
|
||||||
|
use smarm::cluster::{start, Config, StaticSeeds, Timing};
|
||||||
|
use smarm::{channel, register, Name};
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
const ROLES: &[(&str, fn())] = &[("receiver", role_receiver), ("sender", role_sender)];
|
||||||
|
|
||||||
|
const INBOX: Name<String> = Name::new("c9.inbox");
|
||||||
|
const HIDDEN: Name<String> = Name::new("c9.hidden");
|
||||||
|
|
||||||
|
fn base_config(name: &str, seeds: Vec<(String, String)>) -> Config {
|
||||||
|
Config {
|
||||||
|
node_name: name.to_string(),
|
||||||
|
meta: NodeMeta {
|
||||||
|
role: "c9".to_string(),
|
||||||
|
region: "local".to_string(),
|
||||||
|
},
|
||||||
|
listen_addr: "127.0.0.1:0".to_string(),
|
||||||
|
strategy: Box::new(StaticSeeds::new(seeds)),
|
||||||
|
timing: Timing::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Receiver: register + expose INBOX; register HIDDEN unexposed **in a
|
||||||
|
/// separate actor** (one actor holds one channel per message type — a
|
||||||
|
/// second `register` of the same `M` on one actor silently replaces the
|
||||||
|
/// first, closing it); print every payload that lands in either.
|
||||||
|
fn role_receiver() {
|
||||||
|
smarm::run(|| {
|
||||||
|
let cluster = start(base_config("recv", vec![])).expect("listener binds");
|
||||||
|
println!("LISTENING {}", cluster.local_addr());
|
||||||
|
|
||||||
|
// HIDDEN's holder: its own actor, so its String channel does not
|
||||||
|
// displace INBOX's on the root actor.
|
||||||
|
let (hidden_ready_tx, hidden_ready_rx) = channel::<()>();
|
||||||
|
smarm::spawn(move || {
|
||||||
|
let (hid_tx, hid_rx) = channel::<String>();
|
||||||
|
register(HIDDEN, hid_tx).unwrap();
|
||||||
|
hidden_ready_tx.send(()).unwrap();
|
||||||
|
loop {
|
||||||
|
match hid_rx.recv() {
|
||||||
|
Ok(s) => println!("GOT-HIDDEN {s}"),
|
||||||
|
Err(_) => break,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
hidden_ready_rx.recv().unwrap();
|
||||||
|
|
||||||
|
let (in_tx, in_rx) = channel::<String>();
|
||||||
|
register(INBOX, in_tx).unwrap();
|
||||||
|
expose(INBOX);
|
||||||
|
println!("READY");
|
||||||
|
loop {
|
||||||
|
match in_rx.recv() {
|
||||||
|
Ok(s) => println!("GOT {s}"),
|
||||||
|
Err(_) => break,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
loop {
|
||||||
|
smarm::sleep(Duration::from_secs(3600));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sender: connect to recv, wait for node_up, then in this ORDER on the one
|
||||||
|
/// connection: hidden-name send, wrong-hash sends (two flavours), then the
|
||||||
|
/// positive send. Plus a send to a node that is not connected at all.
|
||||||
|
fn role_sender() {
|
||||||
|
let recv_addr = std::env::var("SMARM_RECV_ADDR").expect("SMARM_RECV_ADDR");
|
||||||
|
smarm::run(move || {
|
||||||
|
let _cluster = start(base_config("send", vec![("recv".to_string(), recv_addr)]))
|
||||||
|
.expect("listener binds");
|
||||||
|
let events = subscribe().expect("manager is up");
|
||||||
|
loop {
|
||||||
|
match events.rx.recv() {
|
||||||
|
Ok(NodeEvent::NodeUp(info)) if info.name == "recv" => break,
|
||||||
|
Ok(_) => continue,
|
||||||
|
Err(_) => panic!("manager gone"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
println!("MEMBER-UP recv");
|
||||||
|
|
||||||
|
// Not connected: purely local knowledge, no frame leaves.
|
||||||
|
let ghost: RemoteName<String> = RemoteName::new("nowhere", INBOX);
|
||||||
|
let r = smarm::cluster::remote::send(ghost, "lost".to_string());
|
||||||
|
println!(
|
||||||
|
"SEND-RESULT not-connected {}",
|
||||||
|
match r {
|
||||||
|
Err(RemoteSendError::NotConnected(_)) => "NotConnected",
|
||||||
|
Ok(()) => "Ok",
|
||||||
|
Err(_) => "OtherErr",
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// Unexposed name at the peer: the frame goes (local knowledge can't
|
||||||
|
// know the peer's exposed set) and the peer refuses it.
|
||||||
|
let hidden: RemoteName<String> = RemoteName::new("recv", HIDDEN);
|
||||||
|
let r = smarm::cluster::remote::send(hidden, "should not land".to_string());
|
||||||
|
println!(
|
||||||
|
"SEND-RESULT hidden {}",
|
||||||
|
if r.is_ok() { "Ok" } else { "Err" }
|
||||||
|
);
|
||||||
|
|
||||||
|
// Wrong hash, two flavours: (a) a u64 payload under a made-up hash
|
||||||
|
// (unknown type at the peer); (b) a u64 payload under u64's real
|
||||||
|
// hash against a String-typed name (decoder known, wrong channel).
|
||||||
|
// Both are raw sends — the typed API cannot express them, by design.
|
||||||
|
let bogus = 0xdead_beef_u64;
|
||||||
|
let r = send_remote_raw(
|
||||||
|
"recv",
|
||||||
|
"c9.inbox",
|
||||||
|
bogus,
|
||||||
|
&smarm::cluster::envelope::encode_payload(&7u64).unwrap(),
|
||||||
|
);
|
||||||
|
println!(
|
||||||
|
"SEND-RESULT wrong-hash-unknown {}",
|
||||||
|
if r.is_ok() { "Ok" } else { "Err" }
|
||||||
|
);
|
||||||
|
let r = send_remote_raw(
|
||||||
|
"recv",
|
||||||
|
"c9.inbox",
|
||||||
|
smarm::cluster::expose::type_hash::<u64>(),
|
||||||
|
&smarm::cluster::envelope::encode_payload(&7u64).unwrap(),
|
||||||
|
);
|
||||||
|
println!(
|
||||||
|
"SEND-RESULT wrong-hash-known {}",
|
||||||
|
if r.is_ok() { "Ok" } else { "Err" }
|
||||||
|
);
|
||||||
|
|
||||||
|
// Positive: last on the stream, so its arrival proves the negatives
|
||||||
|
// were already processed.
|
||||||
|
let inbox: RemoteName<String> = RemoteName::new("recv", INBOX);
|
||||||
|
let r = smarm::cluster::remote::send(inbox, "hello from send".to_string());
|
||||||
|
println!(
|
||||||
|
"SEND-RESULT positive {}",
|
||||||
|
if r.is_ok() { "Ok" } else { "Err" }
|
||||||
|
);
|
||||||
|
|
||||||
|
loop {
|
||||||
|
smarm::sleep(Duration::from_secs(3600));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn wait_send_result(node: &mut Node, case: &str) -> String {
|
||||||
|
let prefix = format!("SEND-RESULT {case} ");
|
||||||
|
let line = node.wait_line(&prefix, |l| l.starts_with(&prefix));
|
||||||
|
line[prefix.len()..].to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn remote_name_send_delivers_and_refusals_never_misroute() {
|
||||||
|
maybe_child(ROLES);
|
||||||
|
|
||||||
|
let mut recv = spawn_node("receiver", &[]);
|
||||||
|
let addr = recv.wait_listening();
|
||||||
|
recv.wait_line("READY", |l| l == "READY");
|
||||||
|
|
||||||
|
let mut send = spawn_node("sender", &[("SMARM_RECV_ADDR", &addr)]);
|
||||||
|
send.wait_line("MEMBER-UP recv", |l| l == "MEMBER-UP recv");
|
||||||
|
|
||||||
|
// Local-knowledge-only failure for an unknown node.
|
||||||
|
assert_eq!(wait_send_result(&mut send, "not-connected"), "NotConnected");
|
||||||
|
// Every frame-bearing send is Ok — Ok means "handed to the transport",
|
||||||
|
// nothing about what the peer does with it (RFC §3, documented here).
|
||||||
|
assert_eq!(wait_send_result(&mut send, "hidden"), "Ok");
|
||||||
|
assert_eq!(wait_send_result(&mut send, "wrong-hash-unknown"), "Ok");
|
||||||
|
assert_eq!(wait_send_result(&mut send, "wrong-hash-known"), "Ok");
|
||||||
|
assert_eq!(wait_send_result(&mut send, "positive"), "Ok");
|
||||||
|
|
||||||
|
// The positive payload lands...
|
||||||
|
recv.wait_line("GOT hello from send", |l| l == "GOT hello from send");
|
||||||
|
// ...and, by stream ordering, every negative before it was refused: no
|
||||||
|
// GOT for the wrong-hash frames, no GOT-HIDDEN at all. The transcript
|
||||||
|
// up to this point is the proof.
|
||||||
|
let transcript = recv.transcript();
|
||||||
|
let gots: Vec<&str> = transcript
|
||||||
|
.iter()
|
||||||
|
.map(|s| s.as_str())
|
||||||
|
.filter(|l| l.starts_with("GOT"))
|
||||||
|
.collect();
|
||||||
|
assert_eq!(
|
||||||
|
gots,
|
||||||
|
["GOT hello from send"],
|
||||||
|
"exactly one delivery, the exposed one"
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,272 @@
|
|||||||
|
//! RFC 010 c3 — transport conformance suite, run against both shipped impls
|
||||||
|
//! (TCP and in-memory loopback), plus impl-specific cases.
|
||||||
|
//!
|
||||||
|
//! Shared suite (roadmap): frame roundtrips through the framed codec, framing
|
||||||
|
//! across a split write, coalesced frames in one write, peer-close mid-frame
|
||||||
|
//! (must error, not EOF), clean close at a frame boundary (EOF as `Ok(None)`).
|
||||||
|
//!
|
||||||
|
//! The TCP impl parks the calling actor, so its runs live inside `smarm::run`;
|
||||||
|
//! loopback blocks the OS thread and runs as plain tests.
|
||||||
|
#![cfg(feature = "cluster")]
|
||||||
|
|
||||||
|
use smarm::cluster::envelope::Frame;
|
||||||
|
use smarm::cluster::transport::loopback::LoopbackTransport;
|
||||||
|
use smarm::cluster::transport::tcp::TcpTransport;
|
||||||
|
use smarm::cluster::transport::{Conn, FramedConn, RecvError, Transport};
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Helpers
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
/// Listener + dial + accept against one transport, both conns returned.
|
||||||
|
/// Relies on dial not requiring a concurrent accept (TCP backlog / loopback
|
||||||
|
/// queue), so a single thread or actor can hold both ends.
|
||||||
|
fn pair(t: &dyn Transport, addr: &str) -> (Box<dyn Conn>, Box<dyn Conn>) {
|
||||||
|
let mut l = t.listen(addr).unwrap();
|
||||||
|
let a = t.dial(&l.local_addr()).unwrap();
|
||||||
|
let b = l.accept().unwrap();
|
||||||
|
(a, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn frames() -> Vec<Frame> {
|
||||||
|
vec![
|
||||||
|
Frame::Heartbeat,
|
||||||
|
Frame::Send {
|
||||||
|
index: 42,
|
||||||
|
generation: 3,
|
||||||
|
type_hash: 0x1234_5678_9ABC_DEF0,
|
||||||
|
payload: vec![1, 2, 3, 4, 5],
|
||||||
|
},
|
||||||
|
Frame::SendNamed {
|
||||||
|
name: "the_counter".into(),
|
||||||
|
type_hash: 0xFFFF_0000_FFFF_0000,
|
||||||
|
payload: vec![],
|
||||||
|
},
|
||||||
|
Frame::Demonitor { monitor_id: 77 },
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn encode(f: &Frame) -> Vec<u8> {
|
||||||
|
let mut out = Vec::new();
|
||||||
|
f.encode(&mut out).unwrap();
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Shared conformance suite — generic over an established pair
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
fn suite_roundtrip(a: Box<dyn Conn>, b: Box<dyn Conn>) {
|
||||||
|
let mut fa = FramedConn::new(a);
|
||||||
|
let mut fb = FramedConn::new(b);
|
||||||
|
// a -> b, then b -> a: both directions carry every frame shape.
|
||||||
|
for f in frames() {
|
||||||
|
fa.send(&f).unwrap();
|
||||||
|
assert_eq!(fb.recv().unwrap().unwrap(), f);
|
||||||
|
}
|
||||||
|
for f in frames() {
|
||||||
|
fb.send(&f).unwrap();
|
||||||
|
assert_eq!(fa.recv().unwrap().unwrap(), f);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn suite_split_write(mut a: Box<dyn Conn>, b: Box<dyn Conn>) {
|
||||||
|
let f = Frame::Send {
|
||||||
|
index: 7,
|
||||||
|
generation: 1,
|
||||||
|
type_hash: 0xAB,
|
||||||
|
payload: vec![9; 64],
|
||||||
|
};
|
||||||
|
let bytes = encode(&f);
|
||||||
|
// Split inside the length prefix, then inside the body: the reader must
|
||||||
|
// reassemble regardless of where the boundary falls.
|
||||||
|
a.write_all(&bytes[..2]).unwrap();
|
||||||
|
a.write_all(&bytes[2..10]).unwrap();
|
||||||
|
a.write_all(&bytes[10..]).unwrap();
|
||||||
|
let mut fb = FramedConn::new(b);
|
||||||
|
assert_eq!(fb.recv().unwrap().unwrap(), f);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn suite_coalesced(mut a: Box<dyn Conn>, b: Box<dyn Conn>) {
|
||||||
|
let f1 = Frame::Heartbeat;
|
||||||
|
let f2 = Frame::Demonitor { monitor_id: 5 };
|
||||||
|
let mut bytes = encode(&f1);
|
||||||
|
bytes.extend_from_slice(&encode(&f2));
|
||||||
|
a.write_all(&bytes).unwrap();
|
||||||
|
let mut fb = FramedConn::new(b);
|
||||||
|
assert_eq!(fb.recv().unwrap().unwrap(), f1);
|
||||||
|
assert_eq!(fb.recv().unwrap().unwrap(), f2);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn suite_close_mid_frame(mut a: Box<dyn Conn>, b: Box<dyn Conn>) {
|
||||||
|
let bytes = encode(&Frame::Send {
|
||||||
|
index: 1,
|
||||||
|
generation: 1,
|
||||||
|
type_hash: 1,
|
||||||
|
payload: vec![0; 128],
|
||||||
|
});
|
||||||
|
a.write_all(&bytes[..bytes.len() / 2]).unwrap();
|
||||||
|
a.close();
|
||||||
|
let mut fb = FramedConn::new(b);
|
||||||
|
match fb.recv() {
|
||||||
|
Err(RecvError::TruncatedByPeer) => {}
|
||||||
|
other => panic!("expected TruncatedByPeer, got {other:?}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn suite_clean_close(mut a: Box<dyn Conn>, b: Box<dyn Conn>) {
|
||||||
|
let f = Frame::Heartbeat;
|
||||||
|
a.write_all(&encode(&f)).unwrap();
|
||||||
|
a.close();
|
||||||
|
let mut fb = FramedConn::new(b);
|
||||||
|
// The buffered frame is still delivered, then EOF at the boundary.
|
||||||
|
assert_eq!(fb.recv().unwrap().unwrap(), f);
|
||||||
|
assert!(fb.recv().unwrap().is_none());
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run_suite(t: &dyn Transport, addr: &str) {
|
||||||
|
let (a, b) = pair(t, addr);
|
||||||
|
suite_roundtrip(a, b);
|
||||||
|
let (a, b) = pair(t, addr);
|
||||||
|
suite_split_write(a, b);
|
||||||
|
let (a, b) = pair(t, addr);
|
||||||
|
suite_coalesced(a, b);
|
||||||
|
let (a, b) = pair(t, addr);
|
||||||
|
suite_close_mid_frame(a, b);
|
||||||
|
let (a, b) = pair(t, addr);
|
||||||
|
suite_clean_close(a, b);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Loopback — plain tests, no runtime
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn loopback_conformance() {
|
||||||
|
// Fresh transport per pair() call is fine, but one instance must also
|
||||||
|
// support sequential re-listen on distinct addresses.
|
||||||
|
let t = LoopbackTransport::default();
|
||||||
|
run_suite(&t, "alpha");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn loopback_dial_unknown_addr_refused() {
|
||||||
|
let t = LoopbackTransport::default();
|
||||||
|
let err = t.dial("nobody-home").unwrap_err();
|
||||||
|
assert_eq!(err.kind(), std::io::ErrorKind::ConnectionRefused);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn loopback_addr_in_use() {
|
||||||
|
let t = LoopbackTransport::default();
|
||||||
|
let _l = t.listen("alpha").unwrap();
|
||||||
|
let err = t.listen("alpha").unwrap_err();
|
||||||
|
assert_eq!(err.kind(), std::io::ErrorKind::AddrInUse);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn loopback_listener_drop_frees_addr_and_refuses_dial() {
|
||||||
|
let t = LoopbackTransport::default();
|
||||||
|
let l = t.listen("alpha").unwrap();
|
||||||
|
drop(l);
|
||||||
|
let err = t.dial("alpha").unwrap_err();
|
||||||
|
assert_eq!(err.kind(), std::io::ErrorKind::ConnectionRefused);
|
||||||
|
// Address is reusable after the listener is gone.
|
||||||
|
let _l2 = t.listen("alpha").unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn loopback_write_after_peer_close_broken_pipe() {
|
||||||
|
let t = LoopbackTransport::default();
|
||||||
|
let (mut a, mut b) = pair(&t, "alpha");
|
||||||
|
b.close();
|
||||||
|
let err = a.write_all(&[1, 2, 3]).unwrap_err();
|
||||||
|
assert_eq!(err.kind(), std::io::ErrorKind::BrokenPipe);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn loopback_cross_thread_blocking_read() {
|
||||||
|
// Reader blocks on an empty pipe until the writer thread delivers.
|
||||||
|
let t = LoopbackTransport::default();
|
||||||
|
let (a, b) = pair(&t, "alpha");
|
||||||
|
let mut fb = FramedConn::new(b);
|
||||||
|
let writer = std::thread::spawn(move || {
|
||||||
|
let mut a = a;
|
||||||
|
std::thread::sleep(std::time::Duration::from_millis(30));
|
||||||
|
a.write_all(&encode(&Frame::Heartbeat)).unwrap();
|
||||||
|
});
|
||||||
|
assert_eq!(fb.recv().unwrap().unwrap(), Frame::Heartbeat);
|
||||||
|
writer.join().unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// TCP — inside the runtime (read/write park the calling actor)
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn tcp_conformance() {
|
||||||
|
smarm::run(|| {
|
||||||
|
run_suite(&TcpTransport, "127.0.0.1:0");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn tcp_dial_refused() {
|
||||||
|
smarm::run(|| {
|
||||||
|
// Bind to an OS-assigned port, learn it, close the listener, dial it.
|
||||||
|
let addr = {
|
||||||
|
let l = TcpTransport.listen("127.0.0.1:0").unwrap();
|
||||||
|
l.local_addr()
|
||||||
|
};
|
||||||
|
let err = TcpTransport.dial(&addr).unwrap_err();
|
||||||
|
assert_eq!(err.kind(), std::io::ErrorKind::ConnectionRefused);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn tcp_bad_addr_rejected_without_resolution() {
|
||||||
|
// Addresses are opaque pre-resolved strings; the c9 seam resolves names.
|
||||||
|
// A hostname is therefore invalid input here, not something to resolve.
|
||||||
|
let err = TcpTransport.dial("localhost:1234").unwrap_err();
|
||||||
|
assert_eq!(err.kind(), std::io::ErrorKind::InvalidInput);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn tcp_local_addr_reports_real_port() {
|
||||||
|
let l = TcpTransport.listen("127.0.0.1:0").unwrap();
|
||||||
|
let addr = l.local_addr();
|
||||||
|
let port: u16 = addr.rsplit(':').next().unwrap().parse().unwrap();
|
||||||
|
assert_ne!(port, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn tcp_big_frame_across_socket_buffers() {
|
||||||
|
// A payload far beyond socket buffer sizes forces genuine fragmentation
|
||||||
|
// and write backpressure: writer and reader must run concurrently.
|
||||||
|
smarm::run(|| {
|
||||||
|
let (tx, rx) = smarm::channel::<Frame>();
|
||||||
|
let payload = vec![0xA5u8; 4 * 1024 * 1024];
|
||||||
|
let f = Frame::Send {
|
||||||
|
index: 9,
|
||||||
|
generation: 2,
|
||||||
|
type_hash: 0xC0FFEE,
|
||||||
|
payload,
|
||||||
|
};
|
||||||
|
let mut l = TcpTransport.listen("127.0.0.1:0").unwrap();
|
||||||
|
let addr = l.local_addr();
|
||||||
|
let fw = f.clone();
|
||||||
|
let writer = smarm::spawn(move || {
|
||||||
|
let mut fa = FramedConn::new(TcpTransport.dial(&addr).unwrap());
|
||||||
|
fa.send(&fw).unwrap();
|
||||||
|
});
|
||||||
|
let reader = smarm::spawn(move || {
|
||||||
|
let mut fb = FramedConn::new(l.accept().unwrap());
|
||||||
|
let got = fb.recv().unwrap().unwrap();
|
||||||
|
tx.send(got).unwrap();
|
||||||
|
});
|
||||||
|
let got = rx.recv().unwrap();
|
||||||
|
assert_eq!(got, f);
|
||||||
|
writer.join().unwrap();
|
||||||
|
reader.join().unwrap();
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -0,0 +1,108 @@
|
|||||||
|
//! RFC 010 c4 — two-node harness smoke tests.
|
||||||
|
//!
|
||||||
|
//! Roadmap: "spawn two, handshake-less connect, both exit clean." The
|
||||||
|
//! listener node binds port 0 and announces its concrete address; the
|
||||||
|
//! dialer connects raw (no Hello — c5 doesn't exist yet), pushes one
|
||||||
|
//! Heartbeat through the real framed codec, and closes. Assertions are on
|
||||||
|
//! protocol-visible lines only. Flake budget: see tests/common/mod.rs.
|
||||||
|
#![cfg(feature = "cluster")]
|
||||||
|
|
||||||
|
mod common;
|
||||||
|
|
||||||
|
use common::{maybe_child, spawn_node};
|
||||||
|
use smarm::cluster::envelope::Frame;
|
||||||
|
use smarm::cluster::transport::tcp::TcpTransport;
|
||||||
|
use smarm::cluster::transport::{FramedConn, Transport};
|
||||||
|
|
||||||
|
const ROLES: &[(&str, fn())] = &[
|
||||||
|
("listener", role_listener),
|
||||||
|
("dialer", role_dialer),
|
||||||
|
("hang", role_hang),
|
||||||
|
("fail", role_fail),
|
||||||
|
];
|
||||||
|
|
||||||
|
fn role_listener() {
|
||||||
|
smarm::run(|| {
|
||||||
|
let mut l = TcpTransport.listen("127.0.0.1:0").unwrap();
|
||||||
|
println!("LISTENING {}", l.local_addr());
|
||||||
|
let mut fc = FramedConn::new(l.accept().unwrap());
|
||||||
|
match fc.recv() {
|
||||||
|
Ok(Some(Frame::Heartbeat)) => println!("RECV heartbeat"),
|
||||||
|
other => {
|
||||||
|
println!("RECV unexpected: {other:?}");
|
||||||
|
std::process::exit(3);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
match fc.recv() {
|
||||||
|
Ok(None) => println!("PEER-CLOSED clean"),
|
||||||
|
other => {
|
||||||
|
println!("PEER-CLOSED unexpected: {other:?}");
|
||||||
|
std::process::exit(3);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
println!("EXIT ok");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn role_dialer() {
|
||||||
|
let addr = std::env::var("SMARM_PEER_ADDR").expect("SMARM_PEER_ADDR not set");
|
||||||
|
smarm::run(move || {
|
||||||
|
let mut fc = FramedConn::new(TcpTransport.dial(&addr).unwrap());
|
||||||
|
fc.send(&Frame::Heartbeat).unwrap();
|
||||||
|
fc.close();
|
||||||
|
println!("SENT heartbeat");
|
||||||
|
});
|
||||||
|
println!("EXIT ok");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn role_hang() {
|
||||||
|
println!("HANGING");
|
||||||
|
loop {
|
||||||
|
std::thread::sleep(std::time::Duration::from_secs(3600));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn role_fail() {
|
||||||
|
std::process::exit(7);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The roadmap smoke test: two real processes, raw transport connect, one
|
||||||
|
/// frame across, clean close observed on both sides, both exit 0.
|
||||||
|
#[test]
|
||||||
|
fn two_nodes_connect_and_exit_clean() {
|
||||||
|
maybe_child(ROLES);
|
||||||
|
let mut listener = spawn_node("listener", &[]);
|
||||||
|
let addr = listener.wait_listening();
|
||||||
|
let mut dialer = spawn_node("dialer", &[("SMARM_PEER_ADDR", &addr)]);
|
||||||
|
dialer.wait_line("SENT heartbeat", |l| l == "SENT heartbeat");
|
||||||
|
listener.wait_line("RECV heartbeat", |l| l == "RECV heartbeat");
|
||||||
|
listener.wait_line("clean peer close", |l| l == "PEER-CLOSED clean");
|
||||||
|
dialer.wait_exit_ok();
|
||||||
|
listener.wait_exit_ok();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Reap guarantee: dropping a Node kills a hung child — no orphan survives
|
||||||
|
/// a panicking test.
|
||||||
|
#[test]
|
||||||
|
fn drop_reaps_hung_node() {
|
||||||
|
maybe_child(ROLES);
|
||||||
|
let mut node = spawn_node("hang", &[]);
|
||||||
|
node.wait_line("HANGING", |l| l == "HANGING");
|
||||||
|
let pid = node.pid().expect("live child has a pid") as libc::pid_t;
|
||||||
|
drop(node);
|
||||||
|
// After Drop's kill+wait the pid is fully reaped: signalling it fails
|
||||||
|
// with ESRCH (pid-reuse in this instant is not a realistic race).
|
||||||
|
let rc = unsafe { libc::kill(pid, 0) };
|
||||||
|
assert_eq!(rc, -1, "process still signallable after Drop");
|
||||||
|
let errno = std::io::Error::last_os_error().raw_os_error();
|
||||||
|
assert_eq!(errno, Some(libc::ESRCH), "expected ESRCH, got {errno:?}");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Nonzero child exits surface as statuses, not hangs or panics.
|
||||||
|
#[test]
|
||||||
|
fn nonzero_exit_is_reported() {
|
||||||
|
maybe_child(ROLES);
|
||||||
|
let mut node = spawn_node("fail", &[]);
|
||||||
|
let status = node.wait_exit();
|
||||||
|
assert_eq!(status.code(), Some(7));
|
||||||
|
}
|
||||||
@@ -0,0 +1,250 @@
|
|||||||
|
//! RFC 010 c4 — subprocess multi-node test harness.
|
||||||
|
//!
|
||||||
|
//! The runtime is a process singleton, so two real nodes means two
|
||||||
|
//! processes. This harness re-execs the *current test binary* as node
|
||||||
|
//! processes (precedent: tests/stack_diag.rs), tails their output live,
|
||||||
|
//! waits on protocol-visible lines, and reaps reliably no matter how the
|
||||||
|
//! test dies.
|
||||||
|
//!
|
||||||
|
//! Usage, per test file:
|
||||||
|
//!
|
||||||
|
//! - Declare roles as plain `fn()`s. A role prints protocol-visible facts
|
||||||
|
//! as single lines (Rust's piped stdout is line-buffered, so `println!`
|
||||||
|
//! is enough) and exits.
|
||||||
|
//! - **Every** `#[test]` in the file starts with
|
||||||
|
//! [`maybe_child`]`(ROLES)` — in the child re-exec, whichever test
|
||||||
|
//! libtest runs first performs the role and exits before the rest of the
|
||||||
|
//! suite runs (children are spawned with `--test-threads=1 --quiet`).
|
||||||
|
//! - The parent side spawns nodes with [`spawn_node`], waits on lines with
|
||||||
|
//! [`Node::wait_line`], and on exits with [`Node::wait_exit`].
|
||||||
|
//!
|
||||||
|
//! Port assignment: children bind port 0 and *report* the concrete address
|
||||||
|
//! (e.g. `LISTENING 127.0.0.1:41733`) rather than the parent pre-picking a
|
||||||
|
//! port — no bind/steal race by construction.
|
||||||
|
//!
|
||||||
|
//! Reaping: [`Node`]'s `Drop` SIGKILLs and `wait(2)`s the child, so a
|
||||||
|
//! panicking test (including a `wait_line` timeout) leaves no orphan and
|
||||||
|
//! no zombie. Tail threads exit on pipe EOF.
|
||||||
|
//!
|
||||||
|
//! Flake budget (explicit, per roadmap): every wait is bounded by
|
||||||
|
//! [`WAIT`] (10 s) against a typical cost of well under 1 s; the smoke
|
||||||
|
//! suite ran 10/10 clean at authoring time. Treat >1 failure in 100 runs
|
||||||
|
//! as a harness or runtime regression, not weather. On timeout the panic
|
||||||
|
//! message carries the node's full transcript so far.
|
||||||
|
|
||||||
|
#![allow(dead_code)] // Reusable surface: later phases use more of it than any one file.
|
||||||
|
|
||||||
|
use std::env;
|
||||||
|
use std::io::{BufRead, BufReader};
|
||||||
|
use std::process::{Child, Command, ExitStatus, Stdio};
|
||||||
|
use std::sync::mpsc::{Receiver, RecvTimeoutError};
|
||||||
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
|
/// Env var selecting the child role in a re-exec.
|
||||||
|
const ROLE_ENV: &str = "SMARM_TWO_NODE_ROLE";
|
||||||
|
|
||||||
|
/// Upper bound for every wait in the harness. See the flake budget above.
|
||||||
|
pub const WAIT: Duration = Duration::from_secs(10);
|
||||||
|
|
||||||
|
/// In the child re-exec: run the matching role and exit. In the parent (no
|
||||||
|
/// role env set): return immediately. Call this first in every `#[test]` of
|
||||||
|
/// any file using the harness, passing the file's full role table.
|
||||||
|
pub fn maybe_child(roles: &[(&str, fn())]) {
|
||||||
|
let role = match env::var(ROLE_ENV) {
|
||||||
|
Ok(r) => r,
|
||||||
|
Err(_) => return,
|
||||||
|
};
|
||||||
|
for (name, f) in roles {
|
||||||
|
if *name == role {
|
||||||
|
f();
|
||||||
|
std::process::exit(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
eprintln!("two_node harness: unknown role {role:?}");
|
||||||
|
std::process::exit(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// One spawned node process with live-tailed output.
|
||||||
|
pub struct Node {
|
||||||
|
/// Role name, for panic messages.
|
||||||
|
pub role: String,
|
||||||
|
child: Option<Child>,
|
||||||
|
stdout_rx: Receiver<String>,
|
||||||
|
stderr_rx: Receiver<String>,
|
||||||
|
/// Every line consumed from stdout/stderr so far, for failure dumps.
|
||||||
|
transcript: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn tail(stream: impl std::io::Read + Send + 'static, prefix: &'static str) -> Receiver<String> {
|
||||||
|
let (tx, rx) = std::sync::mpsc::channel();
|
||||||
|
std::thread::spawn(move || {
|
||||||
|
for line in BufReader::new(stream).lines() {
|
||||||
|
let line = match line {
|
||||||
|
Ok(l) => l,
|
||||||
|
Err(_) => break,
|
||||||
|
};
|
||||||
|
// Receiver gone (Node dropped): stop tailing.
|
||||||
|
if tx.send(format!("{prefix}{line}")).is_err() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
rx
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Re-exec the current test binary as `role`, with any extra env vars.
|
||||||
|
pub fn spawn_node(role: &str, extra_env: &[(&str, &str)]) -> Node {
|
||||||
|
let exe = env::current_exe().expect("current_exe");
|
||||||
|
let mut cmd = Command::new(exe);
|
||||||
|
cmd.env(ROLE_ENV, role)
|
||||||
|
// --test-threads=1: exactly one test fn starts, hits maybe_child,
|
||||||
|
// and becomes the role. --nocapture: libtest must not swallow the
|
||||||
|
// role's println! lines — the parent tails them live.
|
||||||
|
.args(["--test-threads=1", "--quiet", "--nocapture"])
|
||||||
|
.stdout(Stdio::piped())
|
||||||
|
.stderr(Stdio::piped());
|
||||||
|
for (k, v) in extra_env {
|
||||||
|
cmd.env(k, v);
|
||||||
|
}
|
||||||
|
let mut child = cmd.spawn().expect("failed to spawn node process");
|
||||||
|
let stdout_rx = tail(child.stdout.take().expect("piped stdout"), "");
|
||||||
|
let stderr_rx = tail(child.stderr.take().expect("piped stderr"), "[stderr] ");
|
||||||
|
Node {
|
||||||
|
role: role.to_string(),
|
||||||
|
child: Some(child),
|
||||||
|
stdout_rx,
|
||||||
|
stderr_rx,
|
||||||
|
transcript: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Node {
|
||||||
|
fn drain_stderr(&mut self) {
|
||||||
|
while let Ok(l) = self.stderr_rx.try_recv() {
|
||||||
|
self.transcript.push(l);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn dump(&self) -> String {
|
||||||
|
if self.transcript.is_empty() {
|
||||||
|
"<no output>".to_string()
|
||||||
|
} else {
|
||||||
|
self.transcript.join("\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Every stdout/stderr line seen so far, in arrival order. For
|
||||||
|
/// ordering-proof assertions ("by the time X arrived, Y had not").
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn transcript(&self) -> &[String] {
|
||||||
|
&self.transcript
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Wait until a stdout line satisfies `pred`; return it. Panics with the
|
||||||
|
/// full transcript after [`WAIT`]. `what` names the expectation in the
|
||||||
|
/// panic message.
|
||||||
|
pub fn wait_line(&mut self, what: &str, pred: impl Fn(&str) -> bool) -> String {
|
||||||
|
let deadline = Instant::now() + WAIT;
|
||||||
|
loop {
|
||||||
|
self.drain_stderr();
|
||||||
|
let left = deadline.saturating_duration_since(Instant::now());
|
||||||
|
match self.stdout_rx.recv_timeout(left) {
|
||||||
|
Ok(line) => {
|
||||||
|
self.transcript.push(line.clone());
|
||||||
|
if pred(&line) {
|
||||||
|
return line;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(RecvTimeoutError::Timeout) => {
|
||||||
|
// Pull in whatever stderr arrived since the last drain,
|
||||||
|
// so a role's eprintln! diagnostics survive into the dump.
|
||||||
|
self.drain_stderr();
|
||||||
|
panic!(
|
||||||
|
"node {:?}: timed out waiting for {what} after {WAIT:?}; transcript:\n{}",
|
||||||
|
self.role,
|
||||||
|
self.dump()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Err(RecvTimeoutError::Disconnected) => {
|
||||||
|
self.drain_stderr();
|
||||||
|
panic!(
|
||||||
|
"node {:?}: output closed while waiting for {what}; transcript:\n{}",
|
||||||
|
self.role,
|
||||||
|
self.dump()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Shorthand: wait for a `LISTENING <addr>` announcement, return the addr.
|
||||||
|
pub fn wait_listening(&mut self) -> String {
|
||||||
|
let line = self.wait_line("LISTENING announcement", |l| l.starts_with("LISTENING "));
|
||||||
|
line["LISTENING ".len()..].to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Wait for the process to exit; panics with the transcript on timeout.
|
||||||
|
pub fn wait_exit(&mut self) -> ExitStatus {
|
||||||
|
let deadline = Instant::now() + WAIT;
|
||||||
|
loop {
|
||||||
|
let polled = match self.child.as_mut() {
|
||||||
|
Some(c) => c.try_wait(),
|
||||||
|
None => panic!("node {:?}: already reaped", self.role),
|
||||||
|
};
|
||||||
|
match polled {
|
||||||
|
Ok(Some(status)) => {
|
||||||
|
// Drain remaining output into the transcript for dumps.
|
||||||
|
self.drain_stderr();
|
||||||
|
while let Ok(l) = self.stdout_rx.try_recv() {
|
||||||
|
self.transcript.push(l);
|
||||||
|
}
|
||||||
|
self.child = None;
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
Ok(None) => {
|
||||||
|
if Instant::now() >= deadline {
|
||||||
|
self.drain_stderr();
|
||||||
|
self.kill();
|
||||||
|
panic!(
|
||||||
|
"node {:?}: did not exit within {WAIT:?}; transcript:\n{}",
|
||||||
|
self.role,
|
||||||
|
self.dump()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
std::thread::sleep(Duration::from_millis(10));
|
||||||
|
}
|
||||||
|
Err(e) => panic!("node {:?}: try_wait failed: {e}", self.role),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Wait for exit and require success, dumping the transcript otherwise.
|
||||||
|
pub fn wait_exit_ok(&mut self) {
|
||||||
|
let status = self.wait_exit();
|
||||||
|
assert!(
|
||||||
|
status.success(),
|
||||||
|
"node {:?}: exited with {status}; transcript:\n{}",
|
||||||
|
self.role,
|
||||||
|
self.dump()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The child's OS pid, if not yet reaped.
|
||||||
|
pub fn pid(&self) -> Option<u32> {
|
||||||
|
self.child.as_ref().map(Child::id)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// SIGKILL + reap now (idempotent).
|
||||||
|
pub fn kill(&mut self) {
|
||||||
|
if let Some(mut child) = self.child.take() {
|
||||||
|
let _ = child.kill();
|
||||||
|
let _ = child.wait();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for Node {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
self.kill();
|
||||||
|
}
|
||||||
|
}
|
||||||
+35
-28
@@ -1,9 +1,7 @@
|
|||||||
//! Low-level context-switch tests. These poke `init_actor_stack` and the
|
//! Low-level context-switch tests. These poke `init_actor_stack` and the
|
||||||
//! naked asm shims directly — no scheduler involved.
|
//! naked asm shims directly — no scheduler involved.
|
||||||
|
|
||||||
use smarm::context::{
|
use smarm::context::{init_actor_stack, switch_to_actor, switch_to_scheduler};
|
||||||
get_actor_sp, init_actor_stack, set_actor_sp, switch_to_actor, switch_to_scheduler,
|
|
||||||
};
|
|
||||||
use smarm::stack::Stack;
|
use smarm::stack::Stack;
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
|
|
||||||
@@ -11,9 +9,15 @@ thread_local! {
|
|||||||
static LOG: Cell<u64> = const { Cell::new(0) };
|
static LOG: Cell<u64> = const { Cell::new(0) };
|
||||||
}
|
}
|
||||||
|
|
||||||
fn log(v: u64) { LOG.with(|c| c.set(c.get() | v)); }
|
fn log(v: u64) {
|
||||||
fn get_log() -> u64 { LOG.with(|c| c.get()) }
|
LOG.with(|c| c.set(c.get() | v));
|
||||||
fn reset_log() { LOG.with(|c| c.set(0)); }
|
}
|
||||||
|
fn get_log() -> u64 {
|
||||||
|
LOG.with(|c| c.get())
|
||||||
|
}
|
||||||
|
fn reset_log() {
|
||||||
|
LOG.with(|c| c.set(0));
|
||||||
|
}
|
||||||
|
|
||||||
extern "C-unwind" fn actor_simple() {
|
extern "C-unwind" fn actor_simple() {
|
||||||
log(0x1);
|
log(0x1);
|
||||||
@@ -23,10 +27,9 @@ extern "C-unwind" fn actor_simple() {
|
|||||||
#[test]
|
#[test]
|
||||||
fn actor_runs_and_returns_to_scheduler() {
|
fn actor_runs_and_returns_to_scheduler() {
|
||||||
reset_log();
|
reset_log();
|
||||||
let stack = Stack::new(64 * 1024).unwrap();
|
let stack = Stack::new(64 * 1024, 4096).unwrap();
|
||||||
let sp = init_actor_stack(stack.top(), actor_simple);
|
let sp = init_actor_stack(stack.top(), actor_simple);
|
||||||
set_actor_sp(sp);
|
let _ = unsafe { switch_to_actor(sp) };
|
||||||
unsafe { switch_to_actor() };
|
|
||||||
assert_eq!(get_log(), 0x1);
|
assert_eq!(get_log(), 0x1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,14 +43,13 @@ extern "C-unwind" fn actor_two_steps() {
|
|||||||
#[test]
|
#[test]
|
||||||
fn actor_yields_and_resumes() {
|
fn actor_yields_and_resumes() {
|
||||||
reset_log();
|
reset_log();
|
||||||
let stack = Stack::new(64 * 1024).unwrap();
|
let stack = Stack::new(64 * 1024, 4096).unwrap();
|
||||||
let sp = init_actor_stack(stack.top(), actor_two_steps);
|
let sp = init_actor_stack(stack.top(), actor_two_steps);
|
||||||
set_actor_sp(sp);
|
|
||||||
|
|
||||||
unsafe { switch_to_actor() };
|
let sp = unsafe { switch_to_actor(sp) };
|
||||||
assert_eq!(get_log(), 0x1, "after first resume");
|
assert_eq!(get_log(), 0x1, "after first resume");
|
||||||
|
|
||||||
unsafe { switch_to_actor() };
|
let _ = unsafe { switch_to_actor(sp) };
|
||||||
assert_eq!(get_log(), 0x1 | 0x2, "after second resume");
|
assert_eq!(get_log(), 0x1 | 0x2, "after second resume");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,7 +58,7 @@ fn actor_yields_and_resumes() {
|
|||||||
use std::sync::OnceLock;
|
use std::sync::OnceLock;
|
||||||
|
|
||||||
static REG_BEFORE: OnceLock<[u64; 4]> = OnceLock::new();
|
static REG_BEFORE: OnceLock<[u64; 4]> = OnceLock::new();
|
||||||
static REG_AFTER: OnceLock<[u64; 4]> = OnceLock::new();
|
static REG_AFTER: OnceLock<[u64; 4]> = OnceLock::new();
|
||||||
|
|
||||||
extern "C-unwind" fn actor_reg_check() {
|
extern "C-unwind" fn actor_reg_check() {
|
||||||
unsafe {
|
unsafe {
|
||||||
@@ -73,7 +75,10 @@ extern "C-unwind" fn actor_reg_check() {
|
|||||||
REG_BEFORE.set([s0, s1, s2, s3]).ok();
|
REG_BEFORE.set([s0, s1, s2, s3]).ok();
|
||||||
switch_to_scheduler();
|
switch_to_scheduler();
|
||||||
|
|
||||||
let a0: u64; let a1: u64; let a2: u64; let a3: u64;
|
let a0: u64;
|
||||||
|
let a1: u64;
|
||||||
|
let a2: u64;
|
||||||
|
let a3: u64;
|
||||||
core::arch::asm!(
|
core::arch::asm!(
|
||||||
"mov {a0}, r12", "mov {a1}, r13", "mov {a2}, r14", "mov {a3}, r15",
|
"mov {a0}, r12", "mov {a1}, r13", "mov {a2}, r14", "mov {a3}, r15",
|
||||||
a0 = out(reg) a0, a1 = out(reg) a1, a2 = out(reg) a2, a3 = out(reg) a3,
|
a0 = out(reg) a0, a1 = out(reg) a1, a2 = out(reg) a2, a3 = out(reg) a3,
|
||||||
@@ -85,11 +90,16 @@ extern "C-unwind" fn actor_reg_check() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn callee_saved_registers_survive_yield() {
|
fn callee_saved_registers_survive_yield() {
|
||||||
let stack = Stack::new(64 * 1024).unwrap();
|
let stack = Stack::new(64 * 1024, 4096).unwrap();
|
||||||
let sp = init_actor_stack(stack.top(), actor_reg_check);
|
let sp = init_actor_stack(stack.top(), actor_reg_check);
|
||||||
set_actor_sp(sp);
|
unsafe {
|
||||||
unsafe { switch_to_actor(); switch_to_actor(); }
|
let sp = switch_to_actor(sp);
|
||||||
assert_eq!(REG_BEFORE.get().copied().unwrap(), REG_AFTER.get().copied().unwrap());
|
let _ = switch_to_actor(sp);
|
||||||
|
}
|
||||||
|
assert_eq!(
|
||||||
|
REG_BEFORE.get().copied().unwrap(),
|
||||||
|
REG_AFTER.get().copied().unwrap()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Two actors, independent stacks.
|
// Two actors, independent stacks.
|
||||||
@@ -117,20 +127,17 @@ extern "C-unwind" fn actor_b() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn two_actors_dont_corrupt_each_other() {
|
fn two_actors_dont_corrupt_each_other() {
|
||||||
let stack_a = Stack::new(64 * 1024).unwrap();
|
let stack_a = Stack::new(64 * 1024, 4096).unwrap();
|
||||||
let stack_b = Stack::new(64 * 1024).unwrap();
|
let stack_b = Stack::new(64 * 1024, 4096).unwrap();
|
||||||
|
|
||||||
let sp_a = init_actor_stack(stack_a.top(), actor_a);
|
let sp_a = init_actor_stack(stack_a.top(), actor_a);
|
||||||
let sp_b = init_actor_stack(stack_b.top(), actor_b);
|
let sp_b = init_actor_stack(stack_b.top(), actor_b);
|
||||||
|
|
||||||
set_actor_sp(sp_a); unsafe { switch_to_actor() };
|
let sp_a = unsafe { switch_to_actor(sp_a) };
|
||||||
let sp_a = get_actor_sp();
|
let sp_b = unsafe { switch_to_actor(sp_b) };
|
||||||
|
|
||||||
set_actor_sp(sp_b); unsafe { switch_to_actor() };
|
let _ = unsafe { switch_to_actor(sp_a) };
|
||||||
let sp_b = get_actor_sp();
|
let _ = unsafe { switch_to_actor(sp_b) };
|
||||||
|
|
||||||
set_actor_sp(sp_a); unsafe { switch_to_actor() };
|
|
||||||
set_actor_sp(sp_b); unsafe { switch_to_actor() };
|
|
||||||
|
|
||||||
assert_eq!(A_VAL.with(|c| c.get()), 0xA00D);
|
assert_eq!(A_VAL.with(|c| c.get()), 0xA00D);
|
||||||
assert_eq!(B_VAL.with(|c| c.get()), 0xB00D);
|
assert_eq!(B_VAL.with(|c| c.get()), 0xB00D);
|
||||||
|
|||||||
@@ -0,0 +1,125 @@
|
|||||||
|
//! Cross-thread wake: a thread that is *not* a smarm scheduler thread must be
|
||||||
|
//! able to wake (and stop) a parked actor.
|
||||||
|
//!
|
||||||
|
//! The gap this pins down: every off-runtime wake primitive (`unpark`,
|
||||||
|
//! `unpark_at`, `request_stop`) reaches the runtime through the `RUNTIME`
|
||||||
|
//! thread-local, which is `None` on any non-scheduler thread — so a wake
|
||||||
|
//! issued from a foreign OS thread is a silent no-op and the parked actor
|
||||||
|
//! sleeps forever. Both failure modes below manifest as `Runtime::run` never
|
||||||
|
//! returning, so each test is wrapped in a watchdog: a timeout is the failure.
|
||||||
|
//!
|
||||||
|
//! The fix mirrors RFC 018's IO backend — the waker reaches the runtime
|
||||||
|
//! through a `Weak<RuntimeInner>` it already holds (the receiver captures one
|
||||||
|
//! when it parks; `Runtime::handle()` hands one to an app thread).
|
||||||
|
|
||||||
|
use std::sync::mpsc;
|
||||||
|
use std::thread;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
const WATCHDOG: Duration = Duration::from_secs(10);
|
||||||
|
/// Give the target actor time to actually park before the foreign thread pokes
|
||||||
|
/// it, so we exercise the *wake* of a parked actor rather than the entry-side
|
||||||
|
/// stop check.
|
||||||
|
const SETTLE: Duration = Duration::from_millis(200);
|
||||||
|
|
||||||
|
fn assert_send_sync<T: Send + Sync>() {}
|
||||||
|
|
||||||
|
/// A cross-thread `send` from a plain OS thread must wake a receiver parked in
|
||||||
|
/// `recv`. Under the thread-local-only wake path the send enqueues the message
|
||||||
|
/// but never wakes the receiver, so `recv` — and therefore `run` — hangs.
|
||||||
|
#[test]
|
||||||
|
fn foreign_thread_send_wakes_parked_receiver() {
|
||||||
|
let (done_tx, done_rx) = mpsc::channel();
|
||||||
|
thread::spawn(move || {
|
||||||
|
let rt = smarm::init(smarm::Config::exact(2));
|
||||||
|
rt.run(|| {
|
||||||
|
let (tx, rx) = smarm::channel::<u32>();
|
||||||
|
// Receiver actor: parks on recv until the foreign thread sends.
|
||||||
|
let h = smarm::spawn(move || {
|
||||||
|
assert_eq!(rx.recv().expect("recv"), 42);
|
||||||
|
});
|
||||||
|
// Foreign (non-scheduler) OS thread owns the Sender and sends
|
||||||
|
// after the receiver has parked.
|
||||||
|
let sender = thread::spawn(move || {
|
||||||
|
thread::sleep(SETTLE);
|
||||||
|
tx.send(42).expect("send");
|
||||||
|
});
|
||||||
|
let _ = h.join();
|
||||||
|
sender.join().expect("sender thread");
|
||||||
|
});
|
||||||
|
let _ = done_tx.send(());
|
||||||
|
});
|
||||||
|
done_rx
|
||||||
|
.recv_timeout(WATCHDOG)
|
||||||
|
.expect("run did not return: a foreign-thread send never woke the parked receiver");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A cross-thread `request_stop` through a `RuntimeHandle` must wake and stop a
|
||||||
|
/// parked actor. The actor parks on a long sleep (only a stop can end it); the
|
||||||
|
/// handle is grabbed before `run` and driven from a foreign thread.
|
||||||
|
#[test]
|
||||||
|
fn foreign_thread_request_stop_wakes_parked_actor() {
|
||||||
|
assert_send_sync::<smarm::RuntimeHandle>();
|
||||||
|
|
||||||
|
let rt = smarm::init(smarm::Config::exact(2));
|
||||||
|
let handle = rt.handle();
|
||||||
|
|
||||||
|
// Foreign thread: learn the target pid from inside the run, let it park,
|
||||||
|
// then stop it through the handle.
|
||||||
|
let (pid_tx, pid_rx) = mpsc::channel::<smarm::Pid>();
|
||||||
|
let stopper = thread::spawn(move || {
|
||||||
|
let pid = pid_rx.recv().expect("pid");
|
||||||
|
thread::sleep(SETTLE);
|
||||||
|
handle.request_stop(pid);
|
||||||
|
});
|
||||||
|
|
||||||
|
let (done_tx, done_rx) = mpsc::channel();
|
||||||
|
thread::spawn(move || {
|
||||||
|
rt.run(move || {
|
||||||
|
let h = smarm::spawn(|| {
|
||||||
|
// Parks indefinitely; only a cooperative stop unwinds it.
|
||||||
|
smarm::sleep(Duration::from_secs(3600));
|
||||||
|
});
|
||||||
|
pid_tx.send(h.pid()).expect("send pid");
|
||||||
|
let _ = h.join();
|
||||||
|
});
|
||||||
|
let _ = done_tx.send(());
|
||||||
|
});
|
||||||
|
|
||||||
|
done_rx
|
||||||
|
.recv_timeout(WATCHDOG)
|
||||||
|
.expect("run did not return: a foreign-thread request_stop never woke the parked actor");
|
||||||
|
stopper.join().expect("stopper thread");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A `RuntimeHandle` held across (and beyond) a run must not keep the runtime
|
||||||
|
/// alive or block all-done: `run` still returns, and once the `Runtime` is
|
||||||
|
/// dropped the handle degrades to a harmless no-op (Weak lifecycle) rather than
|
||||||
|
/// panicking or touching freed memory.
|
||||||
|
#[test]
|
||||||
|
fn lingering_handle_does_not_block_all_done() {
|
||||||
|
let rt = smarm::init(smarm::Config::exact(1));
|
||||||
|
let handle = rt.handle(); // outlives the run below
|
||||||
|
|
||||||
|
let (pid_tx, pid_rx) = mpsc::channel::<smarm::Pid>();
|
||||||
|
let (done_tx, done_rx) = mpsc::channel();
|
||||||
|
let runner = thread::spawn(move || {
|
||||||
|
rt.run(move || {
|
||||||
|
let h = smarm::spawn(|| {});
|
||||||
|
pid_tx.send(h.pid()).expect("send pid");
|
||||||
|
let _ = h.join();
|
||||||
|
});
|
||||||
|
// `rt` is dropped here, at the end of this thread.
|
||||||
|
let _ = done_tx.send(());
|
||||||
|
});
|
||||||
|
|
||||||
|
done_rx
|
||||||
|
.recv_timeout(WATCHDOG)
|
||||||
|
.expect("run did not return while a RuntimeHandle was held live");
|
||||||
|
runner.join().expect("runner thread");
|
||||||
|
|
||||||
|
// Runtime is now dropped. A stop through the lingering handle must be a
|
||||||
|
// silent no-op, not a panic or use-after-free.
|
||||||
|
let dead_pid = pid_rx.recv().expect("pid");
|
||||||
|
handle.request_stop(dead_pid);
|
||||||
|
}
|
||||||
+22
-7
@@ -11,8 +11,8 @@
|
|||||||
//! OUTSIDE `run` — an in-actor assertion alone passes vacuously.
|
//! OUTSIDE `run` — an in-actor assertion alone passes vacuously.
|
||||||
|
|
||||||
use smarm::{
|
use smarm::{
|
||||||
channel, run, select, select_timeout, spawn, try_select, wait_readable,
|
channel, run, select, select_timeout, spawn, try_select, wait_readable, wait_readable_timeout,
|
||||||
wait_readable_timeout, wait_writable_timeout, yield_now, FdArm,
|
wait_writable_timeout, yield_now, FdArm,
|
||||||
};
|
};
|
||||||
use std::os::fd::RawFd;
|
use std::os::fd::RawFd;
|
||||||
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
|
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
|
||||||
@@ -33,7 +33,10 @@ impl Pipe {
|
|||||||
let mut fds: [libc::c_int; 2] = [0; 2];
|
let mut fds: [libc::c_int; 2] = [0; 2];
|
||||||
let r = unsafe { libc::pipe2(fds.as_mut_ptr(), libc::O_CLOEXEC | libc::O_NONBLOCK) };
|
let r = unsafe { libc::pipe2(fds.as_mut_ptr(), libc::O_CLOEXEC | libc::O_NONBLOCK) };
|
||||||
assert_eq!(r, 0, "pipe2 failed");
|
assert_eq!(r, 0, "pipe2 failed");
|
||||||
Pipe { read: fds[0], write: fds[1] }
|
Pipe {
|
||||||
|
read: fds[0],
|
||||||
|
write: fds[1],
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -253,12 +256,18 @@ fn wait_readable_timeout_times_out_then_succeeds_with_data() {
|
|||||||
let (rfd, wfd) = (p.read, p.write);
|
let (rfd, wfd) = (p.read, p.write);
|
||||||
|
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
assert_eq!(wait_readable_timeout(rfd, Duration::from_millis(30)).unwrap(), false);
|
assert_eq!(
|
||||||
|
wait_readable_timeout(rfd, Duration::from_millis(30)).unwrap(),
|
||||||
|
false
|
||||||
|
);
|
||||||
assert!(start.elapsed() >= Duration::from_millis(30));
|
assert!(start.elapsed() >= Duration::from_millis(30));
|
||||||
|
|
||||||
// Timed-out wait must leave the fd clean; ready path returns true.
|
// Timed-out wait must leave the fd clean; ready path returns true.
|
||||||
assert_eq!(raw_write(wfd, b"d"), 1);
|
assert_eq!(raw_write(wfd, b"d"), 1);
|
||||||
assert_eq!(wait_readable_timeout(rfd, Duration::from_secs(5)).unwrap(), true);
|
assert_eq!(
|
||||||
|
wait_readable_timeout(rfd, Duration::from_secs(5)).unwrap(),
|
||||||
|
true
|
||||||
|
);
|
||||||
let mut buf = [0u8; 1];
|
let mut buf = [0u8; 1];
|
||||||
assert_eq!(raw_read(rfd, &mut buf), 1);
|
assert_eq!(raw_read(rfd, &mut buf), 1);
|
||||||
ok2.store(true, Ordering::SeqCst);
|
ok2.store(true, Ordering::SeqCst);
|
||||||
@@ -274,7 +283,10 @@ fn wait_readable_timeout_wakes_on_late_data() {
|
|||||||
let p = Pipe::new();
|
let p = Pipe::new();
|
||||||
let (rfd, wfd) = (p.read, p.write);
|
let (rfd, wfd) = (p.read, p.write);
|
||||||
let h = spawn(move || {
|
let h = spawn(move || {
|
||||||
assert_eq!(wait_readable_timeout(rfd, Duration::from_secs(5)).unwrap(), true);
|
assert_eq!(
|
||||||
|
wait_readable_timeout(rfd, Duration::from_secs(5)).unwrap(),
|
||||||
|
true
|
||||||
|
);
|
||||||
let mut buf = [0u8; 1];
|
let mut buf = [0u8; 1];
|
||||||
assert_eq!(raw_read(rfd, &mut buf), 1);
|
assert_eq!(raw_read(rfd, &mut buf), 1);
|
||||||
got2.store(buf[0] as u32, Ordering::SeqCst);
|
got2.store(buf[0] as u32, Ordering::SeqCst);
|
||||||
@@ -292,7 +304,10 @@ fn wait_writable_timeout_ready_now_on_empty_pipe() {
|
|||||||
run(move || {
|
run(move || {
|
||||||
let p = Pipe::new();
|
let p = Pipe::new();
|
||||||
// An empty pipe's write end is writable: ready-now path, no park.
|
// An empty pipe's write end is writable: ready-now path, no park.
|
||||||
assert_eq!(wait_writable_timeout(p.write, Duration::from_secs(5)).unwrap(), true);
|
assert_eq!(
|
||||||
|
wait_writable_timeout(p.write, Duration::from_secs(5)).unwrap(),
|
||||||
|
true
|
||||||
|
);
|
||||||
ok2.store(true, Ordering::SeqCst);
|
ok2.store(true, Ordering::SeqCst);
|
||||||
});
|
});
|
||||||
assert!(ok.load(Ordering::SeqCst));
|
assert!(ok.load(Ordering::SeqCst));
|
||||||
|
|||||||
+88
-33
@@ -1,7 +1,7 @@
|
|||||||
//! gen_server tests: call round-trip, cast, lifecycle callbacks, and the two
|
//! gen_server tests: call round-trip, cast, lifecycle callbacks, and the two
|
||||||
//! server-down detection paths (reply-channel close vs. inbox-send failure).
|
//! server-down detection paths (reply-channel close vs. inbox-send failure).
|
||||||
|
|
||||||
use smarm::gen_server::{start, CallError, GenServer, ServerBuilder};
|
use smarm::gen_server::{start, CallError, GenServer, GenServerBuilder};
|
||||||
use smarm::run;
|
use smarm::run;
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
@@ -73,7 +73,7 @@ impl GenServer for Lifecycle {
|
|||||||
type Info = ();
|
type Info = ();
|
||||||
type Timer = ();
|
type Timer = ();
|
||||||
|
|
||||||
fn init(&mut self, _ctx: &smarm::gen_server::ServerCtx<Self>) {
|
fn init(&mut self, _ctx: &smarm::gen_server::GenServerCtx<Self>) {
|
||||||
self.log.lock().unwrap().push("init");
|
self.log.lock().unwrap().push("init");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,7 +88,7 @@ impl GenServer for Lifecycle {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// init -> handle_call -> (drop last ref closes inbox) -> terminate.
|
// init -> handle_call -> shutdown -> terminate.
|
||||||
#[test]
|
#[test]
|
||||||
fn init_and_terminate_run() {
|
fn init_and_terminate_run() {
|
||||||
let log = Arc::new(Mutex::new(Vec::new()));
|
let log = Arc::new(Mutex::new(Vec::new()));
|
||||||
@@ -96,9 +96,9 @@ fn init_and_terminate_run() {
|
|||||||
run(move || {
|
run(move || {
|
||||||
let server = start(Lifecycle { log: log2 });
|
let server = start(Lifecycle { log: log2 });
|
||||||
server.call(()).unwrap();
|
server.call(()).unwrap();
|
||||||
// Dropping the only ref closes the inbox; the server breaks out of its
|
// Refs are addresses: dropping one does not end the server. The
|
||||||
// recv loop and runs terminate. run() will not return until it has.
|
// explicit close does, and waits for terminate.
|
||||||
drop(server);
|
server.shutdown();
|
||||||
});
|
});
|
||||||
assert_eq!(*log.lock().unwrap(), vec!["init", "call", "terminate"]);
|
assert_eq!(*log.lock().unwrap(), vec!["init", "call", "terminate"]);
|
||||||
}
|
}
|
||||||
@@ -275,7 +275,7 @@ fn info_is_dispatched() {
|
|||||||
let got2 = got.clone();
|
let got2 = got.clone();
|
||||||
run(move || {
|
run(move || {
|
||||||
let (info_tx, info_rx) = smarm::channel::<&'static str>();
|
let (info_tx, info_rx) = smarm::channel::<&'static str>();
|
||||||
let server = ServerBuilder::new(Logger { log: Vec::new() })
|
let server = GenServerBuilder::new(Logger { log: Vec::new() })
|
||||||
.with_info(info_rx)
|
.with_info(info_rx)
|
||||||
.start();
|
.start();
|
||||||
info_tx.send("info").unwrap();
|
info_tx.send("info").unwrap();
|
||||||
@@ -293,7 +293,7 @@ fn info_outranks_inbox() {
|
|||||||
let got2 = got.clone();
|
let got2 = got.clone();
|
||||||
run(move || {
|
run(move || {
|
||||||
let (info_tx, info_rx) = smarm::channel::<&'static str>();
|
let (info_tx, info_rx) = smarm::channel::<&'static str>();
|
||||||
let server = ServerBuilder::new(Logger { log: Vec::new() })
|
let server = GenServerBuilder::new(Logger { log: Vec::new() })
|
||||||
.with_info(info_rx)
|
.with_info(info_rx)
|
||||||
.start();
|
.start();
|
||||||
// The server actor hasn't run yet: both messages are queued before
|
// The server actor hasn't run yet: both messages are queued before
|
||||||
@@ -314,7 +314,7 @@ fn info_arms_keep_declaration_priority() {
|
|||||||
run(move || {
|
run(move || {
|
||||||
let (hi_tx, hi_rx) = smarm::channel::<&'static str>();
|
let (hi_tx, hi_rx) = smarm::channel::<&'static str>();
|
||||||
let (lo_tx, lo_rx) = smarm::channel::<&'static str>();
|
let (lo_tx, lo_rx) = smarm::channel::<&'static str>();
|
||||||
let server = ServerBuilder::new(Logger { log: Vec::new() })
|
let server = GenServerBuilder::new(Logger { log: Vec::new() })
|
||||||
.with_info(hi_rx)
|
.with_info(hi_rx)
|
||||||
.with_info(lo_rx)
|
.with_info(lo_rx)
|
||||||
.start();
|
.start();
|
||||||
@@ -335,7 +335,7 @@ fn closed_info_arm_is_dropped_silently() {
|
|||||||
let got2 = got.clone();
|
let got2 = got.clone();
|
||||||
run(move || {
|
run(move || {
|
||||||
let (info_tx, info_rx) = smarm::channel::<&'static str>();
|
let (info_tx, info_rx) = smarm::channel::<&'static str>();
|
||||||
let server = ServerBuilder::new(Logger { log: Vec::new() })
|
let server = GenServerBuilder::new(Logger { log: Vec::new() })
|
||||||
.with_info(info_rx)
|
.with_info(info_rx)
|
||||||
.start();
|
.start();
|
||||||
drop(info_tx); // closed before the server's first select
|
drop(info_tx); // closed before the server's first select
|
||||||
@@ -371,7 +371,7 @@ impl GenServer for Pool {
|
|||||||
type Info = ();
|
type Info = ();
|
||||||
type Timer = ();
|
type Timer = ();
|
||||||
|
|
||||||
fn init(&mut self, ctx: &smarm::gen_server::ServerCtx<Self>) {
|
fn init(&mut self, ctx: &smarm::gen_server::GenServerCtx<Self>) {
|
||||||
self.watcher = Some(ctx.watcher());
|
self.watcher = Some(ctx.watcher());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -396,17 +396,31 @@ impl GenServer for Pool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// A worker spawned and watched from inside a handler delivers its Down to
|
// A worker spawned and watched from inside a handler delivers its Down to
|
||||||
// handle_down. Down arms outrank the inbox, so the death is in the log by
|
// handle_down. Nothing orders the worker's death before the follow-up
|
||||||
// the time the follow-up call is answered.
|
// call (the worker sits in the shared queue while call/reply wakes ride the
|
||||||
|
// wake slot), so poll: the log must become exactly [Panic] within a bounded
|
||||||
|
// number of yields. Down-outranks-inbox ordering is covered by
|
||||||
|
// `watch_dead_pid_is_noproc_down`.
|
||||||
#[test]
|
#[test]
|
||||||
fn worker_pool_down_reaches_handle_down() {
|
fn worker_pool_down_reaches_handle_down() {
|
||||||
let got = Arc::new(Mutex::new(Vec::new()));
|
let got = Arc::new(Mutex::new(Vec::new()));
|
||||||
let got2 = got.clone();
|
let got2 = got.clone();
|
||||||
run(move || {
|
run(move || {
|
||||||
let server = start(Pool { watcher: None, log: Vec::new() });
|
let server = start(Pool {
|
||||||
|
watcher: None,
|
||||||
|
log: Vec::new(),
|
||||||
|
});
|
||||||
server.cast(PoolCast::SpawnDoomedWorker).unwrap();
|
server.cast(PoolCast::SpawnDoomedWorker).unwrap();
|
||||||
let _ = server.call(()).unwrap(); // sync point: cast handled, worker live
|
let _ = server.call(()).unwrap(); // sync point: cast handled, worker live
|
||||||
*got2.lock().unwrap() = server.call(()).unwrap();
|
let mut log = Vec::new();
|
||||||
|
for _ in 0..10_000 {
|
||||||
|
log = server.call(()).unwrap();
|
||||||
|
if !log.is_empty() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
smarm::yield_now();
|
||||||
|
}
|
||||||
|
*got2.lock().unwrap() = log;
|
||||||
});
|
});
|
||||||
assert_eq!(*got.lock().unwrap(), vec![DownReason::Panic]);
|
assert_eq!(*got.lock().unwrap(), vec![DownReason::Panic]);
|
||||||
}
|
}
|
||||||
@@ -421,7 +435,10 @@ fn watch_dead_pid_is_noproc_down() {
|
|||||||
let h = spawn(|| {});
|
let h = spawn(|| {});
|
||||||
let dead = h.pid();
|
let dead = h.pid();
|
||||||
h.join().unwrap();
|
h.join().unwrap();
|
||||||
let server = start(Pool { watcher: None, log: Vec::new() });
|
let server = start(Pool {
|
||||||
|
watcher: None,
|
||||||
|
log: Vec::new(),
|
||||||
|
});
|
||||||
server.cast(PoolCast::Watch(dead)).unwrap();
|
server.cast(PoolCast::Watch(dead)).unwrap();
|
||||||
*got2.lock().unwrap() = server.call(()).unwrap();
|
*got2.lock().unwrap() = server.call(()).unwrap();
|
||||||
});
|
});
|
||||||
@@ -448,7 +465,7 @@ fn unused_ctx_closes_control_arm_silently() {
|
|||||||
// RFC 015 — gen_server timers. A server that arms one-shot timers from a cast
|
// RFC 015 — gen_server timers. A server that arms one-shot timers from a cast
|
||||||
// and records each fire's payload, plus the cancel race signal.
|
// and records each fire's payload, plus the cancel race signal.
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
use smarm::gen_server::{ServerCtx, TimerHandle};
|
use smarm::gen_server::{GenServerCtx, TimerHandle};
|
||||||
use smarm::TimerId;
|
use smarm::TimerId;
|
||||||
|
|
||||||
enum TkCast {
|
enum TkCast {
|
||||||
@@ -471,7 +488,7 @@ impl GenServer for Timed {
|
|||||||
type Info = ();
|
type Info = ();
|
||||||
type Timer = u32;
|
type Timer = u32;
|
||||||
|
|
||||||
fn init(&mut self, ctx: &ServerCtx<Self>) {
|
fn init(&mut self, ctx: &GenServerCtx<Self>) {
|
||||||
self.timer = Some(ctx.timer());
|
self.timer = Some(ctx.timer());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -497,7 +514,12 @@ impl GenServer for Timed {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn timed(fired: Arc<Mutex<Vec<u32>>>, cancel_won: Arc<Mutex<Option<bool>>>) -> Timed {
|
fn timed(fired: Arc<Mutex<Vec<u32>>>, cancel_won: Arc<Mutex<Option<bool>>>) -> Timed {
|
||||||
Timed { timer: None, fired, cancel_won, last: None }
|
Timed {
|
||||||
|
timer: None,
|
||||||
|
fired,
|
||||||
|
cancel_won,
|
||||||
|
last: None,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// A one-shot armed from a handler fires into handle_timer with its payload.
|
// A one-shot armed from a handler fires into handle_timer with its payload.
|
||||||
@@ -534,7 +556,11 @@ fn cancel_before_fire_suppresses_it() {
|
|||||||
let count = server.call(()).unwrap();
|
let count = server.call(()).unwrap();
|
||||||
assert_eq!(count, 0, "cancelled timer must not fire");
|
assert_eq!(count, 0, "cancelled timer must not fire");
|
||||||
});
|
});
|
||||||
assert_eq!(*cancel_won.lock().unwrap(), Some(true), "cancel beat the fire");
|
assert_eq!(
|
||||||
|
*cancel_won.lock().unwrap(),
|
||||||
|
Some(true),
|
||||||
|
"cancel beat the fire"
|
||||||
|
);
|
||||||
assert!(fired.lock().unwrap().is_empty());
|
assert!(fired.lock().unwrap().is_empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -549,11 +575,16 @@ fn tick_every_rearms_repeatedly() {
|
|||||||
run(move || {
|
run(move || {
|
||||||
let cw = Arc::new(Mutex::new(None));
|
let cw = Arc::new(Mutex::new(None));
|
||||||
let server = start(timed(f2, cw));
|
let server = start(timed(f2, cw));
|
||||||
server.cast(TkCast::Tick(Duration::from_millis(20))).unwrap();
|
server
|
||||||
|
.cast(TkCast::Tick(Duration::from_millis(20)))
|
||||||
|
.unwrap();
|
||||||
let _ = server.call(()).unwrap(); // sync: periodic armed
|
let _ = server.call(()).unwrap(); // sync: periodic armed
|
||||||
smarm::sleep(Duration::from_millis(130)); // ~6 periods
|
smarm::sleep(Duration::from_millis(130)); // ~6 periods
|
||||||
let count = server.call(()).unwrap();
|
let count = server.call(()).unwrap();
|
||||||
assert!(count >= 3, "periodic should have re-armed several times, got {count}");
|
assert!(
|
||||||
|
count >= 3,
|
||||||
|
"periodic should have re-armed several times, got {count}"
|
||||||
|
);
|
||||||
});
|
});
|
||||||
// Every tick delivered the same payload.
|
// Every tick delivered the same payload.
|
||||||
assert!(fired.lock().unwrap().iter().all(|&v| v == 9));
|
assert!(fired.lock().unwrap().iter().all(|&v| v == 9));
|
||||||
@@ -568,7 +599,9 @@ fn cancel_stops_a_periodic() {
|
|||||||
let c2 = cancel_won.clone();
|
let c2 = cancel_won.clone();
|
||||||
run(move || {
|
run(move || {
|
||||||
let server = start(timed(f2, c2));
|
let server = start(timed(f2, c2));
|
||||||
server.cast(TkCast::Tick(Duration::from_millis(20))).unwrap();
|
server
|
||||||
|
.cast(TkCast::Tick(Duration::from_millis(20)))
|
||||||
|
.unwrap();
|
||||||
let _ = server.call(()).unwrap();
|
let _ = server.call(()).unwrap();
|
||||||
smarm::sleep(Duration::from_millis(70)); // a few ticks
|
smarm::sleep(Duration::from_millis(70)); // a few ticks
|
||||||
server.cast(TkCast::CancelLast).unwrap();
|
server.cast(TkCast::CancelLast).unwrap();
|
||||||
@@ -597,7 +630,7 @@ impl GenServer for Idler {
|
|||||||
type Info = ();
|
type Info = ();
|
||||||
type Timer = ();
|
type Timer = ();
|
||||||
|
|
||||||
fn init(&mut self, ctx: &ServerCtx<Self>) {
|
fn init(&mut self, ctx: &GenServerCtx<Self>) {
|
||||||
ctx.idle_after(self.window);
|
ctx.idle_after(self.window);
|
||||||
}
|
}
|
||||||
fn handle_call(&mut self, _: ()) -> u32 {
|
fn handle_call(&mut self, _: ()) -> u32 {
|
||||||
@@ -616,11 +649,17 @@ fn idle_fires_repeatedly_on_quiet() {
|
|||||||
let idles = Arc::new(Mutex::new(0));
|
let idles = Arc::new(Mutex::new(0));
|
||||||
let i2 = idles.clone();
|
let i2 = idles.clone();
|
||||||
run(move || {
|
run(move || {
|
||||||
let server = start(Idler { window: Duration::from_millis(25), idles: i2 });
|
let server = start(Idler {
|
||||||
|
window: Duration::from_millis(25),
|
||||||
|
idles: i2,
|
||||||
|
});
|
||||||
smarm::sleep(Duration::from_millis(130)); // quiet ⇒ ~5 windows
|
smarm::sleep(Duration::from_millis(130)); // quiet ⇒ ~5 windows
|
||||||
drop(server); // keep the server alive across the quiet span
|
drop(server); // keep the server alive across the quiet span
|
||||||
});
|
});
|
||||||
assert!(*idles.lock().unwrap() >= 2, "idle should re-arm and fire several times");
|
assert!(
|
||||||
|
*idles.lock().unwrap() >= 2,
|
||||||
|
"idle should re-arm and fire several times"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Traffic within the window keeps idle from firing; only once the inbox goes
|
// Traffic within the window keeps idle from firing; only once the inbox goes
|
||||||
@@ -632,7 +671,10 @@ fn traffic_resets_the_idle_window() {
|
|||||||
let before_quiet = Arc::new(Mutex::new(u32::MAX));
|
let before_quiet = Arc::new(Mutex::new(u32::MAX));
|
||||||
let bq = before_quiet.clone();
|
let bq = before_quiet.clone();
|
||||||
run(move || {
|
run(move || {
|
||||||
let server = start(Idler { window: Duration::from_millis(60), idles: i2 });
|
let server = start(Idler {
|
||||||
|
window: Duration::from_millis(60),
|
||||||
|
idles: i2,
|
||||||
|
});
|
||||||
// Poke every 25ms (< 60ms window) for ~100ms: each cast resets the
|
// Poke every 25ms (< 60ms window) for ~100ms: each cast resets the
|
||||||
// window before it can elapse.
|
// window before it can elapse.
|
||||||
for _ in 0..4 {
|
for _ in 0..4 {
|
||||||
@@ -643,8 +685,15 @@ fn traffic_resets_the_idle_window() {
|
|||||||
smarm::sleep(Duration::from_millis(140)); // now genuinely quiet
|
smarm::sleep(Duration::from_millis(140)); // now genuinely quiet
|
||||||
drop(server);
|
drop(server);
|
||||||
});
|
});
|
||||||
assert_eq!(*before_quiet.lock().unwrap(), 0, "steady traffic must suppress idle");
|
assert_eq!(
|
||||||
assert!(*idles.lock().unwrap() >= 1, "idle fires once the inbox falls quiet");
|
*before_quiet.lock().unwrap(),
|
||||||
|
0,
|
||||||
|
"steady traffic must suppress idle"
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
*idles.lock().unwrap() >= 1,
|
||||||
|
"idle fires once the inbox falls quiet"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// RFC 015 §4.7 — no armed timer survives loop exit. A server with a live
|
// RFC 015 §4.7 — no armed timer survives loop exit. A server with a live
|
||||||
@@ -658,15 +707,21 @@ fn no_timer_survives_exit() {
|
|||||||
let f_read = fired.clone();
|
let f_read = fired.clone();
|
||||||
run(move || {
|
run(move || {
|
||||||
let server = start(timed(f_server, Arc::new(Mutex::new(None))));
|
let server = start(timed(f_server, Arc::new(Mutex::new(None))));
|
||||||
server.cast(TkCast::Tick(Duration::from_millis(15))).unwrap();
|
server
|
||||||
|
.cast(TkCast::Tick(Duration::from_millis(15)))
|
||||||
|
.unwrap();
|
||||||
let _ = server.call(()).unwrap(); // sync: periodic armed
|
let _ = server.call(()).unwrap(); // sync: periodic armed
|
||||||
smarm::sleep(Duration::from_millis(45)); // a couple of ticks
|
smarm::sleep(Duration::from_millis(45)); // a couple of ticks
|
||||||
let mon = smarm::monitor(server.pid());
|
let mon = smarm::monitor(server.pid());
|
||||||
drop(server); // inbox closes → loop exits → guard drains timers
|
server.shutdown(); // loop exits → guard drains timers
|
||||||
// Clean Down ⇒ the loop returned without the no-leak assert aborting.
|
// Clean Down ⇒ the loop returned without the no-leak assert aborting.
|
||||||
assert!(mon.rx.recv().is_ok());
|
assert!(mon.rx.recv().is_ok());
|
||||||
let at_exit = f_read.lock().unwrap().len();
|
let at_exit = f_read.lock().unwrap().len();
|
||||||
smarm::sleep(Duration::from_millis(90)); // would be several more ticks
|
smarm::sleep(Duration::from_millis(90)); // would be several more ticks
|
||||||
assert_eq!(f_read.lock().unwrap().len(), at_exit, "no tick may fire after exit");
|
assert_eq!(
|
||||||
|
f_read.lock().unwrap().len(),
|
||||||
|
at_exit,
|
||||||
|
"no tick may fire after exit"
|
||||||
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,243 @@
|
|||||||
|
//! gen_server lifetime is the actor's, not its refs' (OTP: a pid is an
|
||||||
|
//! address, a process lives until it stops, is shut down, or is killed).
|
||||||
|
//!
|
||||||
|
//! - Dropping the last `GenServerRef` does NOT end the server. It ends via
|
||||||
|
//! `StopHandle::stop`, `request_shutdown` / `GenServerRef::shutdown`,
|
||||||
|
//! `request_stop`, or a handler panic.
|
||||||
|
//! - `GenServerBuilder::named(N).run()` runs the loop inline as the *current*
|
||||||
|
//! actor, so a server is a direct `ChildSpec` child: the supervisor's
|
||||||
|
//! shutdown reaches it as `handle_shutdown`, a restart re-binds the name,
|
||||||
|
//! and by-name `call`/`cast` reach whichever incarnation is live.
|
||||||
|
|
||||||
|
use smarm::gen_server::{
|
||||||
|
self, GenServer, GenServerBuilder, GenServerCtx, GenServerName, ShutdownAction, StopHandle,
|
||||||
|
};
|
||||||
|
use smarm::registry::RegisterError;
|
||||||
|
use smarm::supervisor::{ChildSpec, OneForOne, Restart, Shutdown};
|
||||||
|
use smarm::{monitor, request_shutdown, run, sleep, spawn, DownReason};
|
||||||
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||||
|
use std::sync::{Arc, Mutex};
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
#[derive(Default, Clone)]
|
||||||
|
struct Log(Arc<Mutex<Vec<String>>>);
|
||||||
|
impl Log {
|
||||||
|
fn push(&self, s: impl Into<String>) {
|
||||||
|
self.0.lock().unwrap().push(s.into());
|
||||||
|
}
|
||||||
|
fn get(&self) -> Vec<String> {
|
||||||
|
self.0.lock().unwrap().clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Counter {
|
||||||
|
log: Log,
|
||||||
|
n: u64,
|
||||||
|
trap: bool,
|
||||||
|
stop: Option<StopHandle<Counter>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
enum Call {
|
||||||
|
Get,
|
||||||
|
}
|
||||||
|
enum Cast {
|
||||||
|
Inc,
|
||||||
|
Stop,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl GenServer for Counter {
|
||||||
|
type Call = Call;
|
||||||
|
type Reply = u64;
|
||||||
|
type Cast = Cast;
|
||||||
|
type Info = ();
|
||||||
|
type Timer = ();
|
||||||
|
|
||||||
|
fn init(&mut self, ctx: &GenServerCtx<Self>) {
|
||||||
|
if self.trap {
|
||||||
|
ctx.trap_exit();
|
||||||
|
}
|
||||||
|
self.stop = Some(ctx.stop_handle());
|
||||||
|
self.log.push("init");
|
||||||
|
}
|
||||||
|
fn handle_call(&mut self, Call::Get: Call) -> u64 {
|
||||||
|
self.n
|
||||||
|
}
|
||||||
|
fn handle_cast(&mut self, c: Cast) {
|
||||||
|
match c {
|
||||||
|
Cast::Inc => self.n += 1,
|
||||||
|
Cast::Stop => self.stop.as_ref().unwrap().stop(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn handle_shutdown(&mut self) -> ShutdownAction {
|
||||||
|
self.log.push("handle_shutdown");
|
||||||
|
ShutdownAction::Exit
|
||||||
|
}
|
||||||
|
fn terminate(&mut self) {
|
||||||
|
self.log.push("terminate");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn counter(log: &Log, trap: bool) -> Counter {
|
||||||
|
Counter {
|
||||||
|
log: log.clone(),
|
||||||
|
n: 0,
|
||||||
|
trap,
|
||||||
|
stop: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Refs are addresses: dropping the last one does not end the server.
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn dropping_last_ref_does_not_end_server() {
|
||||||
|
let log = Log::default();
|
||||||
|
let l = log.clone();
|
||||||
|
run(move || {
|
||||||
|
let srv = gen_server::start(counter(&l, true));
|
||||||
|
let pid = srv.pid();
|
||||||
|
srv.cast(Cast::Inc).unwrap();
|
||||||
|
assert_eq!(srv.call(Call::Get).unwrap(), 1);
|
||||||
|
let mon = monitor(pid);
|
||||||
|
drop(srv);
|
||||||
|
sleep(Duration::from_millis(30));
|
||||||
|
assert!(
|
||||||
|
mon.rx.try_recv().unwrap().is_none(),
|
||||||
|
"server must outlive its last ref"
|
||||||
|
);
|
||||||
|
assert_eq!(l.get(), vec!["init"], "terminate must not have run");
|
||||||
|
// Explicit teardown still works, and is what ends it.
|
||||||
|
request_shutdown(pid);
|
||||||
|
let down = mon.rx.recv().unwrap();
|
||||||
|
assert_eq!(down.reason, DownReason::Exit);
|
||||||
|
});
|
||||||
|
assert_eq!(log.get(), vec!["init", "handle_shutdown", "terminate"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn ref_shutdown_is_the_explicit_close() {
|
||||||
|
let log = Log::default();
|
||||||
|
let l = log.clone();
|
||||||
|
run(move || {
|
||||||
|
let srv = gen_server::start(counter(&l, true));
|
||||||
|
srv.call(Call::Get).unwrap(); // sync: init (and trap_exit) has run
|
||||||
|
srv.shutdown(); // graceful, waits
|
||||||
|
assert_eq!(l.get(), vec!["init", "handle_shutdown", "terminate"]);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn forgotten_server_is_shut_down_at_root_exit() {
|
||||||
|
// A ref-less server is not a hung run: root exit shuts it down.
|
||||||
|
let log = Log::default();
|
||||||
|
let l = log.clone();
|
||||||
|
run(move || {
|
||||||
|
let srv = gen_server::start(counter(&l, false));
|
||||||
|
drop(srv);
|
||||||
|
});
|
||||||
|
assert_eq!(log.get(), vec!["init", "terminate"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Inline run: a gen_server as a direct ChildSpec child.
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
const COUNTER: GenServerName<Counter> = GenServerName::new("lifetime-counter");
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn named_run_is_a_direct_supervised_child_and_gets_shutdown() {
|
||||||
|
let log = Log::default();
|
||||||
|
let l = log.clone();
|
||||||
|
run(move || {
|
||||||
|
let l2 = l.clone();
|
||||||
|
let sup = spawn(move || {
|
||||||
|
let l3 = l2.clone();
|
||||||
|
OneForOne::new()
|
||||||
|
.child(
|
||||||
|
ChildSpec::new(Restart::Permanent, move || {
|
||||||
|
GenServerBuilder::new(counter(&l3, true))
|
||||||
|
.named(COUNTER)
|
||||||
|
.run()
|
||||||
|
.expect("name free");
|
||||||
|
})
|
||||||
|
.shutdown(Shutdown::Infinity),
|
||||||
|
)
|
||||||
|
.run();
|
||||||
|
});
|
||||||
|
sleep(Duration::from_millis(10));
|
||||||
|
gen_server::cast(COUNTER, Cast::Inc).unwrap();
|
||||||
|
assert_eq!(gen_server::call(COUNTER, Call::Get).unwrap(), 1);
|
||||||
|
request_shutdown(sup.pid());
|
||||||
|
sup.join()
|
||||||
|
.expect("ordered shutdown, supervisor returns normally");
|
||||||
|
assert_eq!(l.get(), vec!["init", "handle_shutdown", "terminate"]);
|
||||||
|
assert!(gen_server::whereis_server(COUNTER).is_none());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn named_run_child_restarts_and_rebinds_name() {
|
||||||
|
let log = Log::default();
|
||||||
|
let inits = Arc::new(AtomicUsize::new(0));
|
||||||
|
let l = log.clone();
|
||||||
|
let i = inits.clone();
|
||||||
|
run(move || {
|
||||||
|
let l2 = l.clone();
|
||||||
|
let i2 = i.clone();
|
||||||
|
let sup = spawn(move || {
|
||||||
|
let l3 = l2.clone();
|
||||||
|
let i3 = i2.clone();
|
||||||
|
OneForOne::new()
|
||||||
|
.child(ChildSpec::new(Restart::Permanent, move || {
|
||||||
|
i3.fetch_add(1, Ordering::SeqCst);
|
||||||
|
GenServerBuilder::new(counter(&l3, false))
|
||||||
|
.named(COUNTER)
|
||||||
|
.run()
|
||||||
|
.expect("name free on (re)start");
|
||||||
|
}))
|
||||||
|
.run();
|
||||||
|
});
|
||||||
|
sleep(Duration::from_millis(10));
|
||||||
|
gen_server::cast(COUNTER, Cast::Inc).unwrap();
|
||||||
|
assert_eq!(gen_server::call(COUNTER, Call::Get).unwrap(), 1);
|
||||||
|
// Normal self-exit → Permanent restarts it, fresh state, same name.
|
||||||
|
gen_server::cast(COUNTER, Cast::Stop).unwrap();
|
||||||
|
sleep(Duration::from_millis(30));
|
||||||
|
assert_eq!(i.load(Ordering::SeqCst), 2, "restarted once");
|
||||||
|
assert_eq!(gen_server::call(COUNTER, Call::Get).unwrap(), 0);
|
||||||
|
request_shutdown(sup.pid());
|
||||||
|
sup.join().unwrap();
|
||||||
|
});
|
||||||
|
assert_eq!(log.get(), vec!["init", "terminate", "init", "terminate"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn named_run_name_clash_fails_before_init() {
|
||||||
|
let log = Log::default();
|
||||||
|
let l = log.clone();
|
||||||
|
run(move || {
|
||||||
|
let first = GenServerBuilder::new(counter(&l, false))
|
||||||
|
.named(COUNTER)
|
||||||
|
.start()
|
||||||
|
.unwrap();
|
||||||
|
let l2 = l.clone();
|
||||||
|
let res = Arc::new(Mutex::new(None));
|
||||||
|
let r2 = res.clone();
|
||||||
|
let first_pid = first.pid();
|
||||||
|
spawn(move || {
|
||||||
|
let r = GenServerBuilder::new(counter(&l2, false))
|
||||||
|
.named(COUNTER)
|
||||||
|
.run();
|
||||||
|
*r2.lock().unwrap() = Some(r);
|
||||||
|
})
|
||||||
|
.join()
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
*res.lock().unwrap(),
|
||||||
|
Some(Err(RegisterError::NameTaken { holder: first_pid }))
|
||||||
|
);
|
||||||
|
assert_eq!(l.get(), vec!["init"], "clashing server never ran init");
|
||||||
|
first.shutdown();
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -0,0 +1,222 @@
|
|||||||
|
//! gen_server graceful shutdown.
|
||||||
|
//!
|
||||||
|
//! - A server that does not opt in (`ctx.trap_exit()` in `init`) is stopped
|
||||||
|
//! outright by `request_shutdown`, exactly as by `request_stop`.
|
||||||
|
//! - A trapping server receives the request as `handle_shutdown`. The default
|
||||||
|
//! returns `ShutdownAction::Exit`: the loop breaks and `terminate` runs on
|
||||||
|
//! the normal (non-unwind) path, so it may block. `Continue` keeps the loop
|
||||||
|
//! dispatching; the state later ends itself with a `StopHandle` — the only
|
||||||
|
//! way for a gen_server to exit *normally* on its own (`request_stop` on
|
||||||
|
//! self is an abnormal `Stopped`, which `Transient` restarts).
|
||||||
|
//! - Other exit signals (linked peers dying) reach a trapping server via
|
||||||
|
//! `handle_exit`.
|
||||||
|
|
||||||
|
use smarm::gen_server::{
|
||||||
|
start, GenServer, GenServerBuilder, GenServerCtx, GenServerRef, ShutdownAction, StopHandle,
|
||||||
|
};
|
||||||
|
use smarm::supervisor::{ChildSpec, OneForOne, Restart};
|
||||||
|
use smarm::{link, monitor, request_shutdown, run, self_pid, sleep, spawn, DownReason, ExitSignal};
|
||||||
|
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
|
||||||
|
use std::sync::{Arc, Mutex};
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
#[derive(Default, Clone)]
|
||||||
|
struct Log {
|
||||||
|
events: Arc<Mutex<Vec<&'static str>>>,
|
||||||
|
}
|
||||||
|
impl Log {
|
||||||
|
fn push(&self, e: &'static str) {
|
||||||
|
self.events.lock().unwrap().push(e);
|
||||||
|
}
|
||||||
|
fn get(&self) -> Vec<&'static str> {
|
||||||
|
self.events.lock().unwrap().clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A server with configurable shutdown behaviour.
|
||||||
|
struct Srv {
|
||||||
|
log: Log,
|
||||||
|
trap: bool,
|
||||||
|
action: ShutdownAction,
|
||||||
|
stop: Option<StopHandle<Srv>>,
|
||||||
|
exits: Arc<Mutex<Vec<ExitSignal>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Srv {
|
||||||
|
fn new(log: &Log, trap: bool, action: ShutdownAction) -> Self {
|
||||||
|
Srv {
|
||||||
|
log: log.clone(),
|
||||||
|
trap,
|
||||||
|
action,
|
||||||
|
stop: None,
|
||||||
|
exits: Default::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum Cast {
|
||||||
|
Note(&'static str),
|
||||||
|
StopNow,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl GenServer for Srv {
|
||||||
|
type Call = ();
|
||||||
|
type Reply = ();
|
||||||
|
type Cast = Cast;
|
||||||
|
type Info = ();
|
||||||
|
type Timer = ();
|
||||||
|
|
||||||
|
fn init(&mut self, ctx: &GenServerCtx<Self>) {
|
||||||
|
if self.trap {
|
||||||
|
ctx.trap_exit();
|
||||||
|
}
|
||||||
|
self.stop = Some(ctx.stop_handle());
|
||||||
|
}
|
||||||
|
fn handle_call(&mut self, _: ()) {}
|
||||||
|
fn handle_cast(&mut self, c: Cast) {
|
||||||
|
match c {
|
||||||
|
Cast::Note(s) => self.log.push(s),
|
||||||
|
Cast::StopNow => self.stop.as_ref().unwrap().stop(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn handle_shutdown(&mut self) -> ShutdownAction {
|
||||||
|
self.log.push("handle_shutdown");
|
||||||
|
self.action
|
||||||
|
}
|
||||||
|
fn handle_exit(&mut self, sig: ExitSignal) {
|
||||||
|
self.log.push("handle_exit");
|
||||||
|
self.exits.lock().unwrap().push(sig);
|
||||||
|
}
|
||||||
|
fn terminate(&mut self) {
|
||||||
|
// Allowed to block on the graceful path.
|
||||||
|
if self.trap {
|
||||||
|
sleep(Duration::from_millis(10));
|
||||||
|
}
|
||||||
|
self.log.push("terminate");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn spawn_settled<G: GenServer>(state: G) -> GenServerRef<G> {
|
||||||
|
let r = start(state);
|
||||||
|
sleep(Duration::from_millis(20)); // let init (trap_exit) run
|
||||||
|
r
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn non_trapping_server_is_stopped_outright() {
|
||||||
|
let log = Log::default();
|
||||||
|
let l = log.clone();
|
||||||
|
run(move || {
|
||||||
|
let r = spawn_settled(Srv::new(&l, false, ShutdownAction::Exit));
|
||||||
|
let mon = monitor(r.pid());
|
||||||
|
request_shutdown(r.pid());
|
||||||
|
let d = mon.rx.recv().unwrap();
|
||||||
|
assert_eq!(d.reason, DownReason::Stopped);
|
||||||
|
});
|
||||||
|
assert_eq!(log.get(), vec!["terminate"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn trapping_server_exits_normally_via_handle_shutdown() {
|
||||||
|
let log = Log::default();
|
||||||
|
let l = log.clone();
|
||||||
|
run(move || {
|
||||||
|
let r = spawn_settled(Srv::new(&l, true, ShutdownAction::Exit));
|
||||||
|
let mon = monitor(r.pid());
|
||||||
|
request_shutdown(r.pid());
|
||||||
|
let d = mon.rx.recv().unwrap();
|
||||||
|
assert_eq!(d.reason, DownReason::Exit);
|
||||||
|
});
|
||||||
|
assert_eq!(log.get(), vec!["handle_shutdown", "terminate"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn continue_keeps_dispatching_until_stop_handle() {
|
||||||
|
let log = Log::default();
|
||||||
|
let l = log.clone();
|
||||||
|
run(move || {
|
||||||
|
let r = spawn_settled(Srv::new(&l, true, ShutdownAction::Continue));
|
||||||
|
let mon = monitor(r.pid());
|
||||||
|
request_shutdown(r.pid());
|
||||||
|
sleep(Duration::from_millis(20));
|
||||||
|
r.cast(Cast::Note("after-shutdown-request")).unwrap();
|
||||||
|
r.cast(Cast::StopNow).unwrap();
|
||||||
|
let d = mon.rx.recv().unwrap();
|
||||||
|
assert_eq!(d.reason, DownReason::Exit);
|
||||||
|
});
|
||||||
|
assert_eq!(
|
||||||
|
log.get(),
|
||||||
|
vec!["handle_shutdown", "after-shutdown-request", "terminate"]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn stop_handle_is_a_normal_exit_that_transient_does_not_restart() {
|
||||||
|
let starts = Arc::new(AtomicUsize::new(0));
|
||||||
|
let s = starts.clone();
|
||||||
|
run(move || {
|
||||||
|
let s2 = s.clone();
|
||||||
|
let sup = spawn(move || {
|
||||||
|
let s3 = s2.clone();
|
||||||
|
OneForOne::new()
|
||||||
|
.child(ChildSpec::new(Restart::Transient, move || {
|
||||||
|
s3.fetch_add(1, Ordering::SeqCst);
|
||||||
|
let log = Log::default();
|
||||||
|
let r = GenServerBuilder::new(Srv::new(&log, false, ShutdownAction::Exit))
|
||||||
|
.under(self_pid())
|
||||||
|
.start();
|
||||||
|
r.cast(Cast::StopNow).unwrap();
|
||||||
|
// Block until the server is gone; a bare spawn parent
|
||||||
|
// returning would not itself end the server.
|
||||||
|
let mon = monitor(r.pid());
|
||||||
|
let _ = mon.rx.recv();
|
||||||
|
}))
|
||||||
|
.run();
|
||||||
|
});
|
||||||
|
sup.join().unwrap(); // returns only if the child was not restarted forever
|
||||||
|
});
|
||||||
|
assert_eq!(starts.load(Ordering::SeqCst), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn linked_peer_death_reaches_handle_exit() {
|
||||||
|
let log = Log::default();
|
||||||
|
let l = log.clone();
|
||||||
|
let alive = Arc::new(AtomicBool::new(false));
|
||||||
|
let a = alive.clone();
|
||||||
|
run(move || {
|
||||||
|
let r = spawn_settled(Srv::new(&l, true, ShutdownAction::Exit));
|
||||||
|
let pid = r.pid();
|
||||||
|
let peer = spawn(move || {
|
||||||
|
link(pid);
|
||||||
|
panic!("peer dies");
|
||||||
|
});
|
||||||
|
let _ = peer.join();
|
||||||
|
sleep(Duration::from_millis(20));
|
||||||
|
r.cast(Cast::Note("still-serving")).unwrap();
|
||||||
|
sleep(Duration::from_millis(20));
|
||||||
|
a.store(true, Ordering::SeqCst);
|
||||||
|
r.shutdown(); // graceful; waits for terminate
|
||||||
|
});
|
||||||
|
assert!(alive.load(Ordering::SeqCst));
|
||||||
|
assert_eq!(
|
||||||
|
log.get(),
|
||||||
|
vec![
|
||||||
|
"handle_exit",
|
||||||
|
"still-serving",
|
||||||
|
"handle_shutdown",
|
||||||
|
"terminate"
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn gen_server_ref_shutdown_is_graceful_for_a_trapping_server() {
|
||||||
|
let log = Log::default();
|
||||||
|
let l = log.clone();
|
||||||
|
run(move || {
|
||||||
|
let r = spawn_settled(Srv::new(&l, true, ShutdownAction::Exit));
|
||||||
|
r.shutdown();
|
||||||
|
});
|
||||||
|
assert_eq!(log.get(), vec!["handle_shutdown", "terminate"]);
|
||||||
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user