Merge "Remove two unneeded db lookups during delete of a resizing instance"
This commit is contained in:
commit
c44f711a04
@ -1764,25 +1764,19 @@ class API(base.Base):
|
||||
|
||||
def _create_reservations(self, context, instance, original_task_state,
|
||||
project_id, user_id):
|
||||
instance_vcpus = instance.vcpus
|
||||
instance_memory_mb = instance.memory_mb
|
||||
# NOTE(wangpan): if the instance is resizing, and the resources
|
||||
# are updated to new instance type, we should use
|
||||
# the old instance type to create reservation.
|
||||
# see https://bugs.launchpad.net/nova/+bug/1099729 for more details
|
||||
if original_task_state in (task_states.RESIZE_MIGRATED,
|
||||
task_states.RESIZE_FINISH):
|
||||
try:
|
||||
migration = objects.Migration.get_by_instance_and_status(
|
||||
context.elevated(), instance.uuid, 'post-migrating')
|
||||
except exception.MigrationNotFoundByStatus:
|
||||
migration = None
|
||||
if (migration and
|
||||
instance.instance_type_id ==
|
||||
migration.new_instance_type_id):
|
||||
get_inst_attrs = compute_utils.get_inst_attrs_from_migration
|
||||
instance_vcpus, instance_memory_mb = get_inst_attrs(migration,
|
||||
instance)
|
||||
old_flavor = instance.old_flavor
|
||||
instance_vcpus = old_flavor.vcpus
|
||||
vram_mb = old_flavor.extra_specs.get('hw_video:ram_max_mb', 0)
|
||||
instance_memory_mb = old_flavor.memory_mb + vram_mb
|
||||
else:
|
||||
instance_vcpus = instance.vcpus
|
||||
instance_memory_mb = instance.memory_mb
|
||||
|
||||
quotas = objects.Quotas(context=context)
|
||||
quotas.reserve(project_id=project_id,
|
||||
|
@ -24,7 +24,6 @@ from oslo_log import log
|
||||
import six
|
||||
|
||||
from nova import block_device
|
||||
from nova.compute import flavors
|
||||
from nova.compute import power_state
|
||||
from nova.compute import task_states
|
||||
from nova import exception
|
||||
@ -496,28 +495,6 @@ def reserve_quota_delta(context, deltas, instance):
|
||||
return quotas
|
||||
|
||||
|
||||
def get_inst_attrs_from_migration(migration, instance):
|
||||
"""Get the instance vcpus and memory_mb attributes.
|
||||
|
||||
Provides instance vcpus and memory_mb attributes according to
|
||||
old flavor type using migration object if old flavor exists.
|
||||
"""
|
||||
instance_vcpus = instance.vcpus
|
||||
instance_memory_mb = instance.memory_mb
|
||||
|
||||
old_inst_type_id = migration.old_instance_type_id
|
||||
try:
|
||||
old_inst_type = flavors.get_flavor(old_inst_type_id)
|
||||
except exception.FlavorNotFound:
|
||||
LOG.warning(_LW("Flavor %d not found"), old_inst_type_id)
|
||||
else:
|
||||
instance_vcpus = old_inst_type.vcpus
|
||||
vram_mb = old_inst_type.extra_specs.get('hw_video:ram_max_mb', 0)
|
||||
instance_memory_mb = old_inst_type.memory_mb + vram_mb
|
||||
|
||||
return instance_vcpus, instance_memory_mb
|
||||
|
||||
|
||||
def remove_shelved_keys_from_system_metadata(instance):
|
||||
# Delete system_metadata for a shelved instance
|
||||
for key in ['shelved_at', 'shelved_image_id', 'shelved_host']:
|
||||
|
@ -677,27 +677,10 @@ class _ComputeAPIUnitTestMixIn(object):
|
||||
self._test_reboot_type_fails('SOFT', task_state=task_states.SUSPENDING)
|
||||
|
||||
def _test_delete_resizing_part(self, inst, deltas):
|
||||
fake_db_migration = test_migration.fake_db_migration()
|
||||
migration = objects.Migration._from_db_object(
|
||||
self.context, objects.Migration(),
|
||||
fake_db_migration)
|
||||
inst.instance_type_id = migration.new_instance_type_id
|
||||
old_flavor = self._create_flavor(vcpus=1, memory_mb=512)
|
||||
old_flavor = inst.old_flavor
|
||||
deltas['cores'] = -old_flavor.vcpus
|
||||
deltas['ram'] = -old_flavor.memory_mb
|
||||
|
||||
self.mox.StubOutWithMock(objects.Migration,
|
||||
'get_by_instance_and_status')
|
||||
self.mox.StubOutWithMock(compute_utils,
|
||||
'get_inst_attrs_from_migration')
|
||||
|
||||
self.context.elevated().AndReturn(self.context)
|
||||
objects.Migration.get_by_instance_and_status(
|
||||
self.context, inst.uuid, 'post-migrating').AndReturn(migration)
|
||||
compute_utils.get_inst_attrs_from_migration(
|
||||
migration, inst).AndReturn((old_flavor.vcpus,
|
||||
old_flavor.memory_mb))
|
||||
|
||||
def _test_delete_resized_part(self, inst):
|
||||
migration = objects.Migration._from_db_object(
|
||||
self.context, objects.Migration(),
|
||||
@ -898,8 +881,10 @@ class _ComputeAPIUnitTestMixIn(object):
|
||||
self._test_delete('delete', launched_at=None)
|
||||
|
||||
def test_delete_in_resizing(self):
|
||||
old_flavor = objects.Flavor(vcpus=1, memory_mb=512, extra_specs={})
|
||||
self._test_delete('delete',
|
||||
task_state=task_states.RESIZE_FINISH)
|
||||
task_state=task_states.RESIZE_FINISH,
|
||||
old_flavor=old_flavor)
|
||||
|
||||
def test_delete_in_resized(self):
|
||||
self._test_delete('delete', vm_state=vm_states.RESIZED)
|
||||
|
@ -769,48 +769,3 @@ class ComputeUtilsQuotaDeltaTestCase(test.TestCase):
|
||||
compute_utils.reserve_quota_delta(self.context, deltas, inst)
|
||||
mock_reserve.assert_called_once_with(project_id=inst.project_id,
|
||||
user_id=inst.user_id, **deltas)
|
||||
|
||||
|
||||
class ComputeUtilsMigrationTestCase(test.TestCase):
|
||||
def setUp(self):
|
||||
super(ComputeUtilsMigrationTestCase, self).setUp()
|
||||
self.context = context.RequestContext('fake', 'fake')
|
||||
|
||||
def _test_get_resources(self):
|
||||
old_flavor = flavors.get_flavor_by_name('m1.tiny')
|
||||
new_flavor = flavors.get_flavor_by_name('m1.medium')
|
||||
|
||||
params = {'flavor': new_flavor, 'old_flavor': old_flavor,
|
||||
'new_flavor': new_flavor, 'vcpus': new_flavor['vcpus'],
|
||||
'memory_mb': new_flavor['memory_mb']}
|
||||
instance = create_instance(self.context, params=params)
|
||||
|
||||
updates = {'old_instance_type_id': old_flavor['id'],
|
||||
'new_instance_type_id': new_flavor['id']}
|
||||
|
||||
fake_migration = test_migration.fake_db_migration(**updates)
|
||||
migration = objects.Migration._from_db_object(self.context,
|
||||
objects.Migration(),
|
||||
fake_migration)
|
||||
return old_flavor, migration, instance
|
||||
|
||||
@mock.patch.object(objects.Flavor, 'get_by_id')
|
||||
def test_get_inst_attrs_from_migration(self, mock_get_flavor):
|
||||
old_flavor, migration, instance = self._test_get_resources()
|
||||
expected_result = (old_flavor['vcpus'], old_flavor['memory_mb'])
|
||||
|
||||
mock_get_flavor.return_value = old_flavor
|
||||
result = compute_utils.get_inst_attrs_from_migration(migration,
|
||||
instance)
|
||||
self.assertEqual(expected_result, result)
|
||||
|
||||
@mock.patch.object(objects.Flavor, 'get_by_id')
|
||||
def test_get_inst_attrs_from_migration_flavor_not_found(
|
||||
self, mock_get_flavor):
|
||||
old_flavor, migration, instance = self._test_get_resources()
|
||||
expected_result = (instance.vcpus, instance.memory_mb)
|
||||
|
||||
mock_get_flavor.side_effect = exception.FlavorNotFound(old_flavor.id)
|
||||
result = compute_utils.get_inst_attrs_from_migration(migration,
|
||||
instance)
|
||||
self.assertEqual(expected_result, result)
|
||||
|
Loading…
x
Reference in New Issue
Block a user