From 5908b60b0420f1ad528e56b0c147a330e9a1a5d6 Mon Sep 17 00:00:00 2001 From: Dan Smith Date: Fri, 22 Mar 2013 12:57:59 -0700 Subject: Make _downsize_quota_delta() use stashed instance types This change changes the _downsize_quota_delta() method to take instance as a param instead of migration_ref, and use it to extract the old and new instance_types instead of looking them up in the database. This is needed for this case since the method can be called from compute manager during the periodic task to confirm pending migrations, and thus access to the database may not be allowed. Fixes bug #1158897 Change-Id: I647c51bc4d30e6dfd62abe7b65a9109d6aa2bb6f --- nova/compute/api.py | 16 +++++++--------- nova/tests/compute/test_compute.py | 14 +++++++++++++- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/nova/compute/api.py b/nova/compute/api.py index d9105470a..997251c24 100644 --- a/nova/compute/api.py +++ b/nova/compute/api.py @@ -1074,8 +1074,7 @@ class API(base.Base): # 2. down-resize: here -instance['vcpus'/'memory_mb'] are # shy by delta(old, new) from the quota usages accounted # for this instance, so we must adjust - deltas = self._downsize_quota_delta(context, - migration_ref) + deltas = self._downsize_quota_delta(context, instance) downsize_reservations = self._reserve_quota_delta(context, deltas) @@ -1857,7 +1856,7 @@ class API(base.Base): elevated, instance['uuid'], 'finished') # reserve quota only for any decrease in resource usage - deltas = self._downsize_quota_delta(context, migration_ref) + deltas = self._downsize_quota_delta(context, instance) reservations = self._reserve_quota_delta(context, deltas) instance = self.update(context, instance, vm_state=vm_states.ACTIVE, @@ -1932,15 +1931,14 @@ class API(base.Base): old_instance_type, -1, -1) @staticmethod - def _downsize_quota_delta(context, migration_ref): + def _downsize_quota_delta(context, instance): """ Calculate deltas required to adjust quota for an instance downsize. """ - old_instance_type = instance_types.get_instance_type( - migration_ref['old_instance_type_id']) - new_instance_type = instance_types.get_instance_type( - migration_ref['new_instance_type_id']) - + old_instance_type = instance_types.extract_instance_type(instance, + 'old_') + new_instance_type = instance_types.extract_instance_type(instance, + 'new_') return API._resize_quota_delta(context, new_instance_type, old_instance_type, 1, -1) diff --git a/nova/tests/compute/test_compute.py b/nova/tests/compute/test_compute.py index ebf7a38b4..ce688eb90 100644 --- a/nova/tests/compute/test_compute.py +++ b/nova/tests/compute/test_compute.py @@ -5293,6 +5293,17 @@ class ComputeAPITestCase(BaseTestCase): instance = db.instance_get_by_uuid(self.context, instance['uuid']) self.compute_api.resize(self.context, instance, '4') + # Do the prep/finish_resize steps (manager does this) + old_type = instance_types.extract_instance_type(instance) + new_type = instance_types.get_instance_type_by_flavor_id('4') + sys_meta = utils.metadata_to_dict(instance['system_metadata']) + sys_meta = instance_types.save_instance_type_info(sys_meta, + old_type, 'old_') + sys_meta = instance_types.save_instance_type_info(sys_meta, + new_type, 'new_') + sys_meta = instance_types.save_instance_type_info(sys_meta, + new_type) + # create a fake migration record (manager does this) db.migration_create(self.context.elevated(), {'instance_uuid': instance['uuid'], @@ -5300,7 +5311,8 @@ class ComputeAPITestCase(BaseTestCase): # set the state that the instance gets when resize finishes instance = db.instance_update(self.context, instance['uuid'], {'task_state': None, - 'vm_state': vm_states.RESIZED}) + 'vm_state': vm_states.RESIZED, + 'system_metadata': sys_meta}) self.compute_api.confirm_resize(self.context, instance) self.compute.terminate_instance(self.context, -- cgit