use crate::types::{NodeId, OnFailure, TargetGraph};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum State {
Pending,
Started,
Succeeded,
Failed,
Skipped,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Finish {
Succeeded,
Failed,
}
#[derive(Debug, Default, PartialEq, Eq)]
pub(crate) struct Effects {
pub(crate) started: Vec<NodeId>,
pub(crate) skipped: Vec<NodeId>,
}
#[derive(Debug)]
pub(crate) struct Schedule {
states: Vec<State>,
waiting: Vec<usize>,
on_failure: OnFailure,
stopped: Option<NodeId>,
}
impl Schedule {
pub(crate) fn new(graph: &TargetGraph, on_failure: OnFailure) -> Self {
let waiting: Vec<usize> = (0_u32..)
.take(graph.len())
.map(|id| graph.deps(NodeId::new(id)).map_or(0, <[NodeId]>::len))
.collect();
Self {
states: vec![State::Pending; waiting.len()],
waiting,
on_failure,
stopped: None,
}
}
pub(crate) fn start(&mut self) -> Vec<NodeId> {
let mut started = Vec::new();
let ids = (0_u32..).map(NodeId::new);
for ((state, count), id) in self.states.iter_mut().zip(&self.waiting).zip(ids) {
if *state == State::Pending && *count == 0 {
*state = State::Started;
started.push(id);
}
}
started
}
pub(crate) fn complete(
&mut self,
graph: &TargetGraph,
node: NodeId,
finish: Finish,
) -> Effects {
match self.states.get_mut(node.index()) {
Some(state) if *state == State::Started => {
*state = match finish {
Finish::Succeeded => State::Succeeded,
Finish::Failed => State::Failed,
};
}
_ => return Effects::default(),
}
match (finish, self.on_failure) {
(Finish::Succeeded, _) if self.stopped.is_some() => Effects::default(),
(Finish::Succeeded, _) => Effects {
started: self.release(graph, node),
skipped: Vec::new(),
},
(Finish::Failed, OnFailure::Stop) => Effects {
started: Vec::new(),
skipped: self.stop(node),
},
(Finish::Failed, OnFailure::KeepGoing) => Effects {
started: Vec::new(),
skipped: self.skip_from(graph, node),
},
}
}
pub(crate) fn abandon(&mut self, node: NodeId) {
if let Some(state) = self.states.get_mut(node.index())
&& *state == State::Started
{
*state = State::Skipped;
}
}
pub(crate) const fn stopped(&self) -> Option<NodeId> {
self.stopped
}
fn release(&mut self, graph: &TargetGraph, node: NodeId) -> Vec<NodeId> {
let mut started = Vec::new();
for dependent in graph.dependents(node).unwrap_or_default() {
let index = dependent.index();
let (Some(count), Some(state)) =
(self.waiting.get_mut(index), self.states.get_mut(index))
else {
continue;
};
*count = count.saturating_sub(1);
if *count == 0 && *state == State::Pending {
*state = State::Started;
started.push(*dependent);
}
}
started.sort_unstable();
started
}
fn stop(&mut self, failed: NodeId) -> Vec<NodeId> {
if self.stopped.is_none() {
self.stopped = Some(failed);
}
let mut skipped = Vec::new();
let ids = (0_u32..).map(NodeId::new);
for (state, id) in self.states.iter_mut().zip(ids) {
if *state == State::Pending {
*state = State::Skipped;
skipped.push(id);
}
}
skipped
}
fn skip_from(&mut self, graph: &TargetGraph, failed: NodeId) -> Vec<NodeId> {
let mut skipped = Vec::new();
let mut stack = vec![failed];
while let Some(node) = stack.pop() {
for dependent in graph.dependents(node).unwrap_or_default() {
if let Some(state) = self.states.get_mut(dependent.index())
&& *state == State::Pending
{
*state = State::Skipped;
skipped.push(*dependent);
stack.push(*dependent);
}
}
}
skipped.sort_unstable();
skipped
}
}
#[cfg(test)]
mod tests {
use super::{Effects, Finish, Schedule};
use crate::execute::fixtures::{graph, node};
use crate::types::{NodeId, OnFailure, TargetGraph};
fn ids(raw: &[u32]) -> Vec<NodeId> {
raw.iter().copied().map(NodeId::new).collect()
}
fn effects(started: &[u32], skipped: &[u32]) -> Effects {
Effects {
started: ids(started),
skipped: ids(skipped),
}
}
fn diamond() -> TargetGraph {
graph(
vec![
node("//:a", |_| {}),
node("//:b", |_| {}),
node("//:c", |_| {}),
node("//:d", |_| {}),
],
&[&[1, 2], &[3], &[3], &[]],
)
}
fn two_chains() -> TargetGraph {
graph(
vec![
node("//:a", |_| {}),
node("//:b", |_| {}),
node("//:c", |_| {}),
node("//:x", |_| {}),
node("//:y", |_| {}),
],
&[&[], &[0], &[1], &[], &[3]],
)
}
#[test]
fn an_empty_graph_starts_nothing() {
let graph = graph(Vec::new(), &[]);
let mut schedule = Schedule::new(&graph, OnFailure::Stop);
assert_eq!(schedule.start(), []);
assert_eq!(schedule.stopped(), None);
}
#[test]
fn a_target_starts_once_everything_it_depends_on_has_succeeded() {
let graph = diamond();
let mut schedule = Schedule::new(&graph, OnFailure::Stop);
assert_eq!(schedule.start(), ids(&[3]));
assert_eq!(
schedule.complete(&graph, NodeId::new(3), Finish::Succeeded),
effects(&[1, 2], &[])
);
assert_eq!(
schedule.complete(&graph, NodeId::new(2), Finish::Succeeded),
effects(&[], &[])
);
assert_eq!(
schedule.complete(&graph, NodeId::new(1), Finish::Succeeded),
effects(&[0], &[])
);
assert_eq!(
schedule.complete(&graph, NodeId::new(0), Finish::Succeeded),
effects(&[], &[])
);
assert_eq!(schedule.stopped(), None);
}
#[test]
fn nothing_starts_twice() {
let graph = two_chains();
let mut schedule = Schedule::new(&graph, OnFailure::KeepGoing);
assert_eq!(schedule.start(), ids(&[0, 3]));
assert_eq!(schedule.start(), []);
assert_eq!(
schedule.complete(&graph, NodeId::new(0), Finish::Succeeded),
effects(&[1], &[])
);
assert_eq!(
schedule.complete(&graph, NodeId::new(0), Finish::Succeeded),
effects(&[], &[]),
"a target that already finished"
);
assert_eq!(schedule.start(), []);
}
#[test]
fn a_target_that_was_not_started_cannot_finish() {
let graph = diamond();
let mut schedule = Schedule::new(&graph, OnFailure::KeepGoing);
assert_eq!(
schedule.complete(&graph, NodeId::new(3), Finish::Failed),
effects(&[], &[]),
"before anything started"
);
assert_eq!(
schedule.complete(&graph, NodeId::new(9), Finish::Succeeded),
effects(&[], &[]),
"an id the graph did not issue"
);
assert_eq!(schedule.start(), ids(&[3]));
}
#[test]
fn a_failure_stops_the_build_when_told_to_stop() {
let graph = two_chains();
let mut schedule = Schedule::new(&graph, OnFailure::Stop);
assert_eq!(schedule.start(), ids(&[0, 3]));
assert_eq!(schedule.stopped(), None);
assert_eq!(
schedule.complete(&graph, NodeId::new(0), Finish::Failed),
effects(&[], &[1, 2, 4]),
"every target still waiting is skipped, downstream of the failure or not"
);
assert_eq!(schedule.stopped(), Some(NodeId::new(0)));
}
#[test]
fn a_success_after_the_build_stopped_starts_nothing() {
let graph = two_chains();
let mut schedule = Schedule::new(&graph, OnFailure::Stop);
assert_eq!(schedule.start(), ids(&[0, 3]));
assert_eq!(
schedule.complete(&graph, NodeId::new(0), Finish::Failed),
effects(&[], &[1, 2, 4])
);
assert_eq!(
schedule.complete(&graph, NodeId::new(3), Finish::Succeeded),
effects(&[], &[])
);
assert_eq!(schedule.start(), []);
}
#[test]
fn a_stopped_build_remembers_its_first_failure() {
let graph = two_chains();
let mut schedule = Schedule::new(&graph, OnFailure::Stop);
assert_eq!(schedule.start(), ids(&[0, 3]));
assert_eq!(
schedule.complete(&graph, NodeId::new(3), Finish::Failed),
effects(&[], &[1, 2, 4])
);
assert_eq!(
schedule.complete(&graph, NodeId::new(0), Finish::Failed),
effects(&[], &[])
);
assert_eq!(schedule.stopped(), Some(NodeId::new(3)));
}
#[test]
fn a_failure_skips_only_what_depends_on_it_when_told_to_keep_going() {
let graph = two_chains();
let mut schedule = Schedule::new(&graph, OnFailure::KeepGoing);
assert_eq!(schedule.start(), ids(&[0, 3]));
assert_eq!(
schedule.complete(&graph, NodeId::new(0), Finish::Failed),
effects(&[], &[1, 2]),
"the target that depends on it, and the one that depends on that"
);
assert_eq!(schedule.stopped(), None);
assert_eq!(
schedule.complete(&graph, NodeId::new(3), Finish::Succeeded),
effects(&[4], &[]),
"the rest of the build goes on"
);
}
#[test]
fn a_second_failure_skips_only_what_was_still_waiting() {
let graph = diamond();
let mut schedule = Schedule::new(&graph, OnFailure::KeepGoing);
assert_eq!(schedule.start(), ids(&[3]));
assert_eq!(
schedule.complete(&graph, NodeId::new(3), Finish::Succeeded),
effects(&[1, 2], &[])
);
assert_eq!(
schedule.complete(&graph, NodeId::new(1), Finish::Failed),
effects(&[], &[0])
);
assert_eq!(
schedule.complete(&graph, NodeId::new(2), Finish::Failed),
effects(&[], &[]),
"the one target downstream was skipped by the first failure"
);
assert_eq!(schedule.stopped(), None);
}
#[test]
fn a_skipped_target_does_not_start_when_its_other_dependency_succeeds() {
let graph = diamond();
let mut schedule = Schedule::new(&graph, OnFailure::KeepGoing);
assert_eq!(schedule.start(), ids(&[3]));
assert_eq!(
schedule.complete(&graph, NodeId::new(3), Finish::Succeeded),
effects(&[1, 2], &[])
);
assert_eq!(
schedule.complete(&graph, NodeId::new(1), Finish::Failed),
effects(&[], &[0])
);
assert_eq!(
schedule.complete(&graph, NodeId::new(2), Finish::Succeeded),
effects(&[], &[])
);
}
#[test]
fn an_abandoned_target_is_skipped_and_releases_nothing() {
let graph = two_chains();
let mut schedule = Schedule::new(&graph, OnFailure::KeepGoing);
assert_eq!(schedule.start(), ids(&[0, 3]));
schedule.abandon(NodeId::new(0));
assert_eq!(
schedule.complete(&graph, NodeId::new(0), Finish::Succeeded),
effects(&[], &[]),
"an abandoned target is no longer started, so it cannot finish"
);
assert_eq!(schedule.start(), []);
}
#[test]
fn only_a_started_target_can_be_abandoned() {
let graph = two_chains();
let mut schedule = Schedule::new(&graph, OnFailure::KeepGoing);
assert_eq!(schedule.start(), ids(&[0, 3]));
schedule.abandon(NodeId::new(1));
schedule.abandon(NodeId::new(9));
assert_eq!(
schedule.complete(&graph, NodeId::new(0), Finish::Succeeded),
effects(&[1], &[]),
"the pending target was left pending"
);
}
#[test]
fn started_targets_are_abandoned_once_the_build_has_stopped() {
let graph = graph(
vec![
node("//:a", |_| {}),
node("//:b", |_| {}),
node("//:c", |_| {}),
],
&[&[], &[], &[]],
);
let mut schedule = Schedule::new(&graph, OnFailure::Stop);
assert_eq!(schedule.start(), ids(&[0, 1, 2]));
assert_eq!(
schedule.complete(&graph, NodeId::new(0), Finish::Failed),
effects(&[], &[]),
"nothing was pending, so the schedule itself skips nothing"
);
schedule.abandon(NodeId::new(1));
schedule.abandon(NodeId::new(2));
assert_eq!(
schedule.complete(&graph, NodeId::new(1), Finish::Failed),
effects(&[], &[])
);
assert_eq!(schedule.stopped(), Some(NodeId::new(0)));
}
#[test]
fn a_long_chain_is_skipped_without_recursion() {
let length = 100_000_u32;
let nodes = (0..length)
.map(|index| node(&format!("//:n{index:06}"), |_| {}))
.collect();
let lists: Vec<Vec<u32>> = (0..length)
.map(|index| {
if index + 1 < length {
vec![index + 1]
} else {
Vec::new()
}
})
.collect();
let edges: Vec<&[u32]> = lists.iter().map(Vec::as_slice).collect();
let graph = graph(nodes, &edges);
let last = NodeId::new(length - 1);
let mut schedule = Schedule::new(&graph, OnFailure::KeepGoing);
assert_eq!(schedule.start(), [last]);
let effects = schedule.complete(&graph, last, Finish::Failed);
assert_eq!(effects.started, []);
assert_eq!(effects.skipped.len(), 99_999);
assert_eq!(effects.skipped.first(), Some(&NodeId::new(0)));
assert_eq!(effects.skipped.last(), Some(&NodeId::new(length - 2)));
assert!(effects.skipped.windows(2).all(|pair| pair[0] < pair[1]));
}
}