1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
//! Running a flock in dependency order.
//!
//! The sort itself is `shep_core::config::graph`. This module runs what it
//! produces: start a stage, wait for it, advance.
//!
//! It lives outside the supervisor actor deliberately. `do_start` is a
//! synchronous `fn` reached from the actor's own message loop, and that loop
//! is what delivers `Msg::ReadyResult`, so a wait inside it could never end.
use Duration;
use ;
use OsStr;
use ResolvedApp;
use ;
use ;
use ProcessSelector;
use ProcStatus;
use ;
use crate;
use crate;
/// How much longer than the stage's own longest `listen_timeout` the driver
/// waits before giving up on it.
///
/// Every member is already bounded by its own readiness task, which reports
/// at its own deadline, so this covers scheduling jitter only. The same
/// reasoning as `RELOAD_DEADLINE_SLACK`, and the same figure.
pub const STAGE_SLACK: Duration = from_secs;
/// Graph nodes for a flock plus its dogs, with no dog promoted ahead of the
/// flock.
// Only this module's own tests reach the two-argument spelling: every caller
// so far has a promoted list to pass. `expect` rather than `allow`, so it
// deletes itself the moment one does not.
pub
/// The plan for a set of apps plus the dogs this shepherd holds.
///
/// The one place a plan is built, so every caller positions dogs the same
/// way.
pub
/// The plan for a flock given as names and the names each one waits for.
///
/// The teardown's entry point, beside [`plan_for`], which needs
/// [`ResolvedApp`]s and so only suits a caller that has just normalized one.
/// A shutdown has the registry's own names and edges, and nothing there may
/// fail: every node is a [`NodeKind::Sheep`], so no dog can reach the stages
/// through this door.
///
/// The plan's `unresolved` and `cycles` are dropped on the floor rather than
/// warned about, deliberately: the boot already reported both, and a flock
/// with a bad edge would then say so a second time at the one moment nobody
/// can act on it, in a log an operator reads after the machine is down.
pub
/// Graph nodes for a flock plus its dogs.
///
/// `boot_first` names the dogs `[daemon] boot_first_dogs` promotes ahead of
/// every sheep. A dog carries no `depends_on` of its own: `dog_app` builds a
/// dog's config from `AppConfig::minimal`, so its list is always empty.
///
/// A dog sharing a sheep's name is dropped rather than added a second time: a
/// second node would put that one sheep in two stages and start it twice.
/// Nothing refuses the collision, and which of the two runs depends on which
/// went first. An unpromoted dog is spawned after the flock, finds the name
/// registered, and returns, so the sheep is what runs. A promoted one spawns
/// before the restore against an empty flock, and the sheep is the one that
/// never starts; `snapshot::warn_about_dogs_holding_sheep_names` is what says
/// so.
pub
/// Starts `apps` stage by stage, holding each stage until every member a
/// later stage waits on has settled.
///
/// Dogs in `plan` are skipped, and only `[daemon] boot_first_dogs` dogs are
/// positioned by it in any real sense: `boot` runs `dogs::spawn_enabled_dogs`
/// twice, once for the promoted ones before this driver is reached at all and
/// once for the rest after every stage, rather than at each dog's own stage
/// boundary. So a sheep waiting on an unpromoted dog starts while that dog is
/// not running, whatever stage the plan gave it;
/// `snapshot::warn_about_the_graph` reports the edge.
/// Answers with every instance started, in stage order.
///
/// `notify_socket` is the boot restore's own `$NOTIFY_SOCKET`; every other
/// walk passes `None`. With one, each stage announces its `bound` to it just
/// before it starts, so a unit's `TimeoutStartSec` follows the walk instead of
/// having to cover the sum of the stages. A stage nothing waits on announces
/// too, since its spawns take time. A stage that is waited on announces a
/// second time once its spawns have returned, because its wait starts then.
///
/// **An [`BatchPolicy::AllOrNothing`] batch that fails here leaves a PARTIAL
/// flock.** That policy refuses one `Command::Start` before it registers
/// anything, and while `shep start` was one such call the refusal really did
/// mean an untouched flock. It now covers the checks one stage can make in
/// advance, and not even the whole of that stage: stage 0 is running by the
/// time stage 1 proves unstartable, and a spawn that fails part way through a
/// stage leaves the members ahead of it running too. Nothing is rolled back,
/// deliberately, since rolling back means stopping apps that came up fine on
/// a guess about what the operator wanted. [`left_running`] and
/// [`corrected_for_earlier_stages`] are what make the refusal say so.
///
/// # Errors
///
/// - [`SupervisorError`]: under [`BatchPolicy::AllOrNothing`] only, the first
/// stage that did not start, with everything this start left running named
/// in its message. That policy is the operator's, and an operator
/// who typed `shep start` gets the failure back rather than a warning in a
/// log they are not reading; a later stage waits on the stage that failed,
/// so there is nothing useful to run after it either. Under
/// [`BatchPolicy::PerApp`], the boot's policy, a failed stage is warned
/// about and the walk continues, for the reason `spawn_enabled_dogs` gives:
/// a gap is better than an outage.
pub async
/// Tells the init system at `notify_socket`, if any, to hold its start
/// deadline for `bound` more, counted from when it receives this.
///
/// A failed send is a `warn!`: the walk carries on, and only the init
/// system's deadline is then the one it had. Nothing is sent off unix, where
/// there is no `$NOTIFY_SOCKET`.
pub
/// What this start left running: every earlier stage, plus whatever of the
/// failing stage was up before the failure.
///
/// The second half is why the flock is read at all. `do_start` refuses a
/// whole batch before registering anything only for the checks it can make
/// in advance; a spawn that fails anyway leaves the batch part-registered,
/// because only exec knows for certain, and those apps are in no stage this
/// walk has completed. `started` alone therefore named some of what a staged
/// start left running rather than all of it.
///
/// One `Command::List` on a failure path, and only under
/// [`BatchPolicy::AllOrNothing`]: the walk is ending either way.
///
/// A stage member the flock holds as `Stopped` or `Errored` is left out. It
/// is registered, so `shep delete` still has something to do about it, but
/// the message this feeds is about children that are up and are being left
/// alone.
async
/// `err`, told to name what this start already left running.
///
/// [`BatchPolicy::AllOrNothing`] refuses one `Command::Start` before it
/// registers anything, and both its messages say so. That was the whole truth
/// while `shep start` was one such call. It is now the truth about ONE stage,
/// and not even all of that: stage 0 is running by the time stage 1 proves
/// unstartable, and a spawn failure part way through stage 1 leaves the
/// members it reached first running too. Nothing rolls any of it back,
/// because rolling back means stopping apps that came up fine on a guess
/// about what the operator wanted. So the message says which ones they are
/// instead, and an operator who wants them down types `shep stop`.
///
/// Only the two variants carrying a message about registration are touched.
/// Every other one is about a single sheep and says nothing this could
/// correct.
/// Waits until every name in `waiting` has reached a settled answer, or until
/// `bound` elapses. Answers with the names that never settled.
///
/// A member that dies resolves at once rather than holding its stage for the
/// full deadline, which is what keeps a missing binary from costing the boot
/// its whole budget.
///
/// Two sources with one definition. The flock, read through `drop_settled`,
/// is the only thing that decides a name has settled, because a name is an
/// app and an app can hold several instances. The bus decides only *when* to
/// ask: an event is a trigger, never an answer. The read alone would be a
/// snapshot that misses whatever settles after it, and the stream alone would
/// miss what settled while the start call was still returning.
///
/// The cost of that is one `Command::List` round trip per settling event of a
/// name this stage is waiting on, which is bounded by the stage's own size.
///
/// Everything is inside `bound`, the flock read included: it awaits an mpsc
/// send and a oneshot on the actor, neither of which carries a deadline of
/// its own.
///
/// `rpc/dispatch.rs`'s ordered restart reaches this too, for the same question: a
/// restart is done when the sheep it respawned has stopped being `Starting`.
pub async
/// Waits until every name in `waiting` has finished reloading, or until
/// `bound` elapses. Answers with the names that did not finish.
///
/// The reload half of [`await_stage`], and it cannot share that function's
/// shape. A reload leaves an app serving from start to finish, so no status
/// a flock read could return says whether a swap has happened; the bus is
/// the only source there is. `waiting` therefore carries a count rather than
/// a name alone: `advance_reload` replaces one instance at a time and emits
/// a `Reloaded` for each, so a name is finished when it has emitted as many
/// as the reload accepted.
///
/// A `ReloadAbandoned` finishes a name outright, whatever its count: an
/// abandonment ends the whole reload and leaves the instances it had not
/// reached alone, so nothing further about that name is coming.
///
/// A lagged receiver is left to the bound rather than recovered from, which
/// is where this parts company with [`await_stage`]: that one asks the flock,
/// and there is no flock read that could answer this question. A lag costs
/// the stage its bound and no more, and the walk advances either way.
pub async
/// Whether a boot stage has its answer about an instance in this status.
///
/// `Starting` is the one status that is still a question: it is where the
/// supervisor parks a gated sheep until its readiness task reports. Every
/// other status is an answer, including the ones that are bad news, since a
/// stage waits to learn its members' fate rather than to see them succeed.
///
/// Read off the event as well as off the flock, rather than matching on
/// `ProcessEventKind`: an `autorestart = false` sheep that exits is announced
/// as `process.stop`, not `process.exit`, so a kind list written from the
/// boot path's vocabulary misses exactly the member this wait exists to not
/// hang on.
/// Drops from `waiting` every name the flock has no unanswered instance of.
///
/// A name the flock does not hold at all is settled too: the start meant to
/// register it has already returned, and nothing else will.
///
/// A name with several instances is kept while any one of them is still
/// starting. That is the whole reason this is the only definition of settled
/// and the bus is a trigger: every instance of an app publishes under the
/// same `ProcessInfo::name`, so settling on an event would release the name
/// on whichever instance answered first. `AppConfig::depends_on` promises the
/// opposite, and with probe-gated instances warming up unevenly the gap is
/// seconds, not a window.
///
/// An actor that has gone empties `waiting` outright. Nothing will ever
/// report now, so holding the boot for the bound would only delay the same
/// answer.
async
/// Stops every sheep in `plan`, last stage first.
///
/// Dogs are stopped by `SupervisorHandle::shutdown`, after every sheep,
/// because monitoring should outlive what it monitors and a strict reverse
/// would kill the bark dog before the flock it reports on. So a plan handed
/// here is the sheep-only one; a dog named in it would be stopped here
/// instead.
pub async
/// Stops every sheep `edges` names, last stage first, leaving the dogs
/// running.
///
/// The edges are read by the caller rather than here, because the one caller
/// there is reads them before it can call: `shep dev` clears the registry
/// ahead of the final roll, so a walk that read it at this point would plan
/// nothing and that session would get no ordered teardown at all. A name the
/// flock no longer holds costs a `NotFound` warning and nothing else.
///
/// `dogs` names the dogs this shepherd spawned at boot, and each one is
/// dropped from the plan before it is built. [`stop_in_reverse`] stops
/// whatever it is handed, so this is the only place the dogs rule can be
/// enforced: they are stopped afterwards by `SupervisorHandle::shutdown`,
/// because monitoring should outlive what it monitors. The registry holds
/// sheep alone, so the filter usually removes nothing.
///
/// It is the boot-time spawn list specifically, `RpcContext::dog_names`, and
/// not every name this shepherd knows: a dog adopted against a running
/// shepherd lands in `RpcContext::known_dogs` instead and is not filtered
/// here. No live bug follows, because every other door into the registry
/// refuses a dog, and widening the filter to `known_dogs` would cost more
/// than it bought: that list carries dogs nobody enabled, so a sheep sharing
/// a name with one would drop out of the ordered walk and be killed by the
/// backstop with its dependants still running.
///
/// A sheep sharing a dog's name is dropped with it, and killed in that same
/// `shutdown`, which is where every sheep this walk misses is killed anyway.
pub async