Skip to content

Commit 17ae907

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)
1 parent 71e5a1d commit 17ae907

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
@@ -8572,8 +8572,9 @@ def _do_live_migration(self, context, dest, instance, block_migration,
85728572
# host attachment. We fetch BDMs before that to retain connection_info
85738573
# and attachment_id relating to the source host for post migration
85748574
# cleanup.
8575-
post_live_migration = functools.partial(self._post_live_migration,
8576-
source_bdms=source_bdms)
8575+
post_live_migration = functools.partial(
8576+
self._post_live_migration_update_host, source_bdms=source_bdms
8577+
)
85778578
rollback_live_migration = functools.partial(
85788579
self._rollback_live_migration, source_bdms=source_bdms)
85798580

@@ -8845,6 +8846,42 @@ def _post_live_migration_remove_source_vol_connections(
88458846
bdm.attachment_id, self.host,
88468847
str(e), instance=instance)
88478848

8849+
# TODO(sean-k-mooney): add typing
8850+
def _post_live_migration_update_host(
8851+
self, ctxt, instance, dest, block_migration=False,
8852+
migrate_data=None, source_bdms=None
8853+
):
8854+
try:
8855+
self._post_live_migration(
8856+
ctxt, instance, dest, block_migration, migrate_data,
8857+
source_bdms)
8858+
except Exception:
8859+
# Restore the instance object
8860+
node_name = None
8861+
try:
8862+
# get node name of compute, where instance will be
8863+
# running after migration, that is destination host
8864+
compute_node = self._get_compute_info(ctxt, dest)
8865+
node_name = compute_node.hypervisor_hostname
8866+
except exception.ComputeHostNotFound:
8867+
LOG.exception('Failed to get compute_info for %s', dest)
8868+
8869+
# we can never rollback from post live migration and we can only
8870+
# get here if the instance is running on the dest so we ensure
8871+
# the instance.host is set correctly and reraise the original
8872+
# exception unmodified.
8873+
if instance.host != dest:
8874+
# apply saves the new fields while drop actually removes the
8875+
# migration context from the instance, so migration persists.
8876+
instance.apply_migration_context()
8877+
instance.drop_migration_context()
8878+
instance.host = dest
8879+
instance.task_state = None
8880+
instance.node = node_name
8881+
instance.progress = 0
8882+
instance.save()
8883+
raise
8884+
88488885
@wrap_exception()
88498886
@wrap_instance_fault
88508887
def _post_live_migration(self, ctxt, instance, dest,
@@ -8856,7 +8893,7 @@ def _post_live_migration(self, ctxt, instance, dest,
88568893
and mainly updating database record.
88578894

88588895
:param ctxt: security context
8859-
:param instance: instance dict
8896+
:param instance: instance object
88608897
:param dest: destination host
88618898
:param block_migration: if true, prepare for block migration
88628899
: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
@@ -9958,6 +9958,27 @@ def test_post_live_migration_new_allocations(self):
99589958
self.instance,
99599959
migration)
99609960

9961+
def test_post_live_migration_update_host(self):
9962+
@mock.patch.object(self.compute, '_get_compute_info')
9963+
def _test_post_live_migration(_get_compute_info):
9964+
dest_host = 'dest'
9965+
cn = objects.ComputeNode(hypervisor_hostname=dest_host)
9966+
_get_compute_info.return_value = cn
9967+
instance = fake_instance.fake_instance_obj(self.context,
9968+
node='src',
9969+
uuid=uuids.instance)
9970+
with mock.patch.object(self.compute, "_post_live_migration"
9971+
) as plm, mock.patch.object(instance, "save") as save:
9972+
error = ValueError("some failure")
9973+
plm.side_effect = error
9974+
self.assertRaises(
9975+
ValueError, self.compute._post_live_migration_update_host,
9976+
self.context, instance, dest_host)
9977+
save.assert_called_once()
9978+
self.assertEqual(instance.host, dest_host)
9979+
9980+
_test_post_live_migration()
9981+
99619982
def test_post_live_migration_cinder_pre_344_api(self):
99629983
# Because live migration has
99639984
# succeeded,_post_live_migration_remove_source_vol_connections()

0 commit comments

Comments
 (0)