Skip to content

Commit 30d8159

Browse files
committed
libvirt: move checking CONF.my_ip to init_host()
Migrations use the libvirt driver's get_host_ip_addr() method to determine the dest_host field of the migration object. get_host_ip_addr() checks whether CONF.my_ip is actually assigned to one of the host's interfaces. It does so by calling get_machine_ips(), which iterates over all of the host's interfaces. If the host has many interfaces, this can take a long time, and introduces needless delays in processing the migration. get_machine_ips() is only used to print a warning, so this patch moves the get_machine_ips() call to a single method in init_host(). This way, a warning is still emitted at compute service startup, and migration progress is not needlessly slowed down. This patch also has a chicken and egg problem with the patch on top of it, which poisons use of netifaces.interfaces() in tests. While this patch fixes all the tests that break with that poison, it starts breaking different tests because of the move of get_machine_ips() into init_host(). Therefore, while not directly related to the bug, this patch also preventatively mocks or stubs out any use of get_machine_ips() that will get poisoned with the subsequent patch. Closes-bug: 1837075 Change-Id: I58a4038b04d5a9c28927d914e71609e4deea3d9f
1 parent d29d1d1 commit 30d8159

File tree

4 files changed

+24
-7
lines changed

4 files changed

+24
-7
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1933,6 +1933,8 @@ def setUp(self):
19331933

19341934
self.useFixture(
19351935
fixtures.MockPatch('nova.virt.libvirt.utils.get_fs_info'))
1936+
self.useFixture(
1937+
fixtures.MockPatch('nova.compute.utils.get_machine_ips'))
19361938

19371939
# libvirt driver needs to call out to the filesystem to get the
19381940
# parent_ifname for the SRIOV VFs.

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,8 @@ def setUp(self):
886886
'resolve_driver_format',
887887
imagebackend.Image._get_driver_format)
888888

889+
self.stub_out('nova.compute.utils.get_machine_ips', lambda: [])
890+
889891
self.useFixture(fakelibvirt.FakeLibvirtFixture())
890892
self.test_instance = _create_test_instance()
891893
self.test_image_meta = {
@@ -13936,16 +13938,22 @@ def test_get_host_ip_addr(self):
1393613938

1393713939
@mock.patch.object(libvirt_driver.LOG, 'warning')
1393813940
@mock.patch('nova.compute.utils.get_machine_ips')
13939-
def test_get_host_ip_addr_failure(self, mock_ips, mock_log):
13941+
def test_check_my_ip(self, mock_ips, mock_log):
1394013942
mock_ips.return_value = ['8.8.8.8', '75.75.75.75']
1394113943
drvr = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False)
13942-
drvr.get_host_ip_addr()
13944+
drvr._check_my_ip()
1394313945
mock_log.assert_called_once_with(u'my_ip address (%(my_ip)s) was '
1394413946
u'not found on any of the '
1394513947
u'interfaces: %(ifaces)s',
1394613948
{'ifaces': '8.8.8.8, 75.75.75.75',
1394713949
'my_ip': mock.ANY})
1394813950

13951+
def test_init_host_checks_ip(self):
13952+
drvr = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), True)
13953+
with mock.patch.object(drvr, '_check_my_ip') as mock_check:
13954+
drvr.init_host('fake-host')
13955+
mock_check.assert_called_once_with()
13956+
1394913957
def test_conn_event_handler(self):
1395013958
drvr = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), True)
1395113959
service_mock = mock.MagicMock()
@@ -21661,6 +21669,8 @@ def test_get_existing_mdevs_not_assigned(self, get_all_assigned_mdevs,
2166121669
self.assertEqual(set([uuids.mdev1]),
2166221670
drvr._get_existing_mdevs_not_assigned())
2166321671

21672+
@mock.patch('nova.compute.utils.get_machine_ips',
21673+
new=mock.Mock(return_value=[]))
2166421674
@mock.patch.object(nova.privsep.libvirt, 'create_mdev')
2166521675
@mock.patch.object(libvirt_driver.LibvirtDriver,
2166621676
'_get_mdev_capable_devices')

nova/tests/unit/virt/test_virt_drivers.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,7 @@ def setUp(self):
892892
# will try to execute some commands which hangs tests so let's just
893893
# stub out the unplug call to os-vif since we don't care about it.
894894
self.stub_out('os_vif.unplug', lambda a, kw: None)
895+
self.stub_out('nova.compute.utils.get_machine_ips', lambda: [])
895896

896897
def test_init_host_image_type_rbd_force_raw_images_true(self):
897898
CONF.set_override('images_type', 'rbd', group='libvirt')

nova/virt/libvirt/driver.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,8 @@ def init_host(self, host):
540540

541541
self._check_file_backed_memory_support()
542542

543+
self._check_my_ip()
544+
543545
if (CONF.libvirt.virt_type == 'lxc' and
544546
not (CONF.libvirt.uid_maps and CONF.libvirt.gid_maps)):
545547
LOG.warning("Running libvirt-lxc without user namespaces is "
@@ -704,6 +706,13 @@ def _check_file_backed_memory_support(self):
704706
'Running Nova with file_backed_memory requires '
705707
'ram_allocation_ratio configured to 1.0')
706708

709+
def _check_my_ip(self):
710+
ips = compute_utils.get_machine_ips()
711+
if CONF.my_ip not in ips:
712+
LOG.warning('my_ip address (%(my_ip)s) was not found on '
713+
'any of the interfaces: %(ifaces)s',
714+
{'my_ip': CONF.my_ip, 'ifaces': ", ".join(ips)})
715+
707716
def _prepare_migration_flags(self):
708717
migration_flags = 0
709718

@@ -3329,11 +3338,6 @@ def get_console_output(self, context, instance):
33293338
return self._get_console_output_file(instance, console_path)
33303339

33313340
def get_host_ip_addr(self):
3332-
ips = compute_utils.get_machine_ips()
3333-
if CONF.my_ip not in ips:
3334-
LOG.warning('my_ip address (%(my_ip)s) was not found on '
3335-
'any of the interfaces: %(ifaces)s',
3336-
{'my_ip': CONF.my_ip, 'ifaces': ", ".join(ips)})
33373341
return CONF.my_ip
33383342

33393343
def get_vnc_console(self, context, instance):

0 commit comments

Comments
 (0)