crates/kleene-ballista/src/rules.rs
//! What the scheduler is told about kleene's operators: the key they keep state under (so
//! a part's every task runs where its graph and side are) and, when the client gave a
//! placement, the executor of each part.
use ballista_scheduler::state::adaptive::AdaptiveRules;
use datafusion::physical_plan::ExecutionPlan;
use crate::exec::{CollectExec, StepExec, preferred_of};
use crate::whole::{AnswerExec, SliceExec};
/// The scheduler's rules for kleene's operators.
#[derive(Debug, Default)]
pub struct KleeneRules;
impl AdaptiveRules for KleeneRules {
fn executor_state_key(&self, plan: &dyn ExecutionPlan) -> Option<String> {
if let Some(s) = plan.downcast_ref::<StepExec>() {
return Some(s.job().key());
}
if let Some(s) = plan.downcast_ref::<SliceExec>() {
return Some(s.job().key());
}
if let Some(a) = plan.downcast_ref::<AnswerExec>() {
return Some(a.job().key());
}
plan.downcast_ref::<CollectExec>().map(|c| c.job().key())
}
fn preferred_executor(&self, key: &str, partition: usize) -> Option<String> {
preferred_of(key, partition)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::exec::placement_key;
use crate::spec::{GraphSpec, Key};
#[test]
fn a_placement_names_one_executor_per_part() {
let spec = GraphSpec::new("/d/g", "v_link_v", Key::Hash(3)).unwrap();
let m = Some(vec![
"oden".to_string(),
"loki".to_string(),
"p1".to_string(),
]);
let key = placement_key(&spec, &m, &[0, 1, 2]);
let r = KleeneRules;
assert_eq!(r.preferred_executor(&key, 0).as_deref(), Some("oden"));
assert_eq!(r.preferred_executor(&key, 2).as_deref(), Some("p1"));
assert_eq!(r.preferred_executor(&key, 3), None);
assert_eq!(
r.preferred_executor(&placement_key(&spec, &None, &[0]), 0),
None
);
assert_eq!(r.preferred_executor("other", 0), None);
}
}