aboutsummaryrefslogtreecommitdiffstats
path: root/pkgs/populate/src
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-12-01 02:35:07 +0000
committertv <tv@krebsco.de>2026-12-01 04:06:02 +0000
commitcf26dc1f928e9839123bedd1693d792bbb418312 (patch)
tree4e38369848754d258c830750a9e71e18ca07e0c7 /pkgs/populate/src
parent9cbe2fab9a3fe30ce4218458112ab46e49c6ac7c (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')
-rw-r--r--pkgs/populate/src/populate.rs56
-rw-r--r--pkgs/populate/src/rsync.rs117
2 files changed, 141 insertions, 32 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 {
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)
}
}
}
[cgit] Unable to lock slot /tmp/cgit/c9100000.lock: Permission denied (13)