Fix up some bits of resource_tracker to use instance flavors

This cleans up ResourceTracker and associated tests to use flavors
attached to the instance instead of relying on compatibility
interfaces.

Change-Id: Ibe1448b59ca370b260e8021c449a2c8c8b4f1c17
This commit is contained in:
Dan Smith 2015-06-15 12:41:46 -07:00
parent ec49267c48
commit d055f3bda5
3 changed files with 42 additions and 65 deletions

View File

@ -26,7 +26,6 @@ from oslo_serialization import jsonutils
from oslo_utils import importutils
from nova.compute import claims
from nova.compute import flavors
from nova.compute import monitors
from nova.compute import resources as ext_resources
from nova.compute import task_states
@ -177,18 +176,17 @@ class ResourceTracker(object):
"MB", {'flavor': instance_type['memory_mb'],
'overhead': overhead['memory_mb']})
instance_ref = instance_obj.compat_instance(instance)
claim = claims.ResizeClaim(context, instance_ref, instance_type,
claim = claims.ResizeClaim(context, instance, instance_type,
image_meta, self, self.compute_node,
overhead=overhead, limits=limits)
migration = self._create_migration(context, instance_ref,
migration = self._create_migration(context, instance,
instance_type)
claim.migration = migration
# Mark the resources in-use for the resize landing on this
# compute host:
self._update_usage_from_migration(context, instance_ref, image_meta,
self._update_usage_from_migration(context, instance, image_meta,
migration)
elevated = context.elevated()
self._update(elevated)
@ -200,17 +198,16 @@ class ResourceTracker(object):
be done while the COMPUTE_RESOURCES_SEMAPHORE is held so the resource
claim will not be lost if the audit process starts.
"""
old_instance_type = flavors.extract_flavor(instance)
migration = objects.Migration(context=context.elevated())
migration.dest_compute = self.host
migration.dest_node = self.nodename
migration.dest_host = self.driver.get_host_ip_addr()
migration.old_instance_type_id = old_instance_type['id']
migration.old_instance_type_id = instance.flavor.id
migration.new_instance_type_id = instance_type['id']
migration.status = 'pre-migrating'
migration.instance_uuid = instance['uuid']
migration.source_compute = instance['host']
migration.source_node = instance['node']
migration.instance_uuid = instance.uuid
migration.source_compute = instance.host
migration.source_node = instance.node
migration.migration_type = (
migration.old_instance_type_id != migration.new_instance_type_id
and 'resize' or 'migration')
@ -886,18 +883,8 @@ class ResourceTracker(object):
def _get_instance_type(self, context, instance, prefix,
instance_type_id=None):
"""Get the instance type from sys metadata if it's stashed. If not,
fall back to fetching it via the object API.
See bug 1164110
"""
try:
extracted_flavor = flavors.extract_flavor(instance, prefix)
except KeyError:
if not instance_type_id:
instance_type_id = instance['instance_type_id']
return objects.Flavor.get_by_id(context, instance_type_id)
return extracted_flavor
"""Get the instance type from instance."""
return getattr(instance, '%sflavor' % prefix)
def _get_usage_dict(self, object_or_dict, **updates):
"""Make a usage dict _update methods expect.

View File

@ -568,7 +568,7 @@ class UnsupportedDriverTestCase(BaseTestCase):
self.tracker.update_usage(self.context, instance)
def test_disabled_resize_claim(self):
instance = self._fake_instance()
instance = self._fake_instance_obj()
instance_type = self._fake_flavor_create()
claim = self.tracker.resize_claim(self.context, instance,
instance_type)
@ -578,7 +578,7 @@ class UnsupportedDriverTestCase(BaseTestCase):
claim.migration['new_instance_type_id'])
def test_disabled_resize_context_claim(self):
instance = self._fake_instance()
instance = self._fake_instance_obj()
instance_type = self._fake_flavor_create()
with self.tracker.resize_claim(self.context, instance, instance_type) \
as claim:
@ -1218,7 +1218,7 @@ class ResizeClaimTestCase(BaseTrackerTestCase):
def setUp(self):
super(ResizeClaimTestCase, self).setUp()
self.instance = self._fake_instance()
self.instance = self._fake_instance_obj()
self.instance_type = self._fake_flavor_create()
@mock.patch('nova.objects.InstancePCIRequests.get_by_instance_uuid',
@ -1256,7 +1256,7 @@ class ResizeClaimTestCase(BaseTrackerTestCase):
2 * FAKE_VIRT_VCPUS)
self.tracker.resize_claim(self.context, self.instance,
self.instance_type, limits)
instance2 = self._fake_instance()
instance2 = self._fake_instance_obj()
self.tracker.resize_claim(self.context, instance2, self.instance_type,
limits)
@ -1312,28 +1312,6 @@ class ResizeClaimTestCase(BaseTrackerTestCase):
self.assertEqual('fakenode', instance['node'])
class NoInstanceTypesInSysMetadata(ResizeClaimTestCase):
"""Make sure we handle the case where the following are true:
#) Compute node C gets upgraded to code that looks for instance types in
system metadata. AND
#) C already has instances in the process of migrating that do not have
stashed instance types.
bug 1164110
"""
def setUp(self):
super(NoInstanceTypesInSysMetadata, self).setUp()
self.instance = self._fake_instance(stash=False)
def test_get_instance_type_stash_false(self):
with (mock.patch.object(objects.Flavor, 'get_by_id',
return_value=self.instance_type)):
flavor = self.tracker._get_instance_type(self.context,
self.instance, "new_")
self.assertEqual(self.instance_type, flavor)
class OrphanTestCase(BaseTrackerTestCase):
def _driver(self):
class OrphanVirtDriver(FakeVirtDriver):

View File

@ -18,7 +18,6 @@ from oslo_serialization import jsonutils
from oslo_utils import units
from nova.compute import claims
from nova.compute import flavors
from nova.compute import power_state
from nova.compute import resource_tracker
from nova.compute import task_states
@ -116,19 +115,18 @@ _INSTANCE_TYPE_FIXTURES = {
}
# A collection of system_metadata attributes that would exist in instances
# that have the instance type ID matching the dictionary key.
_INSTANCE_TYPE_SYS_META = {
1: flavors.save_flavor_info({}, _INSTANCE_TYPE_FIXTURES[1]),
2: flavors.save_flavor_info({}, _INSTANCE_TYPE_FIXTURES[2]),
_INSTANCE_TYPE_OBJ_FIXTURES = {
1: objects.Flavor(id=1, flavorid='fakeid-1', name='fake1.small',
memory_mb=128, vcpus=1, root_gb=1,
ephemeral_gb=0, swap=0, rxtx_factor=0,
vcpu_weight=1, extra_specs={}),
2: objects.Flavor(id=2, flavorid='fakeid-2', name='fake1.medium',
memory_mb=256, vcpus=2, root_gb=5,
ephemeral_gb=0, swap=0, rxtx_factor=0,
vcpu_weight=1, extra_specs={}),
}
_MIGRATION_SYS_META = flavors.save_flavor_info(
{}, _INSTANCE_TYPE_FIXTURES[1], 'old_')
_MIGRATION_SYS_META = flavors.save_flavor_info(
_MIGRATION_SYS_META, _INSTANCE_TYPE_FIXTURES[2], 'new_')
_2MB = 2 * units.Mi / units.Ki
_INSTANCE_NUMA_TOPOLOGIES = {
@ -179,6 +177,9 @@ _INSTANCE_FIXTURES = [
task_state=None,
os_type='fake-os', # Used by the stats collector.
project_id='fake-project', # Used by the stats collector.
flavor = _INSTANCE_TYPE_OBJ_FIXTURES[1],
old_flavor = _INSTANCE_TYPE_OBJ_FIXTURES[1],
new_flavor = _INSTANCE_TYPE_OBJ_FIXTURES[1],
),
objects.Instance(
id=2,
@ -196,6 +197,9 @@ _INSTANCE_FIXTURES = [
task_state=None,
os_type='fake-os',
project_id='fake-project-2',
flavor = _INSTANCE_TYPE_OBJ_FIXTURES[2],
old_flavor = _INSTANCE_TYPE_OBJ_FIXTURES[2],
new_flavor = _INSTANCE_TYPE_OBJ_FIXTURES[2],
),
]
@ -254,9 +258,12 @@ _MIGRATION_INSTANCE_FIXTURES = {
vm_state=vm_states.ACTIVE,
power_state=power_state.RUNNING,
task_state=task_states.RESIZE_MIGRATING,
system_metadata=_MIGRATION_SYS_META,
system_metadata={},
os_type='fake-os',
project_id='fake-project',
flavor=_INSTANCE_TYPE_OBJ_FIXTURES[1],
old_flavor=_INSTANCE_TYPE_OBJ_FIXTURES[1],
new_flavor=_INSTANCE_TYPE_OBJ_FIXTURES[2],
),
# dest-only
'f6ed631a-8645-4b12-8e1e-2fff55795765': objects.Instance(
@ -273,9 +280,12 @@ _MIGRATION_INSTANCE_FIXTURES = {
vm_state=vm_states.ACTIVE,
power_state=power_state.RUNNING,
task_state=task_states.RESIZE_MIGRATING,
system_metadata=_MIGRATION_SYS_META,
system_metadata={},
os_type='fake-os',
project_id='fake-project',
flavor=_INSTANCE_TYPE_OBJ_FIXTURES[2],
old_flavor=_INSTANCE_TYPE_OBJ_FIXTURES[1],
new_flavor=_INSTANCE_TYPE_OBJ_FIXTURES[2],
),
# source-and-dest
'f4f0bfea-fe7e-4264-b598-01cb13ef1997': objects.Instance(
@ -292,9 +302,12 @@ _MIGRATION_INSTANCE_FIXTURES = {
vm_state=vm_states.ACTIVE,
power_state=power_state.RUNNING,
task_state=task_states.RESIZE_MIGRATING,
system_metadata=_MIGRATION_SYS_META,
system_metadata={},
os_type='fake-os',
project_id='fake-project',
flavor=_INSTANCE_TYPE_OBJ_FIXTURES[2],
old_flavor=_INSTANCE_TYPE_OBJ_FIXTURES[1],
new_flavor=_INSTANCE_TYPE_OBJ_FIXTURES[2],
),
}
@ -1024,7 +1037,7 @@ class TestInstanceClaim(BaseTestCase):
self.elevated = mock.MagicMock()
self.ctx.elevated.return_value = self.elevated
self.instance = copy.deepcopy(_INSTANCE_FIXTURES[0])
self.instance = _INSTANCE_FIXTURES[0].obj_clone()
def assertEqualNUMAHostTopology(self, expected, got):
attrs = ('cpuset', 'memory', 'id', 'cpu_usage', 'memory_usage')
@ -1142,8 +1155,7 @@ class TestResizeClaim(BaseTestCase):
self._setup_rt()
self.rt.compute_node = copy.deepcopy(_COMPUTE_NODE_FIXTURES[0])
self.instance = copy.deepcopy(_INSTANCE_FIXTURES[0])
self.instance.system_metadata = _INSTANCE_TYPE_SYS_META[1]
self.instance = _INSTANCE_FIXTURES[0].obj_clone()
self.flavor = _INSTANCE_TYPE_FIXTURES[1]
self.limits = {}