mirror of
https://github.com/Dasemu/Grove.git
synced 2026-11-26 07:16:11 +00:00
Merge pull request #13 from Dasemu/fix/fair-dispatch
fix(daemon): dispatch due repos longest-waiting first
This commit is contained in:
3 files changed
+54
-4
No files matched your search
@@ -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 {
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user