Skip to content

Commit 7317cfb

Browse files
author
Balazs Gibizer
committed
Consolidate device detach error handling
After I7f2b6330decb92e2838aa7cee47fb228f00f47da the error handling of the callers of _detach_with_retry() can be consolidated. Libvirt related errors now handled in _detach_sync() and various DeviceNotFound cases now don't need special handling on the caller side. Change-Id: Ic6eb66bc2396949aceecda50bda334a1bc7dab31
1 parent 257b4d8 commit 7317cfb

File tree

2 files changed

+63
-122
lines changed

2 files changed

+63
-122
lines changed

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

Lines changed: 56 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -18589,6 +18589,7 @@ def _test_attach_detach_interface_get_config(self, method_name):
1858918589
lambda self, instance: FakeVirtDomain())
1859018590

1859118591
instance = objects.Instance(**self.test_instance)
18592+
instance.info_cache = None
1859218593
network_info = _fake_network_info(self)
1859318594
drvr = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), True)
1859418595

@@ -23013,23 +23014,20 @@ def _test_detach_interface(self, state, device_not_found=False):
2301323014
# This will trigger _detach_with_retry to raise
2301423015
# DeviceNotFound
2301523016
get_interface_calls = [
23016-
expected_cfg, # detach_interface() itself gets the config
2301723017
expected_cfg, # _detach_with_retry: persistent config
2301823018
None, # _detach_with_retry: no device in live config
2301923019
None, # _detach_with_retry: persistent config gone as detached
2302023020
]
2302123021
else:
2302223022
if state in (power_state.RUNNING, power_state.PAUSED):
2302323023
get_interface_calls = [
23024-
expected_cfg, # detach_interface() itself gets the config
2302523024
expected_cfg, # _detach_with_retry: persistent config
2302623025
expected_cfg, # _detach_with_retry: live config
2302723026
None, # _detach_with_retry: persistent config gone
2302823027
None # _detach_with_retry: live config gone
2302923028
]
2303023029
else:
2303123030
get_interface_calls = [
23032-
expected_cfg, # detach_interface() itself gets the config
2303323031
expected_cfg, # _detach_with_retry: persistent config
2303423032
None, # _detach_with_retry: persistent config gone
2303523033
]
@@ -23064,7 +23062,6 @@ def _test_detach_interface(self, state, device_not_found=False):
2306423062
flags=fakelibvirt.VIR_DOMAIN_AFFECT_CONFIG)
2306523063
mock_get_interface.assert_has_calls(
2306623064
[
23067-
mock.call(expected_cfg),
2306823065
mock.call(expected_cfg, from_persistent_config=True),
2306923066
mock.call(expected_cfg),
2307023067
mock.call(expected_cfg, from_persistent_config=True),
@@ -23082,7 +23079,6 @@ def _test_detach_interface(self, state, device_not_found=False):
2308223079
])
2308323080
mock_get_interface.assert_has_calls(
2308423081
[
23085-
mock.call(expected_cfg),
2308623082
mock.call(expected_cfg, from_persistent_config=True),
2308723083
mock.call(expected_cfg),
2308823084
mock.call(expected_cfg, from_persistent_config=True),
@@ -23094,7 +23090,6 @@ def _test_detach_interface(self, state, device_not_found=False):
2309423090
flags=fakelibvirt.VIR_DOMAIN_AFFECT_CONFIG)
2309523091
mock_get_interface.assert_has_calls(
2309623092
[
23097-
mock.call(expected_cfg),
2309823093
mock.call(expected_cfg, from_persistent_config=True),
2309923094
mock.call(expected_cfg, from_persistent_config=True),
2310023095
])
@@ -23120,6 +23115,7 @@ def test_detach_interface_device_not_found(self, mock_log):
2312023115
# Asserts that we don't log an error when the interface device is not
2312123116
# found on the guest after a libvirt error during detach.
2312223117
instance = self._create_instance()
23118+
instance.info_cache = None
2312323119
vif = _fake_network_info(self)[0]
2312423120
guest = mock.Mock(spec=libvirt_guest.Guest)
2312523121
guest.get_power_state = mock.Mock()
@@ -23137,36 +23133,6 @@ def test_detach_interface_device_not_found(self, mock_log):
2313723133
self.assertIn('the device is no longer found on the guest',
2313823134
str(mock_log.warning.call_args[0]))
2313923135

23140-
@mock.patch('nova.virt.libvirt.driver.LibvirtDriver.'
23141-
'_detach_with_retry')
23142-
@mock.patch('nova.virt.libvirt.driver.LOG')
23143-
def test_detach_interface_guest_not_found_after_detach(
23144-
self, mock_log, mock_detach_with_retry
23145-
):
23146-
# Asserts that we don't raise an exception when the guest is gone
23147-
# after a libvirt error during detach.
23148-
instance = self._create_instance()
23149-
vif = _fake_network_info(self, 1)[0]
23150-
guest = mock.MagicMock()
23151-
guest.get_power_state.return_value = power_state.RUNNING
23152-
guest.get_interface_by_cfg.return_value = (
23153-
vconfig.LibvirtConfigGuestInterface())
23154-
get_guest_mock = mock.Mock()
23155-
# Host.get_guest should be called twice: the first time it is found,
23156-
# the second time it is gone.
23157-
get_guest_mock.side_effect = (
23158-
guest, exception.InstanceNotFound(instance_id=instance.uuid))
23159-
self.drvr._host.get_guest = get_guest_mock
23160-
error = fakelibvirt.libvirtError(
23161-
'internal error: End of file from qemu monitor')
23162-
error.err = (fakelibvirt.VIR_ERR_OPERATION_FAILED,)
23163-
mock_detach_with_retry.side_effect = error
23164-
self.drvr.detach_interface(self.context, instance, vif)
23165-
self.assertEqual(1, mock_log.info.call_count)
23166-
self.assertIn('Instance disappeared while detaching interface',
23167-
mock_log.info.call_args[0][0])
23168-
get_guest_mock.assert_has_calls([mock.call(instance)] * 2)
23169-
2317023136
@mock.patch('threading.Event.wait', new=mock.Mock())
2317123137
@mock.patch.object(FakeVirtDomain, 'info')
2317223138
@mock.patch.object(FakeVirtDomain, 'detachDeviceFlags')
@@ -23210,7 +23176,6 @@ def test_detach_interface_device_with_same_mac_address(
2321023176
mock.patch.object(
2321123177
libvirt_guest.Guest, 'get_interface_by_cfg',
2321223178
side_effect=[
23213-
expected, # detach_interface gets the config
2321423179
expected, # _detach_with_retry: persistent config
2321523180
expected, # _detach_with_retry: live config
2321623181
None, # _detach_with_retry: persistent gone
@@ -23224,14 +23189,12 @@ def test_detach_interface_device_with_same_mac_address(
2322423189

2322523190
mock_get_interface.assert_has_calls(
2322623191
[
23227-
mock.call(expected, ),
2322823192
mock.call(expected, from_persistent_config=True),
2322923193
mock.call(expected),
2323023194
mock.call(expected, from_persistent_config=True),
2323123195
mock.call(expected),
2323223196
]
2323323197
)
23234-
self.assertEqual(5, mock_get_interface.call_count)
2323523198
mock_get_config.assert_called_once_with(
2323623199
instance, network_info[0], test.MatchType(objects.ImageMeta),
2323723200
test.MatchType(objects.Flavor), CONF.libvirt.virt_type)
@@ -23253,7 +23216,6 @@ def test_detach_interface_guest_set_metadata(self):
2325323216
instance = self._create_instance()
2325423217
network_info = _fake_network_info(self, num_networks=3)
2325523218
vif = network_info[0]
23256-
interface = vconfig.LibvirtConfigGuestInterface()
2325723219
image_meta = objects.ImageMeta.from_dict({})
2325823220
disk_info = blockinfo.get_disk_info(
2325923221
CONF.libvirt.virt_type, instance, image_meta)
@@ -23266,8 +23228,6 @@ def test_detach_interface_guest_set_metadata(self):
2326623228
self.drvr._host, 'get_guest', return_value=guest),
2326723229
mock.patch.object(
2326823230
self.drvr.vif_driver, 'get_config', return_value=cfg),
23269-
mock.patch.object(
23270-
guest, 'get_interface_by_cfg', return_value=interface),
2327123231
mock.patch.object(
2327223232
instance, 'get_network_info', return_value=network_info),
2327323233
mock.patch.object(
@@ -23276,16 +23236,15 @@ def test_detach_interface_guest_set_metadata(self):
2327623236
self.drvr, '_get_guest_config_meta', return_value=config_meta),
2327723237
mock.patch.object(guest, 'set_metadata')
2327823238
) as (
23279-
mock_get_guest, mock_get_config, mock_get_interface_by_cfg,
23280-
mock_get_network_info, mock_detach_with_retry,
23281-
mock_get_guest_config_meta, mock_set_metadata
23239+
mock_get_guest, mock_get_config, mock_get_network_info,
23240+
mock_detach_with_retry, mock_get_guest_config_meta,
23241+
mock_set_metadata
2328223242
):
2328323243
self.drvr.detach_interface(self.context, instance, vif)
2328423244
mock_get_guest.assert_called_once_with(instance)
2328523245
mock_get_config.assert_called_once_with(
2328623246
instance, vif, test.MatchType(objects.ImageMeta),
2328723247
test.MatchType(objects.Flavor), CONF.libvirt.virt_type)
23288-
mock_get_interface_by_cfg.assert_called_once_with(cfg)
2328923248
mock_detach_with_retry.assert_called_once_with(
2329023249
guest, instance.uuid, mock.ANY, device_name=None)
2329123250
mock_get_network_info.assert_called_once_with()
@@ -23847,6 +23806,56 @@ def test__detach_with_retry_libvirt_reports_not_found_give_up(self):
2384723806
mock_guest.detach_device.assert_called_once_with(
2384823807
mock_dev, persistent=True, live=False)
2384923808

23809+
@mock.patch.object(libvirt_driver.LOG, 'warning')
23810+
def test__detach_with_retry_libvirt_reports_domain_not_found(
23811+
self, mock_warning
23812+
):
23813+
"""Test that libvirt reports that the domain is not found and assert
23814+
that it is only logged.
23815+
"""
23816+
drvr = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False)
23817+
mock_guest = mock.Mock(spec=libvirt_guest.Guest)
23818+
mock_guest.get_power_state.return_value = power_state.SHUTDOWN
23819+
23820+
mock_dev = mock.Mock(spec=vconfig.LibvirtConfigGuestDisk)
23821+
mock_dev.alias = 'virtio-disk1'
23822+
23823+
mock_get_device_conf_func = mock.Mock(
23824+
# The first call is to get the device from the persistent domain
23825+
# before the detach. The second calls to double check that the
23826+
# device is gone after libvirt returned. This second call is
23827+
# pointless in the current case as libvirt returned that the domain
23828+
# does not exists. So the code could know that there is no device.
23829+
# Still for simplicity the call is made and expected to return None
23830+
side_effect=[
23831+
mock_dev,
23832+
None,
23833+
]
23834+
)
23835+
23836+
mock_guest.detach_device.side_effect = fakelibvirt.make_libvirtError(
23837+
fakelibvirt.libvirtError,
23838+
msg='error',
23839+
error_code=fakelibvirt.VIR_ERR_NO_DOMAIN)
23840+
23841+
drvr._detach_with_retry(
23842+
mock_guest,
23843+
uuids.instance_uuid,
23844+
mock_get_device_conf_func,
23845+
device_name='vdb',
23846+
)
23847+
23848+
mock_guest.has_persistent_configuration.assert_called_once_with()
23849+
mock_get_device_conf_func.assert_has_calls([
23850+
mock.call(from_persistent_config=True),
23851+
mock.call(from_persistent_config=True),
23852+
])
23853+
mock_guest.detach_device.assert_called_once_with(
23854+
mock_dev, persistent=True, live=False)
23855+
mock_warning.assert_called_once_with(
23856+
'During device detach, instance disappeared.',
23857+
instance_uuid=uuids.instance_uuid)
23858+
2385023859
@ddt.data(power_state.RUNNING, power_state.PAUSED)
2385123860
def test__detach_with_retry_other_sync_libvirt_error(self, state):
2385223861
"""Test that libvirt reports non device related error during detach
@@ -23865,7 +23874,7 @@ def test__detach_with_retry_other_sync_libvirt_error(self, state):
2386523874
mock_guest.detach_device.side_effect = fakelibvirt.make_libvirtError(
2386623875
fakelibvirt.libvirtError,
2386723876
msg='error',
23868-
error_code=fakelibvirt.VIR_ERR_NO_DOMAIN)
23877+
error_code=fakelibvirt.VIR_ERR_INTERNAL_ERROR)
2386923878

2387023879
self.assertRaises(
2387123880
fakelibvirt.libvirtError,

nova/virt/libvirt/driver.py

Lines changed: 7 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -2526,6 +2526,13 @@ def _detach_sync(
25262526
'still being in progress.', device_name, instance_uuid)
25272527
return
25282528

2529+
if code == libvirt.VIR_ERR_NO_DOMAIN:
2530+
LOG.warning(
2531+
"During device detach, instance disappeared.",
2532+
instance_uuid=instance_uuid)
2533+
# if the domain has disappeared then we have nothing to detach
2534+
return
2535+
25292536
LOG.warning(
25302537
'Unexpected libvirt error while detaching device %s from '
25312538
'instance %s: %s', device_name, instance_uuid, str(ex))
@@ -2560,17 +2567,6 @@ def detach_volume(self, context, connection_info, instance, mountpoint,
25602567
# call.
25612568
LOG.info("Device %s not found in instance.",
25622569
disk_dev, instance=instance)
2563-
except libvirt.libvirtError as ex:
2564-
# NOTE(vish): This is called to cleanup volumes after live
2565-
# migration, so we should still disconnect even if
2566-
# the instance doesn't exist here anymore.
2567-
error_code = ex.get_error_code()
2568-
if error_code == libvirt.VIR_ERR_NO_DOMAIN:
2569-
# NOTE(vish):
2570-
LOG.warning("During detach_volume, instance disappeared.",
2571-
instance=instance)
2572-
else:
2573-
raise
25742570

25752571
self._disconnect_volume(context, connection_info, instance,
25762572
encryption=encryption)
@@ -2737,83 +2733,19 @@ def detach_interface(self, context, instance, vif):
27372733
instance.image_meta,
27382734
instance.flavor,
27392735
CONF.libvirt.virt_type)
2740-
interface = guest.get_interface_by_cfg(cfg)
27412736
try:
2742-
# NOTE(mriedem): When deleting an instance and using Neutron,
2743-
# we can be racing against Neutron deleting the port and
2744-
# sending the vif-deleted event which then triggers a call to
2745-
# detach the interface, so if the interface is not found then
2746-
# we can just log it as a warning.
2747-
if not interface:
2748-
mac = vif.get('address')
2749-
# The interface is gone so just log it as a warning.
2750-
LOG.warning('Detaching interface %(mac)s failed because '
2751-
'the device is no longer found on the guest.',
2752-
{'mac': mac}, instance=instance)
2753-
return
2754-
27552737
get_dev = functools.partial(guest.get_interface_by_cfg, cfg)
27562738
self._detach_with_retry(
27572739
guest,
27582740
instance.uuid,
27592741
get_dev,
27602742
device_name=self.vif_driver.get_vif_devname(vif),
27612743
)
2762-
except exception.DeviceDetachFailed:
2763-
# We failed to detach the device even with the retry loop, so let's
2764-
# dump some debug information to the logs before raising back up.
2765-
with excutils.save_and_reraise_exception():
2766-
devname = self.vif_driver.get_vif_devname(vif)
2767-
interface = guest.get_interface_by_cfg(cfg)
2768-
if interface:
2769-
LOG.warning(
2770-
'Failed to detach interface %(devname)s after '
2771-
'repeated attempts. Final interface xml:\n'
2772-
'%(interface_xml)s\nFinal guest xml:\n%(guest_xml)s',
2773-
{'devname': devname,
2774-
'interface_xml': interface.to_xml(),
2775-
'guest_xml': guest.get_xml_desc()},
2776-
instance=instance)
27772744
except exception.DeviceNotFound:
27782745
# The interface is gone so just log it as a warning.
27792746
LOG.warning('Detaching interface %(mac)s failed because '
27802747
'the device is no longer found on the guest.',
27812748
{'mac': vif.get('address')}, instance=instance)
2782-
except libvirt.libvirtError as ex:
2783-
error_code = ex.get_error_code()
2784-
if error_code == libvirt.VIR_ERR_NO_DOMAIN:
2785-
LOG.warning("During detach_interface, instance disappeared.",
2786-
instance=instance)
2787-
else:
2788-
# NOTE(mriedem): When deleting an instance and using Neutron,
2789-
# we can be racing against Neutron deleting the port and
2790-
# sending the vif-deleted event which then triggers a call to
2791-
# detach the interface, so we might have failed because the
2792-
# network device no longer exists. Libvirt will fail with
2793-
# "operation failed: no matching network device was found"
2794-
# which unfortunately does not have a unique error code so we
2795-
# need to look up the interface by config and if it's not found
2796-
# then we can just log it as a warning rather than tracing an
2797-
# error.
2798-
mac = vif.get('address')
2799-
# Get a fresh instance of the guest in case it is gone.
2800-
try:
2801-
guest = self._host.get_guest(instance)
2802-
except exception.InstanceNotFound:
2803-
LOG.info("Instance disappeared while detaching interface "
2804-
"%s", vif['id'], instance=instance)
2805-
return
2806-
interface = guest.get_interface_by_cfg(cfg)
2807-
if interface:
2808-
LOG.error('detaching network adapter failed.',
2809-
instance=instance, exc_info=True)
2810-
raise exception.InterfaceDetachFailed(
2811-
instance_uuid=instance.uuid)
2812-
2813-
# The interface is gone so just log it as a warning.
2814-
LOG.warning('Detaching interface %(mac)s failed because '
2815-
'the device is no longer found on the guest.',
2816-
{'mac': mac}, instance=instance)
28172749
finally:
28182750
# NOTE(gibi): we need to unplug the vif _after_ the detach is done
28192751
# on the libvirt side as otherwise libvirt will still manage the

0 commit comments

Comments
 (0)