Skip to content

Commit 14596ca

Browse files
stephenfinBalazs Gibizer
authored andcommitted
libvirt: Remove dead error handling code
We removed calls to configure the libvirt-based firewall driver in '_create_guest_with_network' as part of change I5a9e5532c46a5f7064441ae644125d21efe5fda1. Since then, there only other way we can successfully create a guest and still end up in an exception handler is if the plugging of VIFs fails. Despite this, all three code paths still contained references to a 'guest' attribute that would never be set. Clarify things by removing the use of the 'guest' attribute from the other code paths. This means the '_cleanup_failed_start' helper only has one caller and some unnecessary logic itself, so we can fold that into its caller. We also take this opportunity to remove an unnecessary error handler - 'nova.exception.VirtualInterfaceCreateException' is derived from 'Exception' and doesn't need its own handler - and add the type hints for the function. The heavy lifting for the latter was already done in change I2489bf16dabc8a83b2044139247f4245ae29adb1 so this is pretty easy to grok. Next up, we really need to stop passing around dicts of block device mapping info, but that's another change for another day. Change-Id: I0b93bdc12cdce591c7e642ab8830e92445467b9a Signed-off-by: Stephen Finucane <[email protected]> (cherry picked from commit dc814a6)
1 parent 9f90c72 commit 14596ca

File tree

2 files changed

+44
-105
lines changed

2 files changed

+44
-105
lines changed

nova/tests/unit/virt/libvirt/test_driver.py

Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -18963,7 +18963,7 @@ def test_create_guest_with_network__with_other_error(self):
1896318963

1896418964
@mock.patch.object(drvr, 'plug_vifs')
1896518965
@mock.patch.object(drvr, '_create_guest')
18966-
@mock.patch.object(drvr, '_cleanup_failed_start')
18966+
@mock.patch.object(drvr, '_cleanup')
1896718967
def the_test(mock_cleanup, mock_create, mock_plug):
1896818968
instance = objects.Instance(**self.test_instance)
1896918969
mock_create.side_effect = test.TestingException
@@ -18973,67 +18973,11 @@ def the_test(mock_cleanup, mock_create, mock_plug):
1897318973
cleanup_instance_dir=mock.sentinel.cleanup_instance_dir,
1897418974
cleanup_instance_disks=mock.sentinel.cleanup_instance_disks)
1897518975
mock_cleanup.assert_called_once_with(
18976-
self.context, instance, [], None, None,
18976+
self.context, instance, [], None, destroy_vifs=True,
1897718977
cleanup_instance_dir=mock.sentinel.cleanup_instance_dir,
1897818978
cleanup_instance_disks=mock.sentinel.cleanup_instance_disks)
1897918979
the_test()
1898018980

18981-
def test_cleanup_failed_start_no_guest(self):
18982-
drvr = libvirt_driver.LibvirtDriver(mock.MagicMock(), False)
18983-
with mock.patch.object(drvr, '_cleanup') as mock_cleanup:
18984-
drvr._cleanup_failed_start(
18985-
None, None, None, None, None, False, False)
18986-
self.assertTrue(mock_cleanup.called)
18987-
18988-
def test_cleanup_failed_start_inactive_guest(self):
18989-
drvr = libvirt_driver.LibvirtDriver(mock.MagicMock(), False)
18990-
guest = mock.MagicMock()
18991-
guest.is_active.return_value = False
18992-
with mock.patch.object(drvr, '_cleanup') as mock_cleanup:
18993-
drvr._cleanup_failed_start(
18994-
None, None, None, None, guest, False, False)
18995-
self.assertTrue(mock_cleanup.called)
18996-
self.assertFalse(guest.poweroff.called)
18997-
18998-
def test_cleanup_failed_start_active_guest(self):
18999-
drvr = libvirt_driver.LibvirtDriver(mock.MagicMock(), False)
19000-
guest = mock.MagicMock()
19001-
guest.is_active.return_value = True
19002-
with mock.patch.object(drvr, '_cleanup') as mock_cleanup:
19003-
drvr._cleanup_failed_start(
19004-
None, None, None, None, guest, False, False)
19005-
self.assertTrue(mock_cleanup.called)
19006-
self.assertTrue(guest.poweroff.called)
19007-
19008-
def test_cleanup_failed_start_failed_poweroff(self):
19009-
drvr = libvirt_driver.LibvirtDriver(mock.MagicMock(), False)
19010-
guest = mock.MagicMock()
19011-
guest.is_active.return_value = True
19012-
guest.poweroff.side_effect = test.TestingException
19013-
with mock.patch.object(drvr, '_cleanup') as mock_cleanup:
19014-
self.assertRaises(test.TestingException,
19015-
drvr._cleanup_failed_start,
19016-
None, None, None, None, guest, False, False)
19017-
self.assertTrue(mock_cleanup.called)
19018-
self.assertTrue(guest.poweroff.called)
19019-
19020-
def test_cleanup_failed_start_failed_poweroff_destroy_disks(self):
19021-
drvr = libvirt_driver.LibvirtDriver(mock.MagicMock(), False)
19022-
guest = mock.MagicMock()
19023-
guest.is_active.return_value = True
19024-
guest.poweroff.side_effect = test.TestingException
19025-
with mock.patch.object(drvr, '_cleanup') as mock_cleanup:
19026-
self.assertRaises(
19027-
test.TestingException, drvr._cleanup_failed_start,
19028-
None, None, None, None, guest,
19029-
cleanup_instance_dir=mock.sentinel.cleanup_instance_dir,
19030-
cleanup_instance_disks=mock.sentinel.cleanup_instance_disks)
19031-
mock_cleanup.assert_called_once_with(
19032-
None, None, None, block_device_info=None, destroy_vifs=True,
19033-
cleanup_instance_dir=mock.sentinel.cleanup_instance_dir,
19034-
cleanup_instance_disks=mock.sentinel.cleanup_instance_disks)
19035-
self.assertTrue(guest.poweroff.called)
19036-
1903718981
@mock.patch('os_brick.encryptors.get_encryption_metadata')
1903818982
@mock.patch('nova.virt.libvirt.blockinfo.get_info_from_bdm')
1903918983
def test_create_guest_with_network__with_bdm(

nova/virt/libvirt/driver.py

Lines changed: 42 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7224,27 +7224,20 @@ def _get_neutron_events(self, network_info):
72247224
return [('network-vif-plugged', vif['id'])
72257225
for vif in network_info if vif.get('active', True) is False]
72267226

7227-
def _cleanup_failed_start(self, context, instance, network_info,
7228-
block_device_info, guest,
7229-
cleanup_instance_dir=False,
7230-
cleanup_instance_disks=False):
7231-
try:
7232-
if guest and guest.is_active():
7233-
guest.poweroff()
7234-
finally:
7235-
self._cleanup(context, instance, network_info,
7236-
block_device_info=block_device_info,
7237-
destroy_vifs=True,
7238-
cleanup_instance_dir=cleanup_instance_dir,
7239-
cleanup_instance_disks=cleanup_instance_disks)
7240-
7241-
def _create_guest_with_network(self, context, xml, instance, network_info,
7242-
block_device_info, power_on=True,
7243-
vifs_already_plugged=False,
7244-
post_xml_callback=None,
7245-
external_events=None,
7246-
cleanup_instance_dir=False,
7247-
cleanup_instance_disks=False):
7227+
def _create_guest_with_network(
7228+
self,
7229+
context: nova_context.RequestContext,
7230+
xml: str,
7231+
instance: 'objects.Instance',
7232+
network_info: network_model.NetworkInfo,
7233+
block_device_info: ty.Optional[ty.Dict[str, ty.Any]],
7234+
power_on: bool = True,
7235+
vifs_already_plugged: bool = False,
7236+
post_xml_callback: ty.Callable = None,
7237+
external_events: ty.Optional[ty.List[str]] = None,
7238+
cleanup_instance_dir: bool = False,
7239+
cleanup_instance_disks: bool = False,
7240+
) -> libvirt_guest.Guest:
72487241
"""Do required network setup and create domain."""
72497242

72507243
timeout = CONF.vif_plugging_timeout
@@ -7258,54 +7251,56 @@ def _create_guest_with_network(self, context, xml, instance, network_info,
72587251
events = []
72597252

72607253
pause = bool(events)
7261-
guest: ty.Optional[libvirt_guest.Guest] = None
72627254
try:
72637255
with self.virtapi.wait_for_instance_event(
7264-
instance, events, deadline=timeout,
7265-
error_callback=self._neutron_failed_callback):
7256+
instance, events, deadline=timeout,
7257+
error_callback=self._neutron_failed_callback,
7258+
):
72667259
self.plug_vifs(instance, network_info)
7267-
with self._lxc_disk_handler(context, instance,
7268-
instance.image_meta,
7269-
block_device_info):
7260+
with self._lxc_disk_handler(
7261+
context, instance, instance.image_meta, block_device_info,
7262+
):
72707263
guest = self._create_guest(
72717264
context, xml, instance,
72727265
pause=pause, power_on=power_on,
72737266
post_xml_callback=post_xml_callback)
7274-
except exception.VirtualInterfaceCreateException:
7275-
# Neutron reported failure and we didn't swallow it, so
7276-
# bail here
7277-
with excutils.save_and_reraise_exception():
7278-
self._cleanup_failed_start(
7279-
context, instance, network_info, block_device_info, guest,
7280-
cleanup_instance_dir=cleanup_instance_dir,
7281-
cleanup_instance_disks=cleanup_instance_disks)
72827267
except eventlet.timeout.Timeout:
72837268
# We never heard from Neutron
7284-
LOG.warning('Timeout waiting for %(events)s for '
7285-
'instance with vm_state %(vm_state)s and '
7286-
'task_state %(task_state)s.',
7287-
{'events': events,
7288-
'vm_state': instance.vm_state,
7289-
'task_state': instance.task_state},
7290-
instance=instance)
7269+
LOG.warning(
7270+
'Timeout waiting for %(events)s for instance with '
7271+
'vm_state %(vm_state)s and task_state %(task_state)s',
7272+
{
7273+
'events': events,
7274+
'vm_state': instance.vm_state,
7275+
'task_state': instance.task_state,
7276+
},
7277+
instance=instance)
7278+
72917279
if CONF.vif_plugging_is_fatal:
7292-
self._cleanup_failed_start(
7293-
context, instance, network_info, block_device_info, guest,
7280+
# NOTE(stephenfin): don't worry, guest will be in scope since
7281+
# we can only hit this branch if the VIF plug timed out
7282+
if guest.is_active():
7283+
guest.poweroff()
7284+
self._cleanup(
7285+
context, instance, network_info, block_device_info,
7286+
destroy_vifs=True,
72947287
cleanup_instance_dir=cleanup_instance_dir,
72957288
cleanup_instance_disks=cleanup_instance_disks)
72967289
raise exception.VirtualInterfaceCreateException()
72977290
except Exception:
72987291
# Any other error, be sure to clean up
72997292
LOG.error('Failed to start libvirt guest', instance=instance)
73007293
with excutils.save_and_reraise_exception():
7301-
self._cleanup_failed_start(
7302-
context, instance, network_info, block_device_info, guest,
7294+
self._cleanup(
7295+
context, instance, network_info, block_device_info,
7296+
destroy_vifs=True,
73037297
cleanup_instance_dir=cleanup_instance_dir,
73047298
cleanup_instance_disks=cleanup_instance_disks)
7299+
73057300
# Resume only if domain has been paused
73067301
if pause:
7307-
assert guest is not None
73087302
guest.resume()
7303+
73097304
return guest
73107305

73117306
def _get_pcpu_available(self):

0 commit comments

Comments
 (0)