| @@ -1,7 +1,18 @@ |
| 1 | | −/// Supervisors: restart children that crash ("let it crash", ADR 0004). |
| 1 | +/// Supervisors: restart children that crash ("let it crash", ADR 0004), |
| 2 | +/// within a restart intensity, and stop them in order (ADR 0060). |
| 2 | 3 | |
| 3 | 4 | import std/list |
| 4 | | −import std/process.{type Pid, type ExitSignal, type proc, ExitSignal, Normal} |
| 5 | +import std/os.{type Signal} |
| 6 | +import std/process.{ |
| 7 | + type Pid |
| 8 | +, type ExitSignal |
| 9 | +, type proc |
| 10 | +, type Subject |
| 11 | +, Crashed |
| 12 | +, ExitSignal |
| 13 | +, Normal |
| 14 | +, Shutdown |
| 15 | +} |
| 5 | 16 | import std/random.{type random} |
| 6 | 17 | import std/time.{type clock} |
| 7 | 18 | |
| @@ -15,64 +26,184 @@ pub type Strategy { |
| 15 | 26 | , RestForOne |
| 16 | 27 | } |
| 17 | 28 | |
| 29 | +/// When a child is restarted (OTP's child kinds). |
| 30 | +@derive(Eq, Show) |
| 31 | +pub type Restart { |
| 32 | + /// Always: after it crashes, and after it returns. |
| 33 | + Permanent |
| 34 | + /// After it crashes only: a child that returns (or is stopped with |
| 35 | + /// `Shutdown`) is done. |
| 36 | +, Transient |
| 37 | + /// Never. |
| 38 | +, Temporary |
| 39 | +} |
| 40 | + |
| 18 | 41 | /// How to start a child. The child is linked to its supervisor. |
| 19 | 42 | pub type Child { |
| 43 | + /// Restarted after a crash only (`Transient`), given 5 seconds to stop. |
| 20 | 44 | Child(name: String, start: send fn() -> () / <proc, clock, random, io, ffi>) |
| 45 | + /// Restarted as `restart` says, given `shutdown_timeout` milliseconds to |
| 46 | + /// stop when its supervisor shuts down (then killed). |
| 47 | +, ChildWith( |
| 48 | + name: String |
| 49 | + , start: send fn() -> () / <proc, clock, random, io, ffi> |
| 50 | + , restart: Restart |
| 51 | + , shutdown_timeout: Int |
| 52 | + ) |
| 21 | 53 | } |
| 22 | 54 | |
| 23 | | −type Running = List<(Pid, Child)> |
| 55 | +/// A supervisor: its children, started in order, its strategy, and its |
| 56 | +/// restart intensity: more than `intensity` restarts within `period_ms` |
| 57 | +/// milliseconds make it give up (crash). A negative `period_ms` counts |
| 58 | +/// restarts over the supervisor's whole life. |
| 59 | +pub type Spec { |
| 60 | + Spec( |
| 61 | + strategy: Strategy |
| 62 | + , children: List<Child> |
| 63 | + , intensity: Int |
| 64 | + , period_ms: Int |
| 65 | + ) |
| 66 | +} |
| 24 | 67 | |
| 25 | 68 | /// Run a supervisor in the current actor: start the children, restart |
| 26 | | −/// them as `strategy` says when they crash, and crash itself after |
| 27 | | −/// `max_restarts` restarts. Returns when every child has stopped normally. |
| 69 | +/// them as `strategy` and their kinds say, and crash itself after |
| 70 | +/// `max_restarts` restarts (over its whole life; `run` takes a window). |
| 71 | +/// Returns when every child is done. |
| 28 | 72 | pub fn supervise( |
| 29 | 73 | strategy: Strategy |
| 30 | 74 | , children: List<Child> |
| 31 | 75 | , max_restarts: Int |
| 32 | 76 | ) -> () / <proc, io> { |
| 77 | + // Over its whole life: no clock needed. |
| 78 | + run_with(Spec(strategy, children, max_restarts, -1), fn() { 0 }) |
| 79 | +} |
| 80 | + |
| 81 | +/// Run the supervisor `spec` in the current actor (see `Spec`). It traps |
| 82 | +/// exits: told to stop (`process.stop`, a `Shutdown` exit signal), it |
| 83 | +/// stops its children in reverse start order, each with `Shutdown` and |
| 84 | +/// killed after its `shutdown_timeout`, then returns. Returns too when |
| 85 | +/// every child is done. |
| 86 | +pub fn run(spec: Spec) -> () / <proc, io, clock> { |
| 87 | + run_with(spec, now_ms) |
| 88 | +} |
| 89 | + |
| 90 | +/// The monotonic clock, in milliseconds. |
| 91 | +fn now_ms() -> Int / <clock> { |
| 92 | + time.in_milliseconds(time.monotonic()) |
| 93 | +} |
| 94 | + |
| 95 | +/// `run`, reading the time from `now` (for the restart window). |
| 96 | +fn run_with(spec: Spec, now: fn() -> Int / <e>) -> () / <proc, io | e> { |
| 33 | 97 | process.trap_exits(True) |
| 34 | 98 | let exits = process.exits() |
| 35 | | − watch(strategy, exits, list.map(children, start_child), max_restarts) |
| 99 | + watch(spec, exits, list.map(spec.children, start_child), [], now) |
| 36 | 100 | } |
| 37 | 101 | |
| 102 | +type Running = List<(Pid, Child)> |
| 103 | + |
| 38 | 104 | fn start_child(child: Child) -> (Pid, Child) / <proc> { |
| 39 | | − (process.spawn_link(child.start), child) |
| 105 | + (process.spawn_link(start_of(child)), child) |
| 106 | +} |
| 107 | + |
| 108 | +fn start_of(child: Child) -> send fn() -> () / <proc, clock, random, io, ffi> { |
| 109 | + match child { |
| 110 | + Child(_, start) -> start |
| 111 | + , ChildWith(_, start, _, _) -> start |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +fn restart_of(child: Child) -> Restart { |
| 116 | + match child { |
| 117 | + Child(_, _) -> Transient |
| 118 | + , ChildWith(_, _, restart, _) -> restart |
| 119 | + } |
| 40 | 120 | } |
| 41 | 121 | |
| 122 | +fn shutdown_timeout_of(child: Child) -> Int { |
| 123 | + match child { |
| 124 | + Child(_, _) -> 5000 |
| 125 | + , ChildWith(_, _, _, timeout) -> timeout |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +/// `restarts`: when the restarts within the window happened (`now`'s |
| 130 | +/// milliseconds, the latest first). |
| 42 | 131 | fn watch( |
| 43 | | − strategy: Strategy |
| 44 | | −, exits: process.Subject<ExitSignal> |
| 132 | + spec: Spec |
| 133 | +, exits: Subject<ExitSignal> |
| 45 | 134 | , running: Running |
| 46 | | −, restarts_left: Int |
| 47 | | −) -> () / <proc, io> { |
| 135 | +, restarts: List<Int> |
| 136 | +, now: fn() -> Int / <e> |
| 137 | +) -> () / <proc, io | e> { |
| 48 | 138 | match running { |
| 49 | 139 | [] -> () |
| 50 | 140 | , _ -> { |
| 51 | 141 | let ExitSignal(pid, reason) = process.receive_forever(exits) |
| 52 | 142 | if !is_running(running, pid) { |
| 53 | | − // A child replaced or killed by a restart: already handled. |
| 54 | | − watch(strategy, exits, running, restarts_left) |
| 55 | | − } else if reason == Normal { |
| 56 | | − watch(strategy, exits, remove(running, pid), restarts_left) |
| 57 | | − } else if restarts_left == 0 { |
| 58 | | − panic("supervisor: too many restarts") |
| 143 | + if reason == Shutdown { |
| 144 | + // Told to stop, by whoever started it. |
| 145 | + shutdown(running, exits) |
| 146 | + } else { |
| 147 | + // A child replaced or killed by a restart: already handled. |
| 148 | + watch(spec, exits, running, restarts, now) |
| 149 | + } |
| 59 | 150 | } else { |
| 60 | | − let restarted = match strategy { |
| 61 | | − OneForOne -> restart_one(running, pid) |
| 62 | | − , OneForAll -> restart_all(running) |
| 63 | | − , RestForOne -> restart_rest(running, pid) |
| 151 | + let child = child_of(running, pid) |
| 152 | + let done = match (restart_of(child), reason) { |
| 153 | + (Temporary, _) -> True |
| 154 | + , (Transient, Normal) -> True |
| 155 | + , (Transient, Shutdown) -> True |
| 156 | + , _ -> False |
| 157 | + } |
| 158 | + if done { |
| 159 | + watch(spec, exits, remove(running, pid), restarts, now) |
| 160 | + } else { |
| 161 | + let restarts = counted(restarts, spec.period_ms, now()) |
| 162 | + if list.length(restarts) > spec.intensity { |
| 163 | + // Stop the others (the crashed child is gone already). |
| 164 | + shutdown(remove(running, pid), exits) |
| 165 | + panic("supervisor: too many restarts") |
| 166 | + } else { |
| 167 | + let restarted = match spec.strategy { |
| 168 | + OneForOne -> restart_one(running, pid) |
| 169 | + , OneForAll -> restart_all(running) |
| 170 | + , RestForOne -> restart_rest(running, pid) |
| 171 | + } |
| 172 | + watch(spec, exits, restarted, restarts, now) |
| 173 | + } |
| 64 | 174 | } |
| 65 | | − watch(strategy, exits, restarted, restarts_left - 1) |
| 66 | 175 | } |
| 67 | 176 | } |
| 68 | 177 | } |
| 69 | 178 | } |
| 70 | 179 | |
| 180 | +/// `restarts` with one more `now`, forgetting those older than |
| 181 | +/// `period_ms` (none if it is negative). |
| 182 | +fn counted(restarts: List<Int>, period_ms: Int, now: Int) -> List<Int> { |
| 183 | + let recent = if period_ms < 0 { |
| 184 | + restarts |
| 185 | + } else { |
| 186 | + list.filter(restarts, fn(t) { now - t < period_ms }) |
| 187 | + } |
| 188 | + [now, ..recent] |
| 189 | +} |
| 190 | + |
| 71 | 191 | fn is_running(running: Running, pid: Pid) -> Bool { |
| 72 | 192 | use found, entry <- list.fold(running, False) |
| 73 | 193 | found || entry.0 == pid |
| 74 | 194 | } |
| 75 | 195 | |
| 196 | +fn child_of(running: Running, pid: Pid) -> Child { |
| 197 | + match running { |
| 198 | + [] -> panic("supervisor: not a child") |
| 199 | + , [entry, ..rest] -> if entry.0 == pid { |
| 200 | + entry.1 |
| 201 | + } else { |
| 202 | + child_of(rest, pid) |
| 203 | + } |
| 204 | + } |
| 205 | +} |
| 206 | + |
| 76 | 207 | fn remove(running: Running, pid: Pid) -> Running { |
| 77 | 208 | use entry <- list.filter(running) |
| 78 | 209 | entry.0 != pid |
| @@ -85,8 +216,7 @@ fn restart_one(running: Running, pid: Pid) -> Running / <proc> { |
| 85 | 216 | |
| 86 | 217 | fn restart_all(running: Running) -> Running / <proc> { |
| 87 | 218 | list.each(running, fn(entry) { process.kill(entry.0) }) |
| 88 | | − use entry <- list.map(running) |
| 89 | | − start_child(entry.1) |
| 219 | + restart_kept(running) |
| 90 | 220 | } |
| 91 | 221 | |
| 92 | 222 | /// Restart `pid` and the children after it, in order; the later ones are |
| @@ -97,10 +227,187 @@ fn restart_rest(running: Running, pid: Pid) -> Running / <proc> { |
| 97 | 227 | , [entry, ..rest] -> if entry.0 == pid { |
| 98 | 228 | // Stop the later children last-started first, as OTP does. |
| 99 | 229 | list.each(list.reverse(rest), fn(later) { process.kill(later.0) }) |
| 100 | | − use e <- list.map([entry, ..rest]) |
| 101 | | − start_child(e.1) |
| 230 | + restart_kept([entry, ..rest]) |
| 102 | 231 | } else { |
| 103 | 232 | [entry, ..restart_rest(rest, pid)] |
| 104 | 233 | } |
| 105 | 234 | } |
| 106 | 235 | } |
| 236 | + |
| 237 | +/// Start `running`'s children again, but the temporary ones, which are |
| 238 | +/// done. |
| 239 | +fn restart_kept(running: Running) -> Running / <proc> { |
| 240 | + let kept = list.filter( |
| 241 | + running |
| 242 | + , fn(entry) { restart_of(entry.1) != Temporary } |
| 243 | + ) |
| 244 | + list.map(kept, fn(entry) { start_child(entry.1) }) |
| 245 | +} |
| 246 | + |
| 247 | +/// Stop every child, last-started first: each with `Shutdown`, killed if |
| 248 | +/// it is not done within its `shutdown_timeout`. |
| 249 | +fn shutdown(running: Running, exits: Subject<ExitSignal>) -> () / <proc> { |
| 250 | + let _ = list.fold( |
| 251 | + list.reverse(running) |
| 252 | + , [] |
| 253 | + , fn(gone, entry) { |
| 254 | + stop_child(entry.0, shutdown_timeout_of(entry.1), exits, gone) |
| 255 | + } |
| 256 | + ) |
| 257 | + () |
| 258 | +} |
| 259 | + |
| 260 | +/// Stop `pid`, waiting up to `timeout` milliseconds for its exit signal, |
| 261 | +/// then kill it and wait for it. `gone`: the children whose exit signals |
| 262 | +/// arrived meanwhile (each arrives once); returns it updated. |
| 263 | +fn stop_child( |
| 264 | + pid: Pid |
| 265 | +, timeout: Int |
| 266 | +, exits: Subject<ExitSignal> |
| 267 | +, gone: List<Pid> |
| 268 | +) -> List<Pid> / <proc> { |
| 269 | + if list.any(gone, fn(p) { p == pid }) { |
| 270 | + gone |
| 271 | + } else { |
| 272 | + process.stop(pid) |
| 273 | + match wait_exit(pid, timeout, exits, gone) { |
| 274 | + (True, gone) -> gone |
| 275 | + , (False, gone) -> { |
| 276 | + process.kill(pid) |
| 277 | + wait_exit(pid, -1, exits, gone).1 |
| 278 | + } |
| 279 | + } |
| 280 | + } |
| 281 | +} |
| 282 | + |
| 283 | +/// Wait up to `timeout` milliseconds (negative: forever) for `pid`'s exit |
| 284 | +/// signal: whether it came, and the other children that exited meanwhile. |
| 285 | +fn wait_exit( |
| 286 | + pid: Pid |
| 287 | +, timeout: Int |
| 288 | +, exits: Subject<ExitSignal> |
| 289 | +, gone: List<Pid> |
| 290 | +) -> (Bool, List<Pid>) / <proc> { |
| 291 | + match process.receive(exits, timeout) { |
| 292 | + None -> (False, gone) |
| 293 | + , Some(ExitSignal(from, _)) -> if from == pid { |
| 294 | + (True, gone) |
| 295 | + } else { |
| 296 | + wait_exit(pid, timeout, exits, [from, ..gone]) |
| 297 | + } |
| 298 | + } |
| 299 | +} |
| 300 | + |
| 301 | +/// How the runtime supervises a program's `main` (ADR 0060): the |
| 302 | +/// `[application]` section of `louise.toml`. `main` is restarted as |
| 303 | +/// `restart` says after it crashes, at most `intensity` times within |
| 304 | +/// `period_ms` milliseconds; on one of `signals`, it is stopped |
| 305 | +/// (`Shutdown`), and killed if it is not done within `shutdown_timeout_ms`. |
| 306 | +pub type Application { |
| 307 | + Application( |
| 308 | + restart: Restart |
| 309 | + , intensity: Int |
| 310 | + , period_ms: Int |
| 311 | + , shutdown_timeout_ms: Int |
| 312 | + , signals: List<Signal> |
| 313 | + ) |
| 314 | +} |
| 315 | + |
| 316 | +/// Run `main` under `app` (the program's root when `louise.toml` has an |
| 317 | +/// `[application]` section; the compiler generates the call). Returns, |
| 318 | +/// and the program exits 0, when `main` returns, or once it stopped after |
| 319 | +/// a signal; a crash beyond the restart intensity (or any crash, for a |
| 320 | +/// `Temporary` main) ends the program with that panic. |
| 321 | +pub fn application( |
| 322 | + app: Application |
| 323 | +, main: send fn() -> () / <proc, clock, random, io, ffi> |
| 324 | +) -> () / <proc, io, clock> { |
| 325 | + process.trap_exits(True) |
| 326 | + let exits = process.exits() |
| 327 | + let signals = os.signals(app.signals) |
| 328 | + let events = process.new_selector() |
| 329 | + |> process.selecting(exits, Exited) |
| 330 | + |> process.selecting(signals, Signalled) |
| 331 | + supervise_main(app, main, events, process.spawn_link(main), []) |
| 332 | +} |
| 333 | + |
| 334 | +/// What `application` waits for. |
| 335 | +type MainEvent { |
| 336 | + Exited(ExitSignal) |
| 337 | +, Signalled(Signal) |
| 338 | +} |
| 339 | + |
| 340 | +fn supervise_main( |
| 341 | + app: Application |
| 342 | +, main: send fn() -> () / <proc, clock, random, io, ffi> |
| 343 | +, events: process.Selector<MainEvent> |
| 344 | +, pid: Pid |
| 345 | +, restarts: List<Int> |
| 346 | +) -> () / <proc, io, clock> { |
| 347 | + match process.select_forever(events) { |
| 348 | + Signalled(_) -> stop_main(pid, app.shutdown_timeout_ms, events) |
| 349 | + , Exited( |
| 350 | + ExitSignal(from, reason) |
| 351 | + ) -> if from != pid { |
| 352 | + // Another linked actor: not `main`'s business here. |
| 353 | + supervise_main(app, main, events, pid, restarts) |
| 354 | + } else { |
| 355 | + match (app.restart, reason) { |
| 356 | + (_, Normal) -> () |
| 357 | + , (_, Shutdown) -> () |
| 358 | + , (Temporary, _) -> panic(crash_message(reason)) |
| 359 | + , _ -> { |
| 360 | + let restarts = counted(restarts, app.period_ms, now_ms()) |
| 361 | + if list.length(restarts) > app.intensity { |
| 362 | + panic(crash_message(reason)) |
| 363 | + } else { |
| 364 | + supervise_main( |
| 365 | + app |
| 366 | + , main |
| 367 | + , events |
| 368 | + , process.spawn_link(main) |
| 369 | + , restarts |
| 370 | + ) |
| 371 | + } |
| 372 | + } |
| 373 | + } |
| 374 | + } |
| 375 | + } |
| 376 | +} |
| 377 | + |
| 378 | +fn crash_message(reason: process.ExitReason) -> String { |
| 379 | + match reason { |
| 380 | + Crashed(message) -> message |
| 381 | + , _ -> "main was killed" |
| 382 | + } |
| 383 | +} |
| 384 | + |
| 385 | +/// Stop `main` (`Shutdown`), waiting up to `timeout` milliseconds for it, |
| 386 | +/// then kill it. Further signals meanwhile are ignored. |
| 387 | +fn stop_main( |
| 388 | + pid: Pid |
| 389 | +, timeout: Int |
| 390 | +, events: process.Selector<MainEvent> |
| 391 | +) -> () / <proc> { |
| 392 | + process.stop(pid) |
| 393 | + if !main_exited(pid, timeout, events) { |
| 394 | + process.kill(pid) |
| 395 | + let _ = main_exited(pid, -1, events) |
| 396 | + } |
| 397 | +} |
| 398 | + |
| 399 | +/// Whether `main` (`pid`) exited within `timeout` milliseconds (negative: |
| 400 | +/// forever). |
| 401 | +fn main_exited( |
| 402 | + pid: Pid |
| 403 | +, timeout: Int |
| 404 | +, events: process.Selector<MainEvent> |
| 405 | +) -> Bool / <proc> { |
| 406 | + match process.select(events, timeout) { |
| 407 | + None -> False |
| 408 | + , Some( |
| 409 | + Exited(ExitSignal(from, _)) |
| 410 | + ) -> if from == pid { True } else { main_exited(pid, timeout, events) } |
| 411 | + , Some(Signalled(_)) -> main_exited(pid, timeout, events) |
| 412 | + } |
| 413 | +} |