Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[engine] rm python graphmaker; create dot formatted display #4295

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/python/pants/engine/subsystem/native_engine_version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
33d19203e5948e2c510aa872df99e2053ad44976
aa91e2c950bf916906836bec8bd28bd4f2408327
18 changes: 8 additions & 10 deletions src/rust/engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ use std::os::raw;
use std::path::Path;
use std::sync::Arc;

use std::fs::OpenOptions;
use std::io::{BufWriter, Write};
use std::fs::File;
use std::io;

use core::{Function, Key, TypeConstraint, TypeId, Value};
Expand Down Expand Up @@ -55,7 +54,7 @@ use graph::Graph;
use nodes::Failure;
use scheduler::{RootResult, Scheduler, ExecutionStat};
use tasks::Tasks;
use rule_graph::{GraphMaker, RootSubjectTypes, RuleGraph};
use rule_graph::{GraphMaker, RuleGraph};

pub struct RawScheduler {
scheduler: Scheduler,
Expand Down Expand Up @@ -438,7 +437,7 @@ pub extern fn validator_run(
with_scheduler(scheduler_ptr, |raw| {
with_vec(subject_types_ptr, subject_types_len as usize, |subject_types| {
let graph_maker = GraphMaker::new(&raw.scheduler.tasks,
RootSubjectTypes { subject_types: subject_types.clone() });
subject_types.clone());
let graph = graph_maker.full_graph();

match graph.validate() {
Expand Down Expand Up @@ -494,7 +493,7 @@ pub extern fn rule_subgraph_visualize(

fn graph_full(raw: &mut RawScheduler, subject_types: &Vec<TypeId>) -> RuleGraph {
let graph_maker = GraphMaker::new(&raw.scheduler.tasks,
RootSubjectTypes { subject_types: subject_types.clone() });
subject_types.clone());
graph_maker.full_graph()
}

Expand All @@ -504,15 +503,14 @@ fn graph_sub(
product_type: TypeConstraint
) -> RuleGraph {
let graph_maker = GraphMaker::new(&raw.scheduler.tasks,
RootSubjectTypes { subject_types: vec![subject_type.clone()] });
vec![subject_type.clone()]);
graph_maker.sub_graph(&subject_type, &product_type)
}

fn write_to_file(path: &Path, graph: &RuleGraph) -> io::Result<()> {
let file = try!(OpenOptions::new().append(true).open(path));
let mut f = BufWriter::new(file);

try!(write!(&mut f, "{}\n", format!("{}", graph)));
let file = File::create(path)?;
let mut f = io::BufWriter::new(file);
graph.visualize(&mut f)?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than:

graph.visualize(&mut f)?;
Ok(())

... you can just return the result of visualize:

graph.visualize(&mut f)

Ok(())
}

Expand Down
67 changes: 30 additions & 37 deletions src/rust/engine/src/rule_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use nodes::Runnable;
use std::collections::{hash_map, HashMap, HashSet, VecDeque};
use std::hash::Hash;
use std::fmt;
use std::io;

use tasks::{Task, Tasks};

Expand Down Expand Up @@ -137,11 +138,6 @@ type RuleDependencyEdges = HashMap<InnerEntry, RuleEdges>;
type RuleDiagnostics = Vec<Diagnostic>;
type UnfulfillableRuleMap = HashMap<Entry, RuleDiagnostics>;

#[derive(Debug)]
pub struct RootSubjectTypes {
pub subject_types: Vec<TypeId>
}

#[derive(Eq, Hash, PartialEq, Clone, Debug)]
pub struct Diagnostic {
subject_type: TypeId,
Expand All @@ -152,11 +148,11 @@ pub struct Diagnostic {
// to be found statically rather than dynamically.
pub struct GraphMaker<'a> {
tasks: &'a Tasks,
root_subject_types: RootSubjectTypes
root_subject_types: Vec<TypeId>
}

impl <'a> GraphMaker<'a> {
pub fn new<'t>(tasks: &'t Tasks, root_subject_types: RootSubjectTypes) -> GraphMaker {
pub fn new<'t>(tasks: &'t Tasks, root_subject_types: Vec<TypeId>) -> GraphMaker {
GraphMaker { tasks: tasks, root_subject_types: root_subject_types }
}

Expand All @@ -165,15 +161,15 @@ impl <'a> GraphMaker<'a> {
let mut full_dependency_edges: RuleDependencyEdges = HashMap::new();
let mut full_unfulfillable_rules: UnfulfillableRuleMap = HashMap::new();

let beginning_root_opt = self.gen_root_entry(subject_type, product_type);
if beginning_root_opt.is_none() {
let beginning_root = if let Some(beginning_root) = self.gen_root_entry(subject_type, product_type) {
beginning_root
} else {
return RuleGraph { root_subject_types: vec![],
root_dependencies: full_root_rule_dependency_edges,
rule_dependency_edges: full_dependency_edges,
unfulfillable_rules: full_unfulfillable_rules,
}
}
let beginning_root = beginning_root_opt.unwrap();
};

let constructed_graph = self._construct_graph(
beginning_root,
Expand All @@ -189,14 +185,15 @@ impl <'a> GraphMaker<'a> {

self.add_unreachable_rule_diagnostics(&full_dependency_edges, &mut full_unfulfillable_rules);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It certainly seems like you should be able to pass these in without cloning them... is the issue just that constructed_graph isn't mutable? let mut constructed_graph = ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. I started to see that when I extracted the method. Thanks for pointing it out.


let unfinished_graph = RuleGraph {
root_subject_types: self.root_subject_types.subject_types.clone(),
let mut unfinished_graph = RuleGraph {
root_subject_types: self.root_subject_types.clone(),
root_dependencies: full_root_rule_dependency_edges,
rule_dependency_edges: full_dependency_edges,
unfulfillable_rules: full_unfulfillable_rules
};

self._remove_unfulfillable_rules_and_dependents(unfinished_graph)
self._remove_unfulfillable_rules_and_dependents(&mut unfinished_graph);
unfinished_graph
}

pub fn full_graph(&self) -> RuleGraph {
Expand All @@ -221,14 +218,15 @@ impl <'a> GraphMaker<'a> {

self.add_unreachable_rule_diagnostics(&full_dependency_edges, &mut full_unfulfillable_rules);

let unfinished_graph = RuleGraph {
root_subject_types: self.root_subject_types.subject_types.clone(),
let mut in_progress_graph = RuleGraph {
root_subject_types: self.root_subject_types.clone(),
root_dependencies: full_root_rule_dependency_edges,
rule_dependency_edges: full_dependency_edges,
unfulfillable_rules: full_unfulfillable_rules
};

self._remove_unfulfillable_rules_and_dependents(unfinished_graph)
self._remove_unfulfillable_rules_and_dependents(&mut in_progress_graph);
in_progress_graph
}

fn add_unreachable_rule_diagnostics(&self, full_dependency_edges: &RuleDependencyEdges, full_unfulfillable_rules: &mut UnfulfillableRuleMap) {
Expand Down Expand Up @@ -427,15 +425,15 @@ impl <'a> GraphMaker<'a> {
}
}
RuleGraph {
root_subject_types: self.root_subject_types.subject_types.clone(),
root_subject_types: self.root_subject_types.clone(),
root_dependencies: root_rule_dependency_edges,
rule_dependency_edges: rule_dependency_edges,
unfulfillable_rules: unfulfillable_rules
}
}

fn _remove_unfulfillable_rules_and_dependents(&self,
mut rule_graph: RuleGraph) -> RuleGraph {
rule_graph: &mut RuleGraph) {
// Removes all unfulfillable rules transitively from the roots and the dependency edges.
//
// Takes the current root rule set and dependency table and removes all rules that are not
Expand Down Expand Up @@ -478,12 +476,11 @@ impl <'a> GraphMaker<'a> {
}
}
}
rule_graph
}

fn gen_root_entries(&self, product_types: &Vec<TypeConstraint>) -> Vec<RootEntry> {
fn gen_root_entries(&self, product_types: &HashSet<TypeConstraint>) -> Vec<RootEntry> {
let mut result: Vec<RootEntry> = Vec::new();
for subj_type in &self.root_subject_types.subject_types {
for subj_type in &self.root_subject_types {
for pt in product_types {
if let Some(entry) = self.gen_root_entry(subj_type, pt) {
result.push(entry);
Expand Down Expand Up @@ -688,26 +685,23 @@ impl RuleGraph {
_ => false,
})
}
}

impl fmt::Display for RuleGraph {
// TODO instead of this, make own fmt thing that accepts externs
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
pub fn visualize(&self, f: &mut io::Write) -> io::Result<()> {
if self.root_dependencies.is_empty() && self.rule_dependency_edges.is_empty() {
try!(f.write_str("digraph {\n"));
try!(f.write_str(" // empty graph\n"));
return f.write_str("}");
write!(f, "digraph {{\n")?;
write!(f, " // empty graph\n")?;
return write!(f, "}}");
}


let mut root_subject_type_strs = self.root_subject_types.iter()
.map(|&t| type_str(t))
.collect::<Vec<String>>();
root_subject_type_strs.sort();
try!(f.write_str("digraph {\n"));
try!(write!(f, " // root subject types: {}\n", root_subject_type_strs
.join(", ")));
try!(f.write_str(" // root entries\n"));
write!(f, "digraph {{\n")?;
write!(f, " // root subject types: {}\n", root_subject_type_strs.join(", "))?;
write!(f, " // root entries\n")?;
let mut root_rule_strs = self.root_dependencies.iter()
.map(|(k, deps)| {
let root_str = entry_str(&Entry::from(k.clone()));
Expand All @@ -721,20 +715,19 @@ impl fmt::Display for RuleGraph {
})
.collect::<Vec<String>>();
root_rule_strs.sort();
try!(write!(f, "{}\n", root_rule_strs.join("\n")));
write!(f, "{}\n", root_rule_strs.join("\n"))?;


try!(f.write_str(" // internal entries\n"));
write!(f, " // internal entries\n")?;
let mut internal_rule_strs = self.rule_dependency_edges.iter()
.map(|(k, deps)| format!(" \"{}\" -> {{{}}}", entry_str(&Entry::from(k.clone())), deps.dependencies.iter()
.map(|d| format!("\"{}\"", entry_str(d)))
.collect::<Vec<String>>()
.join(" ")))
.collect::<Vec<String>>();
internal_rule_strs.sort();
try!(write!(f, "{}\n", internal_rule_strs.join("\n")));

f.write_str("}")
write!(f, "{}\n", internal_rule_strs.join("\n"))?;
write!(f, "}}")
}
}

Expand Down
11 changes: 4 additions & 7 deletions src/rust/engine/src/tasks.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2017 Pants project contributors (see CONTRIBUTORS.md).
// Licensed under the Apache License, Version 2.0 (see LICENSE).

use std::collections::HashMap;
use std::collections::{HashMap, HashSet};

use core::{Field, Function, FNV, Key, TypeConstraint, TypeId};
use selectors::{Selector, Select, SelectDependencies, SelectLiteral, SelectProjection};
Expand Down Expand Up @@ -70,12 +70,9 @@ impl Tasks {
}
}

pub fn all_product_types(&self) -> Vec<TypeConstraint> {
let mut product_types: Vec<_> = self.all_rules().iter().map(|t| t.product).collect();
// NB sorted by id so that dedup will consolidate runs of duplicates.
product_types.sort_by_key(|tc| tc.0);
product_types.dedup();
product_types
pub fn all_product_types(&self) -> HashSet<TypeConstraint> {
self.all_rules().iter().map(|t| t.product)
.collect::<HashSet<_>>()
}

pub fn is_singleton_task(&self, sought_task: &Task) -> bool {
Expand Down