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
//! Replacement ordering shared by blocking clients and async servers.
//!
//! The caller holds the persistent upgrade lock throughout the transaction.
//! Adapters perform one operation at a time; this module decides their order.
use crate::ProcessLock;
use std::time::{Duration, Instant};
/// Whether the application permits old and new processes to coexist.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(kani, derive(kani::Arbitrary))]
pub enum Mode {
/// Verify and select the candidate before retiring the incumbent.
Overlap,
/// Drain and release the incumbent before starting the candidate.
Exclusive,
}
/// One operation supplied by an application adapter.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Step {
/// Re-read selection under the transaction lock. Unchanged skips replacement.
Recheck,
/// Validate the artifact and capture application preparation state.
Prepare,
/// Close incumbent admission and release exclusive resources.
Drain,
/// Launch the candidate through its process owner.
Start,
/// Obtain a live identity/readiness proof. Pending retries this step only.
Verify,
/// Revalidate selection and application preparation after the live proof.
Validate,
/// Atomically select the verified candidate; never return an error after committing.
Commit,
/// Start retiring the incumbent. Pending keeps its obligations alive.
Retire,
}
/// Result of one adapter operation.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(kani, derive(kani::Arbitrary))]
pub enum Progress {
/// The operation completed.
Done,
/// The proof/drain is not ready, or retirement remains in progress.
Pending,
/// Recheck found no replacement necessary or another updater already won.
Unchanged,
}
/// A replacement result never implies replay of application work.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Outcome {
/// No replacement was performed.
Unchanged,
/// Selection committed and the incumbent has no remaining obligations.
Complete,
/// Selection committed; the incumbent must remain alive for its obligations.
RetirementPending,
}
/// Failure retains the phase and whether selection has already committed.
#[derive(Debug)]
#[non_exhaustive]
pub struct Failure<E> {
/// Failed operation.
pub step: Step,
/// Whether the new selection is authoritative despite this failure.
pub committed: bool,
/// Why the operation failed.
pub reason: Reason<E>,
}
/// Cause of a replacement failure.
#[derive(Debug)]
#[non_exhaustive]
pub enum Reason<E> {
/// Setup or the consumer's explicit drain budget expired.
Deadline,
/// Application or operating system operation failed.
Adapter(E),
/// The adapter returned a result that is not valid for the current step.
InvalidProgress,
}
impl<E: std::fmt::Display> std::fmt::Display for Failure<E> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "replacement {:?} failed: ", self.step)?;
match &self.reason {
Reason::Deadline => f.write_str("deadline expired"),
Reason::Adapter(error) => error.fmt(f),
Reason::InvalidProgress => f.write_str("invalid adapter progress"),
}
}
}
impl<E: std::error::Error + 'static> std::error::Error for Failure<E> {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match &self.reason {
Reason::Adapter(error) => Some(error),
Reason::Deadline | Reason::InvalidProgress => None,
}
}
}
/// Short setup budget and independent application-owned drain policy.
#[derive(Clone, Copy, Debug)]
pub struct Budgets {
/// Absolute budget shared by setup operations, restarted after exclusive drain.
pub setup: Duration,
/// None allows drain to take arbitrarily long. Expiry never kills work.
pub drain: Option<Duration>,
}
/// Blocking application operations; no runtime is introduced into the client.
pub trait Driver {
/// Application error.
type Error;
/// Perform exactly the requested operation, bounding I/O by the given deadline.
fn perform(&mut self, step: Step, deadline: Option<Instant>) -> Result<Progress, Self::Error>;
/// Client shims to prefault after selection commits. Empty by default.
///
/// Typical entries are the relay or compiler wrapper next to this daemon.
/// Prefault errors are discarded: publication has already succeeded.
fn warmup_paths(&self) -> Vec<std::path::PathBuf> {
Vec::new()
}
}
/// Async application operations.
#[cfg(feature = "async")]
pub trait AsyncDriver {
/// Application error.
type Error;
/// Perform exactly the requested operation. Cancellation must preserve ownership.
fn perform(
&mut self,
step: Step,
deadline: Option<tokio::time::Instant>,
) -> impl std::future::Future<Output = Result<Progress, Self::Error>> + Send;
/// Client shims to prefault after selection commits. Empty by default.
fn warmup_paths(&self) -> Vec<std::path::PathBuf> {
Vec::new()
}
}
struct Machine {
mode: Mode,
step: Step,
committed: bool,
}
impl Machine {
fn new(mode: Mode) -> Self {
Self {
mode,
step: Step::Recheck,
committed: false,
}
}
fn failure<E>(&self, reason: Reason<E>) -> Failure<E> {
Failure {
step: self.step,
committed: self.committed,
reason,
}
}
fn advance<E>(&mut self, progress: Progress) -> Result<Option<Outcome>, Failure<E>> {
use Step::*;
match (self.step, progress) {
(Recheck, Progress::Unchanged) => return Ok(Some(Outcome::Unchanged)),
(Recheck | Verify | Drain, Progress::Pending) => return Ok(None),
(Retire, Progress::Pending) => return Ok(Some(Outcome::RetirementPending)),
(_, Progress::Done) => {}
_ => return Err(self.failure(Reason::InvalidProgress)),
}
self.step = match self.step {
Recheck => Prepare,
Prepare if self.mode == Mode::Exclusive => Drain,
Prepare | Drain => Start,
Start => Verify,
Verify => Validate,
Validate => Commit,
Commit => {
self.committed = true;
if self.mode == Mode::Exclusive {
return Ok(Some(Outcome::Complete));
}
Retire
}
Retire => return Ok(Some(Outcome::Complete)),
};
Ok(None)
}
}
/// Execute a serialized replacement. The lock is retained by the caller on return.
///
/// The adapter must preserve a selected candidate when reporting post-commit
/// retirement failures. No callback automatically replays requests or kills a daemon.
pub fn run<D: Driver>(
_ownership: &ProcessLock,
mode: Mode,
budgets: Budgets,
driver: &mut D,
) -> Result<Outcome, Failure<D::Error>> {
let mut machine = Machine::new(mode);
let mut deadline = Some(Instant::now() + budgets.setup);
loop {
if deadline.is_some_and(|limit| Instant::now() >= limit) {
return Err(machine.failure(Reason::Deadline));
}
let step = machine.step;
let progress = driver
.perform(step, deadline)
.map_err(|e| machine.failure(Reason::Adapter(e)))?;
// A committed result must be recorded even if a blocking commit ran late.
if step != Step::Commit && deadline.is_some_and(|limit| Instant::now() >= limit) {
return Err(machine.failure(Reason::Deadline));
}
let committed_before = machine.committed;
if let Some(outcome) = machine.advance(progress)? {
if machine.committed && !committed_before {
crate::warmup::prefault_all(driver.warmup_paths());
}
return Ok(outcome);
}
if machine.committed && !committed_before {
crate::warmup::prefault_all(driver.warmup_paths());
}
if machine.step != step {
if machine.step == Step::Drain || machine.step == Step::Retire {
deadline = budgets.drain.map(|budget| Instant::now() + budget);
} else if step == Step::Drain {
deadline = Some(Instant::now() + budgets.setup);
}
} else {
let wait = deadline.map_or(crate::readiness::POLL_INTERVAL, |limit| {
crate::readiness::POLL_INTERVAL.min(limit.saturating_duration_since(Instant::now()))
});
#[expect(
clippy::disallowed_methods,
reason = "The replacement driver returned Pending without a wake source; retry within the phase budget."
)]
std::thread::sleep(wait);
}
}
}
/// Execute the same transition machine on an async runtime.
#[cfg(feature = "async")]
pub async fn run_async<D: AsyncDriver>(
_ownership: &ProcessLock,
mode: Mode,
budgets: Budgets,
driver: &mut D,
) -> Result<Outcome, Failure<D::Error>> {
let mut machine = Machine::new(mode);
let mut deadline = Some(tokio::time::Instant::now() + budgets.setup);
loop {
if deadline.is_some_and(|limit| tokio::time::Instant::now() >= limit) {
return Err(machine.failure(Reason::Deadline));
}
let step = machine.step;
// Commit is an atomic publication operation. Interrupting it could leave its
// result unknown, so its adapter must not await after publication.
let result = if let Some(limit) = deadline.filter(|_| step != Step::Commit) {
tokio::time::timeout_at(limit, driver.perform(step, deadline))
.await
.map_err(|_| machine.failure(Reason::Deadline))?
} else {
driver.perform(step, deadline).await
};
let progress = result.map_err(|e| machine.failure(Reason::Adapter(e)))?;
if step != Step::Commit
&& deadline.is_some_and(|limit| tokio::time::Instant::now() >= limit)
{
return Err(machine.failure(Reason::Deadline));
}
let committed_before = machine.committed;
if let Some(outcome) = machine.advance(progress)? {
if machine.committed && !committed_before {
prefault_async(driver.warmup_paths()).await;
}
return Ok(outcome);
}
if machine.committed && !committed_before {
prefault_async(driver.warmup_paths()).await;
}
if machine.step != step {
if machine.step == Step::Drain || machine.step == Step::Retire {
deadline = budgets
.drain
.map(|budget| tokio::time::Instant::now() + budget);
} else if step == Step::Drain {
deadline = Some(tokio::time::Instant::now() + budgets.setup);
}
} else {
let wake = tokio::time::Instant::now() + crate::readiness::POLL_INTERVAL;
#[expect(
clippy::disallowed_methods,
reason = "The replacement driver returned Pending without a wake source; retry within the phase budget."
)]
tokio::time::sleep_until(deadline.map_or(wake, |limit| limit.min(wake))).await;
}
}
}
// Filesystem reads must not occupy a Tokio worker after a successful commit.
// The paths are owned and bounded by the caller; failed warming is non-fatal.
#[cfg(feature = "async")]
async fn prefault_async(paths: Vec<std::path::PathBuf>) {
if !paths.is_empty() {
let _ = tokio::task::spawn_blocking(move || crate::warmup::prefault_all(paths)).await;
}
}
/// Properties of the transition machine for every adapter answer, not only the
/// sequences a test author thought of. Each one restates a promise from this
/// module's documentation.
#[cfg(kani)]
mod proofs {
use super::*;
/// Long enough to reach `Complete` in either mode with a `Pending` at every
/// step that allows one: Recheck, Prepare, Drain, Start, Verify, Validate,
/// Commit and Retire, each preceded by a retry.
const ANSWERS: usize = 17;
#[kani::proof]
#[kani::unwind(18)]
fn replacement_never_claims_what_it_has_not_done() {
let mode: Mode = kani::any();
let mut machine = Machine::new(mode);
let mut drained = false;
let mut verified = false;
let mut validated = false;
for _ in 0..ANSWERS {
let step = machine.step;
let was_committed = machine.committed;
let progress: Progress = kani::any();
let result = machine.advance::<()>(progress);
// Selection is never withdrawn once it has been made.
assert!(!was_committed || machine.committed);
if progress == Progress::Done {
drained |= step == Step::Drain;
verified |= step == Step::Verify;
validated |= step == Step::Validate;
}
// Overlap keeps the incumbent serving: it never drains it.
if mode == Mode::Overlap {
assert!(machine.step != Step::Drain);
}
// Exclusive releases the incumbent before starting the candidate.
if mode == Mode::Exclusive && machine.step == Step::Start {
assert!(drained);
}
// A candidate is selected only after a live proof and revalidation.
if machine.committed {
assert!(verified && validated);
}
// The incumbent is retired only after the new selection is committed.
if machine.step == Step::Retire {
assert!(machine.committed);
}
match result {
Ok(Some(Outcome::Unchanged)) => {
// "Unchanged" is only ever the answer to Recheck, before
// anything was replaced.
assert!(step == Step::Recheck && !machine.committed);
return;
}
Ok(Some(Outcome::Complete | Outcome::RetirementPending)) => {
// Success is never reported for a candidate that was not
// selected.
assert!(machine.committed);
return;
}
Ok(None) => {}
Err(failure) => {
// A failure after commit says so, so the caller does not
// treat an authoritative selection as rolled back.
assert!(failure.committed == machine.committed);
assert!(failure.step == step);
return;
}
}
}
}
}
#[cfg(test)]
mod error_tests {
use super::*;
#[test]
fn an_adapter_failure_preserves_its_source() {
let failure = Failure {
step: Step::Start,
committed: false,
reason: Reason::Adapter(std::io::Error::other("failed to spawn")),
};
assert_eq!(
std::error::Error::source(&failure).unwrap().to_string(),
"failed to spawn"
);
for reason in [Reason::Deadline, Reason::InvalidProgress] {
let failure: Failure<std::io::Error> = Failure {
step: Step::Start,
committed: false,
reason,
};
assert!(std::error::Error::source(&failure).is_none());
}
}
}