Skip to content

Commit 560317c

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. NOTE(artom) While the following paragraph still applies, the poison patch will not be backported. Stubbing out use of netifaces.interfaces() is still a good thing to do, however. 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 (cherry picked from commit 30d8159)
1 parent 950e044 commit 560317c

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
@@ -1772,6 +1772,8 @@ def setUp(self):
17721772

17731773
self.useFixture(
17741774
fixtures.MockPatch('nova.virt.libvirt.utils.get_fs_info'))
1775+
self.useFixture(
1776+
fixtures.MockPatch('nova.compute.utils.get_machine_ips'))
17751777

17761778
# libvirt driver needs to call out to the filesystem to get the
17771779
# 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
@@ -894,6 +894,8 @@ def setUp(self):
894894
'resolve_driver_format',
895895
imagebackend.Image._get_driver_format)
896896

897+
self.stub_out('nova.compute.utils.get_machine_ips', lambda: [])
898+
897899
self.useFixture(fakelibvirt.FakeLibvirtFixture())
898900
self.test_instance = _create_test_instance()
899901
self.test_image_meta = {
@@ -13801,16 +13803,22 @@ def test_get_host_ip_addr(self):
1380113803

1380213804
@mock.patch.object(libvirt_driver.LOG, 'warning')
1380313805
@mock.patch('nova.compute.utils.get_machine_ips')
13804-
def test_get_host_ip_addr_failure(self, mock_ips, mock_log):
13806+
def test_check_my_ip(self, mock_ips, mock_log):
1380513807
mock_ips.return_value = ['8.8.8.8', '75.75.75.75']
1380613808
drvr = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False)
13807-
drvr.get_host_ip_addr()
13809+
drvr._check_my_ip()
1380813810
mock_log.assert_called_once_with(u'my_ip address (%(my_ip)s) was '
1380913811
u'not found on any of the '
1381013812
u'interfaces: %(ifaces)s',
1381113813
{'ifaces': '8.8.8.8, 75.75.75.75',
1381213814
'my_ip': mock.ANY})
1381313815

13816+
def test_init_host_checks_ip(self):
13817+
drvr = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), True)
13818+
with mock.patch.object(drvr, '_check_my_ip') as mock_check:
13819+
drvr.init_host('fake-host')
13820+
mock_check.assert_called_once_with()
13821+
1381413822
def test_conn_event_handler(self):
1381513823
drvr = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), True)
1381613824
service_mock = mock.MagicMock()
@@ -21446,6 +21454,8 @@ def test_get_existing_mdevs_not_assigned(self, get_all_assigned_mdevs,
2144621454
self.assertEqual(set([uuids.mdev1]),
2144721455
drvr._get_existing_mdevs_not_assigned())
2144821456

21457+
@mock.patch('nova.compute.utils.get_machine_ips',
21458+
new=mock.Mock(return_value=[]))
2144921459
@mock.patch.object(nova.privsep.libvirt, 'create_mdev')
2145021460
@mock.patch.object(libvirt_driver.LibvirtDriver,
2145121461
'_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
@@ -900,6 +900,7 @@ def setUp(self):
900900
# will try to execute some commands which hangs tests so let's just
901901
# stub out the unplug call to os-vif since we don't care about it.
902902
self.stub_out('os_vif.unplug', lambda a, kw: None)
903+
self.stub_out('nova.compute.utils.get_machine_ips', lambda: [])
903904

904905
def test_force_hard_reboot(self):
905906
self.flags(wait_soft_reboot_seconds=0, group='libvirt')

nova/virt/libvirt/driver.py

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

492492
self._check_file_backed_memory_support()
493493

494+
self._check_my_ip()
495+
494496
if (CONF.libvirt.virt_type == 'lxc' and
495497
not (CONF.libvirt.uid_maps and CONF.libvirt.gid_maps)):
496498
LOG.warning("Running libvirt-lxc without user namespaces is "
@@ -643,6 +645,13 @@ def _check_file_backed_memory_support(self):
643645
'Running Nova with file_backed_memory requires '
644646
'ram_allocation_ratio configured to 1.0')
645647

648+
def _check_my_ip(self):
649+
ips = compute_utils.get_machine_ips()
650+
if CONF.my_ip not in ips:
651+
LOG.warning('my_ip address (%(my_ip)s) was not found on '
652+
'any of the interfaces: %(ifaces)s',
653+
{'my_ip': CONF.my_ip, 'ifaces': ", ".join(ips)})
654+
646655
def _prepare_migration_flags(self):
647656
migration_flags = 0
648657

@@ -3279,11 +3288,6 @@ def get_console_output(self, context, instance):
32793288
return self._get_console_output_file(instance, console_path)
32803289

32813290
def get_host_ip_addr(self):
3282-
ips = compute_utils.get_machine_ips()
3283-
if CONF.my_ip not in ips:
3284-
LOG.warning('my_ip address (%(my_ip)s) was not found on '
3285-
'any of the interfaces: %(ifaces)s',
3286-
{'my_ip': CONF.my_ip, 'ifaces': ", ".join(ips)})
32873291
return CONF.my_ip
32883292

32893293
def get_vnc_console(self, context, instance):

0 commit comments

Comments
 (0)