Don't allow mutating operations on the underlying graph
Instead of allowing a direct graph return of the underlying graph_flow graph we should return a frozen version instead so that users of the returned value can not mutate the graph without going through the graph_flow pattern (which could have undesired and harmful side-effects if this occurs). Change-Id: I38b35119d6e7bd7387b8ab467eba53aee5500629
This commit is contained in:
@@ -18,6 +18,7 @@
|
|||||||
|
|
||||||
import collections
|
import collections
|
||||||
|
|
||||||
|
import networkx as nx
|
||||||
from networkx.algorithms import dag
|
from networkx.algorithms import dag
|
||||||
from networkx.classes import digraph
|
from networkx.classes import digraph
|
||||||
|
|
||||||
@@ -36,25 +37,47 @@ class Flow(flow.Flow):
|
|||||||
|
|
||||||
def __init__(self, name, uuid=None):
|
def __init__(self, name, uuid=None):
|
||||||
super(Flow, self).__init__(name, uuid)
|
super(Flow, self).__init__(name, uuid)
|
||||||
self._graph = digraph.DiGraph()
|
self._graph = nx.freeze(digraph.DiGraph())
|
||||||
|
|
||||||
|
def _validate(self, graph=None):
|
||||||
|
if graph is None:
|
||||||
|
graph = self._graph
|
||||||
|
# Ensure that there is a valid topological ordering.
|
||||||
|
if not dag.is_directed_acyclic_graph(graph):
|
||||||
|
raise exc.DependencyFailure("No path through the items in the"
|
||||||
|
" graph produces an ordering that"
|
||||||
|
" will allow for correct dependency"
|
||||||
|
" resolution")
|
||||||
|
|
||||||
def link(self, u, v):
|
def link(self, u, v):
|
||||||
if not self._graph.has_node(u):
|
if not self._graph.has_node(u):
|
||||||
raise ValueError('Item %s not found to link from' % (u))
|
raise ValueError('Item %s not found to link from' % (u))
|
||||||
if not self._graph.has_node(v):
|
if not self._graph.has_node(v):
|
||||||
raise ValueError('Item %s not found to link to' % (v))
|
raise ValueError('Item %s not found to link to' % (v))
|
||||||
self._graph.add_edge(u, v)
|
if self._graph.has_edge(u, v):
|
||||||
|
return self
|
||||||
|
|
||||||
# Ensure that there is a valid topological ordering.
|
# NOTE(harlowja): Add an edge to a temporary copy and only if that
|
||||||
if not dag.is_directed_acyclic_graph(self._graph):
|
# copy is valid then do we swap with the underlying graph.
|
||||||
self._graph.remove_edge(u, v)
|
tmp_graph = digraph.DiGraph(self._graph)
|
||||||
raise exc.DependencyFailure("No path through the items in the"
|
tmp_graph.add_edge(u, v)
|
||||||
" graph produces an ordering that"
|
self._swap(tmp_graph)
|
||||||
" will allow for correct dependency"
|
return self
|
||||||
" resolution")
|
|
||||||
|
def _swap(self, replacement_graph):
|
||||||
|
"""Validates the replacement graph and then swaps the underlying graph
|
||||||
|
with a frozen version of the replacement graph (this maintains the
|
||||||
|
invariant that the underlying graph is immutable).
|
||||||
|
"""
|
||||||
|
self._validate(replacement_graph)
|
||||||
|
self._graph = nx.freeze(replacement_graph)
|
||||||
|
|
||||||
def add(self, *items):
|
def add(self, *items):
|
||||||
"""Adds a given task/tasks/flow/flows to this flow."""
|
"""Adds a given task/tasks/flow/flows to this flow."""
|
||||||
|
items = [i for i in items if not self._graph.has_node(i)]
|
||||||
|
if not items:
|
||||||
|
return self
|
||||||
|
|
||||||
requirements = collections.defaultdict(list)
|
requirements = collections.defaultdict(list)
|
||||||
provided = {}
|
provided = {}
|
||||||
|
|
||||||
@@ -67,42 +90,42 @@ class Flow(flow.Flow):
|
|||||||
for value in node.provides:
|
for value in node.provides:
|
||||||
provided[value] = node
|
provided[value] = node
|
||||||
|
|
||||||
try:
|
# NOTE(harlowja): Add items and edges to a temporary copy of the
|
||||||
for item in items:
|
# underlying graph and only if that is successful added to do we then
|
||||||
self._graph.add_node(item)
|
# swap with the underlying graph.
|
||||||
update_requirements(item)
|
tmp_graph = digraph.DiGraph(self._graph)
|
||||||
for value in item.provides:
|
for item in items:
|
||||||
if value in provided:
|
tmp_graph.add_node(item)
|
||||||
raise exc.DependencyFailure(
|
update_requirements(item)
|
||||||
"%(item)s provides %(value)s but is already being"
|
for value in item.provides:
|
||||||
" provided by %(flow)s and duplicate producers"
|
if value in provided:
|
||||||
" are disallowed"
|
raise exc.DependencyFailure(
|
||||||
% dict(item=item.name,
|
"%(item)s provides %(value)s but is already being"
|
||||||
flow=provided[value].name,
|
" provided by %(flow)s and duplicate producers"
|
||||||
value=value))
|
" are disallowed"
|
||||||
provided[value] = item
|
% dict(item=item.name,
|
||||||
|
flow=provided[value].name,
|
||||||
|
value=value))
|
||||||
|
provided[value] = item
|
||||||
|
|
||||||
for value in item.requires:
|
for value in item.requires:
|
||||||
if value in provided:
|
if value in provided:
|
||||||
self.link(provided[value], item)
|
tmp_graph.add_edge(provided[value], item)
|
||||||
|
|
||||||
for value in item.provides:
|
for value in item.provides:
|
||||||
if value in requirements:
|
if value in requirements:
|
||||||
for node in requirements[value]:
|
for node in requirements[value]:
|
||||||
self.link(item, node)
|
tmp_graph.add_edge(item, node)
|
||||||
|
|
||||||
except Exception:
|
|
||||||
self._graph.remove_nodes_from(items)
|
|
||||||
raise
|
|
||||||
|
|
||||||
|
self._swap(tmp_graph)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
return self._graph.number_of_nodes()
|
return self._graph.number_of_nodes()
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
for child in self._graph.nodes_iter():
|
for n in self._graph.nodes_iter():
|
||||||
yield child
|
yield n
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def provides(self):
|
def provides(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user