Skip to content

Commit 43c0e40

Browse files
SeanMooneyauniyal61
authored andcommitted
[compute] always set instance.host in post_livemigration
This change add a new _post_live_migration_update_host function that wraps _post_live_migration and just ensures that if we exit due to an exception instance.host is set to the destination host. when we are in _post_live_migration the guest has already started running on the destination host and we cannot revert. Sometimes admins or users will hard reboot the instance expecting that to fix everything when the vm enters the error state after the failed migrations. Previously this would end up recreating the instance on the source node leading to possible data corruption if the instance used shared storage. Change-Id: Ibc4bc7edf1c8d1e841c72c9188a0a62836e9f153 Partial-Bug: #1628606 (cherry picked from commit 8449b7c) (cherry picked from commit 643b0c7) (cherry picked from commit 17ae907) (cherry picked from commit 15502dd)
1 parent ed1ea71 commit 43c0e40

File tree

3 files changed

+63
-6
lines changed

3 files changed

+63
-6
lines changed

nova/compute/manager.py

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8460,8 +8460,9 @@ def _do_live_migration(self, context, dest, instance, block_migration,
84608460
# host attachment. We fetch BDMs before that to retain connection_info
84618461
# and attachment_id relating to the source host for post migration
84628462
# cleanup.
8463-
post_live_migration = functools.partial(self._post_live_migration,
8464-
source_bdms=source_bdms)
8463+
post_live_migration = functools.partial(
8464+
self._post_live_migration_update_host, source_bdms=source_bdms
8465+
)
84658466
rollback_live_migration = functools.partial(
84668467
self._rollback_live_migration, source_bdms=source_bdms)
84678468

@@ -8707,6 +8708,42 @@ def _post_live_migration_remove_source_vol_connections(
87078708
bdm.attachment_id, self.host,
87088709
str(e), instance=instance)
87098710

8711+
# TODO(sean-k-mooney): add typing
8712+
def _post_live_migration_update_host(
8713+
self, ctxt, instance, dest, block_migration=False,
8714+
migrate_data=None, source_bdms=None
8715+
):
8716+
try:
8717+
self._post_live_migration(
8718+
ctxt, instance, dest, block_migration, migrate_data,
8719+
source_bdms)
8720+
except Exception:
8721+
# Restore the instance object
8722+
node_name = None
8723+
try:
8724+
# get node name of compute, where instance will be
8725+
# running after migration, that is destination host
8726+
compute_node = self._get_compute_info(ctxt, dest)
8727+
node_name = compute_node.hypervisor_hostname
8728+
except exception.ComputeHostNotFound:
8729+
LOG.exception('Failed to get compute_info for %s', dest)
8730+
8731+
# we can never rollback from post live migration and we can only
8732+
# get here if the instance is running on the dest so we ensure
8733+
# the instance.host is set correctly and reraise the original
8734+
# exception unmodified.
8735+
if instance.host != dest:
8736+
# apply saves the new fields while drop actually removes the
8737+
# migration context from the instance, so migration persists.
8738+
instance.apply_migration_context()
8739+
instance.drop_migration_context()
8740+
instance.host = dest
8741+
instance.task_state = None
8742+
instance.node = node_name
8743+
instance.progress = 0
8744+
instance.save()
8745+
raise
8746+
87108747
@wrap_exception()
87118748
@wrap_instance_fault
87128749
def _post_live_migration(self, ctxt, instance, dest,
@@ -8718,7 +8755,7 @@ def _post_live_migration(self, ctxt, instance, dest,
87188755
and mainly updating database record.
87198756

87208757
:param ctxt: security context
8721-
:param instance: instance dict
8758+
:param instance: instance object
87228759
:param dest: destination host
87238760
:param block_migration: if true, prepare for block migration
87248761
:param migrate_data: if not None, it is a dict which has data

nova/tests/functional/regressions/test_bug_1628606.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,5 @@ def test_post_live_migration(self, mock_migration):
5656
server = self._live_migrate(
5757
server, migration_expected_state='error',
5858
server_expected_state='ERROR')
59-
# FIXME(amit): this should point to the dest as after migration
60-
# but does not because of bug 1628606
61-
self.assertEqual(self.src.host, server['OS-EXT-SRV-ATTR:host'])
59+
60+
self.assertEqual(self.dest.host, server['OS-EXT-SRV-ATTR:host'])

nova/tests/unit/compute/test_compute_mgr.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9949,6 +9949,27 @@ def test_post_live_migration_new_allocations(self):
99499949
self.instance,
99509950
migration)
99519951

9952+
def test_post_live_migration_update_host(self):
9953+
@mock.patch.object(self.compute, '_get_compute_info')
9954+
def _test_post_live_migration(_get_compute_info):
9955+
dest_host = 'dest'
9956+
cn = objects.ComputeNode(hypervisor_hostname=dest_host)
9957+
_get_compute_info.return_value = cn
9958+
instance = fake_instance.fake_instance_obj(self.context,
9959+
node='src',
9960+
uuid=uuids.instance)
9961+
with mock.patch.object(self.compute, "_post_live_migration"
9962+
) as plm, mock.patch.object(instance, "save") as save:
9963+
error = ValueError("some failure")
9964+
plm.side_effect = error
9965+
self.assertRaises(
9966+
ValueError, self.compute._post_live_migration_update_host,
9967+
self.context, instance, dest_host)
9968+
save.assert_called_once()
9969+
self.assertEqual(instance.host, dest_host)
9970+
9971+
_test_post_live_migration()
9972+
99529973
def test_post_live_migration_cinder_pre_344_api(self):
99539974
# Because live migration has
99549975
# succeeded,_post_live_migration_remove_source_vol_connections()

0 commit comments

Comments
 (0)