Merge "Avoid unnecessary lazy-loads in mutated_migration_context" into stable/ocata

This commit is contained in:
Zuul 2018-04-19 14:32:36 +00:00 committed by Gerrit Code Review
commit 0578eb8236
2 changed files with 20 additions and 0 deletions

View File

@ -997,6 +997,12 @@ class Instance(base.NovaPersistentObject, base.NovaObject,
context will be saved which can cause incorrect resource tracking, and
should be avoided.
"""
# First check to see if we even have a migration context set and if not
# we can exit early without lazy-loading other attributes.
if 'migration_context' in self and self.migration_context is None:
yield
return
current_values = {}
for attr_name in _MIGRATION_CONTEXT_ATTRS:
current_values[attr_name] = getattr(self, attr_name)

View File

@ -1439,6 +1439,20 @@ class _TestInstanceObject(object):
inst_value = getattr(inst, attr_name)
self.assertIs(expected_objs[attr_name], inst_value)
@mock.patch('nova.objects.Instance.obj_load_attr',
new_callable=mock.NonCallableMock) # asserts not called
def test_mutated_migration_context_early_exit(self, obj_load_attr):
"""Tests that we exit early from mutated_migration_context if the
migration_context attribute is set to None meaning this instance is
not being migrated.
"""
inst = instance.Instance(context=self.context, migration_context=None)
for attr in instance._MIGRATION_CONTEXT_ATTRS:
self.assertNotIn(attr, inst)
with inst.mutated_migration_context():
for attr in instance._MIGRATION_CONTEXT_ATTRS:
self.assertNotIn(attr, inst)
def test_clear_numa_topology(self):
numa_topology = (test_instance_numa_topology.
fake_obj_numa_topology.obj_clone())