Merge "Updated graph model to use attr_dict"

This commit is contained in:
Jenkins
2017-02-02 17:25:52 +00:00
committed by Gerrit Code Review
6 changed files with 48 additions and 40 deletions

View File

@@ -402,11 +402,15 @@ class WildcardCharacterIsUsed(WatcherException):
# Model # Model
class InstanceNotFound(WatcherException): class ComputeResourceNotFound(WatcherException):
msg_fmt = _("The compute resource '%(name)s' could not be found")
class InstanceNotFound(ComputeResourceNotFound):
msg_fmt = _("The instance '%(name)s' could not be found") msg_fmt = _("The instance '%(name)s' could not be found")
class ComputeNodeNotFound(WatcherException): class ComputeNodeNotFound(ComputeResourceNotFound):
msg_fmt = _("The compute node %(name)s could not be found") msg_fmt = _("The compute node %(name)s could not be found")

View File

@@ -30,7 +30,8 @@ LOG = log.getLogger(__name__)
@six.add_metaclass(abc.ABCMeta) @six.add_metaclass(abc.ABCMeta)
class Element(base.WatcherObject, base.WatcherObjectDictCompat): class Element(base.WatcherObject, base.WatcherObjectDictCompat,
base.WatcherComparableObject):
# Initial version # Initial version
VERSION = '1.0' VERSION = '1.0'

View File

@@ -57,13 +57,13 @@ class ModelRoot(nx.DiGraph, base.Model):
@lockutils.synchronized("model_root") @lockutils.synchronized("model_root")
def add_node(self, node): def add_node(self, node):
self.assert_node(node) self.assert_node(node)
super(ModelRoot, self).add_node(node) super(ModelRoot, self).add_node(node.uuid, node)
@lockutils.synchronized("model_root") @lockutils.synchronized("model_root")
def remove_node(self, node): def remove_node(self, node):
self.assert_node(node) self.assert_node(node)
try: try:
super(ModelRoot, self).remove_node(node) super(ModelRoot, self).remove_node(node.uuid)
except nx.NetworkXError as exc: except nx.NetworkXError as exc:
LOG.exception(exc) LOG.exception(exc)
raise exception.ComputeNodeNotFound(name=node.uuid) raise exception.ComputeNodeNotFound(name=node.uuid)
@@ -72,7 +72,7 @@ class ModelRoot(nx.DiGraph, base.Model):
def add_instance(self, instance): def add_instance(self, instance):
self.assert_instance(instance) self.assert_instance(instance)
try: try:
super(ModelRoot, self).add_node(instance) super(ModelRoot, self).add_node(instance.uuid, instance)
except nx.NetworkXError as exc: except nx.NetworkXError as exc:
LOG.exception(exc) LOG.exception(exc)
raise exception.InstanceNotFound(name=instance.uuid) raise exception.InstanceNotFound(name=instance.uuid)
@@ -80,7 +80,7 @@ class ModelRoot(nx.DiGraph, base.Model):
@lockutils.synchronized("model_root") @lockutils.synchronized("model_root")
def remove_instance(self, instance): def remove_instance(self, instance):
self.assert_instance(instance) self.assert_instance(instance)
super(ModelRoot, self).remove_node(instance) super(ModelRoot, self).remove_node(instance.uuid)
@lockutils.synchronized("model_root") @lockutils.synchronized("model_root")
def map_instance(self, instance, node): def map_instance(self, instance, node):
@@ -98,7 +98,7 @@ class ModelRoot(nx.DiGraph, base.Model):
self.assert_node(node) self.assert_node(node)
self.assert_instance(instance) self.assert_instance(instance)
self.add_edge(instance, node) self.add_edge(instance.uuid, node.uuid)
@lockutils.synchronized("model_root") @lockutils.synchronized("model_root")
def unmap_instance(self, instance, node): def unmap_instance(self, instance, node):
@@ -107,7 +107,7 @@ class ModelRoot(nx.DiGraph, base.Model):
if isinstance(node, six.string_types): if isinstance(node, six.string_types):
node = self.get_node_by_uuid(node) node = self.get_node_by_uuid(node)
self.remove_edge(instance, node) self.remove_edge(instance.uuid, node.uuid)
def delete_instance(self, instance, node=None): def delete_instance(self, instance, node=None):
self.assert_instance(instance) self.assert_instance(instance)
@@ -130,55 +130,59 @@ class ModelRoot(nx.DiGraph, base.Model):
return False return False
# unmap # unmap
self.remove_edge(instance, source_node) self.remove_edge(instance.uuid, source_node.uuid)
# map # map
self.add_edge(instance, destination_node) self.add_edge(instance.uuid, destination_node.uuid)
return True return True
@lockutils.synchronized("model_root") @lockutils.synchronized("model_root")
def get_all_compute_nodes(self): def get_all_compute_nodes(self):
return {cn.uuid: cn for cn in self.nodes() return {uuid: cn for uuid, cn in self.nodes(data=True)
if isinstance(cn, element.ComputeNode)} if isinstance(cn, element.ComputeNode)}
@lockutils.synchronized("model_root") @lockutils.synchronized("model_root")
def get_node_by_uuid(self, uuid): def get_node_by_uuid(self, uuid):
for graph_node in self.nodes(): try:
if (isinstance(graph_node, element.ComputeNode) and return self._get_by_uuid(uuid)
graph_node.uuid == uuid): except exception.ComputeResourceNotFound:
return graph_node raise exception.ComputeNodeNotFound(name=uuid)
raise exception.ComputeNodeNotFound(name=uuid)
@lockutils.synchronized("model_root") @lockutils.synchronized("model_root")
def get_instance_by_uuid(self, uuid): def get_instance_by_uuid(self, uuid):
return self._get_instance_by_uuid(uuid) try:
return self._get_by_uuid(uuid)
except exception.ComputeResourceNotFound:
raise exception.InstanceNotFound(name=uuid)
def _get_instance_by_uuid(self, uuid): def _get_by_uuid(self, uuid):
for graph_node in self.nodes(): try:
if (isinstance(graph_node, element.Instance) and return self.node[uuid]
graph_node.uuid == str(uuid)): except Exception as exc:
return graph_node LOG.exception(exc)
raise exception.InstanceNotFound(name=uuid) raise exception.ComputeResourceNotFound(name=uuid)
@lockutils.synchronized("model_root") @lockutils.synchronized("model_root")
def get_node_by_instance_uuid(self, instance_uuid): def get_node_by_instance_uuid(self, instance_uuid):
instance = self._get_instance_by_uuid(instance_uuid) instance = self._get_by_uuid(instance_uuid)
for node in self.neighbors(instance): for node_uuid in self.neighbors(instance.uuid):
node = self._get_by_uuid(node_uuid)
if isinstance(node, element.ComputeNode): if isinstance(node, element.ComputeNode):
return node return node
raise exception.ComputeNodeNotFound(name=instance_uuid) raise exception.ComputeNodeNotFound(name=instance_uuid)
@lockutils.synchronized("model_root") @lockutils.synchronized("model_root")
def get_all_instances(self): def get_all_instances(self):
return {inst.uuid: inst for inst in self.nodes() return {uuid: inst for uuid, inst in self.nodes(data=True)
if isinstance(inst, element.Instance)} if isinstance(inst, element.Instance)}
@lockutils.synchronized("model_root") @lockutils.synchronized("model_root")
def get_node_instances(self, node): def get_node_instances(self, node):
self.assert_node(node) self.assert_node(node)
node_instances = [] node_instances = []
for neighbor in self.predecessors(node): for instance_uuid in self.predecessors(node.uuid):
if isinstance(neighbor, element.Instance): instance = self._get_by_uuid(instance_uuid)
node_instances.append(neighbor) if isinstance(instance, element.Instance):
node_instances.append(instance)
return node_instances return node_instances

View File

@@ -169,7 +169,7 @@ class DefaultScope(base.BaseScope):
try: try:
node_name = cluster_model.get_node_by_instance_uuid( node_name = cluster_model.get_node_by_instance_uuid(
instance_uuid).uuid instance_uuid).uuid
except exception.InstanceNotFound: except exception.ComputeResourceNotFound:
LOG.warning(_LW("The following instance %s cannot be found. " LOG.warning(_LW("The following instance %s cannot be found. "
"It might be deleted from CDM along with node" "It might be deleted from CDM along with node"
" instance was hosted on."), " instance was hosted on."),

View File

@@ -92,6 +92,10 @@ class WatcherObjectDictCompat(ovo_base.VersionedObjectDictCompat):
pass pass
class WatcherComparableObject(ovo_base.ComparableVersionedObject):
pass
class WatcherPersistentObject(object): class WatcherPersistentObject(object):
"""Mixin class for Persistent objects. """Mixin class for Persistent objects.

View File

@@ -42,10 +42,8 @@ class TestDefaultScope(base.TestCase):
for i in range(2)] for i in range(2)]
model = default.DefaultScope(audit_scope, model = default.DefaultScope(audit_scope,
osc=mock.Mock()).get_scoped_model(cluster) osc=mock.Mock()).get_scoped_model(cluster)
expected_edges = [('INSTANCE_2', 'Node_1')] expected_edges = [('INSTANCE_2', 'Node_1')]
edges = [(src.uuid, dst.uuid) for src, dst in model.edges()] self.assertEqual(sorted(expected_edges), sorted(model.edges()))
self.assertEqual(sorted(expected_edges), sorted(edges))
@mock.patch.object(nova_helper.NovaHelper, 'get_availability_zone_list') @mock.patch.object(nova_helper.NovaHelper, 'get_availability_zone_list')
def test_get_scoped_model_without_scope(self, mock_zone_list): def test_get_scoped_model_without_scope(self, mock_zone_list):
@@ -67,8 +65,7 @@ class TestDefaultScope(base.TestCase):
('INSTANCE_6', 'Node_3'), ('INSTANCE_6', 'Node_3'),
('INSTANCE_7', 'Node_4'), ('INSTANCE_7', 'Node_4'),
] ]
edges = [(src.uuid, dst.uuid) for src, dst in model.edges()] self.assertEqual(sorted(expected_edges), sorted(model.edges()))
self.assertEqual(sorted(expected_edges), sorted(edges))
@mock.patch.object(nova_helper.NovaHelper, 'get_aggregate_detail') @mock.patch.object(nova_helper.NovaHelper, 'get_aggregate_detail')
@mock.patch.object(nova_helper.NovaHelper, 'get_aggregate_list') @mock.patch.object(nova_helper.NovaHelper, 'get_aggregate_list')
@@ -199,8 +196,7 @@ class TestDefaultScope(base.TestCase):
('INSTANCE_1', 'Node_0'), ('INSTANCE_1', 'Node_0'),
('INSTANCE_6', 'Node_3'), ('INSTANCE_6', 'Node_3'),
('INSTANCE_7', 'Node_4')] ('INSTANCE_7', 'Node_4')]
edges = [(src.uuid, dst.uuid) for src, dst in model.edges()] self.assertEqual(sorted(expected_edges), sorted(model.edges()))
self.assertEqual(sorted(expected_edges), sorted(edges))
def test_remove_instances_from_model(self): def test_remove_instances_from_model(self):
model = self.fake_cluster.generate_scenario_1() model = self.fake_cluster.generate_scenario_1()
@@ -213,5 +209,4 @@ class TestDefaultScope(base.TestCase):
('INSTANCE_5', 'Node_2'), ('INSTANCE_5', 'Node_2'),
('INSTANCE_6', 'Node_3'), ('INSTANCE_6', 'Node_3'),
('INSTANCE_7', 'Node_4')] ('INSTANCE_7', 'Node_4')]
edges = [(src.uuid, dst.uuid) for src, dst in model.edges()] self.assertEqual(sorted(expected_edges), sorted(model.edges()))
self.assertEqual(sorted(expected_edges), sorted(edges))