diff options
| author | tv <tv@krebsco.de> | 2026-12-01 02:35:07 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-12-01 04:06:02 +0000 |
| commit | cf26dc1f928e9839123bedd1693d792bbb418312 (patch) | |
| tree | 4e38369848754d258c830750a9e71e18ca07e0c7 /pkgs/populate/src/populate.rs | |
| parent | 9cbe2fab9a3fe30ce4218458112ab46e49c6ac7c (diff) | |
populate file: list file sources ahead of their turns, at once
Comparing a file source with filters takes a run of rsync --list-only,
which also checks its .rsync-filter files, and which takes about 50 ms
with nothing to do, rsync waiting for its own processes, one source after
another: 3.6 s of a no-op run with 50 such sources, locally.
rsync::prepare is gather, which runs the commands and prints nothing, and
finish, which checks the filter files with what gather got, with the
messages, and takes the listing. Right after the probe, gather runs for
all file sources whose target paths the probe could list, at most 16 at a
time, and each source's turn finishes. The same run takes 0.85 s, and
the bench's 1.0 s locally takes 0.7 s.
Diffstat (limited to 'pkgs/populate/src/populate.rs')
| -rw-r--r-- | pkgs/populate/src/populate.rs | 56 |
1 files changed, 55 insertions, 1 deletions
diff --git a/pkgs/populate/src/populate.rs b/pkgs/populate/src/populate.rs index 86639ac..884ecd9 100644 --- a/pkgs/populate/src/populate.rs +++ b/pkgs/populate/src/populate.rs @@ -9,10 +9,13 @@ use crate::run::{self, Context, Failure, Result}; use crate::sh; use crate::spec::{Source, Spec}; use serde_json::{json, Map, Value}; +use std::collections::BTreeMap; use std::fs; use std::os::unix::fs::DirBuilderExt; use std::path::{Path, PathBuf}; use std::process::{Command, Stdio}; +use std::sync::atomic::{AtomicUsize, Ordering}; +use std::sync::Mutex; /// The commands populate runs on the target to deliver spec's sources, each /// followed by the attribute name in nixpkgs of the package that has it. @@ -59,6 +62,7 @@ pub fn populate(context: &Context, spec: &Spec) -> Result<()> { context.run(&sh::backup(), &[dir])?; } let probed = probe::probe(context, spec, probed)?; + let mut gathered = gathered(spec, &probed); for (name, source) in &spec.source { let path = format!("{dir}/{name}"); eprintln!("populating '{}'...", context.name(&path)); @@ -80,6 +84,7 @@ pub fn populate(context: &Context, spec: &Spec) -> Result<()> { path: &path, workdir, target: probed.get(name), + gathered: std::cell::Cell::new(gathered.remove(name)), }; context.note(json!({ "begin": name })); let outcome = kind.deliver(&delivery)?; @@ -195,6 +200,9 @@ struct Delivery<'a> { /// What the probe found at the target path, for a source whose delivery /// may find nothing to do, see probed. target: Option<&'a probe::Target>, + /// What gathered found for a file source, ahead of its turn, which + /// File::deliver takes. + gathered: std::cell::Cell<Option<Result<rsync::Gathered>>>, } impl Delivery<'_> { @@ -303,6 +311,49 @@ fn file_config(source: &Source, source_path: &str) -> Config { } } +/// What rsync::gather finds for the file sources whose target path the +/// probe could list, by name: for all of them at once, at most 16 at a +/// time, ahead of their turns, since listing what rsync would transfer of +/// a source with filters takes a run of rsync, which takes about 50 ms, +/// also with nothing to do. gather prints nothing, so messages still come +/// at each source's turn, from rsync::finish. +fn gathered( + spec: &Spec, + probed: &BTreeMap<String, probe::Target>, +) -> BTreeMap<String, Result<rsync::Gathered>> { + let jobs: Vec<(&String, &Source, &str)> = spec + .source + .iter() + .filter(|(name, source)| { + source.kind == "file" + && probed + .get(*name) + .is_some_and(|target| target.listing.is_some()) + }) + .filter_map(|(name, source)| { + let path = string(&source.settings, "path").ok()?; + Some((name, source, path)) + }) + .collect(); + let next = AtomicUsize::new(0); + let results = Mutex::new(BTreeMap::new()); + std::thread::scope(|scope| { + for _ in 0..jobs.len().min(16) { + scope.spawn(|| { + while let Some((name, source, path)) = + jobs.get(next.fetch_add(1, Ordering::Relaxed)) + { + let gathered = rsync::gather(&file_config(source, path), path, true); + if let Ok(mut results) = results.lock() { + results.insert((*name).clone(), gathered); + } + } + }); + } + }); + results.into_inner().unwrap_or_else(|e| e.into_inner()) +} + /// What the probe looks for at a source's target path, see probe::probe: /// the listing of a file source that's a directory, that rsync compares by /// size and time, not checksum, the symlink of a symlink source, and the @@ -331,7 +382,10 @@ impl Kind for File { // Nothing to do if what rsync would transfer and what's at the // target path are the same as far as rsync's quick check goes. let listing = d.target.and_then(|target| target.listing.as_ref()); - let prepared = rsync::prepare(&config, source_path, listing.is_some())?; + let prepared = match d.gathered.take() { + Some(gathered) => rsync::finish(&config, gathered?)?, + None => rsync::prepare(&config, source_path, listing.is_some())?, + }; let changed = if prepared.listing.is_some() && prepared.listing.as_ref() == listing { Vec::new() } else { |
