diff --git a/services/flysim/crates/fly-legacy-session/src/bin/fly-shadow.rs b/services/flysim/crates/fly-legacy-session/src/bin/fly-shadow.rs index b5f7531..f669945 100644 --- a/services/flysim/crates/fly-legacy-session/src/bin/fly-shadow.rs +++ b/services/flysim/crates/fly-legacy-session/src/bin/fly-shadow.rs @@ -18,7 +18,7 @@ //! | `--mode in-process\|thread\|process` | `in-process` | //! | `--threads N` | `$FLY_SHADOW_THREADS`, else 2: the shadow agent's sweep threads | //! | `--required-brain-seconds S` | 10800 | -//! | `--max-live-lag-seconds S` | 0.5; the shadow pauses 60 s whenever the live `fly_lag_seconds` grew by more than S within 30 s (0 disables) | +//! | `--lag-guard-margin S` | 0.05 s/s; the shadow pauses 60 s while the live `fly_lag_seconds` grows faster than `fly-shadow-run`'s baseline rate (`/baseline.json`) plus this margin (the baseline's own margin wins), and stops pausing for 10 minutes when a pause did not help (0 disables) | //! | `--metrics HOST:PORT` | the live metrics listener | //! | `--spool-max-mib N` | 512 | //! | `--context N` | 30 transition pairs before a divergence | @@ -227,7 +227,7 @@ fn run(mut args: Args) { let required: f64 = args .parsed("--required-brain-seconds") .unwrap_or(shadow::REQUIRED_BRAIN_SECONDS); - let max_lag: f64 = args.parsed("--max-live-lag-seconds").unwrap_or(0.5); + let lag_margin: f64 = args.parsed("--lag-guard-margin").unwrap_or(0.05); let metrics = args.value("--metrics").or_else(|| { config.control.metrics_bind.map(|addr| { if addr.ip().is_unspecified() { @@ -266,14 +266,17 @@ fn run(mut args: Args) { fly_session::legacy_env::DEFAULT_AUDIO_RATE )); } - let lag_guard = (max_lag > 0.0) + let baseline_file = out_dir.join("baseline.json"); + let lag_guard = (lag_margin > 0.0) .then_some(()) .and(metrics) .map(|metrics_addr| LagGuard { metrics_addr, - growth_seconds: max_lag, - window: Duration::from_secs(30), + baseline_file: Some(baseline_file), + margin: lag_margin, + window: Duration::from_secs(60), back_off: Duration::from_secs(60), + suppress: Duration::from_secs(600), }); let shadow_config = ShadowConfig { trace_dir, diff --git a/services/flysim/crates/fly-legacy-session/src/shadow/follow.rs b/services/flysim/crates/fly-legacy-session/src/shadow/follow.rs index 36c3407..b355ebb 100644 --- a/services/flysim/crates/fly-legacy-session/src/shadow/follow.rs +++ b/services/flysim/crates/fly-legacy-session/src/shadow/follow.rs @@ -43,6 +43,28 @@ pub fn superseded(path: &Path) -> bool { matches!(next_file(dir, Some(path)), Ok(Some(_))) } +/// About how many live transitions are written but not yet compared: the bytes beyond `consumed` +/// in `path` and every byte of the newer files, over the mean line length read so far. +pub fn backlog(path: &Path, consumed: u64, lines: u64) -> u64 { + if lines == 0 { + return 0; + } + let mean = (consumed / lines).max(1); + let here = std::fs::metadata(path) + .map(|m| m.len()) + .unwrap_or(consumed) + .saturating_sub(consumed); + let newer: u64 = path + .parent() + .and_then(|dir| trace_files(dir).ok()) + .into_iter() + .flatten() + .filter(|p| p.file_name() > path.file_name()) + .filter_map(|p| std::fs::metadata(p).ok().map(|m| m.len())) + .sum(); + (here + newer) / mean +} + /// A reader of one growing file that returns complete lines only. pub struct Follower { path: PathBuf, @@ -83,19 +105,6 @@ impl Follower { Ok(Some(line)) } - /// About how many complete lines are written but not yet read: the bytes beyond the reader - /// over the mean line length so far. - pub fn backlog_lines(&self) -> u64 { - let Ok(len) = std::fs::metadata(&self.path).map(|m| m.len()) else { - return 0; - }; - if self.lines == 0 { - return 0; - } - let mean = (self.consumed / self.lines).max(1); - len.saturating_sub(self.consumed) / mean - } - /// Bytes of an incomplete last line held back. pub fn pending_bytes(&self) -> usize { self.partial.len() @@ -138,5 +147,8 @@ mod tests { assert_eq!(next_file(dir.path(), None).unwrap(), Some(a.clone())); assert_eq!(next_file(dir.path(), Some(&a)).unwrap(), Some(b.clone())); assert_eq!(next_file(dir.path(), Some(&b)).unwrap(), None); + // The backlog counts what is left here and everything in newer files. + std::fs::write(&b, b"01234567890\n01234567890\n").unwrap(); + assert_eq!(backlog(&a, follower.consumed, follower.lines), 2); } } diff --git a/services/flysim/crates/fly-legacy-session/src/shadow/mod.rs b/services/flysim/crates/fly-legacy-session/src/shadow/mod.rs index da8fb81..c866b1f 100644 --- a/services/flysim/crates/fly-legacy-session/src/shadow/mod.rs +++ b/services/flysim/crates/fly-legacy-session/src/shadow/mod.rs @@ -107,38 +107,162 @@ pub struct ShadowConfig { pub release: String, } -/// The back-off that keeps the shadow from costing the live fly real time. +/// The back-off that keeps the shadow from costing the live fly real time, judged against the +/// live fly's own pace before the shadow existed (SHADOW-01 review round 2, G1). /// -/// flysim's `fly_lag_seconds` is the *accumulated* pacing shortfall of the process: it never -/// recovers, and it grows only when the loop fell more than a second behind its schedule -/// (`pacing::MAX_CATCHUP`). So the signal is its growth, not its value: when it has grown by more -/// than `growth_seconds` within the last `window`, the shadow pauses between frames for -/// `back_off`, and again for as long as the growth continues. A metrics listener that cannot be -/// read never pauses the shadow (the guard is a backstop; the unit's `SCHED_IDLE` is the plan). +/// flysim's `fly_lag_seconds` is the accumulated pacing shortfall of the process: it grows, in +/// chunks of more than a second, whenever the loop runs behind real time, and a fly that runs at a +/// realtime factor of 0.7 grows it by about 0.3 s every second whatever the shadow does. So the +/// signal is the lag *growth rate* against the baseline rate `fly-shadow-run start` measured +/// before the shadow started (`baseline.json`: `lagRate`, `margin`; 0 and `margin` when there is +/// none). See [`LagJudge`] for the rule, which lifts the back-off when pausing the shadow does not +/// help: then the shadow is not the cause, and the host guard (`fly-shadow-run guard`) is what +/// acts on a lasting degradation. A metrics listener that cannot be read never pauses the shadow. #[derive(Clone, Debug)] pub struct LagGuard { /// `host:port` of the live metrics listener. pub metrics_addr: String, - pub growth_seconds: f64, + /// `fly-shadow-run`'s baseline, re-read every poll when it exists. + pub baseline_file: Option, + /// The allowed rise of the lag growth rate (s/s) when no baseline gives one. + pub margin: f64, + /// The span the growth rate is measured over. pub window: Duration, pub back_off: Duration, + /// How long back-offs stay off after one that did not help. + pub suppress: Duration, } -/// Whether the live lag grew by more than `growth` seconds within `window` of the newest sample. -/// Samples are `(when, fly_lag_seconds)`, oldest first; a restart (the value falling) is growth -/// from zero. -pub fn lag_grew(samples: &VecDeque<(Instant, f64)>, window: Duration, growth: f64) -> bool { - let Some(&(newest_at, newest)) = samples.back() else { - return false; - }; - let mut floor = newest; - for &(at, value) in samples.iter().rev() { - if newest_at.duration_since(at) > window { - break; +#[derive(Clone, Copy, Debug, PartialEq)] +enum JudgeMode { + Normal, + /// Paused until `until`; the lag and time when the pause began. + BackingOff { + until: Instant, + from: (Instant, f64), + }, + Suppressed { + until: Instant, + }, +} + +/// The in-shadow back-off rule, fed `(now, fly_lag_seconds)` every poll: +/// +/// - *Normal*: when the lag has grown faster than `base_rate + margin` over the last `window`, +/// pause the shadow for `back_off`. +/// - *At the end of a pause*: if the live lag kept growing faster than that while the shadow was +/// paused, the shadow is not the cause: no back-off for `suppress`. Otherwise back to normal, +/// with a fresh window. +/// - A new flysim process (the lag falls) starts the window again. +#[derive(Clone, Debug)] +pub struct LagJudge { + pub base_rate: f64, + pub margin: f64, + window: Duration, + back_off: Duration, + suppress: Duration, + samples: VecDeque<(Instant, f64)>, + mode: JudgeMode, +} + +impl LagJudge { + pub fn new( + base_rate: f64, + margin: f64, + window: Duration, + back_off: Duration, + suppress: Duration, + ) -> LagJudge { + LagJudge { + base_rate, + margin, + window, + back_off, + suppress, + samples: VecDeque::new(), + mode: JudgeMode::Normal, } - floor = floor.min(value); } - newest - floor > growth + + fn limit(&self) -> f64 { + self.base_rate + self.margin + } + + /// The growth rate over the last `window`, once the samples span it. + fn rate(&self) -> Option { + let &(newest_at, newest) = self.samples.back()?; + let &(oldest_at, oldest) = self + .samples + .iter() + .rev() + .find(|(at, _)| newest_at.duration_since(*at) >= self.window)?; + let dt = newest_at.duration_since(oldest_at).as_secs_f64(); + (dt > 0.0).then(|| (newest - oldest) / dt) + } + + /// Whether the shadow should be paused now. + pub fn observe(&mut self, now: Instant, lag: f64) -> bool { + if self.samples.back().is_some_and(|&(_, last)| lag < last) { + self.samples.clear(); + if let JudgeMode::BackingOff { until, .. } = self.mode { + self.mode = JudgeMode::BackingOff { + until, + from: (now, lag), + }; + } + } + self.samples.push_back((now, lag)); + while self + .samples + .front() + .is_some_and(|&(at, _)| now.duration_since(at) > self.window * 3) + { + self.samples.pop_front(); + } + match self.mode { + JudgeMode::Normal => { + if self.rate().is_some_and(|rate| rate > self.limit()) { + self.mode = JudgeMode::BackingOff { + until: now + self.back_off, + from: (now, lag), + }; + return true; + } + false + } + JudgeMode::BackingOff { until, from } => { + if now < until { + return true; + } + let dt = now.duration_since(from.0).as_secs_f64(); + let during = if dt > 0.0 { (lag - from.1) / dt } else { 0.0 }; + self.mode = if during > self.limit() { + JudgeMode::Suppressed { + until: now + self.suppress, + } + } else { + JudgeMode::Normal + }; + self.samples.clear(); + self.samples.push_back((now, lag)); + false + } + JudgeMode::Suppressed { until } => { + if now >= until { + self.mode = JudgeMode::Normal; + self.samples.clear(); + self.samples.push_back((now, lag)); + } + false + } + } + } +} + +/// `fly-shadow-run`'s baseline: `(lagRate, margin)`. +pub fn read_baseline(path: &Path) -> Option<(f64, f64)> { + let v: Value = serde_json::from_str(&std::fs::read_to_string(path).ok()?).ok()?; + Some((v["lagRate"].as_f64()?, v["margin"].as_f64()?)) } /// What the poller shares: the last lag read (ms, `u64::MAX` unknown) and whether to back off. @@ -181,7 +305,7 @@ impl StopFlag { } } -/// Polls the live `fly_lag_seconds` every two seconds and applies [`LagGuard`]. +/// Polls the live `fly_lag_seconds` every two seconds and applies [`LagJudge`]. fn spawn_lag_poller(guard: &LagGuard, stop: StopFlag) -> Arc { let state = Arc::new(LagState { lag_ms: AtomicU64::new(u64::MAX), @@ -192,32 +316,30 @@ fn spawn_lag_poller(guard: &LagGuard, stop: StopFlag) -> Arc { std::thread::Builder::new() .name("fly-shadow-lag".to_owned()) .spawn(move || { - let mut samples: VecDeque<(Instant, f64)> = VecDeque::new(); - let mut until: Option = None; + let mut judge = LagJudge::new( + 0.0, + guard.margin, + guard.window, + guard.back_off, + guard.suppress, + ); while !stop.requested() { - let now = Instant::now(); - match read_lag_seconds(&guard.metrics_addr) { + if let Some((rate, margin)) = guard.baseline_file.as_deref().and_then(read_baseline) + { + judge.base_rate = rate; + judge.margin = margin; + } + let back_off = match read_lag_seconds(&guard.metrics_addr) { Some(lag) => { out.lag_ms.store((lag * 1000.0) as u64, Ordering::Relaxed); - // A new process starts from zero: forget the old one's samples. - if samples.back().is_some_and(|&(_, last)| lag < last) { - samples.clear(); - } - samples.push_back((now, lag)); - while samples - .front() - .is_some_and(|&(at, _)| now.duration_since(at) > guard.window * 2) - { - samples.pop_front(); - } - if lag_grew(&samples, guard.window, guard.growth_seconds) { - until = Some(now + guard.back_off); - } + judge.observe(Instant::now(), lag) } - None => out.lag_ms.store(u64::MAX, Ordering::Relaxed), - } - out.back_off - .store(until.is_some_and(|u| now < u), Ordering::Relaxed); + None => { + out.lag_ms.store(u64::MAX, Ordering::Relaxed); + false + } + }; + out.back_off.store(back_off, Ordering::Relaxed); std::thread::sleep(Duration::from_secs(2)); } }) @@ -350,6 +472,7 @@ pub async fn run(config: ShadowConfig, stop: StopFlag) -> Result<(Ended, Verdict lag, stop, last_write: Instant::now() - Duration::from_secs(60), + following: None, }; shadow.write(); let ended = shadow.follow().await; @@ -388,11 +511,17 @@ struct Shadow { stop: StopFlag, verdict: Verdict, last_write: Instant, + /// The file being compared, the bytes and lines of it compared so far. + following: Option<(PathBuf, u64, u64)>, } impl Shadow { fn write(&mut self) { self.verdict.spool_evicted = self.spool.evicted(); + // Refreshed on every write, backed off or not, so the verdict's catch-up bound is current. + if let Some((path, consumed, lines)) = &self.following { + self.verdict.lag_transitions = follow::backlog(path, *consumed, *lines); + } self.verdict.live_lag_seconds = self.lag.as_ref().and_then(|lag| { let ms = lag.lag_ms.load(Ordering::Relaxed); (ms != u64::MAX).then(|| ms as f64 / 1000.0) @@ -530,7 +659,6 @@ impl Shadow { )), }; } - self.verdict.lag_transitions = 0; self.write_if_due(); tokio::time::sleep(self.config.poll).await; } @@ -1020,9 +1148,11 @@ impl Shadow { self.spool.set_floor(*generation + 1); } self.verdict.agreement.count(&behaviour); - if self.verdict.agreement.transitions.is_multiple_of(600) { - self.verdict.lag_transitions = follower.backlog_lines(); - } + self.following = Some(( + follower.path().to_owned(), + follower.consumed, + follower.lines, + )); self.verdict.brain_ms += behaviour["ticksAdvanced"] .as_str() .and_then(|s| s.parse::().ok()) @@ -1071,25 +1201,60 @@ mod tests { assert_eq!(parse_lag_seconds("fly_lag_seconds_total 3\n"), None); } - #[test] - fn the_guard_backs_off_on_growth_not_on_an_old_lag() { + /// Feeds a judge 2-s polls for `seconds`, the lag growing at `rate(paused)` s/s; returns the + /// fraction of the time the shadow was paused. + fn paused_fraction(judge: &mut LagJudge, seconds: u64, rate: impl Fn(bool) -> f64) -> f64 { let t0 = Instant::now(); - let at = |s: u64| t0 + Duration::from_secs(s); - let window = Duration::from_secs(30); - // An old lag of 40 s that no longer grows: no back-off. - let steady: VecDeque<_> = (0..20).map(|i| (at(i * 2), 40.0)).collect(); - assert!(!lag_grew(&steady, window, 0.5)); - // It grew by 1.2 s within the window: back off. - let mut grew = steady.clone(); - grew.push_back((at(40), 41.2)); - assert!(lag_grew(&grew, window, 0.5)); - // The same growth, but longer ago than the window: no longer. - let mut old = grew.clone(); - for i in 21..45 { - old.push_back((at(i * 2), 41.2)); + let (mut lag, mut paused, mut paused_polls) = (5.0, false, 0u64); + for i in 0..seconds / 2 { + lag += 2.0 * rate(paused); + paused = judge.observe(t0 + Duration::from_secs(i * 2), lag); + paused_polls += u64::from(paused); } - assert!(!lag_grew(&old, window, 0.5)); - assert!(!lag_grew(&VecDeque::new(), window, 0.5)); + paused_polls as f64 / (seconds / 2) as f64 + } + + fn judge(base_rate: f64) -> LagJudge { + LagJudge::new( + base_rate, + 0.05, + Duration::from_secs(60), + Duration::from_secs(60), + Duration::from_secs(600), + ) + } + + #[test] + fn a_fly_below_real_time_is_judged_against_its_own_pace() { + // The release CT at RTF 0.66: 0.34 s of lag every second, before and during the shadow. + let mut j = judge(0.34); + assert_eq!(paused_fraction(&mut j, 3 * 3600, |_| 0.34), 0.0); + // Without a baseline the same fly is paused once, the pause does not help, and the + // back-off lifts: at most one minute in eleven. + let mut j = judge(0.0); + let f = paused_fraction(&mut j, 3 * 3600, |_| 0.34); + assert!(f > 0.0 && f < 0.1, "{f}"); + } + + #[test] + fn a_shadow_that_costs_the_fly_time_stays_paused_while_it_would() { + // The fly keeps real time while the shadow is paused and loses 0.3 s/s while it runs: + // pausing helps, so the judge keeps pausing it, most of the time. + let mut j = judge(0.0); + let f = paused_fraction(&mut j, 3 * 3600, |paused| if paused { 0.0 } else { 0.3 }); + assert!(f > 0.4, "{f}"); + } + + #[test] + fn a_restart_starts_the_window_again() { + let t0 = Instant::now(); + let mut j = judge(0.0); + for i in 0..40u64 { + assert!(!j.observe(t0 + Duration::from_secs(i * 2), 50.0)); + } + // The lag falls to zero (a new process): no rate across the restart. + assert!(!j.observe(t0 + Duration::from_secs(82), 0.0)); + assert!(!j.observe(t0 + Duration::from_secs(84), 0.0)); } #[test] diff --git a/services/flysim/crates/fly-legacy-session/src/shadow/verdict.rs b/services/flysim/crates/fly-legacy-session/src/shadow/verdict.rs index 07d4406..d758780 100644 --- a/services/flysim/crates/fly-legacy-session/src/shadow/verdict.rs +++ b/services/flysim/crates/fly-legacy-session/src/shadow/verdict.rs @@ -23,7 +23,8 @@ //! cutting over, every one of these holds; anything else keeps the legacy fly: //! //! - `format` is `fly-shadow-verdict-v1`, `status` is `pass`, `firstDivergence` is `null`, and -//! `compared.brainSeconds` reaches `required.brainSeconds`; +//! `compared.brainSeconds` reaches `required.brainSeconds` and never less than the operator's +//! 10,800 s (`REQUIRED_BRAIN_SECONDS`), whatever window the shadow was run with; //! - *the release*: `candidate.release` is the directory `/opt/fly/current` resolves to now, //! every binary in `candidate.binaries` still has its recorded SHA-256 there, and the binary //! being switched to (`flysim-session`, SERVE-01's service) is one of them -- the shadow @@ -408,9 +409,12 @@ pub fn allows_cutover( return Err("the verdict is for another compatibility string".to_owned()); } let compared = verdict["compared"]["brainSeconds"].as_f64().unwrap_or(0.0); + // The operator's window is a floor the verdict cannot lower: a shadow run with a shorter + // `--required-brain-seconds` (a rehearsal, a test) never arms the cutover. let required = verdict["required"]["brainSeconds"] .as_f64() - .unwrap_or(f64::INFINITY); + .unwrap_or(f64::INFINITY) + .max(crate::shadow::REQUIRED_BRAIN_SECONDS); if compared < required { return Err(format!("{compared} of {required} brain seconds compared")); } @@ -544,6 +548,10 @@ mod tests { allows_cutover(&pass, "flysim-session", &release, "c", now + 301_000, 300).is_err() ); assert!(ok(&verdict(Status::Pass, 10_799_000.0)).is_err()); + // A verdict that asked for less than the operator's 3 h never passes the check. + let mut short_window = verdict(Status::Pass, 67_000.0); + short_window["required"]["brainSeconds"] = json!(60.0); + assert!(ok(&short_window).is_err()); for status in [ Status::Running, Status::Diverged, diff --git a/tools/shadow-rehearsal.sh b/tools/shadow-rehearsal.sh index 9b5d35f..18978c2 100755 --- a/tools/shadow-rehearsal.sh +++ b/tools/shadow-rehearsal.sh @@ -18,7 +18,7 @@ # --bin DIR flysim, fly-shadow (and fly-session for --mode process); default # $CARGO_TARGET_DIR/release or services/flysim/target/release # --port N first of three local ports (default 17400) -# --guard S the shadow's --max-live-lag-seconds against the live metrics (0.5, as +# --guard S the shadow's --lag-guard-margin against the live metrics (0.05, as # on the container; 0 disables) # # Environment: FLY_ROM (required); FLYSIM_PATHS_DATASET (default data/fafb-v783); @@ -31,7 +31,7 @@ set -euo pipefail die() { echo "shadow-rehearsal: $*" >&2; exit 2; } repo="$(cd "$(dirname "$0")/.." && pwd)" checkpoint=""; brain_minutes=30; restart_at=""; sugar_every=20; speed=0; live_threads=3 -mode=in-process; threads=2; ledgers=1; out=""; bin=""; port=17400; guard=0.5 +mode=in-process; threads=2; ledgers=1; out=""; bin=""; port=17400; guard=0.05 while [ $# -gt 0 ]; do case "$1" in --checkpoint) checkpoint="$2"; shift 2 ;; @@ -99,7 +99,7 @@ stop_live() { wall0=$(date +%s.%N) "$bin/fly-shadow" run --out "$shadow_dir" --trace-dir "$live/trace" --all-files --keep-traces --keep-spool \ - --mode "$mode" --threads "$threads" --max-live-lag-seconds "$guard" \ + --mode "$mode" --threads "$threads" --lag-guard-margin "$guard" \ --required-brain-seconds "$((brain_minutes * 60))" > "$out/shadow.log" 2>&1 & shadow_pid=$! # flysim starts no trace without the shadow's heartbeat.