Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions selene/src/pool/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,12 +489,13 @@ impl Agent {
self.deliver_owed();
if goodbye && let Some(att) = &self.att {
let quiesced = drained && self.xfers.is_empty() && !self.quarantined;
let _ = att
.tx
.send(wire::RankClientMessage {
let _ = tokio::time::timeout(
Duration::from_secs(2),
att.tx.send(wire::RankClientMessage {
m: Some(rc::M::Goodbye(wire::RankGoodbye { quiesced })),
})
.await;
}),
)
.await;
tokio::time::sleep(Duration::from_millis(50)).await;
}
}
Expand Down Expand Up @@ -1712,6 +1713,8 @@ impl Agent {
return;
}
self.quarantined = true;
self.jobs.clear();
self.pending.clear();
let ids: Vec<u64> = self.xfers.keys().copied().collect();
for xfer in ids {
if let Some(run) = self.xfers.remove(&xfer) {
Expand Down
14 changes: 10 additions & 4 deletions selene/src/pool/announce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +134,24 @@ impl Announcer {
self.wake.notify_one();
}

/// A holding moved down the cascade: `to` is added and the `from` holding
/// removed as one step, so a lookup never sees neither.
/// A holding moved down the cascade: `from` is removed before `to` is added
/// so an at-capacity index admits `to` in the freed slot without overflowing.
pub fn spilled(&self, to: IndexEntry, from: Tier) {
let mut state = self.state.lock().unwrap();
let key = to.key;
let added = state.upsert(&to);
let removed = state.remove(key, from);
if added || removed {
let added = state.upsert(&to);
if added {
state.push(Notice::Spilled {
to,
from_tier: from,
});
} else if removed {
state.push(Notice::Evicted {
key,
tier: from,
heir: None,
});
}
drop(state);
self.wake.notify_one();
Expand Down
81 changes: 69 additions & 12 deletions selene/src/pool/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@ pub(crate) fn dial(
let host = address
.rsplit_once(':')
.map_or(address, |(host, _)| host)
.trim_matches(['[', ']'])
.to_string();
Arc::new(net::Tcp {
host,
Expand Down Expand Up @@ -835,10 +836,21 @@ async fn run_session(
let mut closing = false;
let mut close_started = None;
let mut said_goodbye = false;
let mut pending_out = std::collections::VecDeque::new();
let mut stalled_since: Option<Instant> = None;
let mut next_tick = tokio::time::Instant::now() + BUSY_TICK;
loop {
let now = Instant::now();
if !flush(engine, &tx) {
break;
pending_out.extend(engine.take_out());
match drain_out(&mut pending_out, &tx) {
DrainOut::Closed => break,
DrainOut::Drained => stalled_since = None,
DrainOut::Full => {
let since = *stalled_since.get_or_insert(now);
if now.duration_since(since) > CLOSE_TIMEOUT {
break;
}
}
}
if link.stop.load(Ordering::SeqCst) && !closing {
closing = true;
Expand All @@ -849,17 +861,25 @@ async fn run_session(
if close_started.is_some_and(|started| now.duration_since(started) > CLOSE_TIMEOUT) {
break;
}
if closing && engine.quiet() {
if closing && engine.quiet() && pending_out.is_empty() {
engine.goodbye();
flush(engine, &tx);
pending_out.extend(engine.take_out());
let _ = drain_out(&mut pending_out, &tx);
said_goodbye = true;
break;
}
if engine.pump(now) {
// Leave headroom in `tx` for control/fence traffic before pumping bulk resync chunks.
let can_flow = pending_out.is_empty() && tx.capacity() > 64;
if engine.pump_flow(now, can_flow) {
tokio::task::yield_now().await;
continue;
}
let tick = if engine.busy() { BUSY_TICK } else { IDLE_TICK };
let capped = tokio::time::Instant::now() + tick;
if next_tick > capped {
next_tick = capped;
}
let need_permit = !pending_out.is_empty() || tx.capacity() <= 64;
tokio::select! {
message = stream.message() => match message {
Ok(Some(message)) => {
Expand All @@ -874,13 +894,33 @@ async fn run_session(
}
Ok(None) | Err(_) => break,
},
permit = tx.reserve(), if need_permit => {
let Ok(permit) = permit else {
break;
};
if let Some(msg) = pending_out.pop_front() {
permit.send(msg);
stalled_since = None;
}
}
_ = link.wake.notified() => {
let now = Instant::now();
for command in engine.shared.take_commands() {
engine.command(now, command);
}
}
_ = tokio::time::sleep(tick) => engine.tick(Instant::now()),
_ = tokio::time::sleep_until(next_tick) => {
engine.tick(Instant::now());
let next = if engine.busy() { BUSY_TICK } else { IDLE_TICK };
next_tick = tokio::time::Instant::now() + next;
}
}
}
// Return any unsent queued messages to the engine so `session_lost` can preserve
// fence-class messages in `owed` for the next session.
for msg in pending_out {
if let Some(m) = msg.m {
engine.emit(m);
}
}
drop(tx);
Expand All @@ -895,14 +935,31 @@ async fn run_session(
engine.session_lost(Instant::now());
}

/// Send what the engine queued. `false` if the manager is not reading.
fn flush(engine: &mut Engine, tx: &mpsc::Sender<wire::ClientMessage>) -> bool {
for message in engine.take_out() {
if tx.try_send(message).is_err() {
return false;
enum DrainOut {
Drained,
Full,
Closed,
}

/// Send queued outbound frames without dropping the session on transient channel pressure.
fn drain_out(
pending: &mut std::collections::VecDeque<wire::ClientMessage>,
tx: &mpsc::Sender<wire::ClientMessage>,
) -> DrainOut {
while let Some(message) = pending.pop_front() {
match tx.try_send(message) {
Ok(()) => {}
Err(mpsc::error::TrySendError::Full(message)) => {
pending.push_front(message);
return DrainOut::Full;
}
Err(mpsc::error::TrySendError::Closed(message)) => {
pending.push_front(message);
return DrainOut::Closed;
}
}
}
true
DrainOut::Drained
}

#[cfg(test)]
Expand Down
20 changes: 18 additions & 2 deletions selene/src/pool/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,16 @@ impl Engine {
};
self.dial = None;
self.announce.end_session();
self.out.clear();
for msg in self.out.drain(..) {
if let Some(m) = msg.m
&& matches!(
m,
Out::FenceAck(_) | Out::ExportEnd(_) | Out::WindowReleased(_)
)
{
self.owed.push(m);
}
}
if let Some(regions) = sess.regions {
self.draining.push(regions);
}
Expand Down Expand Up @@ -575,13 +584,20 @@ impl Engine {
/// Poll the copier, submit queued jobs, and send resync chunks and notices.
/// Returns whether another call has more to do right away.
pub(crate) fn pump(&mut self, now: Instant) -> bool {
self.pump_flow(now, true)
}

/// Poll the copier and host fillers, optionally pausing index resync/notice
/// emission when the outbound channel has no headroom.
pub(crate) fn pump_flow(&mut self, now: Instant, flow_index: bool) -> bool {
self.collect_net();
self.collect_ranks();
self.collect_outbound_fills();
for (owner, result) in poll_jobs(&self.copier, &mut self.jobs) {
self.copy_done(owner, result);
}
self.submit_pending();
let more = self.flow_index();
let more = if flow_index { self.flow_index() } else { false };
let _ = now;
more
}
Expand Down
7 changes: 7 additions & 0 deletions selene/src/pool/engine/netio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,14 @@ impl Engine {
/// The registered slots, made on first use and kept across sessions. A
/// window never exceeds `max_window_bytes`, except a lone page or row.
pub(crate) fn net_staging(&mut self, limits: &wire::Limits) -> Result<Arc<Staging>> {
let (_, required_bytes) = self.net_shape(limits);
if let Some(staging) = &self.net {
if staging.slot_bytes() < required_bytes {
return Err(Error::new(format!(
"the manager's window size ({required_bytes}) exceeds registered staging ({})",
staging.slot_bytes()
)));
}
return Ok(staging.clone());
}
let (slots, slot_bytes) = self.net_shape(limits);
Expand Down
16 changes: 10 additions & 6 deletions selene/src/pool/engine/requester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -883,12 +883,16 @@ impl Engine {
let failed = result.err();
let restore = claim.restore;
if let Some(error) = failed {
self.fail_restore(
restore,
Some(id),
error,
wire::CopyOutcome::CopyErrorOutcome,
);
let outcome = if error.carried().is_some_and(|cause| {
cause
.downcast_ref::<crate::shared::packed::Checksum>()
.is_some()
}) {
wire::CopyOutcome::CopyChecksum
} else {
wire::CopyOutcome::CopyErrorOutcome
};
self.fail_restore(restore, Some(id), error, outcome);
}
let done = self
.requester
Expand Down
34 changes: 27 additions & 7 deletions selene/src/pool/engine/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ struct Export {
deadline: Instant,
/// The PinAck went out already (a refusal at the deadline).
answered: bool,
/// PinAck(Refused) went out after a partial peer open; skip ExportEnd once peers drain.
refused_on_open: bool,
cancelled: bool,
failed: bool,
lost: bool,
Expand Down Expand Up @@ -146,6 +148,7 @@ impl Engine {
reserved: request.reserved_bytes,
deadline,
answered: false,
refused_on_open: false,
cancelled: false,
failed: false,
lost: false,
Expand Down Expand Up @@ -325,9 +328,23 @@ impl Engine {
};
if let Err(why) = opened {
eprintln!("warning: export {id} refused: {why}");
self.cancel_outbound_peers(super::ranks::XferOwner::Export(id));
self.source.exports.remove(&id);
self.pin_refused(id, wire::ClaimReason::SourceBusy);
let has_opened_peers = self
.source
.exports
.get(&id)
.is_some_and(|run| !run.out.peers.parts.is_empty());
if has_opened_peers {
if let Some(run) = self.source.exports.get_mut(&id) {
run.phase = Phase::Pinned;
run.pin = Some(pin);
run.cancelled = true;
run.refused_on_open = true;
}
self.drive_export(id);
return;
}
self.source.exports.remove(&id);
return release(self, pin);
}
}
Expand Down Expand Up @@ -766,17 +783,20 @@ impl Engine {
wire::ExportEndStatus::ExportReleased
};
let run = self.source.exports.remove(&id).expect("present");
let refused_on_open = run.refused_on_open;
if matches!(run.pin, Some(Pin::Gpu(_))) {
self.event(PoolEvent::ExportEnded {
export: ExportId(id),
});
}
drop(run);
self.emit(Out::ExportEnd(wire::ExportEnd {
export_id: id,
status: status as i32,
fence: wire::FenceOutcome::Drained as i32,
}));
if !refused_on_open {
self.emit(Out::ExportEnd(wire::ExportEnd {
export_id: id,
status: status as i32,
fence: wire::FenceOutcome::Drained as i32,
}));
}
}

/// Collect finished host fills and answer export requests the worker
Expand Down
Loading
Loading