diff options
| -rw-r--r-- | CHANGELOG.md | 3 | ||||
| -rw-r--r-- | pkgs/populate/docs/design.md | 6 | ||||
| -rw-r--r-- | pkgs/populate/src/populate.rs | 56 | ||||
| -rw-r--r-- | pkgs/populate/src/rsync.rs | 117 |
4 files changed, 149 insertions, 33 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 9564113..659b41f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,9 @@ - A `pipe` source with `directory` set makes a directory: its command fills an empty one, mode 0700, and populate delivers what it made, with the modes it has. +- What rsync would transfer of `file` sources gets listed for all of them + at once, before delivering anything, which makes runs with many file + sources with filters and nothing to change much faster. ## 2.0.0 diff --git a/pkgs/populate/docs/design.md b/pkgs/populate/docs/design.md index db8299f..8928809 100644 --- a/pkgs/populate/docs/design.md +++ b/pkgs/populate/docs/design.md @@ -124,7 +124,11 @@ step doesn't rewrite: as far as rsync's quick check goes, neither `break_hard_links` nor rsync runs. Entries populate can't compare, e.g. paths with control characters, devices, or a source that needs `--insecure-links`, make it - run rsync. + run rsync. The listings of the deploying machine, and the runs of rsync + checking the `.rsync-filter` files, run for all such sources at once, + at most 16 at a time, right after the probe, since a run of rsync takes + about 50 ms also with nothing to do; what they find gets checked, with + its messages, at each source's turn. - For symlink sources, the symlink there, if any. If it points to the target already, nothing runs. - For git sources with a commit as ref, and without `fetchAlways`, the 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 { diff --git a/pkgs/populate/src/rsync.rs b/pkgs/populate/src/rsync.rs index 3a28c23..68891c3 100644 --- a/pkgs/populate/src/rsync.rs +++ b/pkgs/populate/src/rsync.rs @@ -102,28 +102,82 @@ pub struct Prepared { /// --list-only, which the check uses itself, if it runs rsync. Not when /// rsync needs --insecure-links, nor for what isn't a directory. pub fn prepare(config: &Config, source_path: &str, listing: bool) -> Result<Prepared> { + finish(config, gather(config, source_path, listing)?) +} + +/// What prepare finds out by running commands, before it decides, and +/// prints, anything, see gather. +pub struct Gathered { + source: String, + directory: bool, + listing_wanted: bool, + /// The version of rsync, and what its --list-only with debugging + /// printed, if it checked the .rsync-filter files that apply. + checked: Option<(String, String)>, + /// The listing, if listing_wanted and populate got it without that. + listing: Option<Listing>, +} + +/// The part of prepare that runs commands, which prints nothing, so it +/// can run for many sources at once, ahead of their turns, see finish. +/// Also with older versions of rsync, a source that's missing, or that +/// contains a directory find can't read, fails here, before rsync runs. +pub fn gather(config: &Config, source_path: &str, listing_wanted: bool) -> Result<Gathered> { let directory = Path::new(source_path).is_dir(); let mut source = source_path.to_string(); if directory { source.push('/'); } - // Also with older versions of rsync, so a source that's missing, or - // that contains a directory find can't read, fails before rsync runs. let filtered = config.filter_files && filtered(&source)?; - let (insecure_links, log) = if filtered { - insecure_links(config, &source)? + let checked = if filtered { + let version = rsync_version()?; + if version_at_least(&version, "3.5.0") { + let log = list_only_debug(config, &source)?; + Some((version, log)) + } else { + None + } } else { - (false, None) + None }; - let listing = if !listing || !directory || insecure_links { + let listing = if !listing_wanted || !directory || checked.is_some() { None - } else if let Some(log) = log { - listed(&log) } else if !filtered && config.exclude.is_empty() && config.filters.is_empty() { found_locally(&source) } else { list_only(config, &source) }; + Ok(Gathered { + source, + directory, + listing_wanted, + checked, + listing, + }) +} + +/// The rest of prepare, at the source's turn: checks the .rsync-filter +/// files with what gather got, which may print warnings, or fail, and +/// takes the listing. +pub fn finish(config: &Config, gathered: Gathered) -> Result<Prepared> { + let Gathered { + source, + directory, + listing_wanted, + checked, + listing, + } = gathered; + let (insecure_links, log) = match checked { + Some((version, log)) => (insecure_links(config, &source, &version, &log)?, Some(log)), + None => (false, None), + }; + let listing = if !listing_wanted || !directory || insecure_links { + None + } else if let Some(log) = log { + listed(&log) + } else { + listing + }; Ok(Prepared { source, insecure_links, @@ -131,6 +185,23 @@ pub fn prepare(config: &Config, source_path: &str, listing: bool) -> Result<Prep }) } +/// What rsync --list-only prints for source with the config's filters, in +/// UTC, and paths as they are, with the debugging insecure_links reads, +/// and what rsync prints on errors. +fn list_only_debug(config: &Config, source: &str) -> Result<String> { + run::output( + Command::new(run::SH) + .env("TZ", "UTC0") + .arg("-c") + .arg(r#"rsync "$@" 2>&1 || :"#) + .arg("sh") + .args(["-8", "--list-only", "--debug=FILTER2"]) + .args(config.filter_args()) + .arg("-rlptD") + .arg(source), + ) +} + /// What rsync --list-only prints for source, with the config's filters, /// in UTC, and paths as they are, or None if it fails. fn list_only(config: &Config, source: &str) -> Option<Listing> { @@ -245,26 +316,10 @@ fn changed_path(line: &str) -> Option<String> { /// config's insecure_links decides: populate fails with an error if it's /// None, syncs without them if it's false, and makes rsync open them like /// the files it transfers with --insecure-links if it's true, both with a -/// warning. Returns whether rsync needs --insecure-links, and, if it -/// asked rsync, what rsync --list-only printed: the listing of what it -/// would transfer, see listed, in UTC, and its debugging lines. -fn insecure_links(config: &Config, source: &str) -> Result<(bool, Option<String>)> { - let version = rsync_version()?; - if !version_at_least(&version, "3.5.0") { - return Ok((false, None)); - } - // Listing what the transfer would send, as far as filtering goes. - let log = run::output( - Command::new(run::SH) - .env("TZ", "UTC0") - .arg("-c") - .arg(r#"rsync "$@" 2>&1 || :"#) - .arg("sh") - .args(["-8", "--list-only", "--debug=FILTER2"]) - .args(config.filter_args()) - .arg("-rlptD") - .arg(source), - )?; +/// warning. log is what rsync --list-only with debugging printed for +/// source, see list_only_debug, and version rsync's. Returns whether rsync +/// needs --insecure-links. +fn insecure_links(config: &Config, source: &str, version: &str, log: &str) -> Result<bool> { let mut parsed = false; let mut ignored = None; for line in log.lines() { @@ -293,7 +348,7 @@ fn insecure_links(config: &Config, source: &str) -> Result<(bool, Option<String> ); } let Some(ignored) = ignored else { - return Ok((false, Some(log))); + return Ok(false); }; match config.insecure_links { None => Err(Failure::Message(format!( @@ -303,13 +358,13 @@ fn insecure_links(config: &Config, source: &str) -> Result<(bool, Option<String> eprintln!( "warning: rsync {version} can't read {ignored}; it reads it with --insecure-links anyway, because insecureLinks is true." ); - Ok((true, Some(log))) + Ok(true) } Some(false) => { eprintln!( "warning: rsync {version} can't read {ignored}, so it syncs {source} without it, because insecureLinks is false." ); - Ok((false, Some(log))) + Ok(false) } } } |
