From c56aaefa72ca8e2eb245e5f5b87e27aef625b65d Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Sat, 28 Nov 2026 12:34:30 -0700 Subject: [PATCH] History: resume the saved tracker at start; first build observations draw at once The polling task never loaded the tracker the last run saved, so every restart began again from a baseline and dropped what had not yet reached a row. The empty bootstrap drawn at the first poll also started the one-minute wait for the first real observations. Co-Authored-By: Claude Sonnet 5.5 --- crates/server/src/app.rs | 9 ++ crates/server/src/view.rs | 6 +- crates/server/tests/http.rs | 185 +++++++++++++++++++++++++++++++++--- 3 files changed, 184 insertions(+), 16 deletions(-) diff --git a/crates/server/src/app.rs b/crates/server/src/app.rs index f2f0b5b..6ad0fed 100644 --- a/crates/server/src/app.rs +++ b/crates/server/src/app.rs @@ -194,6 +194,8 @@ pub struct Task { /// When sccache's server was last tried to start, and why it is still not running. started_at: Option, start_error: Option, + /// Whether the totals the last run remembered have been put back into the shared state. + resumed: bool, } impl Task { @@ -217,6 +219,7 @@ impl Task { builds: Builds::new(), started_at: None, start_error: None, + resumed: false, } } @@ -240,6 +243,12 @@ impl Task { (Probe::NotRunning, false) => {} } let mut state = app.lock(); + if !self.resumed { + // Carry on where the last run stopped: its running totals are the base that the work + // not yet in a row is counted from, so a restart neither drops it nor counts it twice. + self.resumed = true; + state.tracker = self.history.tracker(); + } state.client_version = version; match read { Ok((text, snapshot)) => { diff --git a/crates/server/src/view.rs b/crates/server/src/view.rs index 694a28a..a60108a 100644 --- a/crates/server/src/view.rs +++ b/crates/server/src/view.rs @@ -147,7 +147,11 @@ impl Builds { self.draws = Resampled::new(&obs); self.times = times; self.stamp = Some(stamp); - self.drawn_at = Some(now); + // Drawing nothing is free, so it does not start the minute's wait: the first + // observations are drawn at the very next poll. + if stamp.0 > 0 { + self.drawn_at = Some(now); + } } /// Time saved for one range's hits, from the draws already made. diff --git a/crates/server/tests/http.rs b/crates/server/tests/http.rs index cb04c34..8839e11 100644 --- a/crates/server/tests/http.rs +++ b/crates/server/tests/http.rs @@ -576,9 +576,10 @@ fn the_bootstrap_is_not_redrawn_on_every_poll_of_a_build_in_flight() { task.poll(&app, NOW); assert_eq!(rust_builds(&app), 0, "nothing seen yet"); - // A build adds one Rust build between every pair of polls. Each is stored at once, but the - // histogram (and with it the bootstrap) waits for the one-minute beat. - for step in 1..6u64 { + // A build adds one Rust build between every pair of polls. Each is stored at once. The first + // is drawn at the next poll (the empty draw before it started no wait); the rest wait for + // the one-minute beat that began with that draw. + for step in 1..7u64 { fake.answers(&with_more_rust_builds(step)); task.poll(&app, NOW + (step as i64) * 10); assert_eq!( @@ -588,23 +589,23 @@ fn the_bootstrap_is_not_redrawn_on_every_poll_of_a_build_in_flight() { ); assert_eq!( rust_builds(&app), - 0, - "but the bootstrap drawn at the first poll stands (step {step})" + 1, + "the first draw stands for the minute (step {step})" ); } - fake.answers(&with_more_rust_builds(6)); - task.poll(&app, NOW + 60); - assert_eq!( - rust_builds(&app), - 6, - "a minute after the last draw, everything seen so far is drawn in" - ); - fake.answers(&with_more_rust_builds(7)); task.poll(&app, NOW + 70); assert_eq!( rust_builds(&app), - 6, + 7, + "a minute after the last draw, everything seen so far is drawn in" + ); + + fake.answers(&with_more_rust_builds(8)); + task.poll(&app, NOW + 80); + assert_eq!( + rust_builds(&app), + 7, "and it stands again until the next beat" ); } @@ -647,7 +648,10 @@ fn a_full_history_costs_few_rows_on_the_second_poll() { "the first reading is the baseline" ); let observations = task.history.build_obs_stamp().unwrap().0; - assert_eq!(observations, 4_319, "one for every tenth minute after the baseline"); + assert_eq!( + observations, 4_319, + "one for every tenth minute after the baseline" + ); task.poll(&app, NOW); let first = app.lock().views_rows; @@ -662,3 +666,154 @@ fn a_full_history_costs_few_rows_on_the_second_poll() { let v = app.view(Range::Day); assert_eq!(v.buckets.len(), 120); } + +/// What sccache prints after `k` more rounds of work: every round adds 3 requests, 2 hits and +/// 1 Rust miss that took 5 s to compile. +fn after_rounds(k: u64) -> String { + let mut v: serde_json::Value = serde_json::from_str(LIVE).unwrap(); + let s = &mut v["stats"]; + let add = |s: &mut serde_json::Value, key: &str, n: u64| { + s[key] = (s[key].as_u64().unwrap() + n).into(); + }; + add(s, "compile_requests", 3 * k); + add(s, "requests_executed", k); + add(s, "cache_writes", k); + add(s, "compilations", k); + s["cache_hits"]["counts"]["Rust"] = (229 + 2 * k).into(); + s["cache_misses"]["counts"]["Rust"] = (3256 + k).into(); + let secs = 25343 + 5 * k; + s["compiler_write_duration"]["secs"] = secs.into(); + v.to_string() +} + +/// Polls once a `step` seconds from `from` to `to`, the counters rising a round each time. +fn run_polls( + fake: &Fake, + app: &App, + task: &mut Task, + rounds: &mut u64, + span: (i64, i64), + step: i64, +) { + let mut t = span.0; + while t <= span.1 { + fake.answers(&after_rounds(*rounds)); + task.poll(app, t); + *rounds += 1; + t += step; + } +} + +#[test] +fn a_restart_loses_nothing_and_counts_nothing_twice() { + let fake = Fake::new(); + let mut rounds = 0; + + let app = App::with_probe(fake.config(), fake.probe()); + let mut task = Task::open(&fake.history, NOW).unwrap(); + // Five minutes of polls, every ten seconds, with work in each; the last 30 s after a row. + run_polls(&fake, &app, &mut task, &mut rounds, (NOW, NOW + 330), 10); + let before = app.view(Range::Day); + assert!(before.totals.requests > 0, "{:?}", before.totals); + assert!( + task.history.row_count().unwrap() >= 4, + "a row a minute on disk while it runs, not at the end: {}", + task.history.row_count().unwrap() + ); + drop(task); + drop(app); + + // The restart: a new process opens the same file and polls once, ten seconds later, with + // the same counters sccache had (no work in between). + let app = App::with_probe(fake.config(), fake.probe()); + let mut task = Task::open(&fake.history, NOW + 340).unwrap(); + rounds -= 1; + run_polls( + &fake, + &app, + &mut task, + &mut rounds, + (NOW + 340, NOW + 340), + 10, + ); + let after = app.view(Range::Day); + assert_eq!( + (after.totals, after.previous), + (before.totals, before.previous), + "a view's totals are the same just before and just after a restart" + ); +} + +#[test] +fn a_restart_counts_the_work_done_while_it_was_down() { + let fake = Fake::new(); + let mut rounds = 0; + let app = App::with_probe(fake.config(), fake.probe()); + let mut task = Task::open(&fake.history, NOW).unwrap(); + run_polls(&fake, &app, &mut task, &mut rounds, (NOW, NOW + 200), 10); + let before = app.view(Range::Day).totals.requests; + drop(task); + drop(app); + + // sccache went on working for 4 rounds while sccache-dash was stopped. + rounds += 3; + let app = App::with_probe(fake.config(), fake.probe()); + let mut task = Task::open(&fake.history, NOW + 260).unwrap(); + run_polls( + &fake, + &app, + &mut task, + &mut rounds, + (NOW + 260, NOW + 260), + 10, + ); + assert_eq!( + app.view(Range::Day).totals.requests, + before + 4 * 3, + "the difference since the last poll before the stop is counted, once" + ); +} + +#[test] +fn build_times_show_within_a_minute_of_the_observations() { + let fake = Fake::new(); + let mut rounds = 0; + let app = App::with_probe(fake.config(), fake.probe()); + let mut task = Task::open(&fake.history, NOW).unwrap(); + // The first poll draws the (empty) bootstrap; the observations come after it. + run_polls(&fake, &app, &mut task, &mut rounds, (NOW, NOW + 600), 10); + let shown = |t: &Task| t.history.build_obs_stamp().unwrap().0; + assert!(shown(&task) >= 50, "observations were recorded"); + let times = app.view(Range::Day).build_times; + assert_eq!(times.len(), 1, "Rust has a histogram after ten minutes"); + + // Stop polling for work: after one more poll a minute on, the newest are in too. + let seen = times[0].compiles; + run_polls( + &fake, + &app, + &mut task, + &mut rounds, + (NOW + 610, NOW + 680), + 10, + ); + let now = app.view(Range::Day).build_times[0].compiles; + assert!(now > seen, "the histogram keeps up: {seen} then {now}"); +} + +#[test] +fn the_first_observations_show_at_the_next_poll_not_a_minute_later() { + let fake = Fake::new(); + let mut rounds = 0; + let app = App::with_probe(fake.config(), fake.probe()); + let mut task = Task::open(&fake.history, NOW).unwrap(); + // The first poll draws nothing: there is nothing to draw, and that must not start the + // minute's wait for the first real observations. + run_polls(&fake, &app, &mut task, &mut rounds, (NOW, NOW + 30), 10); + assert!(task.history.build_obs_stamp().unwrap().0 >= 3); + assert_eq!( + app.view(Range::Day).build_times.len(), + 1, + "seconds after the first observation, not a minute" + ); +}