From f364e8ebe3eb466bef79d6bafd926460971f601b Mon Sep 17 00:00:00 2001 From: Joe Gordon Date: Mon, 5 Aug 2013 19:47:12 -0700 Subject: [PATCH] libvirt: sync get_available_resources and get_host_stats get_available_resources is actually used by the scheduler while get_host_stats was previously used, and is still used for host capabilities. This patch makes get_available_resources and get_host_stats use the same logic to clean up the code. As part of making them use the same logic, some of the unused data returned from get_host_stats is changed to be what get_available_resources expects. This is also in preparation for removing the periodic RPC fanout from compute nodes to the scheduler. This patch cleans up libvirt and fake drivers only. This is not needed for other virt backends as this is a cleanup only. Further cleanup of libvirt and other drivers will happen after get_host_stats isn't used for the compute fanout to schedulers. Because this makes a change to the fake driver, several api samples needed to be changed as well. The fake driver is changed so the test_virt_driver tests can continue to be used Part of bp no-compute-fanout-to-scheduler Change-Id: I1eec5c117a1cb0490e9f9c09e731909bc31698a9 --- .../os-hypervisors/hypervisors-show-resp.json | 4 +- .../os-hypervisors/hypervisors-show-resp.xml | 4 +- .../hypervisors-show-resp.json.tpl | 2 +- .../hypervisors-show-resp.xml.tpl | 2 +- nova/tests/virt/libvirt/test_libvirt.py | 21 +++--- nova/tests/virt/test_virt_drivers.py | 20 ++++-- nova/virt/fake.py | 25 ++++---- nova/virt/libvirt/driver.py | 64 ++++++++----------- 8 files changed, 69 insertions(+), 73 deletions(-) diff --git a/doc/api_samples/os-hypervisors/hypervisors-show-resp.json b/doc/api_samples/os-hypervisors/hypervisors-show-resp.json index c0feecd02c..59ac652331 100644 --- a/doc/api_samples/os-hypervisors/hypervisors-show-resp.json +++ b/doc/api_samples/os-hypervisors/hypervisors-show-resp.json @@ -2,7 +2,7 @@ "hypervisor": { "cpu_info": "?", "current_workload": 0, - "disk_available_least": null, + "disk_available_least": 0, "free_disk_gb": 1028, "free_ram_mb": 7680, "hypervisor_hostname": "fake-mini", @@ -21,4 +21,4 @@ "vcpus": 1, "vcpus_used": 0 } -} \ No newline at end of file +} diff --git a/doc/api_samples/os-hypervisors/hypervisors-show-resp.xml b/doc/api_samples/os-hypervisors/hypervisors-show-resp.xml index 04feef0671..3b21782c07 100644 --- a/doc/api_samples/os-hypervisors/hypervisors-show-resp.xml +++ b/doc/api_samples/os-hypervisors/hypervisors-show-resp.xml @@ -1,4 +1,4 @@ - + - \ No newline at end of file + diff --git a/nova/tests/integrated/api_samples/os-hypervisors/hypervisors-show-resp.json.tpl b/nova/tests/integrated/api_samples/os-hypervisors/hypervisors-show-resp.json.tpl index 4eaded8d75..c9638423d2 100644 --- a/nova/tests/integrated/api_samples/os-hypervisors/hypervisors-show-resp.json.tpl +++ b/nova/tests/integrated/api_samples/os-hypervisors/hypervisors-show-resp.json.tpl @@ -2,7 +2,7 @@ "hypervisor": { "cpu_info": "?", "current_workload": 0, - "disk_available_least": null, + "disk_available_least": 0, "free_disk_gb": 1028, "free_ram_mb": 7680, "hypervisor_hostname": "fake-mini", diff --git a/nova/tests/integrated/api_samples/os-hypervisors/hypervisors-show-resp.xml.tpl b/nova/tests/integrated/api_samples/os-hypervisors/hypervisors-show-resp.xml.tpl index 336f23be29..d7af1246c9 100644 --- a/nova/tests/integrated/api_samples/os-hypervisors/hypervisors-show-resp.xml.tpl +++ b/nova/tests/integrated/api_samples/os-hypervisors/hypervisors-show-resp.xml.tpl @@ -1,4 +1,4 @@ - + diff --git a/nova/tests/virt/libvirt/test_libvirt.py b/nova/tests/virt/libvirt/test_libvirt.py index 07bd7716e1..5ed05894bd 100644 --- a/nova/tests/virt/libvirt/test_libvirt.py +++ b/nova/tests/virt/libvirt/test_libvirt.py @@ -4418,6 +4418,9 @@ class HostStateTestCase(test.TestCase): def get_cpu_info(self): return HostStateTestCase.cpu_info + def get_disk_over_committed_size_total(self): + return 0 + def get_local_gb_info(self): return {'total': 100, 'used': 20, 'free': 80} @@ -4450,22 +4453,22 @@ class HostStateTestCase(test.TestCase): hs = libvirt_driver.HostState(self.FakeConnection()) stats = hs._stats self.assertEquals(stats["vcpus"], 1) + self.assertEquals(stats["memory_mb"], 497) + self.assertEquals(stats["local_gb"], 100) self.assertEquals(stats["vcpus_used"], 0) - self.assertEquals(stats["cpu_info"], + self.assertEquals(stats["memory_mb_used"], 88) + self.assertEquals(stats["local_gb_used"], 20) + self.assertEquals(stats["hypervisor_type"], 'QEMU') + self.assertEquals(stats["hypervisor_version"], 13091) + self.assertEquals(stats["hypervisor_hostname"], 'compute1') + self.assertEquals(jsonutils.loads(stats["cpu_info"]), {"vendor": "Intel", "model": "pentium", "arch": "i686", "features": ["ssse3", "monitor", "pni", "sse2", "sse", "fxsr", "clflush", "pse36", "pat", "cmov", "mca", "pge", "mtrr", "sep", "apic"], "topology": {"cores": "1", "threads": "1", "sockets": "1"} }) - self.assertEquals(stats["disk_total"], 100) - self.assertEquals(stats["disk_used"], 20) - self.assertEquals(stats["disk_available"], 80) - self.assertEquals(stats["host_memory_total"], 497) - self.assertEquals(stats["host_memory_free"], 409) - self.assertEquals(stats["hypervisor_type"], 'QEMU') - self.assertEquals(stats["hypervisor_version"], 13091) - self.assertEquals(stats["hypervisor_hostname"], 'compute1') + self.assertEquals(stats["disk_available_least"], 80) class NWFilterFakes: diff --git a/nova/tests/virt/test_virt_drivers.py b/nova/tests/virt/test_virt_drivers.py index 50bbf43ca7..7583bafc13 100644 --- a/nova/tests/virt/test_virt_drivers.py +++ b/nova/tests/virt/test_virt_drivers.py @@ -567,16 +567,23 @@ class _VirtDriverTestCase(_FakeDriverBackendTestCase): lambda *a: None, lambda *a: None) @catch_notimplementederror - def _check_host_status_fields(self, host_status): - self.assertIn('disk_total', host_status) - self.assertIn('disk_used', host_status) - self.assertIn('host_memory_total', host_status) - self.assertIn('host_memory_free', host_status) + def _check_available_resouce_fields(self, host_status): + keys = ['vcpus', 'memory_mb', 'local_gb', 'vcpus_used', + 'memory_mb_used', 'hypervisor_type', 'hypervisor_version', + 'hypervisor_hostname', 'cpu_info', 'disk_available_least'] + for key in keys: + self.assertIn(key, host_status) @catch_notimplementederror def test_get_host_stats(self): host_status = self.connection.get_host_stats() - self._check_host_status_fields(host_status) + self._check_available_resouce_fields(host_status) + + @catch_notimplementederror + def test_get_available_resource(self): + available_resource = self.connection.get_available_resource( + 'myhostname') + self._check_available_resouce_fields(available_resource) @catch_notimplementederror def test_set_host_enabled(self): @@ -682,6 +689,7 @@ class AbstractDriverTestCase(_VirtDriverTestCase, test.TestCase): class FakeConnectionTestCase(_VirtDriverTestCase, test.TestCase): def setUp(self): self.driver_module = 'nova.virt.fake.FakeDriver' + fake.set_nodes(['myhostname']) super(FakeConnectionTestCase, self).setUp() diff --git a/nova/virt/fake.py b/nova/virt/fake.py index cbe251c2d2..817c97a798 100755 --- a/nova/virt/fake.py +++ b/nova/virt/fake.py @@ -90,21 +90,17 @@ class FakeDriver(driver.ComputeDriver): super(FakeDriver, self).__init__(virtapi) self.instances = {} self.host_status_base = { - 'host_name-description': 'Fake Host', - 'host_hostname': CONF.host, - 'host_memory_total': 8000000000, - 'host_memory_overhead': 10000000, - 'host_memory_free': 7900000000, - 'host_memory_free_computed': 7900000000, - 'host_other_config': {}, - 'host_ip_address': '192.168.1.109', - 'host_cpu_info': {}, - 'disk_available': 500000000000, - 'disk_total': 600000000000, - 'disk_used': 100000000000, - 'host_uuid': 'cedb9b39-9388-41df-8891-c5c9a0c0fe5f', - 'host_name_label': 'fake-host', + 'vcpus': 100000, + 'memory_mb': 8000000000, + 'local_gb': 600000000000, + 'vcpus_used': 0, + 'memory_mb_used': 0, + 'local_gb_used': 100000000000, + 'hypervisor_type': 'fake', + 'hypervisor_version': '1.0', 'hypervisor_hostname': CONF.host, + 'cpu_info': {}, + 'disk_available_least': 500000000000, } self._mounts = {} self._interfaces = {} @@ -354,6 +350,7 @@ class FakeDriver(driver.ComputeDriver): 'hypervisor_type': 'fake', 'hypervisor_version': '1.0', 'hypervisor_hostname': nodename, + 'disk_available_least': 0, 'cpu_info': '?'} return dic diff --git a/nova/virt/libvirt/driver.py b/nova/virt/libvirt/driver.py index a192a6f6a5..4f02db19e9 100755 --- a/nova/virt/libvirt/driver.py +++ b/nova/virt/libvirt/driver.py @@ -3090,36 +3090,7 @@ class LibvirtDriver(driver.ComputeDriver): :returns: dictionary describing resources """ - - def _get_disk_available_least(): - """Return total real disk available least size. - - The size of available disk, when block_migration command given - disk_over_commit param is FALSE. - - The size that deducted real instance disk size from the total size - of the virtual disk of all instances. - - """ - disk_free_gb = disk_info_dict['free'] - disk_over_committed = self.get_disk_over_committed_size_total() - # Disk available least size - available_least = disk_free_gb * (1024 ** 3) - disk_over_committed - return (available_least / (1024 ** 3)) - - disk_info_dict = self.get_local_gb_info() - dic = {'vcpus': self.get_vcpu_total(), - 'memory_mb': self.get_memory_mb_total(), - 'local_gb': disk_info_dict['total'], - 'vcpus_used': self.get_vcpu_used(), - 'memory_mb_used': self.get_memory_mb_used(), - 'local_gb_used': disk_info_dict['used'], - 'hypervisor_type': self.get_hypervisor_type(), - 'hypervisor_version': self.get_hypervisor_version(), - 'hypervisor_hostname': self.get_hypervisor_hostname(), - 'cpu_info': self.get_cpu_info(), - 'disk_available_least': _get_disk_available_least()} - return dic + return self.host_state.get_host_stats(refresh=True) def check_instance_shared_storage_local(self, context, instance): dirpath = libvirt_utils.get_instance_path(instance) @@ -4125,21 +4096,38 @@ class HostState(object): def update_status(self): """Retrieve status info from libvirt.""" + def _get_disk_available_least(): + """Return total real disk available least size. + + The size of available disk, when block_migration command given + disk_over_commit param is FALSE. + + The size that deducted real instance disk size from the total size + of the virtual disk of all instances. + + """ + disk_free_gb = disk_info_dict['free'] + disk_over_committed = (self.driver. + get_disk_over_committed_size_total()) + # Disk available least size + available_least = disk_free_gb * (1024 ** 3) - disk_over_committed + return (available_least / (1024 ** 3)) + LOG.debug(_("Updating host stats")) + disk_info_dict = self.driver.get_local_gb_info() data = {} data["vcpus"] = self.driver.get_vcpu_total() + data["memory_mb"] = self.driver.get_memory_mb_total() + data["local_gb"] = disk_info_dict['total'] data["vcpus_used"] = self.driver.get_vcpu_used() - data["cpu_info"] = jsonutils.loads(self.driver.get_cpu_info()) - disk_info_dict = self.driver.get_local_gb_info() - data["disk_total"] = disk_info_dict['total'] - data["disk_used"] = disk_info_dict['used'] - data["disk_available"] = disk_info_dict['free'] - data["host_memory_total"] = self.driver.get_memory_mb_total() - data["host_memory_free"] = (data["host_memory_total"] - - self.driver.get_memory_mb_used()) + data["memory_mb_used"] = self.driver.get_memory_mb_used() + data["local_gb_used"] = disk_info_dict['used'] data["hypervisor_type"] = self.driver.get_hypervisor_type() data["hypervisor_version"] = self.driver.get_hypervisor_version() data["hypervisor_hostname"] = self.driver.get_hypervisor_hostname() + data["cpu_info"] = self.driver.get_cpu_info() + data['disk_available_least'] = _get_disk_available_least() + data["supported_instances"] = \ self.driver.get_instance_capabilities()