diff --git a/crates/devstack-daemon/src/lanes.rs b/crates/devstack-daemon/src/lanes.rs index 4abd4e61..bb2f4ba9 100644 --- a/crates/devstack-daemon/src/lanes.rs +++ b/crates/devstack-daemon/src/lanes.rs @@ -1219,6 +1219,11 @@ impl Lanes { } fn forget_claimed(&self, name: &str, why: &str) { + // A lane on a node syncs its worktree no more: what goes of it now + // is not the lane's to lose there while the main slot comes back. + if let Some(r) = self.remote(name) { + r.hold_sync(); + } if self.main.served_by().as_deref() == Some(name) && let Err(e) = self.take_main("main") { diff --git a/crates/devstack-daemon/src/remote.rs b/crates/devstack-daemon/src/remote.rs index 4fd9f81d..3cf08eb4 100644 --- a/crates/devstack-daemon/src/remote.rs +++ b/crates/devstack-daemon/src/remote.rs @@ -104,6 +104,8 @@ pub struct RemoteLane { /// The hashes the first sync found, for the syncs that follow (A-4): /// an unchanged file is not read again. hashes: Mutex, + /// Syncs wait: the lane is being removed (its worktree went). + sync_held: std::sync::atomic::AtomicBool, /// Where it says in the lane's state that a daemon here follows it /// ([`FOLLOWED`]), once it is followed. followed: Mutex>, @@ -690,6 +692,20 @@ impl RemoteLane { } = leased; let mut hashes = HashCache::default(); let unwritten: Mutex> = Mutex::new(None); + // The lease lives two minutes unless renewed: a sync of a large tree + // (90 s on a member whose virus scanner checks each file) and the + // start after it take longer. Renewed meanwhile, until the lane is + // followed (its own renewals take over). + let renewing = { + let client = client.clone(); + let lease = offer.id.clone(); + rt.spawn(async move { + loop { + tokio::time::sleep(RENEW_EVERY).await; + let _ = client.renew(&lease).await; + } + }) + }; let started = rt.block_on(async { let t = Instant::now(); step(2, &format!("sync: comparing {} files", list.len())); @@ -765,6 +781,7 @@ impl RemoteLane { ) .await }); + renewing.abort(); let started = match started { Ok(st) => st, Err(e) => { @@ -841,6 +858,7 @@ impl RemoteLane { _watcher: watcher, hashes: Mutex::new(p.hashes), followed: Mutex::new(None), + sync_held: std::sync::atomic::AtomicBool::new(false), }); if lane._watcher.is_none() { // No watcher: every tick counts as a change. @@ -1109,6 +1127,15 @@ impl RemoteLane { if !self.tick(SYNC_EVERY).await { return; } + // Its worktree on its way out (git removes `.git` early), or the + // lane being removed here: what the worktree loses meanwhile is + // not sent, or the lane's builds on the node fail for a lane + // about to go (seen 2026-10-23: six failure notices). + if self.sync_held.load(std::sync::atomic::Ordering::SeqCst) + || !self.worktree.join(".git").exists() + { + continue; + } let changed = self.dirty.swap(false, std::sync::atomic::Ordering::SeqCst); if !changed && full.elapsed() < FULL_SYNC_EVERY { continue; @@ -1127,18 +1154,21 @@ impl RemoteLane { match listed { Ok(Ok((list, elsewhere, fp))) => { if seen.as_ref() != Some(&fp) { - match self - .client - .sync_cached( + let synced = tokio::select! { + r = self.client.sync_cached( &self.lease, &self.worktree, &list, &elsewhere, &mut hashes, &|_, _, _| {}, - ) - .await - { + ) => r, + // Stopped meanwhile: its lease is gone, and the + // sync's failure is no news (a 404 logged as a + // failed sync, seen 2026-10-23). + _ = self.over() => return, + }; + match synced { Ok(s) => { if s.written + s.removed > 0 { crate::diag::info( @@ -1638,6 +1668,12 @@ impl RemoteLane { } } + /// No more syncs: the lane is being removed, and stops soon. + pub fn hold_sync(&self) { + self.sync_held + .store(true, std::sync::atomic::Ordering::SeqCst); + } + /// Stop the lane: the node stops it and removes it with its mirror. pub fn stop(&self, rt: &tokio::runtime::Handle) { self.stopped.send_replace(true); diff --git a/crates/devstack-node/src/nodeclient.rs b/crates/devstack-node/src/nodeclient.rs index 02d34278..e3713ec5 100644 --- a/crates/devstack-node/src/nodeclient.rs +++ b/crates/devstack-node/src/nodeclient.rs @@ -417,7 +417,7 @@ impl NodeClient { .await .map_err(|e| ClientError::Other(e.to_string()))?; *cache = known; - let entries: Vec<(FileEntry, std::path::PathBuf, u64)> = hashed?; + let mut entries: Vec<(FileEntry, std::path::PathBuf, u64)> = hashed?; let hash_ms = t.elapsed().as_millis() as u64; let t = std::time::Instant::now(); let missing: Hashes = self @@ -438,18 +438,43 @@ impl NodeClient { }; let t = std::time::Instant::now(); for h in &missing.hashes { - let Some((_, path, size)) = entries.iter().find(|(e, _, _)| &e.hash == h) else { + let Some(i) = entries.iter().position(|(e, _, _)| &e.hash == h) else { continue; }; - if !self.upload(h, path).await? { - // The file changed since it was hashed: the node refused - // it; the next sync sends what it holds then. - cache.0.remove(path); + let (path, size) = (entries[i].1.clone(), entries[i].2); + if self.upload(h, &path).await? { + stats.uploaded += 1; + stats.bytes += size; + uploaded(stats.uploaded, missing.hashes.len(), stats.bytes); continue; } - stats.uploaded += 1; - stats.bytes += size; - uploaded(stats.uploaded, missing.hashes.len(), stats.bytes); + // It changed, or went, since it was hashed: listed with a blob + // the node does not have, the whole apply would be refused + // (seen 2026-10-23). What it holds now is sent once more; gone + // or changing still, it is left out of this sync (each file with + // that hash), and the next one sends what is there then. + cache.0.remove(&path); + let now = hash_file(&path).ok(); + let sent = match &now { + Some(n) => self.upload(n, &path).await.unwrap_or(false), + None => false, + }; + match now { + Some(n) if sent => { + entries[i].0.hash = n; + stats.uploaded += 1; + stats.bytes += std::fs::metadata(&path).map_or(size, |m| m.len()); + } + _ => { + if path.exists() { + stats.failed.push(format!( + "{}: it changed while it was sent", + entries[i].0.path + )); + } + entries.retain(|(e, _, _)| &e.hash != h); + } + } } stats.upload_ms = t.elapsed().as_millis() as u64; let t = std::time::Instant::now(); @@ -465,7 +490,7 @@ impl NodeClient { stats.apply_ms = t.elapsed().as_millis() as u64; stats.written = applied.written; stats.removed = applied.removed; - stats.failed = applied.failed; + stats.failed.extend(applied.failed); Ok(stats) } diff --git a/crates/devstack-node/tests/node.rs b/crates/devstack-node/tests/node.rs index fa09cb3f..b40a50d6 100644 --- a/crates/devstack-node/tests/node.rs +++ b/crates/devstack-node/tests/node.rs @@ -332,6 +332,58 @@ fn a_lane_is_leased_admitted_and_synced() { .unwrap(); assert_eq!((s3.uploaded, s3.written, s3.removed), (0, 0, 0)); + // A file that changes, or goes, after it was hashed: the sync still + // lands (the changed one with what it holds now), not refused whole + // for a blob it never sent. + let moving = ["src/a.txt", "src/b.txt"]; + for (step, change) in [("changed", true), ("deleted", false)] { + for f in moving { + write(tree.path(), f, &format!("{f} before {step}\n")); + } + let list: Vec = files(tree.path()) + .into_iter() + .chain(moving.iter().map(|f| f.to_string())) + .collect(); + let root = tree.path().to_path_buf(); + let touched = std::sync::atomic::AtomicBool::new(false); + let s = rt + .block_on(client.sync_with(&lease.id, tree.path(), &list, &|_, _, _| { + // After the first upload, the other one is no longer what + // was hashed. + if !touched.swap(true, std::sync::atomic::Ordering::SeqCst) { + for f in moving { + if change { + write(&root, f, &format!("{f} after {step}\n")); + } else { + let _ = std::fs::remove_file(root.join(f)); + } + } + } + })) + .unwrap_or_else(|e| panic!("{step}: {e}")); + assert!(s.failed.is_empty(), "{step}: {:?}", s.failed); + let after: Vec> = moving + .iter() + .map(|f| std::fs::read_to_string(mirror.join(f)).ok()) + .collect(); + if change { + assert!( + after.iter().any(|t| t + .as_deref() + .is_some_and(|t| t.ends_with(" after changed\n"))), + "{after:?}" + ); + assert!(after.iter().all(Option::is_some), "{after:?}"); + } else { + assert!(after.iter().any(Option::is_none), "{after:?}"); + } + for f in moving { + let _ = std::fs::remove_file(tree.path().join(f)); + } + } + rt.block_on(client.sync(&lease.id, tree.path(), &files(tree.path()))) + .unwrap(); + let renewed = rt.block_on(client.renew(&lease.id)).unwrap(); assert!(renewed.expires >= lease.expires); let view = rt.block_on(client.node()).unwrap();