From 9c8a8e3caafb51efbfeee2f592e00f681c4cd171 Mon Sep 17 00:00:00 2001 From: Eoghan Glynn Date: Tue, 11 Dec 2012 15:27:51 +0000 Subject: [PATCH] Instantiate inspector in compute manager As suggested in the review of the original inspector patch (I20700320dd7e3196507173c780ab598b479e4021), the inspector lifecycle is now controlled by the compute manager as opposed to the pollsters. Change-Id: Ifdef5ab22e7fb224e273dae5dbff3fb4dab10031 --- ceilometer/compute/manager.py | 24 ++++++++++++++++ ceilometer/compute/pollsters.py | 49 +++++---------------------------- tests/compute/test_pollsters.py | 22 +++++++-------- 3 files changed, 42 insertions(+), 53 deletions(-) diff --git a/ceilometer/compute/manager.py b/ceilometer/compute/manager.py index 0d2faa18..098f7cb2 100644 --- a/ceilometer/compute/manager.py +++ b/ceilometer/compute/manager.py @@ -16,9 +16,12 @@ # License for the specific language governing permissions and limitations # under the License. +from stevedore import driver + from ceilometer import extension_manager from ceilometer import nova_client +from ceilometer.compute.virt import inspector as virt_inspector from ceilometer.openstack.common import cfg from ceilometer.openstack.common import log from ceilometer import publish @@ -28,6 +31,10 @@ OPTS = [ default=[], help='list of compute agent pollsters to disable', ), + cfg.StrOpt('hypervisor_inspector', + default='libvirt', + help='Inspector to use for inspecting the hypervisor layer', + ), ] cfg.CONF.register_opts(OPTS) @@ -38,6 +45,18 @@ LOG = log.getLogger(__name__) PLUGIN_NAMESPACE = 'ceilometer.poll.compute' +def get_hypervisor_inspector(): + try: + namespace = 'ceilometer.compute.virt' + mgr = driver.DriverManager(namespace, + cfg.CONF.hypervisor_inspector, + invoke_on_load=True) + return mgr.driver + except ImportError as e: + LOG.error("Unable to load the hypervisor inspector: %s" % (e)) + return virt_inspector.Inspector() + + class AgentManager(object): def __init__(self): @@ -45,6 +64,7 @@ class AgentManager(object): namespace=PLUGIN_NAMESPACE, disabled_names=cfg.CONF.disabled_compute_pollsters, ) + self._inspector = get_hypervisor_inspector() return @staticmethod @@ -79,3 +99,7 @@ class AgentManager(object): for instance in nv.instance_get_all_by_host(cfg.CONF.host): if getattr(instance, 'OS-EXT-STS:vm_state', None) != 'error': self.poll_instance(context, instance) + + @property + def inspector(self): + return self._inspector diff --git a/ceilometer/compute/pollsters.py b/ceilometer/compute/pollsters.py index 6602940f..ed4d2701 100644 --- a/ceilometer/compute/pollsters.py +++ b/ceilometer/compute/pollsters.py @@ -21,26 +21,12 @@ import copy import datetime -from stevedore import driver - from ceilometer import counter from ceilometer.compute import plugin from ceilometer.compute import instance as compute_instance -from ceilometer.compute.virt import inspector as virt_inspector -from ceilometer.openstack.common import cfg -from ceilometer.openstack.common import importutils from ceilometer.openstack.common import log from ceilometer.openstack.common import timeutils -OPTS = [ - cfg.StrOpt('hypervisor_inspector', - help='Inspector to use for inspecting the hypervisor layer', - default='libvirt') -] - -CONF = cfg.CONF -CONF.register_opts(OPTS) - LOG = log.getLogger(__name__) @@ -49,18 +35,6 @@ def _instance_name(instance): return getattr(instance, 'OS-EXT-SRV-ATTR:instance_name', None) -def get_hypervisor_inspector(): - try: - namespace = 'ceilometer.compute.virt' - mgr = driver.DriverManager(namespace, - CONF.hypervisor_inspector, - invoke_on_load=True) - return mgr.driver - except ImportError as e: - LOG.error("Unable to load the hypervisor inspector: %s" % (e)) - return virt_inspector.Inspector() - - def make_counter_from_instance(instance, name, type, volume): return counter.Counter( name=name, @@ -74,16 +48,7 @@ def make_counter_from_instance(instance, name, type, volume): ) -class HypervisorPollster(plugin.ComputePollster): - - inspector = None - - def __init__(self): - if not HypervisorPollster.inspector: - HypervisorPollster.inspector = get_hypervisor_inspector() - - -class InstancePollster(HypervisorPollster): +class InstancePollster(plugin.ComputePollster): def get_counters(self, manager, instance): yield make_counter_from_instance(instance, @@ -99,7 +64,7 @@ class InstancePollster(HypervisorPollster): ) -class DiskIOPollster(HypervisorPollster): +class DiskIOPollster(plugin.ComputePollster): LOG = log.getLogger(__name__ + '.diskio') @@ -119,7 +84,7 @@ class DiskIOPollster(HypervisorPollster): r_requests = 0 w_bytes = 0 w_requests = 0 - for disk, info in self.inspector.inspect_disks(instance_name): + for disk, info in manager.inspector.inspect_disks(instance_name): self.LOG.info(self.DISKIO_USAGE_MESSAGE, instance, disk.device, info.read_requests, info.read_bytes, info.write_requests, @@ -154,7 +119,7 @@ class DiskIOPollster(HypervisorPollster): self.LOG.exception(err) -class CPUPollster(HypervisorPollster): +class CPUPollster(plugin.ComputePollster): LOG = log.getLogger(__name__ + '.cpu') @@ -181,7 +146,7 @@ class CPUPollster(HypervisorPollster): self.LOG.info('checking instance %s', instance.id) instance_name = _instance_name(instance) try: - cpu_info = self.inspector.inspect_cpus(instance_name) + cpu_info = manager.inspector.inspect_cpus(instance_name) self.LOG.info("CPUTIME USAGE: %s %d", instance.__dict__, cpu_info.time) cpu_util = self.get_cpu_util(instance, cpu_info) @@ -208,7 +173,7 @@ class CPUPollster(HypervisorPollster): self.LOG.exception(err) -class NetPollster(HypervisorPollster): +class NetPollster(plugin.ComputePollster): LOG = log.getLogger(__name__ + '.net') @@ -236,7 +201,7 @@ class NetPollster(HypervisorPollster): instance_name = _instance_name(instance) self.LOG.info('checking instance %s', instance.id) try: - for vnic, info in self.inspector.inspect_vnics(instance_name): + for vnic, info in manager.inspector.inspect_vnics(instance_name): self.LOG.info(self.NET_USAGE_MESSAGE, instance_name, vnic.name, info.rx_bytes, info.tx_bytes) yield self.make_vnic_counter(instance, diff --git a/tests/compute/test_pollsters.py b/tests/compute/test_pollsters.py index ae025030..b28331ca 100644 --- a/tests/compute/test_pollsters.py +++ b/tests/compute/test_pollsters.py @@ -35,17 +35,15 @@ class TestPollsterBase(test_base.TestCase): def setUp(self): super(TestPollsterBase, self).setUp() - self.manager = manager.AgentManager() + self.mox.StubOutWithMock(manager, 'get_hypervisor_inspector') + self.inspector = self.mox.CreateMock(virt_inspector.Inspector) + manager.get_hypervisor_inspector().AndReturn(self.inspector) self.instance = mock.MagicMock() self.instance.name = 'instance-00000001' setattr(self.instance, 'OS-EXT-SRV-ATTR:instance_name', self.instance.name) self.instance.id = 1 self.instance.flavor = {'name': 'm1.small', 'id': 2} - self.mox.StubOutWithMock(pollsters, 'get_hypervisor_inspector') - self.inspector = self.mox.CreateMock(virt_inspector.Inspector) - pollsters.get_hypervisor_inspector().AndReturn(self.inspector) - pollsters.HypervisorPollster.inspector = None class TestInstancePollster(TestPollsterBase): @@ -56,9 +54,9 @@ class TestInstancePollster(TestPollsterBase): def test_get_counters(self): self.mox.ReplayAll() + mgr = manager.AgentManager() pollster = pollsters.InstancePollster() - counters = list(pollster.get_counters(self.manager, - self.instance)) + counters = list(pollster.get_counters(mgr, self.instance)) self.assertEquals(len(counters), 2) self.assertEqual(counters[0].name, 'instance') self.assertEqual(counters[1].name, 'instance:m1.small') @@ -79,8 +77,9 @@ class TestDiskIOPollster(TestPollsterBase): self.inspector.inspect_disks(self.instance.name).AndReturn(disks) self.mox.ReplayAll() + mgr = manager.AgentManager() pollster = pollsters.DiskIOPollster() - counters = list(pollster.get_counters(self.manager, self.instance)) + counters = list(pollster.get_counters(mgr, self.instance)) assert counters def _verify_disk_metering(name, expected_volume): @@ -124,8 +123,9 @@ class TestNetPollster(TestPollsterBase): self.inspector.inspect_vnics(self.instance.name).AndReturn(vnics) self.mox.ReplayAll() + mgr = manager.AgentManager() pollster = pollsters.NetPollster() - counters = list(pollster.get_counters(self.manager, self.instance)) + counters = list(pollster.get_counters(mgr, self.instance)) assert counters def _verify_vnic_metering(name, ip, expected_volume): @@ -160,11 +160,11 @@ class TestCPUPollster(TestPollsterBase): virt_inspector.CPUStats(time=2 * (10 ** 6), number=2)) self.mox.ReplayAll() + mgr = manager.AgentManager() pollster = pollsters.CPUPollster() def _verify_cpu_metering(zero, expected_time): - counters = list(pollster.get_counters(self.manager, - self.instance)) + counters = list(pollster.get_counters(mgr, self.instance)) self.assertEquals(len(counters), 2) assert counters[0].name == 'cpu_util' assert (counters[0].volume == 0.0 if zero else