Skip to content

Commit 1a11d5c

Browse files
committed
Raise InstanceFaultRollback for UnableToMigrateToSelf from _prep_resize
It is possible to cold migrate a stopped server. If, however, the cold migrate is scheduled to the instance's current host and the compute driver does not support cold migrating to the same host, then UnableToMigrateToSelf was being raised from _prep_resize. If _reschedule_resize_or_reraise re-raises that exception, then _error_out_instance_on_exception in prep_resize handles it and sets the instance vm_state to ACTIVE. This is wrong since the instance power state is unchanged at this point and the instance is actually stopped. This fixes the problem by wrapping UnableToMigrateToSelf in InstanceFaultRollback and raises that from _prep_resize, and _error_out_instance_on_exception is called with the initial vm_state (STOPPED in this case) so when _error_out_instance_on_exception handles the InstanceFaultRollback exception it sets the instance vm_state to STOPPED (what it already was) rather than ACTIVE. There were no existing unit tests for the UnableToMigrateToSelf case in _prep_resize so those are added here. Conflicts: nova/tests/unit/compute/test_compute_mgr.py NOTE(mriedem): The conflict is due to some unit tests from change I734cc01dce13f9e75a16639faf890ddb1661b7eb not being in Stein. Change-Id: I17543ecb572934ecc7d0bbc7a4ad2f537fa499bc Closes-Bug: #1811235 (cherry picked from commit d1931ac)
1 parent cdaa800 commit 1a11d5c

File tree

3 files changed

+78
-4
lines changed

3 files changed

+78
-4
lines changed

nova/compute/manager.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4364,8 +4364,12 @@ def _prep_resize(self, context, image, instance, instance_type,
43644364
# check driver whether support migrate to same host
43654365
if not self.driver.capabilities.get(
43664366
'supports_migrate_to_same_host', False):
4367-
raise exception.UnableToMigrateToSelf(
4368-
instance_id=instance.uuid, host=self.host)
4367+
# Raise InstanceFaultRollback so that the
4368+
# _error_out_instance_on_exception context manager in
4369+
# prep_resize will set the instance.vm_state properly.
4370+
raise exception.InstanceFaultRollback(
4371+
inner_exception=exception.UnableToMigrateToSelf(
4372+
instance_id=instance.uuid, host=self.host))
43694373

43704374
# NOTE(danms): Stash the new instance_type to avoid having to
43714375
# look it up in the database later
@@ -4439,8 +4443,13 @@ def prep_resize(self, context, image, instance, instance_type,
44394443
if node is None:
44404444
node = self._get_nodename(instance, refresh=True)
44414445

4442-
with self._error_out_instance_on_exception(context, instance), \
4443-
errors_out_migration_ctxt(migration):
4446+
# Pass instance_state=instance.vm_state because we can resize
4447+
# a STOPPED server and we don't want to set it back to ACTIVE
4448+
# in case _prep_resize fails.
4449+
instance_state = instance.vm_state
4450+
with self._error_out_instance_on_exception(
4451+
context, instance, instance_state=instance_state),\
4452+
errors_out_migration_ctxt(migration):
44444453
self._send_prep_resize_notifications(
44454454
context, instance, fields.NotificationPhase.START,
44464455
instance_type)

nova/tests/unit/compute/test_compute_mgr.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8817,6 +8817,68 @@ def doit(mock_pr, mock_r):
88178817

88188818
doit()
88198819

8820+
def test_prep_resize_fails_unable_to_migrate_to_self(self):
8821+
"""Asserts that _prep_resize handles UnableToMigrateToSelf when
8822+
_prep_resize is called on the host on which the instance lives and
8823+
the flavor is not changing.
8824+
"""
8825+
instance = fake_instance.fake_instance_obj(
8826+
self.context, host=self.compute.host,
8827+
expected_attrs=['system_metadata', 'flavor'])
8828+
migration = mock.MagicMock(spec='nova.objects.Migration')
8829+
with mock.patch.dict(self.compute.driver.capabilities,
8830+
{'supports_migrate_to_same_host': False}):
8831+
ex = self.assertRaises(
8832+
exception.InstanceFaultRollback, self.compute._prep_resize,
8833+
self.context, instance.image_meta, instance, instance.flavor,
8834+
filter_properties={}, node=instance.node, migration=migration)
8835+
self.assertIsInstance(
8836+
ex.inner_exception, exception.UnableToMigrateToSelf)
8837+
8838+
@mock.patch('nova.compute.utils.notify_usage_exists')
8839+
@mock.patch('nova.compute.manager.ComputeManager.'
8840+
'_notify_about_instance_usage')
8841+
@mock.patch('nova.compute.utils.notify_about_resize_prep_instance')
8842+
@mock.patch('nova.objects.Instance.save')
8843+
@mock.patch('nova.compute.manager.ComputeManager._revert_allocation')
8844+
@mock.patch('nova.compute.manager.ComputeManager.'
8845+
'_reschedule_resize_or_reraise')
8846+
@mock.patch('nova.compute.utils.add_instance_fault_from_exc')
8847+
def test_prep_resize_fails_rollback(
8848+
self, add_instance_fault_from_exc, _reschedule_resize_or_reraise,
8849+
_revert_allocation, mock_instance_save,
8850+
notify_about_resize_prep_instance, _notify_about_instance_usage,
8851+
notify_usage_exists):
8852+
"""Tests that if _prep_resize raises InstanceFaultRollback, the
8853+
instance.vm_state is reset properly in _error_out_instance_on_exception
8854+
"""
8855+
instance = fake_instance.fake_instance_obj(
8856+
self.context, host=self.compute.host, vm_state=vm_states.STOPPED,
8857+
expected_attrs=['system_metadata', 'flavor'])
8858+
migration = mock.MagicMock(spec='nova.objects.Migration')
8859+
request_spec = mock.MagicMock(spec='nova.objects.RequestSpec')
8860+
ex = exception.InstanceFaultRollback(
8861+
inner_exception=exception.UnableToMigrateToSelf(
8862+
instance_id=instance.uuid, host=instance.host))
8863+
8864+
def fake_reschedule_resize_or_reraise(*args, **kwargs):
8865+
raise ex
8866+
8867+
_reschedule_resize_or_reraise.side_effect = (
8868+
fake_reschedule_resize_or_reraise)
8869+
8870+
with mock.patch.object(self.compute, '_prep_resize', side_effect=ex):
8871+
self.assertRaises(
8872+
# _error_out_instance_on_exception should reraise the
8873+
# UnableToMigrateToSelf inside InstanceFaultRollback.
8874+
exception.UnableToMigrateToSelf, self.compute.prep_resize,
8875+
self.context, instance.image_meta, instance, instance.flavor,
8876+
request_spec, filter_properties={}, node=instance.node,
8877+
clean_shutdown=True, migration=migration, host_list=[])
8878+
# The instance.vm_state should remain unchanged
8879+
# (_error_out_instance_on_exception will set to ACTIVE by default).
8880+
self.assertEqual(vm_states.STOPPED, instance.vm_state)
8881+
88208882
def test_get_updated_nw_info_with_pci_mapping(self):
88218883
old_dev = objects.PciDevice(address='0000:04:00.2')
88228884
new_dev = objects.PciDevice(address='0000:05:00.3')

nova/tests/unit/fake_instance.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ def fake_instance_obj(context, obj_instance_class=None, **updates):
132132
inst.vcpus = flavor.vcpus
133133
if 'memory_mb' in flavor and 'memory_mb' not in updates:
134134
inst.memory_mb = flavor.memory_mb
135+
if ('instance_type_id' not in inst or inst.instance_type_id is None
136+
and 'id' in flavor):
137+
inst.instance_type_id = flavor.id
135138
inst.old_flavor = None
136139
inst.new_flavor = None
137140
inst.obj_reset_changes()

0 commit comments

Comments
 (0)