fix(daemon): dispatch due repos longest-waiting first

With the worker pool, a full pass every fetch_interval_s re-marked every repo
due 'now' and dispatch went by path, so when a cycle took longer than the
interval the repos late in the alphabet never got a pass (52 of 80 on a real
machine). Keep the earlier due time and dispatch by it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dasemuandClaude Opus 5.5 committed 2026-11-17 17:03:51 +01:00
1 parent 380fff5395
commit 51450ffa5e
3 files changed
+54 -4

No files matched your search

+9 -4
View File
@@ -612,19 +612,24 @@ fn run(cfg: Config, st: Settings, rx: Receiver<Msg>, ctx: Ctx) {
// Safety net for events the watcher missed (one `read_dir` per group).
let (found, _) = rescan(&root, &groups, &mut watch, &running, &ctx);
forget_removed(&found, &mut due_at, &mut first_at, &mut backoff_until);
// A repo still waiting keeps its earlier due time, so it is not overtaken.
for r in found {
due_at.insert(r, now);
due_at
.entry(r)
.and_modify(|t| *t = (*t).min(now))
.or_insert(now);
}
next_full = now + fetch_every;
}
// A repo whose pass is running stays due and is dispatched when that pass is done.
let mut due: Vec<PathBuf> = due_at
// Longest waiting first: with more due repos than workers, the order decides who waits.
let mut due: Vec<(Instant, PathBuf)> = due_at
.iter()
.filter(|(r, t)| **t <= now && !running.contains(*r))
.map(|(r, _)| r.clone())
.map(|(r, t)| (*t, r.clone()))
.collect();
due.sort();
for r in due {
for (_, r) in due {
// Only as many passes as idle workers: the rest stay due, so pause stops them and
// `offline` is decided when a pass really starts.
if running.len() >= pool {
+42
View File
@@ -792,3 +792,45 @@ fn a_hanging_fetch_does_not_hold_up_other_repos() {
assert!(started.elapsed() < Duration::from_secs(12));
d.shutdown();
}
#[test]
fn every_repo_gets_a_pass_when_a_cycle_takes_longer_than_the_interval() {
let w = world("fair", &["pca"]);
// Four repos sorting before `g/repo` whose fetch takes 1.5 s each: with one worker a cycle
// takes ~6 s against a 1 s interval.
for n in ["a", "b", "c", "d"] {
let r = w.base.join(format!("pca/g/{n}"));
bare_layout(&r, &w.remote);
git(
&r.join(".bare"),
&[
"config",
"remote.origin.uploadpack",
"sleep 1.5; git-upload-pack",
],
);
}
let mut cfg = config(&w, "pca", 200, 1);
cfg.sync.workers = 1;
let d = Daemon::start(cfg, settings(&w, "pca")).unwrap();
let sock = w.base.join("pca.sock");
let last = |s: &serde_json::Value, name: &str| {
s["repos"]
.as_array()
.unwrap()
.iter()
.find(|r| r["path"].as_str().unwrap().ends_with(&format!("/g/{name}")))
.and_then(|r| r["last_sync"].as_u64())
};
// `repo` keeps getting passes: it is not starved by the slow ones (before, sorted dispatch
// gave every new cycle to `a`..`d` again and `repo` never ran).
let mut seen = Vec::new();
wait_until("two passes of the last repo", 45, || {
let t = last(&status(&sock), "repo");
if t.is_some() && seen.last() != Some(&t) {
seen.push(t);
}
seen.len() >= 2
});
d.shutdown();
}
+3
View File
@@ -21,6 +21,9 @@ is `grove_core::sync::sync_repo`, the same code the CLI runs. The daemon only de
the passes, so a slow or hanging fetch of one repo no longer holds up the others. A repo never
has two passes at once: one that becomes due while its pass runs is dispatched when it ends.
Events that arrive during a pass queue another pass; it finds nothing to do and ends the chain.
Due repos are dispatched longest-waiting first, and a full pass never moves an earlier due
time later, so when a cycle takes longer than `fetch_interval_s` every repo still gets its
turn (dispatching by path starved the repos late in the alphabet).
Passes are handed out only to idle workers, so the rest stay due: pause stops them, and whether
a pass runs offline (backoff) is decided when it starts. A `sync` request for a repo whose pass
is running is not undone by that pass failing. A panicking pass is logged and counts as a